-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemotion_x_song.py
111 lines (94 loc) · 3.69 KB
/
emotion_x_song.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
import csv
# For simplification process we shall asuume emotions to be either positive or negative
# Positive Emotions: Happy, Suprise, Neutral
# Negative Emotions: Sad, Fear, Disgust, Anger
# Get loudness decision
# Spotify Typical Loudness Interval: -60 to 0
def get_loudness_descision(loudness):
if loudness < -50:
return 0 #"SAD"
elif loudness < -40:
return 0 # "FEAR" or DISGUST
elif loudness < -30:
return 1 #"SUPRISE"
elif loudness < -20:
return 0 #"ANGER"
elif loudness < -10:
return 1 # "HAPPY"
else:
return 1 #"NEUTRAL"
# Get tempo decision
# Typical BPM Ramge: 60 - 180
def get_tempo_decision(tempo, tempo_confidence):
if tempo > 150 and tempo_confidence > 0.7:
return 1 # "HAPPY"
elif tempo > 150 and tempo_confidence > 0.5:
return 0 # "ANGRY"
elif tempo > 120 and tempo_confidence > 0.7:
return 1 # "SUPRISE"
elif tempo > 120 and tempo_confidence > 0.3:
return 0 # "FEAR"
elif tempo > 80 and tempo_confidence > 0.7:
return 0 # "SAD"
elif tempo > 80 and tempo_confidence > 0.5:
return 0 # "DISGUST"
else:
return 1 # "NEUTRAL"
# Get mode (major/ minor) decision
def get_mode_decision(mode, mode_confidence):
if mode == "Major" and mode_confidence > 0.7:
return 1 # "HAPPY"
elif mode == "Major" and mode_confidence > 0.5:
return 1 # "SUPRISE"
elif mode == "Minor" and mode_confidence > 0.8:
return 0 # "ANGER"
elif mode == "Minor" and mode_confidence > 0.7:
return 0 # "SAD"
elif mode == "Minor" and mode_confidence > 0.5:
return 0 # "FEAR"
elif mode == "Minor" and mode_confidence > 0.3:
return 0 # "DISGUST"
else:
return 1 # "NEUTRAL"
# Get positivity value
def get_positivity_value(loudness_decision, tempo_decision, mode_decision):
if (loudness_decision + tempo_decision + mode_decision) >= 2:
return "positive"
else:
return "negative"
# Classify the emotion into positive, negative or neutral
def map_emotion_to_positivity(emotion):
if emotion in ["happy", "surprise", "neutral"]:
return "positive"
else:
return "negative"
# Map emotions and songs
def get_song(dominant_emotion):
# Open the file in read mode and pass it to CSV DictReader
file = open('audio_analysis.csv', 'r')
reader = csv.DictReader(file)
# Initialize the song_emotion_matrix
song_emotion_matrix = []
# Iterate through the CSV file and append decisions to the song_emotion_matrix
for song in reader:
song_emotion_matrix.append(
{
"track_id": song["track_id"],
"track_name": song["track_name"],
"artist_name": song["artist_name"],
"loudness_decision": get_loudness_descision(float(song["loudness"])),
"tempo_decision": get_tempo_decision(float(song["tempo"]), float(song["tempo_confidence"])),
"mode_decision": get_mode_decision(song["mode"], float(song["mode_confidence"])),
"final_decision": get_positivity_value(
get_loudness_descision(float(song["loudness"])),
get_tempo_decision(float(song["tempo"]), float(song["tempo_confidence"])),
get_mode_decision(song["mode"], float(song["mode_confidence"]))
)
}
)
# Close the file
file.close()
# Iterate through the song_emotion_matrix and return the song object of the song that matches the dominant emotion
for song in song_emotion_matrix:
if song["final_decision"] == map_emotion_to_positivity(dominant_emotion):
return song