Skip to content

Commit eecd4f6

Browse files
author
Vincent Lostanlen
committed
write transform function
1 parent 3b2ab1e commit eecd4f6

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

birdvoxpaint/birdvoxpaint.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import librosa
2+
from librosa.util.exceptions.ParameterError
3+
import numpy as np
4+
5+
6+
def transform(filename=None, y=None, sr=22050,
7+
n_fft=256, hop_length=32, frame_length=256, fmin=1000, fmax=10000,
8+
indices=[average_energy], segment_duration=10,
9+
verbose=False, n_jobs=-1):
10+
11+
if n_jobs=-1:
12+
n_jobs = joblib.cpu_count()
13+
14+
if filename is not None:
15+
if y is not None:
16+
raise ParameterError(
17+
'Either y or filename must be equal to None')
18+
file_duration = librosa.get_duration(filename=filename)
19+
orig_sr = librosa.get_samplerate(filename)
20+
block_length = segment_duration * orig_sr * n_jobs
21+
y_blocks = librosa.stream(filename, block_length=block_length,
22+
frame_length=frame_length, hop_length=hop_length)
23+
if sr is None:
24+
sr = orig_sr
25+
else:
26+
if (y is None) or (sr is None):
27+
raise ParameterError(
28+
'At least one of (y, sr) or filename must be provided')
29+
librosa.util.valid_audio(y, mono=True)
30+
block_length = segment_duration * sr * n_jobs
31+
file_duration = librosa.get_duration(y=y, sr=sr)
32+
y_blocks = librosa.util.frame(y,
33+
frame_length=block_length, hop_length=block_length)
34+
35+
if fmin < 0:
36+
raise ParameterError("fmin={} must be nonnegative".format(fmin))
37+
38+
if fmax > (sr/2):
39+
raise ParameterError(
40+
"fmax={} must be smaller than sample rate sr={}".format(fmax, sr))
41+
42+
n_indices = len(indices)
43+
n_blocks = int(np.ceil(file_duration / block_duration))
44+
fft_frequencies = librosa.fft_frequencies(sr=sr, n_fft=n_fft)
45+
bin_start = np.where(fft_frequencies>=fmin)[0][0]
46+
bin_stop = np.where(fft_frequencies<fmax)[0][-1]
47+
n_freqs = bin_stop - bin_start
48+
feature_map = joblib.delayed(
49+
lambda x: np.stack([feature_lambda(x) for feature_lambda in indices]))
50+
joblib_parallel = joblib.Parallel(n_jobs=n_jobs)
51+
X_list = []
52+
53+
for block_id in tqdm.tqdm(range(n_blocks), disable=not verbose):
54+
if filename is not None:
55+
y_block = next(y_blocks)
56+
librosa.util.valid_audio(y_block, mono=True)
57+
if sr!=orig_sr:
58+
y_block = librosa.resample(y_block, orig_sr, sr)
59+
else:
60+
y_block = y_blocks[:, block_id]
61+
S = librosa.stft(y_block, n_fft=n_fft,
62+
hop_length=hop_length, win_length=frame_length, center=False)
63+
truncated_length = (S_tensor.shape[1]//segment_length) * segment_length
64+
if truncated_length == 0:
65+
continue
66+
else:
67+
S = S[bin_start:bin_stop, :truncated_length]
68+
S_tensor = np.reshape(S.T, (-1, segment_length, n_freqs)).T
69+
n_segments = S_tensor.shape[2]
70+
job_generator = (feature_map(S_tensor[:, :, segment_id])
71+
for segment_id in range(n_segments))
72+
X_list.append(np.stack(joblib_parallel(job_generator), axis=-1))
73+
74+
X_tensor = np.concatenate(X_list, axis=-1)
75+
76+
return X_tensor

0 commit comments

Comments
 (0)