-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicAnalyzer.py
executable file
·240 lines (160 loc) · 6.76 KB
/
MusicAnalyzer.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
import sys
import shutil
from pathlib import Path
from pydub import AudioSegment
import wave
import audioop
import ffmpeg
from scipy.io import wavfile
import numpy as np
import scipy
import matplotlib.pyplot as plt
# import librosa
# import librosa.display
class MusicAnalyzer:
def list_files(self, directories): # John
directory = directories[0]
workspace = directories[1]
if os.path.exists(workspace):
shutil.rmtree(workspace)
files = []
for _, _, file_list in os.walk(directory):
for file in file_list:
if file.endswith(".wav") or file.endswith(".mp3"):
files.append(file)
return files
def copy_files_to_workspace(self, files, directories): # John
directory = directories[0]
workspace = directories[1]
if os.path.exists(workspace):
shutil.rmtree(workspace)
os.mkdir(workspace)
for file in files:
shutil.copyfile(directory + "/" + file, workspace + "/" + file)
def crossfade(self, directories): # Sailon
directory = directories[0]
workspace = directories[1]
if os.path.exists(workspace):
shutil.rmtree(workspace)
files = []
for _, _, file_list in os.walk(directory):
for file in file_list:
if file.endswith(".wav") or file.endswith(".mp3"):
files.append(file)
directory = directories[0]
playlist = []
for file in files:
if file.endswith(".wav"):
playlist.append(AudioSegment.from_wav(directory + "/" + file))
if file.endswith(".mp3"):
playlist.append(AudioSegment.from_mp3(directory + "/" + file))
combined = playlist[0]
for song in playlist[1:]:
combined = combined.append(song, crossfade=5000)
combined.export(directory + "/Crossfaded_Playlist" + ".wav", format="wav")
def convert_mp3_to_wav(self, file_list, directories): # Sailon
workspace = directories[1]
converted_files = []
for file in file_list:
if file.endswith(".mp3"):
sound = AudioSegment.from_mp3(workspace + "/" + file)
sound.export(
workspace + "/" + file.replace(".mp3", "") + ".wav", format="wav"
)
os.remove(workspace + "/" + file)
converted_files.append(file.replace(".mp3", ".wav"))
return converted_files
def rename_files(self, files, directories): # John
i = 1
workspace = directories[1]
renamed_files = []
for file in files:
if file != (str(i) + ".wav"):
os.rename(workspace + "/" + file, workspace + "/" + str(i) + ".wav")
renamed_files.append(str(i) + ".wav")
else:
renamed_files.append(file)
i += 1
return renamed_files
def convert_stereo_to_mono(self, file_list, directories): # Sailon
workspace = directories[1]
for file in file_list:
fileX = wave.open(workspace + "/" + file, "r")
if fileX.getnchannels() != 1:
frames, audiodata = wavfile.read(workspace + "/" + file)
audiodata = audiodata.astype(float)
newaudiodata = []
d = (audiodata[:, 0] + audiodata[:, 1]) / 2
newaudiodata.append(d)
a = np.array(newaudiodata, dtype="int16")
wavfile.write(workspace + "/" + file, frames, a.T)
# def convert_stereo_to_mono(self,file_list,directories):
# workspace = directories[1]
# for file in file_list:
# fileX = wave.open(workspace + "/" + file, 'r')
# if fileX.getnchannels != 1:
# converted_file = AudioSegment.from_wav(workspace + "/" + file)
# converted_file = converted_file.set_channels(1)
# converted_file.export(workspace + "/" + file, format="wav")
def audio_details(self, file, directories): # John
workspace = directories[1]
audio_file = wave.open(workspace + "/" + file, "r")
channels_num = audio_file.getnchannels()
samp_width = audio_file.getsampwidth()
frame_rate = audio_file.getframerate()
frames_num = audio_file.getnframes()
audio_file.close()
details = [channels_num, samp_width, frame_rate, frames_num]
return details
def plot_graph(self, files, file, directories, position): # John
workspace = directories[1]
spf = wave.open(workspace + "/" + file, "r")
signal = spf.readframes(-1)
signal = np.fromstring(signal, "Int16")
plt.figure(1)
plt.xlabel("Tempo")
plt.ylabel("Amplitude")
plt.title("Gráfico do arquivo: " + files[position] + "...")
plt.rcParams["figure.figsize"] = (11, 7)
plt.plot(signal)
# plt.show()
"""
def show_spectrogram(self, files, file, directories, position): #Sailon
workspace = directories[1]
x , sr = librosa.load(workspace + "/" + file)
X = librosa.stft(x)
Xdb = librosa.amplitude_to_db(abs(X))
#plt.figure(figsize=(14, 5))
plt.figure(2)
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')
plt.colorbar()
plt.title("Espectograma do arquivo: " + files[position] + "...")
#plt.show()
def show_center_of_mass(self, files, file, directories, position): #Sailon
workspace = directories[1]
x , sr = librosa.load(workspace + "/" + file)
spectral_centroids = librosa.feature.spectral_centroid(x, sr=sr)[0]
spectral_centroids.shape
frames = range(len(spectral_centroids))
t = librosa.frames_to_time(frames)
plt.figure(3)
ax1 = plt.subplot(2, 1, 1)
librosa.display.waveplot(x, sr=sr, alpha=0.5)
plt.title("Gráfico do arquivo: " + files[position] + "...")
plt.subplot(2, 1, 2, sharex=ax1)
plt.plot(t, spectral_centroids, color='r')
plt.title("Centroide do arquivo: " + files[position] + "...")
plt.tight_layout()
#plt.show()
def show_notes(self, files, file, directories, position): #Sailon
workspace = directories[1]
x , sr = librosa.load(workspace + "/" + file, duration=15)
chromagram = librosa.feature.chroma_stft(x, sr=sr, hop_length=512)
plt.figure(4)
librosa.display.specshow(chromagram, x_axis='time', y_axis='chroma', hop_length=512, cmap='coolwarm')
plt.title('Gráfico do áudio x')
plt.xlabel('Tempo (s)')
plt.ylabel('Notas')
plt.show()
"""