Adding max frequency and modulation to sound devices

This commit is contained in:
Paul Dino Jones 2025-01-21 17:23:10 -05:00
parent 10c5d2c597
commit fc95f044bf
5 changed files with 92 additions and 40 deletions

View File

@ -108,8 +108,8 @@ typedef struct
{ {
SimDevice m; SimDevice m;
int id; int id;
SoundType type;
int configcheck; int configcheck;
SoundEffectModulationType modulationType;
SoundData sounddata; SoundData sounddata;
#ifdef USE_PULSEAUDIO #ifdef USE_PULSEAUDIO
pa_stream *stream; pa_stream *stream;

View File

@ -38,16 +38,34 @@ int sounddev_engine_update(SimDevice* this, SimData* simdata)
slogt("set engine frequency to %i", sounddevice->sounddata.frequency); 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) int sounddev_tyreslip_update(SimDevice* this, SimData* simdata)
{ {
SoundDevice* sounddevice = (void *) this->derived; 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); 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", play); 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; sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration;
} }
else else
@ -67,7 +85,7 @@ int sounddev_tyrelock_update(SimDevice* this, SimData* simdata)
if (play > 0) 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; sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration;
} }
else else
@ -85,7 +103,7 @@ int sounddev_absbrakes_update(SimDevice* this, SimData* simdata)
if (play > 0) 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; sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration;
} }
else else
@ -129,24 +147,21 @@ int sounddev_free(SimDevice* this)
} }
#endif #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("initializing standalone sound device...");
slogi("volume is: %i", volume); slogi("volume is: %i", sds.volume);
slogi("frequency is: %i", frequency); slogi("frequency is: %i", sds.frequency);
slogi("pan is: %i", pan); slogi("frequency Max is: %i", sds.frequencyMax);
slogi("channels is: %i", channels); slogi("pan is: %i", sds.pan);
slogi("duration is: %f", duration); slogi("channels is: %i", sds.channels);
slogi("duration is: %f", sds.duration);
sounddevice->sounddata.frequency = frequency; sounddevice->modulationType = sds.modulation;
//sounddevice->sounddata.pitch = 1; sounddevice->sounddata.frequency = sds.frequency;
//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->sounddata.curr_frequency = 0; sounddevice->sounddata.curr_frequency = 0;
sounddevice->sounddata.curr_duration = 0; sounddevice->sounddata.curr_duration = 0;
sounddevice->sounddata.phase = 0; sounddevice->sounddata.phase = 0;
@ -155,35 +170,28 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int
const char* streamname= "Engine"; const char* streamname= "Engine";
switch (sounddevice->m.hapticeffect.effecttype) { switch (sounddevice->m.hapticeffect.effecttype) {
case (EFFECT_GEARSHIFT): case (EFFECT_GEARSHIFT):
sounddevice->sounddata.last_gear = 0; sounddevice->sounddata.last_gear = 0;
//sounddevice->sounddata.pitch = 500; sounddevice->sounddata.duration = sds.duration;
//sounddevice->sounddata.amp = 128;
//sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0;
//sounddevice->sounddata.table_size = 48000/(1);
sounddevice->sounddata.duration = duration;
streamname = "Gear"; streamname = "Gear";
break; break;
case (EFFECT_TYRESLIP): case (EFFECT_TYRESLIP):
sounddevice->sounddata.duration = duration;
streamname = "TyreSlip"; streamname = "TyreSlip";
break; break;
case (EFFECT_TYRELOCK): case (EFFECT_TYRELOCK):
sounddevice->sounddata.duration = duration;
streamname = "TyreLock"; streamname = "TyreLock";
break; break;
case (EFFECT_ABSBRAKES): case (EFFECT_ABSBRAKES):
sounddevice->sounddata.duration = duration;
streamname = "ABS"; streamname = "ABS";
break; break;
} }
#ifdef USE_PULSEAUDIO #ifdef USE_PULSEAUDIO
//pa_threaded_mainloop* mainloop; //pa_threaded_mainloop* mainloop;
//pa_context* context; //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 #else
usb_generic_shaker_init(sounddevice); usb_generic_shaker_init(sounddevice);
#endif #endif
@ -261,7 +269,7 @@ SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms) {
slogt("Attempting to use sound device %s", ds->sounddevsettings.dev); 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) if (error != 0)
{ {
free(this); free(this);

View File

@ -7,19 +7,22 @@
#include "portaudio.h" #include "portaudio.h"
#endif #endif
typedef enum typedef enum
{ {
SOUNDDEV_UNKNOWN = 0, SOUND_EFFECT_MODULATION_NONE = 0,
SOUNDDEV_SHAKER = 1 SOUND_EFFECT_MODULATION_FREQUENCY = 1,
SOUND_EFFECT_MODULATION_AMPLIFY = 2,
} }
SoundType; SoundEffectModulationType;
#define MAX_TABLE_SIZE (6000) #define MAX_TABLE_SIZE (6000)
typedef struct typedef struct
{ {
int last_gear; int last_gear;
int volume; int volume;
int frequency; uint32_t frequency;
uint32_t frequencyMax;
double duration; double duration;
int curr_frequency; int curr_frequency;
double curr_duration; double curr_duration;

View File

@ -15,6 +15,8 @@
#include "../slog/slog.h" #include "../slog/slog.h"
#include "parameters.h" #include "parameters.h"
#include "../simulatorapi/simapi/simapi/simmapper.h" #include "../simulatorapi/simapi/simapi/simmapper.h"
#include <pulse/pulseaudio.h> #include <pulse/pulseaudio.h>
@ -660,10 +662,9 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
if (ds->dev_type == SIMDEV_SOUND) if (ds->dev_type == SIMDEV_SOUND)
{ {
slogi("reading configured sound device settings"); slogi("reading configured sound device settings");
ds->sounddevsettings.frequency = -1; ds->sounddevsettings.frequency = 0;
ds->sounddevsettings.volume = -1; ds->sounddevsettings.frequencyMax = 0;
ds->sounddevsettings.lowbound_frequency = -1; ds->sounddevsettings.volume = 0;
ds->sounddevsettings.upperbound_frequency = -1;
ds->sounddevsettings.pan = 0; ds->sounddevsettings.pan = 0;
ds->sounddevsettings.channels = 1; ds->sounddevsettings.channels = 1;
ds->sounddevsettings.duration = 2.0; 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, "volume", &ds->sounddevsettings.volume);
config_setting_lookup_int(device_settings, "frequency", &ds->sounddevsettings.frequency); 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, "pan", &ds->sounddevsettings.pan);
config_setting_lookup_int(device_settings, "channels", &ds->sounddevsettings.channels); config_setting_lookup_int(device_settings, "channels", &ds->sounddevsettings.channels);
config_setting_lookup_float(device_settings, "duration", &ds->sounddevsettings.duration); 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); 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;
}
}
} }
} }
} }

View File

@ -9,6 +9,8 @@
#include "parameters.h" #include "parameters.h"
#include "../devices/sounddevice.h"
typedef enum typedef enum
{ {
SIMDEV_UNKNOWN = 0, SIMDEV_UNKNOWN = 0,
@ -160,14 +162,16 @@ SerialDeviceSettings;
typedef struct typedef struct
{ {
int frequency; uint32_t frequency;
int volume; uint32_t volume;
int lowbound_frequency; int lowbound_frequency;
int upperbound_frequency; int upperbound_frequency;
uint32_t frequencyMax;
int pan; int pan;
int channels; int channels;
double duration; double duration;
char* dev; char* dev;
SoundEffectModulationType modulation;
} }
SoundDeviceSettings; SoundDeviceSettings;