Datasets:
chungimungi
commited on
Commit
•
1a84aa1
1
Parent(s):
4e7f504
Upload sampling.py
Browse filesUsed to convert .wav files to numpy arrays using librosa
- sampling.py +33 -0
sampling.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import librosa
|
2 |
+
import numpy as np
|
3 |
+
import os
|
4 |
+
import glob
|
5 |
+
|
6 |
+
audio_folder = 'images/audio'
|
7 |
+
file_paths = glob.glob(os.path.join(audio_folder, '*.wav'))
|
8 |
+
results = []
|
9 |
+
|
10 |
+
for file_path in file_paths:
|
11 |
+
y, sr = librosa.load(file_path)
|
12 |
+
S = librosa.feature.melspectrogram(y=y, sr=44100, n_mels=256)
|
13 |
+
log_S = librosa.power_to_db(S, ref=np.max)
|
14 |
+
vector = log_S.mean(axis=0)
|
15 |
+
|
16 |
+
results.append([os.path.basename(file_path), vector])
|
17 |
+
|
18 |
+
# Convert the 'results' list to a dictionary
|
19 |
+
results_dict = {item[0]: item[1] for item in results}
|
20 |
+
|
21 |
+
# np.save('vec.npy', results_dict)
|
22 |
+
|
23 |
+
# print("Results saved to 'vec.npy'")
|
24 |
+
|
25 |
+
import json
|
26 |
+
|
27 |
+
with open('vectors.jsonl', 'w') as fout:
|
28 |
+
for filename, vec in results_dict.items():
|
29 |
+
entry = {'filename': filename, 'audio_vector': vec.tolist()}
|
30 |
+
line = json.dumps(entry) + '\n'
|
31 |
+
fout.write(line)
|
32 |
+
|
33 |
+
print("Vectors saved to 'vectors.jls'")
|