From 43d33e5a677cf925ed9583418d4825d7a441875f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 21 Jul 2015 02:12:28 -0700 Subject: [PATCH] don't access device until open() is called and then check for a probe error. --- README.md | 2 +- src/soundio.cpp | 62 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index fc17343..818178d 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,7 @@ view `coverage/index.html` in a browser. ## Roadmap + 0. ALSA: support devices that don't support mmap access (test with pulseaudio alsa default) 0. implement ALSA (Linux) backend, get examples working 0. ALSA: poll instead of callback 0. pipe record to playback example working with dummy linux, osx, windows @@ -261,7 +262,6 @@ view `coverage/index.html` in a browser. unused to a buffer for you. 0. add len arguments to APIs that have char * 0. custom allocator support - 0. ALSA: support devices that don't support mmap access 0. Test in an app that needs to synchronize video to test the latency/synchronization API. 0. Support PulseAudio proplist properties for main context and streams diff --git a/src/soundio.cpp b/src/soundio.cpp index f6427dd..6eede1b 100644 --- a/src/soundio.cpp +++ b/src/soundio.cpp @@ -382,14 +382,6 @@ struct SoundIoOutStream *soundio_outstream_create(struct SoundIoDevice *device) outstream->device = device; soundio_device_ref(device); - const SoundIoChannelLayout *stereo = soundio_channel_layout_get_builtin(SoundIoChannelLayoutIdStereo); - - outstream->format = soundio_device_supports_format(device, SoundIoFormatFloat32NE) ? - SoundIoFormatFloat32NE : device->formats[0]; - outstream->layout = soundio_device_supports_layout(device, stereo) ? *stereo : device->layouts[0]; - outstream->sample_rate = clamp(device->sample_rate_min, 48000, device->sample_rate_max); - outstream->name = "SoundIo"; - return outstream; } @@ -397,11 +389,31 @@ int soundio_outstream_open(struct SoundIoOutStream *outstream) { if (outstream->format <= SoundIoFormatInvalid) return SoundIoErrorInvalid; + SoundIoDevice *device = outstream->device; + if (device->probe_error) + return device->probe_error; + + if (outstream->format == SoundIoFormatInvalid) { + outstream->format = soundio_device_supports_format(device, SoundIoFormatFloat32NE) ? + SoundIoFormatFloat32NE : device->formats[0]; + } + + if (!outstream->layout.channel_count) { + const SoundIoChannelLayout *stereo = soundio_channel_layout_get_builtin(SoundIoChannelLayoutIdStereo); + outstream->layout = soundio_device_supports_layout(device, stereo) ? *stereo : device->layouts[0]; + } + + if (!outstream->sample_rate) + outstream->sample_rate = clamp(device->sample_rate_min, 48000, device->sample_rate_max); + + if (!outstream->name) + outstream->name = "SoundIo"; + SoundIoOutStreamPrivate *os = (SoundIoOutStreamPrivate *)outstream; outstream->bytes_per_frame = soundio_get_bytes_per_frame(outstream->format, outstream->layout.channel_count); outstream->bytes_per_sample = soundio_get_bytes_per_sample(outstream->format); - SoundIo *soundio = outstream->device->soundio; + SoundIo *soundio = device->soundio; SoundIoPrivate *si = (SoundIoPrivate *)soundio; return si->outstream_open(si, os); } @@ -444,14 +456,6 @@ struct SoundIoInStream *soundio_instream_create(struct SoundIoDevice *device) { instream->device = device; soundio_device_ref(device); - const SoundIoChannelLayout *stereo = soundio_channel_layout_get_builtin(SoundIoChannelLayoutIdStereo); - - instream->format = soundio_device_supports_format(device, SoundIoFormatFloat32NE) ? - SoundIoFormatFloat32NE : device->formats[0]; - instream->layout = soundio_device_supports_layout(device, stereo) ? *stereo : device->layouts[0]; - instream->sample_rate = clamp(device->sample_rate_min, 48000, device->sample_rate_max); - instream->name = "SoundIo"; - return instream; } @@ -459,9 +463,31 @@ int soundio_instream_open(struct SoundIoInStream *instream) { if (instream->format <= SoundIoFormatInvalid) return SoundIoErrorInvalid; + SoundIoDevice *device = instream->device; + + if (device->probe_error) + return device->probe_error; + + if (instream->format == SoundIoFormatInvalid) { + instream->format = soundio_device_supports_format(device, SoundIoFormatFloat32NE) ? + SoundIoFormatFloat32NE : device->formats[0]; + } + + if (!instream->layout.channel_count) { + const SoundIoChannelLayout *stereo = soundio_channel_layout_get_builtin(SoundIoChannelLayoutIdStereo); + instream->layout = soundio_device_supports_layout(device, stereo) ? *stereo : device->layouts[0]; + } + + if (!instream->sample_rate) + instream->sample_rate = clamp(device->sample_rate_min, 48000, device->sample_rate_max); + + if (!instream->name) + instream->name = "SoundIo"; + + instream->bytes_per_frame = soundio_get_bytes_per_frame(instream->format, instream->layout.channel_count); instream->bytes_per_sample = soundio_get_bytes_per_sample(instream->format); - SoundIo *soundio = instream->device->soundio; + SoundIo *soundio = device->soundio; SoundIoPrivate *si = (SoundIoPrivate *)soundio; SoundIoInStreamPrivate *is = (SoundIoInStreamPrivate *)instream; return si->instream_open(si, is);