Skip to content

Commit

Permalink
Merge pull request #390 from b-ma/fix/389
Browse files Browse the repository at this point in the history
fix: make sure destination is in node collection to consider graph active
  • Loading branch information
orottier authored Nov 18, 2023
2 parents ba7b5f3 + 465000a commit 117b9ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use online::*;

// magic node values
/// Destination node id is always at index 0
const DESTINATION_NODE_ID: AudioNodeId = AudioNodeId(0);
pub(crate) const DESTINATION_NODE_ID: AudioNodeId = AudioNodeId(0);
/// listener node id is always at index 1
const LISTENER_NODE_ID: AudioNodeId = AudioNodeId(1);
/// listener audio parameters ids are always at index 2 through 10
Expand Down
13 changes: 13 additions & 0 deletions src/render/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ impl Graph {
#[cfg(test)]
mod tests {
use super::*;
use crate::context::DESTINATION_NODE_ID;

#[derive(Debug, Clone)]
struct TestNode {
Expand Down Expand Up @@ -528,6 +529,18 @@ mod tests {
graph.add_edge((AudioNodeId(from), 0), (AudioNodeId(to), usize::MAX));
}

// regression test for:
// https://github.com/orottier/web-audio-api-rs/issues/389
#[test]
fn test_active() {
let mut graph = Graph::new(llq::Queue::new().split().0);
assert!(!graph.is_active());
// graph is active only when AudioDestination is set up
let node = Box::new(TestNode { tail_time: false });
add_node(&mut graph, DESTINATION_NODE_ID.0, node.clone());
assert!(graph.is_active());
}

#[test]
fn test_add_remove() {
let mut graph = Graph::new(llq::Queue::new().split().0);
Expand Down
20 changes: 18 additions & 2 deletions src/render/node_collection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::context::AudioNodeId;
use crate::context::{AudioNodeId, DESTINATION_NODE_ID};
use crate::render::graph::Node;

use std::cell::RefCell;
Expand All @@ -17,8 +17,11 @@ impl NodeCollection {
instance
}

// NodeCollection is considered empty until the destination is set up
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
let destination_id = DESTINATION_NODE_ID.0 as usize;
self.nodes[destination_id].is_none()
}

#[inline(always)]
Expand Down Expand Up @@ -104,3 +107,16 @@ impl IndexMut<AudioNodeId> for NodeCollection {
.unwrap_or_else(|| panic!("Index {} for dropped Node in NodeCollection", index.0))
}
}

#[cfg(test)]
mod tests {
use super::*;

// regression test for:
// https://github.com/orottier/web-audio-api-rs/issues/389
#[test]
fn test_empty() {
let nodes = NodeCollection::new();
assert!(nodes.is_empty());
}
}

0 comments on commit 117b9ae

Please sign in to comment.