From 664c50752a84e983bdb759ef253888dbe369d4fd Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 4 Jul 2015 03:55:00 -0700 Subject: [PATCH] sine example working with dummy --- README.md | 2 +- example/sine.c | 40 ++++++++++++++++++++++++++++++++++++++-- src/soundio.h | 24 ++++++++++++------------ 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 47694f4..cbf5d4b 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ make ## Roadmap - 0. sine example working with dummy backend linux, osx, windows + 0. sine example working with dummy backend osx, windows 0. sine example working with pulseaudio backend linux 0. pipe record to playback example working with dummy linux, osx, windows 0. pipe record to playback example working with pulseaudio linux diff --git a/example/sine.c b/example/sine.c index 76bdc78..ffd5213 100644 --- a/example/sine.c +++ b/example/sine.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include __attribute__ ((cold)) __attribute__ ((noreturn)) @@ -23,8 +25,42 @@ static void panic(const char *format, ...) { abort(); } -static void write_callback(struct SoundIoOutputDevice *device, int frame_count) { - fprintf(stderr, "write_callback\n"); +static const float PI = 3.1415926535f; +static float seconds_offset = 0.0f; + +static void write_callback(struct SoundIoOutputDevice *output_device, int requested_frame_count) { + //device->bytes_per_frame; + float float_sample_rate = output_device->device->default_sample_rate; + float seconds_per_frame = 1.0f / float_sample_rate; + + while (requested_frame_count > 0) { + char *data; + int frame_count = requested_frame_count; + soundio_output_device_begin_write(output_device, &data, &frame_count); + + // clear everything to 0 + memset(data, 0, frame_count * output_device->bytes_per_frame); + + const struct SoundIoChannelLayout *channel_layout = &output_device->device->channel_layout; + + float *ptr = (float *)data; + + // 69 is A 440 + float pitch = 440.0f; + float radians_per_second = pitch * 2.0f * PI; + for (int frame = 0; frame < frame_count; frame += 1) { + float sample = sinf((seconds_offset + frame * seconds_per_frame) * radians_per_second); + for (int channel = 0; channel < channel_layout->channel_count; channel += 1) { + *ptr += sample; + ptr += 1; + } + } + seconds_offset += seconds_per_frame * frame_count; + + soundio_output_device_write(output_device, data, frame_count); + requested_frame_count -= frame_count; + } + } static void underrun_callback(struct SoundIoOutputDevice *device) { diff --git a/src/soundio.h b/src/soundio.h index fe3c0c9..520f681 100644 --- a/src/soundio.h +++ b/src/soundio.h @@ -287,21 +287,21 @@ int soundio_output_device_create(struct SoundIoDevice *device, void (*write_callback)(struct SoundIoOutputDevice *, int frame_count), void (*underrun_callback)(struct SoundIoOutputDevice *), struct SoundIoOutputDevice **out_output_device); -void soundio_output_device_destroy(struct SoundIoOutputDevice *device); +void soundio_output_device_destroy(struct SoundIoOutputDevice *output_device); -int soundio_output_device_start(struct SoundIoOutputDevice *device); +int soundio_output_device_start(struct SoundIoOutputDevice *output_device); -void soundio_output_device_fill_with_silence(struct SoundIoOutputDevice *device); +void soundio_output_device_fill_with_silence(struct SoundIoOutputDevice *output_device); // number of frames available to write -int soundio_output_device_free_count(struct SoundIoOutputDevice *device); -void soundio_output_device_begin_write(struct SoundIoOutputDevice *device, +int soundio_output_device_free_count(struct SoundIoOutputDevice *output_device); +void soundio_output_device_begin_write(struct SoundIoOutputDevice *output_device, char **data, int *frame_count); -void soundio_output_device_write(struct SoundIoOutputDevice *device, +void soundio_output_device_write(struct SoundIoOutputDevice *output_device, char *data, int frame_count); -void soundio_output_device_clear_buffer(struct SoundIoOutputDevice *device); +void soundio_output_device_clear_buffer(struct SoundIoOutputDevice *output_device); @@ -311,14 +311,14 @@ int soundio_input_device_create(struct SoundIoDevice *device, enum SoundIoSampleFormat sample_format, double latency, void *userdata, void (*read_callback)(struct SoundIoOutputDevice *), struct SoundIoOutputDevice **out_input_device); -void soundio_input_device_destroy(struct SoundIoOutputDevice *device); +void soundio_input_device_destroy(struct SoundIoOutputDevice *input_device); -int soundio_input_device_start(struct SoundIoOutputDevice *device); -void soundio_input_device_peek(struct SoundIoOutputDevice *device, +int soundio_input_device_start(struct SoundIoOutputDevice *input_device); +void soundio_input_device_peek(struct SoundIoOutputDevice *input_device, const char **data, int *frame_count); -void soundio_input_device_drop(struct SoundIoOutputDevice *device); +void soundio_input_device_drop(struct SoundIoOutputDevice *input_device); -void soundio_input_device_clear_buffer(struct SoundIoOutputDevice *device); +void soundio_input_device_clear_buffer(struct SoundIoOutputDevice *input_device); #ifdef __cplusplus }