From 757cb4f06908a1dfd2e4b23639ded5c254e439ae Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 13 Aug 2018 18:08:19 -0700 Subject: [PATCH] Set channel count for destination node in offline audio context Otherwise we will crash for offline contexts with > 2 channels --- audio/src/context.rs | 8 ++++---- audio/src/destination_node.rs | 3 ++- audio/src/graph.rs | 4 ++-- audio/src/node.rs | 1 - audio/src/render_thread.rs | 2 -- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/audio/src/context.rs b/audio/src/context.rs index fba24007..8c09ea79 100644 --- a/audio/src/context.rs +++ b/audio/src/context.rs @@ -114,14 +114,14 @@ pub struct AudioContext { impl AudioContext { /// Constructs a new audio context. pub fn new(options: AudioContextOptions) -> Self { - let sample_rate = match options { - AudioContextOptions::RealTimeAudioContext(ref options) => options.sample_rate, - AudioContextOptions::OfflineAudioContext(ref options) => options.sample_rate + let (sample_rate, channels) = match options { + AudioContextOptions::RealTimeAudioContext(ref options) => (options.sample_rate, 2), + AudioContextOptions::OfflineAudioContext(ref options) => (options.sample_rate, options.channels) }; let (sender, receiver) = mpsc::channel(); let sender_ = sender.clone(); - let graph = AudioGraph::new(); + let graph = AudioGraph::new(channels); let dest_node = graph.dest_id(); Builder::new() .name("AudioRenderThread".to_owned()) diff --git a/audio/src/destination_node.rs b/audio/src/destination_node.rs index 6feb0c78..150b6420 100644 --- a/audio/src/destination_node.rs +++ b/audio/src/destination_node.rs @@ -9,10 +9,11 @@ pub(crate) struct DestinationNode { } impl DestinationNode { - pub fn new() -> Self { + pub fn new(channel_count: u8) -> Self { DestinationNode { channel_info: ChannelInfo { mode: ChannelCountMode::Explicit, + count: channel_count, ..Default::default() }, chunk: None, diff --git a/audio/src/graph.rs b/audio/src/graph.rs index 16492603..ffeecb09 100644 --- a/audio/src/graph.rs +++ b/audio/src/graph.rs @@ -145,9 +145,9 @@ struct Connection { } impl AudioGraph { - pub fn new() -> Self { + pub fn new(channel_count: u8) -> Self { let mut graph = StableGraph::new(); - let dest_id = NodeId(graph.add_node(Node::new(Box::new(DestinationNode::new())))); + let dest_id = NodeId(graph.add_node(Node::new(Box::new(DestinationNode::new(channel_count))))); AudioGraph { graph, dest_id } } diff --git a/audio/src/node.rs b/audio/src/node.rs index e20d959f..5a8f13a8 100644 --- a/audio/src/node.rs +++ b/audio/src/node.rs @@ -19,7 +19,6 @@ pub enum AudioNodeInit { ConstantSourceNode, ConvolverNode, DelayNode, - DestinationNode, DynamicsCompressionNode, GainNode(GainNodeOptions), IIRFilterNode, diff --git a/audio/src/render_thread.rs b/audio/src/render_thread.rs index 337b0246..9c9c3c66 100644 --- a/audio/src/render_thread.rs +++ b/audio/src/render_thread.rs @@ -2,7 +2,6 @@ use block::{Chunk, Tick, FRAMES_PER_BLOCK}; use buffer_source_node::AudioBufferSourceNode; use channel_node::{ChannelMergerNode, ChannelSplitterNode}; use context::{AudioContextOptions, ProcessingState, StateChangeResult}; -use destination_node::DestinationNode; use gain_node::GainNode; use graph::{AudioGraph, InputPort, NodeId, OutputPort, PortId}; use node::BlockInfo; @@ -131,7 +130,6 @@ impl AudioRenderThread { AudioNodeInit::AudioBufferSourceNode(options) => { Box::new(AudioBufferSourceNode::new(options)) } - AudioNodeInit::DestinationNode => Box::new(DestinationNode::new()), AudioNodeInit::GainNode(options) => Box::new(GainNode::new(options)), AudioNodeInit::OscillatorNode(options) => Box::new(OscillatorNode::new(options)), AudioNodeInit::ChannelMergerNode(options) => Box::new(ChannelMergerNode::new(options)),