Skip to content

Commit 1799db8

Browse files
authored
Merge pull request #703 from RustAudio/sampleformat-error
Do not use cpal errors when rodio runs into an issue
2 parents b57c043 + 17692f5 commit 1799db8

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/stream.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ pub enum StreamError {
289289
SupportedStreamConfigsError(cpal::SupportedStreamConfigsError),
290290
/// Could not find any output device
291291
NoDevice,
292+
/// New cpal sample format that rodio does not yet support please open
293+
/// an issue if you run into this.
294+
UnsupportedSampleFormat,
292295
}
293296

294297
impl fmt::Display for StreamError {
@@ -299,6 +302,7 @@ impl fmt::Display for StreamError {
299302
Self::DefaultStreamConfigError(e) => e.fmt(f),
300303
Self::SupportedStreamConfigsError(e) => e.fmt(f),
301304
Self::NoDevice => write!(f, "NoDevice"),
305+
Self::UnsupportedSampleFormat => write!(f, "UnsupportedSampleFormat"),
302306
}
303307
}
304308
}
@@ -311,6 +315,7 @@ impl error::Error for StreamError {
311315
Self::DefaultStreamConfigError(e) => Some(e),
312316
Self::SupportedStreamConfigsError(e) => Some(e),
313317
Self::NoDevice => None,
318+
Self::UnsupportedSampleFormat => None,
314319
}
315320
}
316321
}
@@ -321,30 +326,28 @@ impl OutputStream {
321326
config: &OutputStreamConfig,
322327
) -> Result<OutputStream, StreamError> {
323328
let (controller, source) = mixer(config.channel_count, config.sample_rate);
324-
Self::init_stream(device, config, source)
325-
.map_err(StreamError::BuildStreamError)
326-
.and_then(|stream| {
327-
stream.play().map_err(StreamError::PlayStreamError)?;
328-
Ok(Self {
329-
_stream: stream,
330-
mixer: controller,
331-
})
329+
Self::init_stream(device, config, source).and_then(|stream| {
330+
stream.play().map_err(StreamError::PlayStreamError)?;
331+
Ok(Self {
332+
_stream: stream,
333+
mixer: controller,
332334
})
335+
})
333336
}
334337

335338
fn init_stream(
336339
device: &cpal::Device,
337340
config: &OutputStreamConfig,
338341
mut samples: MixerSource<f32>,
339-
) -> Result<cpal::Stream, cpal::BuildStreamError> {
342+
) -> Result<cpal::Stream, StreamError> {
340343
let error_callback = |err| {
341344
#[cfg(feature = "tracing")]
342345
tracing::error!("error initializing output stream: {err}");
343346
#[cfg(not(feature = "tracing"))]
344347
eprintln!("error initializing output stream: {err}");
345348
};
346349
let sample_format = config.sample_format;
347-
let config = config.into();
350+
let config: cpal::StreamConfig = config.into();
348351
match sample_format {
349352
cpal::SampleFormat::F32 => device.build_output_stream::<f32, _, _>(
350353
&config,
@@ -452,8 +455,9 @@ impl OutputStream {
452455
error_callback,
453456
None,
454457
),
455-
_ => Err(cpal::BuildStreamError::StreamConfigNotSupported),
458+
_ => return Err(StreamError::UnsupportedSampleFormat),
456459
}
460+
.map_err(StreamError::BuildStreamError)
457461
}
458462
}
459463

0 commit comments

Comments
 (0)