-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescriptors.py
68 lines (50 loc) · 1.95 KB
/
descriptors.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
# -*- coding: utf-8 -*-
"""
Demonstration code for Descriptors
Thales Roel - [email protected]
Usage:
python3 descriptors.py input1.wav output.pdf
Example:
python3 descriptors.py bell.wav figname.pdf
Within Spyder, use:
runfile('your_current_dir/descriptors.py', wdir='your_current_dir',
args='input1.wav output.pdf')
"""
import librosa
def spectral_centroid(audio_file, sr, frame_size, hop_length):
spectral_centroid = librosa.feature.spectral_centroid(y=audio_file,
sr=sr,
n_fft=frame_size,
hop_length=hop_length)[0]
return spectral_centroid
def spectral_rolloff(audio_file, frame_size, hop_length):
spectral_rolloff = librosa.feature.spectral_rolloff(y=audio_file,
n_fft=frame_size,
hop_length=hop_length)[0]
return spectral_rolloff
def frames_to_time(descriptor_param):
frames = range(len(descriptor_param))
time = librosa.frames_to_time(frames)
return time
if __name__ == "__main__":
import sys
import matplotlib.pyplot as plt
title = input("The title of your plot: ")
color_one = input("The color of the first plot: ")
color_two = input("The color of the second plot: ")
# Default parameters
FRAME_SIZE = 1024
HOP_LENGTH = 512
print(sys.argv[1], sys.argv[2])
audio, sr = librosa.load(sys.argv[1], sr=44100)
sc_audio = spectral_centroid(audio, sr, FRAME_SIZE, HOP_LENGTH)
rolloff_audio = spectral_rolloff(audio, FRAME_SIZE, HOP_LENGTH)
time = frames_to_time(sc_audio)
plt.figure(figsize=(25,10))
plt.subplot(2, 1, 1)
plt.title(title)
plt.plot(time, sc_audio , color_one)
plt.subplot(2, 1, 2)
plt.plot(time, rolloff_audio , color_two)
plt.savefig(sys.argv[2])
plt.show()