From 61a2210d70bea453e3eb2e441023ebed6e74fe25 Mon Sep 17 00:00:00 2001 From: VincentJousse Date: Thu, 30 Mar 2023 16:05:42 +0200 Subject: [PATCH] webaudio: set the channel_count in the AudioDestinationNode (#768) Co-authored-by: Vincent Jousse --- src/host/webaudio/mod.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/host/webaudio/mod.rs b/src/host/webaudio/mod.rs index 1debdfe..8c7efd7 100644 --- a/src/host/webaudio/mod.rs +++ b/src/host/webaudio/mod.rs @@ -220,15 +220,24 @@ impl DeviceTrait for Device { // Create the WebAudio stream. let mut stream_opts = AudioContextOptions::new(); stream_opts.sample_rate(config.sample_rate.0 as f32); - let ctx = Arc::new( - AudioContext::new_with_context_options(&stream_opts).map_err( - |err| -> BuildStreamError { - let description = format!("{:?}", err); - let err = BackendSpecificError { description }; - err.into() - }, - )?, - ); + let ctx = AudioContext::new_with_context_options(&stream_opts).map_err( + |err| -> BuildStreamError { + let description = format!("{:?}", err); + let err = BackendSpecificError { description }; + err.into() + }, + )?; + + let destination = ctx.destination(); + + // If possible, set the destination's channel_count to the given config.channel. + // If not, fallback on the default destination channel_count to keep previous behavior + // and do not return an error. + if config.channels as u32 <= destination.max_channel_count() { + destination.set_channel_count(config.channels as u32); + } + + let ctx = Arc::new(ctx); // A container for managing the lifecycle of the audio callbacks. let mut on_ended_closures: Vec>>>> = Vec::new();