-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusic_detection.py
79 lines (65 loc) · 2.67 KB
/
music_detection.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
# Import utilities
import pydub
import glob, os
import random
import scipy.io.wavfile
import numpy as np
import matplotlib.pyplot as plt
from pydub.playback import play
from threading import Thread
from playsound import playsound
#--------------------------------------------------------------#
# Plot the amplitudes without breaking the execution of the program
class ThreadPlot(Thread):
def __init__(self, time, audData):
Thread.__init__(self)
self.time = time
self.audData = audData
def run(self):
plt.figure("Amplitudes")
plt.plot(self.time, self.audData, linewidth=0.01, alpha=0.7, color='#ff7f00')
plt.xlabel('Time(s)')
plt.ylabel('Amplitude')
plt.rcParams['agg.path.chunksize'] = 10000
plt.show()
#--------------------------------------------------------------#
# Choose a random song from the folder "Music" contained in the main folder of the project
def random_song(): # hp: exists a folder named 'Music' in which we search songs
list_songs = []
os.chdir(os.getcwd() + "/Music")
for file in glob.glob("*.wav"):
list_songs.append(file)
sorted_song = random.choice(list_songs)
print("I chose the song || " + sorted_song + " || LET' S DANCE NAO!")
return sorted_song
# Analyze the selected song and plot the amplitudes in order to show how the intensity change into the song.
# The function returns the list of intensity per second of the song
def analyze_music(song):
wav = pydub.AudioSegment.from_wav(os.getcwd() + "/" + song)
wav_mono = wav.set_channels(1)
os.chdir(os.getcwd() + "/Music_Mono")
wav_mono.export(os.getcwd() + "/" + "_MONO_" + song[:-3] + "wav", format="wav")
rate, audData = scipy.io.wavfile.read(os.getcwd() + "/" + "_MONO_" + song[:-3] + "wav")
#####PLOT AMPLITUDE#####
# create a time variable in seconds
time = np.arange(0, float(audData.shape[0]), 1)/rate
# plot amplitude over time
#thread = ThreadPlot(time, audData)
#print("Now I plot the amplitudes...")
#thread.start()
#######################
# INTENSITY ANALYSIS
list_interval_intensity = []
for i in range(180):
list_interval_intensity.append((np.sum(abs(audData[i*rate:((i+1)*rate)+1]).astype(float)))/rate)
max_interval = max(list_interval_intensity)
list_interval_intensity_percent = []
for el in list_interval_intensity:
list_interval_intensity_percent.append((el*100)/max_interval)
return list_interval_intensity_percent
# Play the selected song
def play_song(song):
os.chdir('..')
playsound(os.getcwd() + "/" + song)
#wav = pydub.AudioSegment.from_wav(os.getcwd() + "/" + song)
#play(wav)