MCPcopy Create free account
hub / github.com/AlayaLab/Hive / FlowSepPreprocessor

Class FlowSepPreprocessor

app.py:30–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29
30class FlowSepPreprocessor:
31 def __init__(self, config):
32 import utilities.audio as Audio
33
34 self.sampling_rate = config["preprocessing"]["audio"]["sampling_rate"]
35 self.duration = config["preprocessing"]["audio"]["duration"]
36 self.hopsize = config["preprocessing"]["stft"]["hop_length"]
37 self.target_length = int(self.duration * self.sampling_rate / self.hopsize)
38
39 self.STFT = Audio.stft.TacotronSTFT(
40 config["preprocessing"]["stft"]["filter_length"],
41 config["preprocessing"]["stft"]["hop_length"],
42 config["preprocessing"]["stft"]["win_length"],
43 config["preprocessing"]["mel"]["n_mel_channels"],
44 config["preprocessing"]["audio"]["sampling_rate"],
45 config["preprocessing"]["mel"]["mel_fmin"],
46 config["preprocessing"]["mel"]["mel_fmax"],
47 )
48
49 def read_wav_file(self, filename):
50 waveform, sr = torchaudio.load(filename)
51 target_length = int(sr * self.duration)
52 if waveform.shape[-1] > target_length:
53 waveform = waveform[:, :target_length]
54 if sr != self.sampling_rate:
55 waveform = torchaudio.functional.resample(waveform, sr, self.sampling_rate)
56 waveform = waveform.numpy()[0, ...]
57 waveform = waveform - np.mean(waveform)
58 waveform = waveform / (np.max(np.abs(waveform)) + 1e-8)
59 waveform = waveform * 0.5
60 waveform = waveform[None, ...]
61 target_samples = int(self.sampling_rate * self.duration)
62 if waveform.shape[-1] < target_samples:
63 temp_wav = np.zeros((1, target_samples), dtype=np.float32)
64 temp_wav[:, :waveform.shape[-1]] = waveform
65 waveform = temp_wav
66 return waveform
67
68 def wav_feature_extraction(self, waveform):
69 import utilities.audio as Audio
70
71 waveform = waveform[0, ...]
72 waveform = torch.FloatTensor(waveform)
73 log_mel_spec, stft, energy = Audio.tools.get_mel_from_wav(waveform, self.STFT)
74 log_mel_spec = torch.FloatTensor(log_mel_spec.T)
75 stft = torch.FloatTensor(stft.T)
76 log_mel_spec = self._pad_spec(log_mel_spec)
77 stft = self._pad_spec(stft)
78 return log_mel_spec, stft
79
80 def _pad_spec(self, log_mel_spec):
81 n_frames = log_mel_spec.shape[0]
82 p = self.target_length - n_frames
83 if p > 0:
84 m = torch.nn.ZeroPad2d((0, 0, 0, p))
85 log_mel_spec = m(log_mel_spec)
86 elif p < 0:
87 log_mel_spec = log_mel_spec[:self.target_length, :]

Callers 1

load_flowsepFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected