# Emotion-Driven Music and IoT Devices for Collaborative Exer-Games
## 5 Music Recommendation and Personalization Based on Emotions
### Function-Based Design of the Music Services  
Figure 4 illustrates the Music services developed to support the generation of musical stimuli. These services offer the functionalities required to achieve requirements R2 (Section 5.2), R3 (Section 5.4), and R4 (Section 5.3) described in Section 4.1. These functionalities are accessible through an event bus, as shown on the right side of the figure. Essentially, two types of service requests can be published: registering a new user and obtaining music recommendations based on emotions. Figure 4: Services involved in the generation of music stimuli. User registration is required to provide personalized recommendations. When a new user is registered, a musical seed that describes their tastes and preferences is generated. This seed is derived from the user's _Spotify_ playlists and recently played songs on the streaming platform. Access to this information requires that the user holds a premium license and provides the necessary credentials. Once access is granted, the seed is generated automatically without user intervention. The user's registration data and musical seed are then stored in a repository. The two services responsible for these operations are the User Registration and Musical Seed Creation services. The latter interacts with the _Spotify_ developer platform [47] to compute the user's seed. Game applications request recommended songs to generate musical stimuli. A recommendation request must include at least the listener's identity (for personalization purposes) and the emotion to be evoked. The Recommendation service is responsible for identifying candidate songs that best match the requested emotion. As part of this process, a Personalization service uses the listener's seed to filter the candidate songs based on their preferences, ensuring a customized result. A repository of emotionally labeled songs supports these emotion-based recommendations. As described earlier and shown on the left side of Figure 4, the RIADA system employs a Random Forest machine learning model to classify songs based on their emotional content, using a set of audio features provided by _Spotify_ (e.g., Valence, Energy, Tempo, Acousticness, Danceability). This system labels each song into one of four emotional quadrants--happy, sad, relaxed, and angry--which are then stored in the song and label repository. These labels serve as the basis for generating music recommendations aligned with the user's emotional state. All music services (represented as white rounded rectangles) are implemented as Azure Durable Functions [48], a type of serverless solution that reduces the costs associated with programming and executing in cloud environments. Azure Durable Functions extend the capabilities of standard Azure Functions by enabling the creation of stateful workflows. They are designed to manage and coordinate complex, long-running business processes and stateful operations without requiring developers to manage the underlying infrastructure. Durable functions are particularly useful in our scenario, as our problem workflows involve long-running processes as music tag processing, music recommendation, human interactions, or multiple steps that need to be executed in sequence. These functions provide a way to build reliable and scalable applications while abstracting away the complexity of state management. In this work, three types of durable functions in Azure Functions are used:  
* Activity functions: These are the building blocks of durable workflows, responsible for performing tasks or operations. They are called by orchestrator functions and can be executed in parallel or sequentially. * Orchestrator functions: These functions define the workflow of the durable application. They manage the coordination and state of activities and control the flow of execution. Orchestrator functions are durable and can handle long-running processes by maintaining their state across restarts. * Client functions: These functions are responsible for starting and interacting with orchestrator functions.