Source code for src.FMOD.Adapters.EngineAdapter

# Copyright (c) 2026 Kai Braun, Ozan Miguel Gündogdu, Yeri Jikong, Sven Winkelmann
#
# SPDX-License-Identifier: MIT
#
# Licensed under the MIT License. 
# See LICENSE file in the project root for full license information.
# Also consult our README to comply with Third-Party Licenses.

from pyfmodex.studio.enums import PLAYBACK_STATE

from ..Sounds.EVSoundEngine import *
from ..utils.EventBus import *
from ..Banks import TriggerBank
from ..utils import DataKey
from ..utils.DataKey import DataKey
from ..utils.EventBus import EventBus
from ..Banks import ExampleBank
import keyboard


[docs] class EngineAdapter: """ Adapter responsible for synchronizing vehicle dynamics with the EV sound engine. This class subscribes to simulation data from the EventBus (speed and throttle) and translates these values into auditory parameters for the EVSoundEngine. It ensures that engine sounds react dynamically to the driver's input and vehicle state. Attributes: ev (EVSoundEngine): The engine responsible for generating electric vehicle sounds. speed (float): The current velocity of the vehicle in km/h. throttle (float): The current throttle input, ranging from 0.0 to 1.0. """
[docs] def __init__(self, bus: EventBus, ev: EVSoundEngine): """ Initializes the EngineAdapter and subscribes to relevant data keys. Args: bus (EventBus): The system bus used for data subscription. ev (EVSoundEngine): The sound engine instance to be controlled. """ self.ev = ev self.speed = 0 self.throttle = 0 bus.subscribe(DataKey.SPEED, self.on_speed) bus.subscribe(DataKey.THROTTLE, self.on_throttle)
[docs] def on_speed(self, speed): """ Callback triggered when a new speed value is published to the EventBus. Args: speed (float): The updated speed value from the simulation. """ self.speed = speed self.update()
[docs] def on_throttle(self, throttle): """ Callback triggered when a new throttle value is published to the EventBus. Args: throttle (float): The updated throttle input from the simulation. """ self.throttle = throttle self.update()
[docs] def calculate_torque(self, throttle): """ Calculates a simplified torque value used for sound modulation. Args: throttle (float): Current throttle position. Returns: int: Returns 1 if throttle is active, 0 otherwise. """ if throttle <= 0: return 0 elif throttle > 0: return 1
[docs] def update(self): """ Updates the engine and sound states for the current frame. Ensures the sound engine is running and pushes the latest speed and torque parameters to the FMOD system. """ if self.ev.is_running is False: self.ev.start() self.ev.update_params(self.speed, self.calculate_torque(self.throttle)) self.ev.system.update()