PulseAudio: fix microphone example

This commit is contained in:
Andrew Kelley 2015-07-29 13:02:17 -07:00
parent 0847d727b1
commit 0dee34a84e
4 changed files with 16 additions and 5 deletions

View file

@ -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 write_count = min_int(available_frame_count, free_count);
int frames_left = write_count; int frames_left = write_count;
for (;;) { for (;;) {
int frame_count = frames_left; int frame_count = frames_left;
@ -115,6 +116,9 @@ static void write_callback(struct SoundIoOutStream *outstream, int requested_fra
for (;;) { for (;;) {
int frame_count = frames_left; int frame_count = frames_left;
if (frame_count <= 0)
break;
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count)))
panic("begin write error: %s", soundio_strerror(err)); 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)); panic("end write error: %s", soundio_strerror(err));
frames_left -= frame_count; frames_left -= frame_count;
if (frames_left <= 0)
break;
} }
soundio_ring_buffer_advance_read_ptr(ring_buffer, read_frames * outstream->bytes_per_frame); 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->period_duration = 0.1;
outstream->write_callback = write_callback; outstream->write_callback = write_callback;
outstream->underflow_callback = underflow_callback; outstream->underflow_callback = underflow_callback;
outstream->prebuf_duration = 0.0;
if ((err = soundio_outstream_open(outstream))) if ((err = soundio_outstream_open(outstream)))
panic("unable to open output stream: %s", soundio_strerror(err)); 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); ring_buffer = soundio_ring_buffer_create(soundio, capacity);
if (!ring_buffer) if (!ring_buffer)
panic("unable to create ring buffer: out of memory"); 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))) if ((err = soundio_instream_start(instream)))
panic("unable to start input device: %s", soundio_strerror(err)); panic("unable to start input device: %s", soundio_strerror(err));

View file

@ -698,7 +698,9 @@ static int outstream_begin_write_pa(SoundIoPrivate *si,
SoundIoOutStream *outstream = &os->pub; SoundIoOutStream *outstream = &os->pub;
SoundIoOutStreamPulseAudio *ospa = &os->backend_data.pulseaudio; SoundIoOutStreamPulseAudio *ospa = &os->backend_data.pulseaudio;
pa_stream *stream = ospa->stream; pa_stream *stream = ospa->stream;
size_t byte_count = *frame_count * outstream->bytes_per_frame; size_t byte_count = *frame_count * outstream->bytes_per_frame;
if (pa_stream_begin_write(stream, (void**)&ospa->write_ptr, &byte_count)) if (pa_stream_begin_write(stream, (void**)&ospa->write_ptr, &byte_count))
return SoundIoErrorStreaming; return SoundIoErrorStreaming;

View file

@ -330,6 +330,7 @@ int soundio_outstream_begin_write(struct SoundIoOutStream *outstream,
SoundIo *soundio = outstream->device->soundio; SoundIo *soundio = outstream->device->soundio;
SoundIoPrivate *si = (SoundIoPrivate *)soundio; SoundIoPrivate *si = (SoundIoPrivate *)soundio;
SoundIoOutStreamPrivate *os = (SoundIoOutStreamPrivate *)outstream; SoundIoOutStreamPrivate *os = (SoundIoOutStreamPrivate *)outstream;
assert(*frame_count > 0);
return si->outstream_begin_write(si, os, areas, frame_count); return si->outstream_begin_write(si, os, areas, frame_count);
} }

View file

@ -368,7 +368,8 @@ struct SoundIoOutStream {
// Defaults to NULL. Put whatever you want here. // Defaults to NULL. Put whatever you want here.
void *userdata; void *userdata;
// In this callback, you call `soundio_outstream_begin_write` and // 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); void (*write_callback)(struct SoundIoOutStream *, int requested_frame_count);
// This optional callback happens when the sound device runs out of buffered // This optional callback happens when the sound device runs out of buffered
// audio data to play. After this occurs, the outstream waits until the // 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 // * `areas` - (out) The memory addresses you can write data to. It is OK to
// modify the pointers if that helps you iterate. // modify the pointers if that helps you iterate.
// * `frame_count` - (in/out) Provide the number of frames you want to write. // * `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 // 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 // correct number of times as determined by `requested_frame_count` from
// `write_callback`. See sine.c for an example. // `write_callback`. See sine.c for an example.