forked from gabrielclark3330/audio-tokenization-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencodedecodebasic.py
More file actions
executable file
·109 lines (90 loc) · 3.6 KB
/
Copy pathencodedecodebasic.py
File metadata and controls
executable file
·109 lines (90 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import numpy as np
import torch
import torchaudio
from speech_tokenization_experiments import SpeechTokenizer
import numpy as np
import itertools
from pathlib import Path
import os
import soundfile as sf
import timeit
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = "mps"
print(f"using device: {device}")
device = "cpu"
tokenizer = SpeechTokenizer(device=device)
def time_this(func):
def wrapper(*args, **kwargs):
start_time = timeit.default_timer()
result = func(*args, **kwargs)
end_time = timeit.default_timer()
execution_time = end_time - start_time
print(f"{func.__name__} Execution Time: {execution_time} seconds")
return result
return wrapper
@time_this
def resample_waveform(waveform, sample_rate, new_sample_rate, chunk_size=10000000):
resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=new_sample_rate)
resampled_waveform = []
for start in range(0, waveform.size(1), chunk_size):
end = min(start + chunk_size, waveform.size(1))
chunk = waveform[:, start:end]
resampled_chunk = resampler(chunk)
resampled_waveform.append(resampled_chunk)
return torch.cat(resampled_waveform, dim=1)
@time_this
def batch_list(lst, batch_size):
it = iter(lst)
return iter(lambda: list(itertools.islice(it, batch_size)), [])
sample_rate = 44000
def pad_waveform(waveform, multiple):
length = waveform.size(1)
remainder = length % multiple
if remainder != 0:
padding_size = multiple - remainder
padding = torch.zeros((waveform.size(0), padding_size), device=waveform.device)
waveform = torch.cat([waveform, padding], dim=1)
return waveform
def batch_list(lst, batch_size):
it = iter(lst)
return iter(lambda: list(itertools.islice(it, batch_size)), [])
@time_this
def encode(waveform, sample_rate):
if sample_rate != tokenizer.sample_rate:
print(f"resampled to {tokenizer.sample_rate}")
waveform = resample_waveform(waveform, sample_rate, tokenizer.sample_rate)
if waveform.size(0) > 1:
print(f"averaged audio to mono")
waveform = torch.mean(waveform, dim=0, keepdim=True)
batch_element_size = 192*64
# Reshape waveform into batches
waveform = pad_waveform(waveform, batch_element_size)
waveform_batches = waveform.view(-1, 1, batch_element_size) # Shape: (num_batches, 1, batch_size)
# Encode the batches
encoded_batches = tokenizer.encode(waveform_batches)
print("encoded batches", len(encoded_batches))
# Concatenate encoded tokens from all batches
#output_tokens = torch.cat(encoded_batches, dim=1)
# Flatten the list of lists into a single list
output_tokens = []
for batch in encoded_batches:
output_tokens.extend(batch)
return [output_tokens]
@time_this
def decode(tokens):
return tokenizer.decode(np.array([tokens[find_first_instance_of_seperator(tokens):find_last_instance_of_seperator(tokens)]]))
audio_path = "scarletheramireal.mp3"
audio_path = "./destin9s.mp3"
audio_path = "Krithik Puthalath (mp3cut.net).mp3"
wav, sample_rate = torchaudio.load(audio_path, backend='soundfile')
print("length of waveform in seconds", len(wav[1]) / sample_rate)
output_tokens = encode(wav, sample_rate)
print("output tokens", output_tokens)
print(len(output_tokens[0][:3168+1]))
wav = tokenizer.decode([output_tokens[0][:3168+1]]) #152
print([x.shape for x in wav])
sf.write("output.wav", wav[0].cpu().numpy().squeeze().astype(np.float32), tokenizer.sample_rate)
# 192 samples / 1 token