forked from gabrielclark3330/audio-tokenization-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampleencode.py
More file actions
executable file
·87 lines (74 loc) · 2.64 KB
/
Copy pathexampleencode.py
File metadata and controls
executable file
·87 lines (74 loc) · 2.64 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
import pyaudio
import numpy as np
from speech_tokenizer import SpeechTokenizer
import torch
import torchaudio
from speech_tokenizer import SpeechTokenizer
import numpy as np
import itertools
from pathlib import Path
import os
import librosa
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}")
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 find_last_instance_of_seperator(lst, element=4097):
for i in range(len(lst)-1, -1, -1):
if lst[i]==element:
return i
raise ValueError
def find_first_instance_of_seperator(lst, element=4097):
for i in range(0, len(lst)):
if lst[i]==element:
return i
raise ValueError
@time_this
def batch_list(lst, batch_size):
it = iter(lst)
return iter(lambda: list(itertools.islice(it, batch_size)), [])
sample_rate = 44000
@time_this
def encode(audio_path):
waveform, sample_rate = torchaudio.load(audio_path, backend='soundfile')
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)
single_doc = []
encoded_batch = tokenizer.encode([waveform])
for x in encoded_batch:
single_doc.extend(x[:-1])
return single_doc
@time_this
def decode(tokens):
return tokenizer.decode(np.array([tokens[find_first_instance_of_seperator(tokens):find_last_instance_of_seperator(tokens)]]))
output_tokens = encode("./destin9s.mp3")
print(",".join([str(x) for x in list(output_tokens)]))