From 24c9f784c506a2b1567080223f6e527f7e27ea34 Mon Sep 17 00:00:00 2001 From: expikr <77922942+expikr@users.noreply.github.com> Date: Tue, 14 Jan 2025 00:58:06 +0800 Subject: [PATCH] better numerical precision for playback example --- examples/audio/01-simple-playback/simple-playback.c | 10 +++++----- .../simple-playback-callback.c | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/audio/01-simple-playback/simple-playback.c b/examples/audio/01-simple-playback/simple-playback.c index a2b95bf0765ef..d550f4d1bbcc6 100644 --- a/examples/audio/01-simple-playback/simple-playback.c +++ b/examples/audio/01-simple-playback/simple-playback.c @@ -73,12 +73,12 @@ SDL_AppResult SDL_AppIterate(void *appstate) static float samples[512]; /* this will feed 512 samples each frame until we get to our maximum. */ int i; + /* generate a 500Hz pure tone */ for (i = 0; i < SDL_arraysize(samples); i++) { - /* You don't have to care about this math; we're just generating a simple sine wave as we go. - https://en.wikipedia.org/wiki/Sine_wave */ - const double time = total_samples_generated / 8000.0; - const int sine_freq = 500; /* run the wave at 500Hz */ - samples[i] = (float)SDL_sin(6.283185 * sine_freq * time); + const int freq = 500; + const int cycle = freq * 8000; + const int phase = total_samples_generated % cycle; + samples[i] = (float)SDL_sin(phase * 2 * SDL_PI_D / 8000.0); total_samples_generated++; } diff --git a/examples/audio/02-simple-playback-callback/simple-playback-callback.c b/examples/audio/02-simple-playback-callback/simple-playback-callback.c index f91aaa6ee2204..fff59ee74c5cd 100644 --- a/examples/audio/02-simple-playback-callback/simple-playback-callback.c +++ b/examples/audio/02-simple-playback-callback/simple-playback-callback.c @@ -33,12 +33,12 @@ static void SDLCALL FeedTheAudioStreamMore(void *userdata, SDL_AudioStream *astr const int total = SDL_min(additional_amount, SDL_arraysize(samples)); int i; - for (i = 0; i < total; i++) { - /* You don't have to care about this math; we're just generating a simple sine wave as we go. - https://en.wikipedia.org/wiki/Sine_wave */ - const double time = total_samples_generated / 8000.0; - const int sine_freq = 500; /* run the wave at 500Hz */ - samples[i] = (float)SDL_sin(6.283185 * sine_freq * time); + /* generate a 500Hz pure tone */ + for (i = 0; i < SDL_arraysize(samples); i++) { + const int freq = 500; + const int cycle = freq * 8000; + const int phase = total_samples_generated % cycle; + samples[i] = (float)SDL_sin(phase * 2 * SDL_PI_D / 8000.0); total_samples_generated++; }