# Emotion-Driven Music and IoT Devices for Collaborative Exer-Games
## 4 Integrating Music into Exer-Games
### Solution Design  
Figure 2 illustrates the devices and software components that constitute the emotion-based music system for exer-games. The right-hand side of the figure outlines the relationship between the six-layer architecture and the system elements. The connectivity between elements across different architectural layers is primarily implemented using an event-driven interaction model. This model enhances the decoupling of distributed components, improves system scalability, and increases fault tolerance. The player wears two devices: an Empatica E4 physiological wearable [40] and Meta Quest 3 smart glasses [41]. The former allows real-time monitoring of the player's physiological signals (such as heart rate, electrodermal activity, blood volume, and temperature), which are then used to recognize the emotions the player is experiencing. The glasses display game elements and manage the player's interaction with these virtual components. Additionally, the glasses play a crucial role in game control for two reasons: (1) they generate events describing relevant conditions about game progress (e.g., player actions, challenges overcome, activities completed) as well as events concerning the player's physiological response during the game, and (2) they receive events specifying changes in game conditions, environment configurations, and stimuli aimed at influencing player behavior. Some of these input/output events are programmed directly using the glasses' core utilities, while others require specific applications executed on the glasses. As part of this work, two applications are developed. The first, the Empatica Physiological Data Acquisition (PDA) system, is an Android application capable of communicating with the Empatica E4 wearable to remotely acquire and filter the player's physiological data and generate corresponding events. These events primarily contain measurements of physiological signals over time. This application is built with the Android 12.0 version, API level 31. The second application, the _Spotify Player_, is another Android application that plays songs from the _Spotify_ discography. This application is responsible for executing musical stimuli through the audio of the glasses. These devices and applications comprise the Device layer of the solution. The flow of events between these elements and the other components of the music system is managed by the Messaging layer, which essentially functions as an event bus. In this work, the Azure Event Grid service is chosen as the integration bus [42]. Data ingestion services subscribe to the event bus to receive events from the glasses. These services specialize in processing specific events and storing the results in data repositories, which are accessible to services in the upper layers. The Ingestion layer of the music system includes two services: the Emotion Recognition Service and the Game Event Processing Service. The Emotion Recognition Service processes events related to the player's physiological data, using machine learning models to determine the player's current emotional state based on their electrodermal activity (i.e., translating physiological events into emotions) [43]. Many physiological events are generated for each player during a match, allowing for the progressive computation of their emotional sequence, which helps define the player's mood and emotional changes over time. The Game Event Processing Service, on the other hand, analyzes the player's behavior patterns during the match, translating game progress events into behavior patterns to identify each player's achievements and challenges. The emotional and behavioral data processed by these ingestion services are stored in a COSMOS database [44], which is accessible to the games that operate within the Application layer. Game applications use these data to execute rules that control gameplay.