Skip to content

Commit

Permalink
better numerical precision for playback example
Browse files Browse the repository at this point in the history
  • Loading branch information
expikr committed Jan 13, 2025
1 parent 611f132 commit e6eebc3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
9 changes: 4 additions & 5 deletions examples/audio/01-simple-playback/simple-playback.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ 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 phase = (total_samples_generated * freq) % 8000;
samples[i] = (float)SDL_sin(phase * 2 * SDL_PI_D / 8000.0);
total_samples_generated++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ static void SDLCALL FeedTheAudioStreamMore(void *userdata, SDL_AudioStream *astr
const int total = SDL_min(additional_amount, SDL_arraysize(samples));
int i;

/* generate a 500Hz pure tone */
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);
const int freq = 500;
const int phase = (total_samples_generated * freq) % 8000;
samples[i] = (float)SDL_sin(phase * 2 * SDL_PI_D / 8000.0);
total_samples_generated++;
}

Expand Down

0 comments on commit e6eebc3

Please sign in to comment.