From fc95f044bf3697a68e1972a8244c4cdb6844dacf Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Tue, 21 Jan 2025 17:23:10 -0500 Subject: [PATCH] Adding max frequency and modulation to sound devices --- src/monocoque/devices/simdevice.h | 2 +- src/monocoque/devices/sounddevice.c | 66 ++++++++++++++++------------- src/monocoque/devices/sounddevice.h | 11 +++-- src/monocoque/helper/confighelper.c | 45 ++++++++++++++++++-- src/monocoque/helper/confighelper.h | 8 +++- 5 files changed, 92 insertions(+), 40 deletions(-) diff --git a/src/monocoque/devices/simdevice.h b/src/monocoque/devices/simdevice.h index 4bd965c..017a60b 100644 --- a/src/monocoque/devices/simdevice.h +++ b/src/monocoque/devices/simdevice.h @@ -108,8 +108,8 @@ typedef struct { SimDevice m; int id; - SoundType type; int configcheck; + SoundEffectModulationType modulationType; SoundData sounddata; #ifdef USE_PULSEAUDIO pa_stream *stream; diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index 783f2d8..739fac9 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -38,16 +38,34 @@ int sounddev_engine_update(SimDevice* this, SimData* simdata) slogt("set engine frequency to %i", sounddevice->sounddata.frequency); } +double modulate(SoundDevice* sounddevice, double raw_effect, SoundEffectModulationType modulation) +{ + double modulated_effect = raw_effect; + switch (modulation) + { + case SOUND_EFFECT_MODULATION_FREQUENCY: + modulated_effect = (sounddevice->sounddata.frequencyMax - sounddevice->sounddata.frequency) * raw_effect; + break; + case SOUND_EFFECT_MODULATION_AMPLIFY: + case SOUND_EFFECT_MODULATION_NONE: + default: + modulated_effect = raw_effect; + } + + return modulated_effect; +} + int sounddev_tyreslip_update(SimDevice* this, SimData* simdata) { SoundDevice* sounddevice = (void *) this->derived; - double play = slipeffect(simdata, this->hapticeffect.effecttype, this->hapticeffect.tyre, this->hapticeffect.threshold, this->hapticeffect.useconfig, this->hapticeffect.configcheck, this->hapticeffect.tyrediameterconfig); - slogt("Updating sound device frequency from original tyre slip effect %f", play); + double effect = slipeffect(simdata, this->hapticeffect.effecttype, this->hapticeffect.tyre, this->hapticeffect.threshold, this->hapticeffect.useconfig, this->hapticeffect.configcheck, this->hapticeffect.tyrediameterconfig); + slogt("Updating sound device frequency from original tyre slip effect %f", effect); - if (play > 0) + if (effect > 0) { - sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency * play; + effect = modulate(sounddevice, effect, sounddevice->modulationType); + sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency; sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration; } else @@ -67,7 +85,7 @@ int sounddev_tyrelock_update(SimDevice* this, SimData* simdata) if (play > 0) { - sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency * play; + sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency; sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration; } else @@ -85,7 +103,7 @@ int sounddev_absbrakes_update(SimDevice* this, SimData* simdata) if (play > 0) { - sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency * play; + sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency; sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration; } else @@ -129,24 +147,21 @@ int sounddev_free(SimDevice* this) } #endif -int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int frequency, int pan, int channels, double duration, double threshold) +int sounddev_init(SoundDevice* sounddevice, const char* devname, MonocoqueTyreIdentifier tyre, SoundDeviceSettings sds) { slogi("initializing standalone sound device..."); - slogi("volume is: %i", volume); - slogi("frequency is: %i", frequency); - slogi("pan is: %i", pan); - slogi("channels is: %i", channels); - slogi("duration is: %f", duration); + slogi("volume is: %i", sds.volume); + slogi("frequency is: %i", sds.frequency); + slogi("frequency Max is: %i", sds.frequencyMax); + slogi("pan is: %i", sds.pan); + slogi("channels is: %i", sds.channels); + slogi("duration is: %f", sds.duration); - sounddevice->sounddata.frequency = frequency; - //sounddevice->sounddata.pitch = 1; - //sounddevice->sounddata.pitch = 261.626; - //sounddevice->sounddata.amp = 32; - //sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0; - //sounddevice->sounddata.table_size = 48000/(100/60); + sounddevice->modulationType = sds.modulation; + sounddevice->sounddata.frequency = sds.frequency; sounddevice->sounddata.curr_frequency = 0; sounddevice->sounddata.curr_duration = 0; sounddevice->sounddata.phase = 0; @@ -155,35 +170,28 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int const char* streamname= "Engine"; switch (sounddevice->m.hapticeffect.effecttype) { case (EFFECT_GEARSHIFT): - sounddevice->sounddata.last_gear = 0; - //sounddevice->sounddata.pitch = 500; - //sounddevice->sounddata.amp = 128; - //sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0; - //sounddevice->sounddata.table_size = 48000/(1); - sounddevice->sounddata.duration = duration; + sounddevice->sounddata.duration = sds.duration; streamname = "Gear"; break; case (EFFECT_TYRESLIP): - sounddevice->sounddata.duration = duration; streamname = "TyreSlip"; break; case (EFFECT_TYRELOCK): - sounddevice->sounddata.duration = duration; streamname = "TyreLock"; break; case (EFFECT_ABSBRAKES): - sounddevice->sounddata.duration = duration; streamname = "ABS"; break; } + #ifdef USE_PULSEAUDIO //pa_threaded_mainloop* mainloop; //pa_context* context; - usb_generic_shaker_init(sounddevice, mainloop, context, devname, volume, pan, channels, streamname); + usb_generic_shaker_init(sounddevice, mainloop, context, devname, sds.volume, sds.pan, sds.channels, streamname); #else usb_generic_shaker_init(sounddevice); #endif @@ -261,7 +269,7 @@ SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms) { slogt("Attempting to use sound device %s", ds->sounddevsettings.dev); - int error = sounddev_init(this, ds->sounddevsettings.dev, ds->sounddevsettings.volume, ds->sounddevsettings.frequency, ds->sounddevsettings.pan, ds->sounddevsettings.channels, ds->sounddevsettings.duration, ds->threshold); + int error = sounddev_init(this, ds->sounddevsettings.dev, ds->tyre, ds->sounddevsettings); if (error != 0) { free(this); diff --git a/src/monocoque/devices/sounddevice.h b/src/monocoque/devices/sounddevice.h index c06aa5d..3de3466 100644 --- a/src/monocoque/devices/sounddevice.h +++ b/src/monocoque/devices/sounddevice.h @@ -7,19 +7,22 @@ #include "portaudio.h" #endif + typedef enum { - SOUNDDEV_UNKNOWN = 0, - SOUNDDEV_SHAKER = 1 + SOUND_EFFECT_MODULATION_NONE = 0, + SOUND_EFFECT_MODULATION_FREQUENCY = 1, + SOUND_EFFECT_MODULATION_AMPLIFY = 2, } -SoundType; +SoundEffectModulationType; #define MAX_TABLE_SIZE (6000) typedef struct { int last_gear; int volume; - int frequency; + uint32_t frequency; + uint32_t frequencyMax; double duration; int curr_frequency; double curr_duration; diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index 75565de..d0424cb 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -15,6 +15,8 @@ #include "../slog/slog.h" #include "parameters.h" + + #include "../simulatorapi/simapi/simapi/simmapper.h" #include @@ -660,10 +662,9 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co if (ds->dev_type == SIMDEV_SOUND) { slogi("reading configured sound device settings"); - ds->sounddevsettings.frequency = -1; - ds->sounddevsettings.volume = -1; - ds->sounddevsettings.lowbound_frequency = -1; - ds->sounddevsettings.upperbound_frequency = -1; + ds->sounddevsettings.frequency = 0; + ds->sounddevsettings.frequencyMax = 0; + ds->sounddevsettings.volume = 0; ds->sounddevsettings.pan = 0; ds->sounddevsettings.channels = 1; ds->sounddevsettings.duration = 2.0; @@ -676,6 +677,7 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co config_setting_lookup_int(device_settings, "volume", &ds->sounddevsettings.volume); config_setting_lookup_int(device_settings, "frequency", &ds->sounddevsettings.frequency); + config_setting_lookup_int(device_settings, "frequencyMax", &ds->sounddevsettings.frequencyMax); config_setting_lookup_int(device_settings, "pan", &ds->sounddevsettings.pan); config_setting_lookup_int(device_settings, "channels", &ds->sounddevsettings.channels); config_setting_lookup_float(device_settings, "duration", &ds->sounddevsettings.duration); @@ -691,6 +693,41 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co { ds->sounddevsettings.dev = strdup(temp); } + + + found = config_setting_lookup_string(device_settings, "modulation", &temp); + ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_NONE; + if (found == 0) + { + ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_NONE; + slogd("Effect modulation not found, set to none"); + } + else + { + if(strcicmp(temp, "FREQUENCY")) + { + ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_FREQUENCY; + if(ds->sounddevsettings.frequencyMax == 0 || ds->sounddevsettings.frequencyMax < ds->sounddevsettings.frequency) + { + ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_NONE; + slogw("Falling back to no frequency modulation since frequencyMax is either not set or set below target frequency"); + } + else + { + slogi("Effect modulation found, set to FREQUENCY"); + } + } + else if(strcicmp(temp, "AMPLIFY")) + { + ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_AMPLIFY; + slogi("Effect modulation found, set to AMPLIFY"); + } + else + { + slogw("%s is not a valid modulation type, falling back to no effect modulation"); + ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_NONE; + } + } } } } diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index c048ff7..22cf3f2 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -9,6 +9,8 @@ #include "parameters.h" +#include "../devices/sounddevice.h" + typedef enum { SIMDEV_UNKNOWN = 0, @@ -160,14 +162,16 @@ SerialDeviceSettings; typedef struct { - int frequency; - int volume; + uint32_t frequency; + uint32_t volume; int lowbound_frequency; int upperbound_frequency; + uint32_t frequencyMax; int pan; int channels; double duration; char* dev; + SoundEffectModulationType modulation; } SoundDeviceSettings;