- Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathaudio-plot-utils.py
143 lines (133 loc) · 4.21 KB
/
audio-plot-utils.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# %% [code]
#-------------------------------------
#
# Audio plot utils:
# - text formater (auxiliar/utils)
# - sound player
# - display sound wave
# - display spectrogram
# - display mel spectrogram
#
#-------------------------------------
importpandasaspd
importnumpyasnp
importmatplotlib.pyplotasplt
importlibrosa
fromscipy.ioimportwavfile
fromlibrosa.displayimportwaveshow
importIPython.displayasipd
fromIPython.displayimportAudio
fromIPython.displayimportdisplay, HTML
defcstr(str_text, color='black'):
"""
Html styling for widgets
Args
str_text: text to disply
color: color to display the text
Returns
Formated text/label
"""
return"<text style=color:{}><strong>{}<strong></text>".format(color, str_text)
defplay_sound(sound_path="",
text="Test",
color="green"):
"""
Display a sound play widget
Args
sound_path: path to the sound file
text: text to display
color: color for text to display
Returns
None
"""
display(HTML(cstr(text, color)))
display(ipd.Audio(sound_path))
defdisplay_sound_wave(sound_path=None,
text="Test",
color="green"):
"""
Display a sound wave
Args
sound_path: path to the sound file
text: text to display
color: color for text to display
Returns
None
"""
ifnotsound_path:
return
y_sound, sr_sound=librosa.load(sound_path)
audio_sound, _=librosa.effects.trim(y_sound)
fig, ax=plt.subplots(1, figsize= (16, 3))
fig.suptitle(f'Sound Wave: {text}', fontsize=12)
librosa.display.waveshow(y=audio_sound, sr=sr_sound, color=color)
defdisplay_wavefile(sound_path=None,
text="Test",
color="green"):
"""
Display a sound wave - load using wavefile
sr: sample rate
y_sound: sound samples
Args
sound_path: path to the sound file
text: text to display
color: color for text to display
Returns
None
"""
ifnotsound_path:
return
sr_sound, y_sound=wavfile.load(sound_path)
fig, ax=plt.subplots(1, figsize= (16, 3))
fig.suptitle(f'Sound Wave: {text}', fontsize=12)
ax.plot(np.linspace(0, sr_sound/len(y_sound), sr_sound), y_sound)
defdisplay_spectrogram(sound_path=None,
text="Test"):
"""
Display a spectrogram
Args
sound_path: path to the sound file
text: text to display (title)
Returns
None
"""
ifnotsound_path:
return
n_fft=2048
hop_length=512
y_sound, sr_sound=librosa.load(sound_path)
audio_sound, _=librosa.effects.trim(y_sound)
# Short-time Fourier transform (STFT)
D_sound=np.abs(librosa.stft(audio_sound, n_fft=n_fft, hop_length=hop_length))
# Convert an amplitude spectrogram to Decibels-scaled spectrogram.
DB_sound=librosa.amplitude_to_db(D_sound, ref=np.max)
# Prepare the plot
fig, ax=plt.subplots(1, 1, figsize=(12, 4))
img=librosa.display.specshow(DB_sound, sr=sr_sound, hop_length=hop_length, x_axis='time',
y_axis='log', cmap='cool', ax=ax)
ax.set_title(f'Log Frequency Spectrogram: {text}', fontsize=10)
plt.colorbar(img,ax=ax)
defdisplay_mel_spectrogram(sound_path=None,
text="Test"):
"""
Display a mel spectrogram
Args
sound_path: path to the sound file
text: text to display (title)
Returns
None
"""
ifnotsound_path:
return
hop_length=512
y_sound, sr_sound=librosa.load(sound_path)
audio_sound, _=librosa.effects.trim(y_sound)
# Create the Mel Spectrograms
S_sound=librosa.feature.melspectrogram(audio_sound, sr=sr_sound)
S_DB_sound=librosa.amplitude_to_db(S_sound, ref=np.max)
# Prepare the plot
fig, ax=plt.subplots(1, 1, figsize=(12, 6))
img=librosa.display.specshow(S_DB_sound, sr=sr_sound, hop_length=hop_length, x_axis='time',
y_axis='log', cmap='cool', ax=ax)
ax.set_title(f'Mel Spectrogram: {text}', fontsize=10)
plt.colorbar(img,ax=ax)