From 0dee34a84ec9f7d79ca9625a7f44dbf663ef3f60 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 29 Jul 2015 13:02:17 -0700 Subject: [PATCH] PulseAudio: fix microphone example --- example/microphone.c | 12 +++++++++--- src/pulseaudio.cpp | 2 ++ src/soundio.cpp | 1 + src/soundio.h | 6 ++++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/example/microphone.c b/example/microphone.c index f228575..be4da6a 100644 --- a/example/microphone.c +++ b/example/microphone.c @@ -61,6 +61,7 @@ static void read_callback(struct SoundIoInStream *instream, int available_frame_ int write_count = min_int(available_frame_count, free_count); int frames_left = write_count; + for (;;) { int frame_count = frames_left; @@ -115,6 +116,9 @@ static void write_callback(struct SoundIoOutStream *outstream, int requested_fra for (;;) { int frame_count = frames_left; + if (frame_count <= 0) + break; + if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) panic("begin write error: %s", soundio_strerror(err)); @@ -133,9 +137,6 @@ static void write_callback(struct SoundIoOutStream *outstream, int requested_fra panic("end write error: %s", soundio_strerror(err)); frames_left -= frame_count; - - if (frames_left <= 0) - break; } soundio_ring_buffer_advance_read_ptr(ring_buffer, read_frames * outstream->bytes_per_frame); @@ -289,6 +290,7 @@ int main(int argc, char **argv) { outstream->period_duration = 0.1; outstream->write_callback = write_callback; outstream->underflow_callback = underflow_callback; + outstream->prebuf_duration = 0.0; if ((err = soundio_outstream_open(outstream))) panic("unable to open output stream: %s", soundio_strerror(err)); @@ -297,6 +299,10 @@ int main(int argc, char **argv) { ring_buffer = soundio_ring_buffer_create(soundio, capacity); if (!ring_buffer) panic("unable to create ring buffer: out of memory"); + char *buf = soundio_ring_buffer_write_ptr(ring_buffer); + int fill_count = 0.2 * outstream->sample_rate * outstream->bytes_per_frame; + memset(buf, 0, fill_count); + soundio_ring_buffer_advance_write_ptr(ring_buffer, fill_count); if ((err = soundio_instream_start(instream))) panic("unable to start input device: %s", soundio_strerror(err)); diff --git a/src/pulseaudio.cpp b/src/pulseaudio.cpp index ccbc5e4..1503232 100644 --- a/src/pulseaudio.cpp +++ b/src/pulseaudio.cpp @@ -698,7 +698,9 @@ static int outstream_begin_write_pa(SoundIoPrivate *si, SoundIoOutStream *outstream = &os->pub; SoundIoOutStreamPulseAudio *ospa = &os->backend_data.pulseaudio; pa_stream *stream = ospa->stream; + size_t byte_count = *frame_count * outstream->bytes_per_frame; + if (pa_stream_begin_write(stream, (void**)&ospa->write_ptr, &byte_count)) return SoundIoErrorStreaming; diff --git a/src/soundio.cpp b/src/soundio.cpp index 047f82a..f5674f0 100644 --- a/src/soundio.cpp +++ b/src/soundio.cpp @@ -330,6 +330,7 @@ int soundio_outstream_begin_write(struct SoundIoOutStream *outstream, SoundIo *soundio = outstream->device->soundio; SoundIoPrivate *si = (SoundIoPrivate *)soundio; SoundIoOutStreamPrivate *os = (SoundIoOutStreamPrivate *)outstream; + assert(*frame_count > 0); return si->outstream_begin_write(si, os, areas, frame_count); } diff --git a/src/soundio.h b/src/soundio.h index f9d6239..3c3688a 100644 --- a/src/soundio.h +++ b/src/soundio.h @@ -368,7 +368,8 @@ struct SoundIoOutStream { // Defaults to NULL. Put whatever you want here. void *userdata; // In this callback, you call `soundio_outstream_begin_write` and - // `soundio_outstream_end_write`. + // `soundio_outstream_end_write`. `requested_frame_count` will always be + // greater than 0. void (*write_callback)(struct SoundIoOutStream *, int requested_frame_count); // This optional callback happens when the sound device runs out of buffered // audio data to play. After this occurs, the outstream waits until the @@ -631,7 +632,8 @@ int soundio_outstream_start(struct SoundIoOutStream *outstream); // * `areas` - (out) The memory addresses you can write data to. It is OK to // modify the pointers if that helps you iterate. // * `frame_count` - (in/out) Provide the number of frames you want to write. -// Returned will be the number of frames you actually can write. +// Returned will be the number of frames you actually can write. Must be +// greater than 0 frames. // It is your responsibility to call this function no more and no fewer than the // correct number of times as determined by `requested_frame_count` from // `write_callback`. See sine.c for an example.