-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudio.py
executable file
·113 lines (92 loc) · 3.61 KB
/
Audio.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
import librosa
import logging
import constants
import numpy as np
import matplotlib.pyplot as plt
# Disable the logging from `matplotlib`
plt_logger = logging.getLogger("matplotlib")
plt_logger.setLevel(logging.ERROR)
class Audio:
def __init__(self):
self.name = "Untitled"
def set_audio(self, audio, i, sr=constants.sr):
self.audio = audio
self.sr = sr
self.name = f"Segment {i}"
def load_audio(self, path):
self.audio, self.sr = librosa.load(path, sr=constants.sr)
self.trim_silence()
def trim_silence(self):
self.audio, _ = librosa.effects.trim(self.audio)
def get_spectrogram(self):
self.spectro = librosa.feature.melspectrogram(
y=self.audio, sr=self.sr, n_mels=constants.n_mels
)
def plot_spectrogram(self):
fig, ax = plt.subplots()
S_dB = librosa.power_to_db(self.spectro, ref=np.max)
img = librosa.display.specshow(
S_dB, x_axis="time", y_axis="mel", sr=self.sr, fmax=8000, ax=ax
)
fig.colorbar(img, ax=ax, format="%+2.0f dB")
ax.set(title="Mel-frequency spectrogram")
plt.show()
def get_chromagram(self):
self.chroma = librosa.feature.chroma_stft(S=self.spectro, sr=self.sr)
def plot_chromagram(self):
fig, ax = plt.subplots()
img = librosa.display.specshow(
self.chroma, y_axis="chroma", x_axis="time", ax=ax
)
fig.colorbar(img, ax=ax)
ax.set(title="Chromagram")
plt.show()
def get_onset(self):
self.onset = librosa.onset.onset_strength(y=self.audio, sr=self.sr)
self.onset = librosa.util.normalize(self.onset)
def plot_onset(self):
fig, ax = plt.subplots()
ax.plot(librosa.times_like(self.onset), self.onset)
ax.set(title="Onset strength", xlabel="Time", ylabel="Normalized strength")
plt.show()
def get_MFCC(self):
self.MFCC = librosa.feature.mfcc(
y=self.audio,
n_fft=constants.n_fft,
hop_length=constants.hop_length,
n_mfcc=constants.n_mfcc,
)
self.MFCC = librosa.util.normalize(self.MFCC)
def plot_MFCC(self):
fig, ax = plt.subplots()
img = librosa.display.specshow(self.MFCC, x_axis="time", ax=ax)
fig.colorbar(img, ax=ax)
ax.set(title="MFCC")
plt.show()
def get_all_features(self):
self.get_spectrogram()
self.get_chromagram()
self.get_onset()
self.get_MFCC()
def plot_all_features(self):
fig, ax = plt.subplots(4, 1, figsize=(12, 16), sharex=True)
S_dB = librosa.power_to_db(self.spectro, ref=np.max)
img0 = librosa.display.specshow(
S_dB, x_axis="time", y_axis="mel", sr=self.sr, fmax=8000, ax=ax[0]
)
fig.colorbar(img0, ax=ax[0], format="%+2.0f dB")
ax[0].set(title="Mel-frequency spectrogram")
img1 = librosa.display.specshow(
self.chroma, y_axis="chroma", x_axis="time", ax=ax[1]
)
fig.colorbar(img1, ax=ax[1])
ax[1].set(title="Chromagram")
ax[2].plot(librosa.times_like(self.onset), self.onset)
fig.colorbar(img0, ax=ax[2])
ax[2].set(title="Onset strength", xlabel="Time", ylabel="Normalized strength")
img3 = librosa.display.specshow(self.MFCC, x_axis="time", ax=ax[3])
fig.colorbar(img3, ax=ax[3])
ax[3].set(title="MFCC")
plt.tight_layout()
plt.savefig(f"output/{self.name}.pdf")
plt.close()