Noise for haptics (#31)

This commit is contained in:
dafteran4 2026-02-14 18:32:14 +01:00 committed by GitHub
parent 8527ce9fa7
commit 75b42e876b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 53 additions and 13 deletions

View File

@ -26,6 +26,7 @@ configs = (
modulation = "frequency";
frequency = 17;
frequencyMax = 37;
noise = 10; // additive noise in Hz
},
{
device = "Sound";

View File

@ -16,6 +16,15 @@
#define M_PI (3.14159265)
#endif
static double apply_noise(double base_freq, double noise_amount) {
if (noise_amount > 0.0) {
double r = (double) rand() / RAND_MAX * 2.0 - 1.0;
return base_freq + r * noise_amount;
} else {
return base_freq;
}
}
void gear_sound_stream(pa_stream *s, size_t length, void *userdata) {
SoundData* data = (SoundData*)userdata;
@ -24,21 +33,44 @@ void gear_sound_stream(pa_stream *s, size_t length, void *userdata) {
for (size_t i = 0; i < num_samples; i++) {
static double t = 0.0;
double sample = 0;
if (data->frequency>0)
if (data->curr_frequency>0.0)
{
sample = ((double)data->curr_amplitude/100) * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * data->curr_duration);
double t = data->phase;
double a = (double)data->curr_amplitude/100;
// fade-out 5ms to prevent discontinuity
static double fade_out_duration = 0.005;
double remaining = data->duration - data->curr_duration;
if (remaining < fade_out_duration) {
a *= remaining / fade_out_duration;
}
sample = a * 32767.0 * sin(2.0 * M_PI * t);
buffer[i] = (int16_t)sample;
double f = apply_noise(data->curr_frequency, data->noise);
t += f / SAMPLE_RATE;
if (t >= 1.0) {
t -= floor(t);
}
data->phase = t;
data->curr_duration += 1.0 / SAMPLE_RATE;
if (data->curr_duration >= data->duration) {
data->curr_duration = 0.0;
data->curr_frequency = 0.0;
data->phase = 0.0;
}
}
buffer[i] = (int16_t)sample;
}
pa_stream_write(s, buffer, length, NULL, 0LL, PA_SEEK_RELATIVE);
}
@ -46,7 +78,6 @@ void gear_sound_stream(pa_stream *s, size_t length, void *userdata) {
void engine_sound_stream(pa_stream *s, size_t length, void *userdata) {
SoundData* data = (SoundData*)userdata;
double freq = data->curr_frequency;
size_t num_samples = length / sizeof(int16_t);
@ -62,7 +93,9 @@ void engine_sound_stream(pa_stream *s, size_t length, void *userdata) {
buffer[i] = (int16_t)sample;
t += freq / SAMPLE_RATE;
double f = apply_noise(data->curr_frequency, data->noise);
t += f / SAMPLE_RATE;
if (t >= 1.0) {
t -= floor(t);
}

View File

@ -177,6 +177,7 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, MonocoqueTyreId
slogi("pan is: %i", sds.pan);
slogi("channels is: %i", sds.channels);
slogi("duration is: %f", sds.duration);
slogi("noise is: %i", sds.noise);
sounddevice->modulationType = sds.modulation;
@ -184,6 +185,7 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, MonocoqueTyreId
sounddevice->sounddata.frequencyMax = sds.frequencyMax;
sounddevice->sounddata.amplitude = sds.amplitude;
sounddevice->sounddata.amplitudeMax = sds.amplitudeMax;
sounddevice->sounddata.noise = (double)sds.noise;
sounddevice->sounddata.curr_duration = 0;
sounddevice->sounddata.phase = 0;

View File

@ -30,6 +30,7 @@ typedef struct
uint32_t curr_amplitude;
double curr_duration;
double phase;
double noise;
}
SoundData;

View File

@ -747,6 +747,7 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
ds->sounddevsettings.pan = 0;
ds->sounddevsettings.channels = 1;
ds->sounddevsettings.duration = 2.0;
ds->sounddevsettings.noise = 0;
if (ds->effect_type == EFFECT_GEARSHIFT)
{
ds->sounddevsettings.duration = .125;
@ -762,6 +763,7 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
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);
config_setting_lookup_int(device_settings, "noise", &ds->sounddevsettings.noise);
const char* temp = NULL;
int found = 0;

View File

@ -185,6 +185,7 @@ typedef struct
double duration;
char* dev;
SoundEffectModulationType modulation;
int noise;
}
SoundDeviceSettings;