From 2711027dcfc70bd72a2d6e476854049c20c318d0 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Thu, 4 Sep 2025 11:23:07 +1000 Subject: [PATCH] separate frequency from accumulator --- lib/main.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/main.c b/lib/main.c index b834c43..a9a59dc 100644 --- a/lib/main.c +++ b/lib/main.c @@ -11,6 +11,8 @@ #define DEFAULT_VOLUME 0.7 #define DEFAULT_FREQ 200 +#define DEFAULT_ACCUMULATOR -1 + #define PWSTREAM_NAME "Dorne" struct data { @@ -43,9 +45,9 @@ static void on_process(void *userdata) { n_frames = SPA_MIN(b->requested, n_frames); for (i = 0; i < n_frames; i++) { - data->accumulator += M_PI_M2 * DEFAULT_FREQ / DEFAULT_RATE; // * 440 - if (data->accumulator >= M_PI_M2) { - data->accumulator -= M_PI_M2; + // data->accumulator += M_PI_M2 * DEFAULT_FREQ / DEFAULT_RATE; // * 440 + if (data->accumulator++ == DEFAULT_RATE) { + data->accumulator = DEFAULT_ACCUMULATOR; data->cycle++; } @@ -55,7 +57,7 @@ static void on_process(void *userdata) { * Another common method to convert a double to * 16 bits is to multiple by 32768.0 and then clamp to * [-32768 32767] to get the full 16 bits range. */ - val = sin(data->accumulator) * DEFAULT_VOLUME * 32767.0; + val = sin(data->accumulator * M_PI_M2 * DEFAULT_FREQ / DEFAULT_RATE) * DEFAULT_VOLUME * 32767.0; for (c = 0; c < DEFAULT_CHANNELS; c++) *dst++ = val; } @@ -75,7 +77,8 @@ static const struct pw_stream_events stream_events = { int main(int argc, char *argv[]) { struct data data = { - 0, + .accumulator = DEFAULT_ACCUMULATOR, + .cycle = 0, }; const struct spa_pod *params[1]; uint8_t buffer[1024];