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 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));

View file

@ -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;

View file

@ -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);
}

View file

@ -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.