From c8e40e9b803d6ee8edd9435891e9b197d121fb75 Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Tue, 3 Dec 2024 20:47:57 -0500 Subject: [PATCH] persist phase to stop disjointed sine waves --- .../devices/sound/usb_generic_shaker_pulse.c | 16 ++++++++++------ src/monocoque/devices/sounddevice.c | 2 +- src/monocoque/devices/sounddevice.h | 1 + 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/monocoque/devices/sound/usb_generic_shaker_pulse.c b/src/monocoque/devices/sound/usb_generic_shaker_pulse.c index 1a4c416..2a9fd4d 100644 --- a/src/monocoque/devices/sound/usb_generic_shaker_pulse.c +++ b/src/monocoque/devices/sound/usb_generic_shaker_pulse.c @@ -11,9 +11,9 @@ #include "../sounddevice.h" #define FORMAT PA_SAMPLE_S16LE -#define SAMPLE_RATE (48000) -#define AMPLITUDE .5 -#define DURATION 4.0 +#define SAMPLE_RATE (44100) +#define AMPLITUDE 1 +#define DURATION 1.0 #ifndef M_PI #define M_PI (3.14159265) @@ -49,20 +49,24 @@ 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); + num_samples = (size_t) (DURATION * SAMPLE_RATE); int16_t buffer[num_samples]; + for (size_t i = 0; i < num_samples; i++) { - static double t = 0.0; - double sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * data->curr_frequency * t); + double t = data->phase; + double sample = AMPLITUDE * 32767.0 * sin(2.0 * M_PI * freq * t); buffer[i] = (int16_t)sample; t += 1.0 / SAMPLE_RATE; if (t >= DURATION) { - t = 0.0; + t = -DURATION; } + data->phase = t; } pa_stream_write(s, buffer, length, NULL, 0LL, PA_SEEK_RELATIVE); } diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index 50a0e06..5f38e09 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -131,7 +131,7 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int //sounddevice->sounddata.table_size = 48000/(100/60); sounddevice->sounddata.curr_frequency = 0; sounddevice->sounddata.curr_duration = 0; - + sounddevice->sounddata.phase = 0; const char* streamname= "Engine"; diff --git a/src/monocoque/devices/sounddevice.h b/src/monocoque/devices/sounddevice.h index 00eb949..c06aa5d 100644 --- a/src/monocoque/devices/sounddevice.h +++ b/src/monocoque/devices/sounddevice.h @@ -23,6 +23,7 @@ typedef struct double duration; int curr_frequency; double curr_duration; + double phase; } SoundData;