# Emotion-Driven Music and IoT Devices for Collaborative Exer-Games
## 5 Music Recommendation and Personalization Based on Emotions
### Music Recommendation  
The music recommendation service is built upon personalization profiles. As depicted in Figure 8, when a user identifier and a desired emotion are provided, the request is triggered by the client.  
The Get Personalized Recommendation Trigger is activated through an HTTP GET request. This trigger retrieves the user ID and the desired emotion label from the JSON body of the request and initiates the orchestrator with these data.  
The Get Personalized Recommendation Orchestrator is responsible for coordinating the execution of all subsequent activities. The first activity, Get User's Seed Activity, retrieves the user's musical seed from the COSMOS database using the provided user ID. This seed is calculated as described previously, representing the user's preferences based on their listening habits.  
Once the seed is retrieved, the Find Songs Activity searches for songs whose audio features fall within a 20% tolerance range of the user's musical seed, the selected _Spotify_ audio features are normalized between 0 and 1, except for loudness, which ranges from \(-60\) to 0 and requires an adjustment. This range-based search was selected due to its efficiency through index creation in the database, with a worst case scenario in time complexity of O(log(n)). Other recommendation systems [51] often employ similarity functions such as Euclidean Distance, Cosine Similarity, or Manhattan Distance, which are more precise and also more computationally expensive with a time complexity of O(n). From our perspective,  
Figure 8: Flow of Azure Durable Function for recommending personalized songs.  
a faster approach, despite being less precise, is preferred, as the result group of similar songs is randomized to introduce variability in the music recommendation. These songs are selected based on their similarity in audio features, ensuring that they align with the user's requested emotion. To achieve this, a range of values is defined for the emotional labels, maximizing the likelihood that the selected songs belong to the requested emotional quadrant while minimizing subjective user interpretations. The defined ranges for each emotion are as follows:  
* Angry: Angry \(>0.6\), Sad \(<0.2\), Relaxed \(<0.2\), Happy \(<0.4\);
* Sad: Angry \(<0.2\), Sad \(>0.8\), Relaxed \(<0.55\), Happy \(<0.2\);
* Relaxed: Angry \(<0.2\), Sad \(<0.6\), Relaxed \(>0.85\), Happy \(<0.2\);
* Happy: Angry \(<0.3\), Sad \(<0.2\), Relaxed \(<0.2\), Happy \(>0.6\).  
The system selects a default of five songs, provided enough songs meet the emotional criteria based on their labels. These labels are generated using the Random Forest classifier trained on _Spotify_'s audio features, ensuring that the selected songs align with the emotional quadrant requested (happy, sad, relaxed, or angry). Songs are selected randomly from this pool to ensure diversity in the recommendations and avoid monotony.  
Once the songs are selected, they are returned to the client as a personalized recommendation, providing a tailored experience based on the user's musical seed and emotional preferences.