Spaces:
Runtime error
Runtime error
Added process from filename
Browse files- server/preprocess.py +19 -0
server/preprocess.py
CHANGED
@@ -4,6 +4,15 @@ Util functions to process any incoming audio data to be processable by the model
|
|
4 |
import torch
|
5 |
import torchaudio
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def process_raw_wav(wav, sample_rate, target_sample_rate=4800, wav_length=5):
|
8 |
num_samples = wav_length * target_sample_rate
|
9 |
|
@@ -14,6 +23,16 @@ def process_raw_wav(wav, sample_rate, target_sample_rate=4800, wav_length=5):
|
|
14 |
|
15 |
return wav
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def _resample(wav, sample_rate, target_sample_rate):
|
18 |
if sample_rate != target_sample_rate:
|
19 |
resampler = torchaudio.transforms.Resample(sample_rate, target_sample_rate)
|
|
|
4 |
import torch
|
5 |
import torchaudio
|
6 |
|
7 |
+
def process_from_filename(filename, target_sample_rate=4800, wav_length=5):
|
8 |
+
wav, sample_rate = torchaudio.load(filename)
|
9 |
+
|
10 |
+
wav = process_raw_wav(wav, sample_rate, target_sample_rate, wav_length)
|
11 |
+
|
12 |
+
spec = _wav_to_spec(wav, target_sample_rate)
|
13 |
+
|
14 |
+
return spec
|
15 |
+
|
16 |
def process_raw_wav(wav, sample_rate, target_sample_rate=4800, wav_length=5):
|
17 |
num_samples = wav_length * target_sample_rate
|
18 |
|
|
|
23 |
|
24 |
return wav
|
25 |
|
26 |
+
def _wav_to_spec(wav, target_sample_rate):
|
27 |
+
mel_spectrogram = torchaudio.transforms.MelSpectrogram(
|
28 |
+
sample_rate=target_sample_rate,
|
29 |
+
n_fft=1024,
|
30 |
+
hop_length=512,
|
31 |
+
n_mels=64
|
32 |
+
)
|
33 |
+
|
34 |
+
return mel_spectrogram(wav)
|
35 |
+
|
36 |
def _resample(wav, sample_rate, target_sample_rate):
|
37 |
if sample_rate != target_sample_rate:
|
38 |
resampler = torchaudio.transforms.Resample(sample_rate, target_sample_rate)
|