From 6703021eba894a60695ac0dd76a0f2929a751484 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 23 Oct 2016 16:11:58 -0400 Subject: [PATCH] sio_sine: add phase wrap around closes #110 Thanks to @iskunk for the fix. --- README.md | 3 ++- example/sio_sine.c | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e18bb4e..e08f3cc 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,8 @@ static void write_callback(struct SoundIoOutStream *outstream, *ptr = sample; } } - seconds_offset += seconds_per_frame * frame_count; + seconds_offset = fmodf(seconds_offset + + seconds_per_frame * frame_count, 1.0f); if ((err = soundio_outstream_end_write(outstream))) { fprintf(stderr, "%s\n", soundio_strerror(err)); diff --git a/example/sio_sine.c b/example/sio_sine.c index 75fe613..cc70c67 100644 --- a/example/sio_sine.c +++ b/example/sio_sine.c @@ -77,13 +77,13 @@ static void write_callback(struct SoundIoOutStream *outstream, int frame_count_m double pitch = 440.0; double radians_per_second = pitch * 2.0 * PI; for (int frame = 0; frame < frame_count; frame += 1) { - double sample = sinf((seconds_offset + frame * seconds_per_frame) * radians_per_second); + double sample = sin((seconds_offset + frame * seconds_per_frame) * radians_per_second); for (int channel = 0; channel < layout->channel_count; channel += 1) { write_sample(areas[channel].ptr, sample); areas[channel].ptr += areas[channel].step; } } - seconds_offset += seconds_per_frame * frame_count; + seconds_offset = fmod(seconds_offset + seconds_per_frame * frame_count, 1.0); if ((err = soundio_outstream_end_write(outstream))) { if (err == SoundIoErrorUnderflow)