-
Notifications
You must be signed in to change notification settings - Fork 16
/
audioprocessors.py
108 lines (80 loc) · 3.09 KB
/
audioprocessors.py
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
import torch
import numpy as np
import soundfile as sf
import librosa
from scipy import signal
import os
def processor_d18(file_path):
n_fft = 2048 # 2048
sr = 22050 # 22050 # 44100 # 32000
mono = True #
log_spec = False
n_mels = 256
hop_length = 512
fmax = None
if mono:
# this is the slowest part resampling
sig, sr = librosa.load(file_path, sr=sr, mono=True)
sig = sig[np.newaxis]
else:
sig, sr = librosa.load(file_path, sr=sr, mono=False)
# sig, sf_sr = sf.read(file_path)
# sig = np.transpose(sig, (1, 0))
# sig = np.asarray([librosa.resample(s, sf_sr, sr) for s in sig])
spectrograms = []
for y in sig:
# compute stftnp.asfortranarray(x)
stft = librosa.stft(np.asfortranarray(y), n_fft=n_fft, hop_length=hop_length, win_length=None, window='hann', center=True,
pad_mode='reflect')
# keep only amplitures
stft = np.abs(stft)
# spectrogram weighting
if log_spec:
stft = np.log10(stft + 1)
else:
freqs = librosa.core.fft_frequencies(sr=sr, n_fft=n_fft)
stft = librosa.perceptual_weighting(stft ** 2, freqs, ref=1.0, amin=1e-10, top_db=80.0)
# apply mel filterbank
spectrogram = librosa.feature.melspectrogram(S=stft, sr=sr, n_mels=n_mels, fmax=fmax)
# keep spectrogram
spectrograms.append(np.asarray(spectrogram))
spectrograms = np.asarray(spectrograms, dtype=np.float32)
return spectrograms
def processor_d18_stereo(file_path):
n_fft = 2048 # 2048
sr = 22050 # 22050 # 44100 # 32000
mono = False #=
log_spec = False
n_mels = 256
hop_length = 512
fmax = None
if mono:
# this is the slowest part resampling
sig, sr = librosa.load(file_path, sr=sr, mono=True)
sig = sig[np.newaxis]
else:
sig, sr = librosa.load(file_path, sr=sr, mono=False)
dpath, filename = os.path.split(file_path)
#librosa.output.write_wav(dpath + "/../audio22k/" + filename, sig, sr)
# sig, sf_sr = sf.read(file_path)
# sig = np.transpose(sig, (1, 0))
# sig = np.asarray([librosa.resample(s, sf_sr, sr) for s in sig])
spectrograms = []
for y in sig:
# compute stft
stft = librosa.stft(np.asfortranarray(y), n_fft=n_fft, hop_length=hop_length, win_length=None, window='hann', center=True,
pad_mode='reflect')
# keep only amplitures
stft = np.abs(stft)
# spectrogram weighting
if log_spec:
stft = np.log10(stft + 1)
else:
freqs = librosa.core.fft_frequencies(sr=sr, n_fft=n_fft)
stft = librosa.perceptual_weighting(stft ** 2, freqs, ref=1.0, amin=1e-10, top_db=80.0)
# apply mel filterbank
spectrogram = librosa.feature.melspectrogram(S=stft, sr=sr, n_mels=n_mels, fmax=fmax)
# keep spectrogram
spectrograms.append(np.asarray(spectrogram))
spectrograms = np.asarray(spectrograms, dtype=np.float32)
return spectrograms