Noise for haptics (#31)
This commit is contained in:
parent
8527ce9fa7
commit
75b42e876b
|
|
@ -26,6 +26,7 @@ configs = (
|
||||||
modulation = "frequency";
|
modulation = "frequency";
|
||||||
frequency = 17;
|
frequency = 17;
|
||||||
frequencyMax = 37;
|
frequencyMax = 37;
|
||||||
|
noise = 10; // additive noise in Hz
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
device = "Sound";
|
device = "Sound";
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,15 @@
|
||||||
#define M_PI (3.14159265)
|
#define M_PI (3.14159265)
|
||||||
#endif
|
#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) {
|
void gear_sound_stream(pa_stream *s, size_t length, void *userdata) {
|
||||||
|
|
||||||
SoundData* data = (SoundData*)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++) {
|
for (size_t i = 0; i < num_samples; i++) {
|
||||||
static double t = 0.0;
|
|
||||||
double sample = 0;
|
|
||||||
if (data->frequency>0)
|
|
||||||
{
|
|
||||||
sample = ((double)data->curr_amplitude/100) * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * data->curr_duration);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
double sample = 0;
|
||||||
|
|
||||||
|
if (data->curr_frequency>0.0)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
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;
|
buffer[i] = (int16_t)sample;
|
||||||
|
|
||||||
data->curr_duration += 1.0 / SAMPLE_RATE;
|
|
||||||
if (data->curr_duration >= data->duration) {
|
|
||||||
data->curr_duration = 0.0;
|
|
||||||
data->curr_frequency = 0.0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pa_stream_write(s, buffer, length, NULL, 0LL, PA_SEEK_RELATIVE);
|
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) {
|
void engine_sound_stream(pa_stream *s, size_t length, void *userdata) {
|
||||||
|
|
||||||
SoundData* data = (SoundData*)userdata;
|
SoundData* data = (SoundData*)userdata;
|
||||||
double freq = data->curr_frequency;
|
|
||||||
|
|
||||||
size_t num_samples = length / sizeof(int16_t);
|
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;
|
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) {
|
if (t >= 1.0) {
|
||||||
t -= floor(t);
|
t -= floor(t);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -177,6 +177,7 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, MonocoqueTyreId
|
||||||
slogi("pan is: %i", sds.pan);
|
slogi("pan is: %i", sds.pan);
|
||||||
slogi("channels is: %i", sds.channels);
|
slogi("channels is: %i", sds.channels);
|
||||||
slogi("duration is: %f", sds.duration);
|
slogi("duration is: %f", sds.duration);
|
||||||
|
slogi("noise is: %i", sds.noise);
|
||||||
|
|
||||||
|
|
||||||
sounddevice->modulationType = sds.modulation;
|
sounddevice->modulationType = sds.modulation;
|
||||||
|
|
@ -184,6 +185,7 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, MonocoqueTyreId
|
||||||
sounddevice->sounddata.frequencyMax = sds.frequencyMax;
|
sounddevice->sounddata.frequencyMax = sds.frequencyMax;
|
||||||
sounddevice->sounddata.amplitude = sds.amplitude;
|
sounddevice->sounddata.amplitude = sds.amplitude;
|
||||||
sounddevice->sounddata.amplitudeMax = sds.amplitudeMax;
|
sounddevice->sounddata.amplitudeMax = sds.amplitudeMax;
|
||||||
|
sounddevice->sounddata.noise = (double)sds.noise;
|
||||||
sounddevice->sounddata.curr_duration = 0;
|
sounddevice->sounddata.curr_duration = 0;
|
||||||
|
|
||||||
sounddevice->sounddata.phase = 0;
|
sounddevice->sounddata.phase = 0;
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ typedef struct
|
||||||
uint32_t curr_amplitude;
|
uint32_t curr_amplitude;
|
||||||
double curr_duration;
|
double curr_duration;
|
||||||
double phase;
|
double phase;
|
||||||
|
double noise;
|
||||||
}
|
}
|
||||||
SoundData;
|
SoundData;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -747,6 +747,7 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
|
||||||
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;
|
||||||
|
ds->sounddevsettings.noise = 0;
|
||||||
if (ds->effect_type == EFFECT_GEARSHIFT)
|
if (ds->effect_type == EFFECT_GEARSHIFT)
|
||||||
{
|
{
|
||||||
ds->sounddevsettings.duration = .125;
|
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, "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);
|
||||||
|
config_setting_lookup_int(device_settings, "noise", &ds->sounddevsettings.noise);
|
||||||
|
|
||||||
const char* temp = NULL;
|
const char* temp = NULL;
|
||||||
int found = 0;
|
int found = 0;
|
||||||
|
|
|
||||||
|
|
@ -185,6 +185,7 @@ typedef struct
|
||||||
double duration;
|
double duration;
|
||||||
char* dev;
|
char* dev;
|
||||||
SoundEffectModulationType modulation;
|
SoundEffectModulationType modulation;
|
||||||
|
int noise;
|
||||||
}
|
}
|
||||||
SoundDeviceSettings;
|
SoundDeviceSettings;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue