workspace commited on
Commit
380246e
1 Parent(s): 283f036

Create extract_features.py

Browse files
Files changed (1) hide show
  1. extract_features.py +33 -0
extract_features.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import soundfile
3
+ import librosa
4
+
5
+ def extract_feature(file_name, **kwargs):
6
+
7
+ chroma = kwargs.get("chroma")
8
+ contrast = kwargs.get("contrast")
9
+ mfcc = kwargs.get("mfcc")
10
+ mel = kwargs.get("mel")
11
+ tonnetz = kwargs.get("tonnetz")
12
+
13
+ with soundfile.SoundFile(file_name) as audio_clip:
14
+ X = audio_clip.read(dtype="float32")
15
+ sound_fourier = np.abs(librosa.stft(X)) # Conducting short time fourier transform of audio clip
16
+ result = np.array([])
17
+
18
+ if mfcc:
19
+ mfccs = np.mean(librosa.feature.mfcc(y=X, sr=audio_clip.samplerate, n_mfcc=40).T, axis=0)
20
+ result = np.hstack((result, mfccs))
21
+ if chroma:
22
+ chroma = np.mean(librosa.feature.chroma_stft(S=sound_fourier, sr=audio_clip.samplerate).T, axis=0)
23
+ result = np.hstack((result, chroma))
24
+ if mel:
25
+ mel = np.mean(librosa.feature.melspectrogram(X, sr=audio_clip.samplerate).T, axis=0)
26
+ result = np.hstack((result, mel))
27
+ if contrast:
28
+ contrast = np.mean(librosa.feature.spectral_contrast(S=sound_fourier, sr=audio_clip.samplerate).T, axis=0)
29
+ result = np.hstack((result, contrast))
30
+ if tonnetz:
31
+ tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=audio_clip.samplerate).T, axis=0)
32
+ result = np.hstack((result, tonnetz))
33
+ return result