mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-03-20 15:51:07 +01:00
better numerical precision for playback example
This commit is contained in:
@@ -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. */
|
static float samples[512]; /* this will feed 512 samples each frame until we get to our maximum. */
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
/* generate a 440Hz pure tone */
|
||||||
for (i = 0; i < SDL_arraysize(samples); i++) {
|
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.
|
const int freq = 440;
|
||||||
https://en.wikipedia.org/wiki/Sine_wave */
|
const int phase = (total_samples_generated * freq) % 8000;
|
||||||
const double time = total_samples_generated / 8000.0;
|
samples[i] = (float)SDL_sin(phase * 2 * SDL_PI_D / 8000.0);
|
||||||
const int sine_freq = 500; /* run the wave at 500Hz */
|
|
||||||
samples[i] = (float)SDL_sin(6.283185 * sine_freq * time);
|
|
||||||
total_samples_generated++;
|
total_samples_generated++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,12 +33,11 @@ static void SDLCALL FeedTheAudioStreamMore(void *userdata, SDL_AudioStream *astr
|
|||||||
const int total = SDL_min(additional_amount, SDL_arraysize(samples));
|
const int total = SDL_min(additional_amount, SDL_arraysize(samples));
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
/* generate a 440Hz pure tone */
|
||||||
for (i = 0; i < total; 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.
|
const int freq = 440;
|
||||||
https://en.wikipedia.org/wiki/Sine_wave */
|
const int phase = (total_samples_generated * freq) % 8000;
|
||||||
const double time = total_samples_generated / 8000.0;
|
samples[i] = (float)SDL_sin(phase * 2 * SDL_PI_D / 8000.0);
|
||||||
const int sine_freq = 500; /* run the wave at 500Hz */
|
|
||||||
samples[i] = (float)SDL_sin(6.283185 * sine_freq * time);
|
|
||||||
total_samples_generated++;
|
total_samples_generated++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user