@@ -16,7 +16,7 @@ use processing_core::config::{Config, ConfigKey};
1616use crate :: device:: { self , AudioDeviceState , AudioStatus } ;
1717use crate :: graph:: { self , AudioNodeKind , AudioPorts } ;
1818use crate :: native:: { self , AudioBackend } ;
19- use crate :: sample;
19+ use crate :: { patchbay , sample} ;
2020
2121const MOCK_RATE : NonZeroU32 = NonZeroU32 :: new ( 48_000 ) . unwrap ( ) ;
2222const MOCK_CHANNELS : usize = 2 ;
@@ -283,6 +283,66 @@ fn sample_plays_through_a_sampler_to_completion() {
283283 assert ! ( began. elapsed( ) > std:: time:: Duration :: from_millis( 500 ) ) ;
284284}
285285
286+ #[ test]
287+ fn meter_reads_a_signal ( ) {
288+ let mut app = test_app ( 2 ) ;
289+ let world = app. world_mut ( ) ;
290+ let noise = graph:: node_create ( In ( ( AudioNodeKind :: WhiteNoise , 1 ) ) , world) . unwrap ( ) ;
291+ let meter = graph:: node_create ( In ( ( AudioNodeKind :: Meter , 1 ) ) , world) . unwrap ( ) ;
292+ let silent = graph:: node_create ( In ( ( AudioNodeKind :: Meter , 1 ) ) , world) . unwrap ( ) ;
293+ graph:: connect ( In ( ( noise, meter, vec ! [ ( 0 , 0 ) ] ) ) , world) . unwrap ( ) ;
294+
295+ let began = std:: time:: Instant :: now ( ) ;
296+ let mut level = 0.0 ;
297+ while level == 0.0 && began. elapsed ( ) < std:: time:: Duration :: from_secs ( 3 ) {
298+ app. update ( ) ;
299+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 10 ) ) ;
300+ level = graph:: meter_level ( In ( meter) , app. world_mut ( ) ) . unwrap ( ) ;
301+ }
302+ assert ! ( level > 0.01 , "meter never saw the noise (level {level})" ) ;
303+ assert_eq ! (
304+ graph:: meter_level( In ( silent) , app. world_mut( ) ) . unwrap( ) ,
305+ 0.0
306+ ) ;
307+ assert ! ( graph:: meter_level( In ( noise) , app. world_mut( ) ) . is_err( ) ) ;
308+ }
309+
310+ #[ test]
311+ fn jacks_pass_signal_through_and_meter_it ( ) {
312+ let mut app = test_app ( 4 ) ;
313+ let world = app. world_mut ( ) ;
314+ let out2 = patchbay:: jack_output ( In ( 2 ) , world) . unwrap ( ) ;
315+ assert ! ( patchbay:: jack_output( In ( 4 ) , world) . is_err( ) ) ;
316+ assert ! ( patchbay:: jack_input( In ( 3 ) , world) . is_ok( ) ) ;
317+
318+ // noise -> a meter -> output jack 2: signal on jack 2 proves the first
319+ // meter passes audio through, and the jack meters it
320+ let noise = graph:: node_create ( In ( ( AudioNodeKind :: WhiteNoise , 1 ) ) , world) . unwrap ( ) ;
321+ let inline = graph:: node_create ( In ( ( AudioNodeKind :: Meter , 1 ) ) , world) . unwrap ( ) ;
322+ graph:: connect ( In ( ( noise, inline, vec ! [ ( 0 , 0 ) ] ) ) , world) . unwrap ( ) ;
323+ graph:: connect ( In ( ( inline, out2, vec ! [ ( 0 , 0 ) ] ) ) , world) . unwrap ( ) ;
324+
325+ let output = graph:: graph_output ( world) . unwrap ( ) ;
326+ let began = std:: time:: Instant :: now ( ) ;
327+ let mut levels = patchbay:: AudioJackLevels :: default ( ) ;
328+ while began. elapsed ( ) < std:: time:: Duration :: from_secs ( 3 ) {
329+ app. update ( ) ;
330+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 10 ) ) ;
331+ levels = patchbay:: jack_levels ( app. world_mut ( ) ) . unwrap ( ) ;
332+ if levels. outputs [ 2 ] > 0.0 {
333+ break ;
334+ }
335+ }
336+ assert_eq ! ( levels. inputs. len( ) , 4 ) ;
337+ assert_eq ! ( levels. outputs. len( ) , 4 ) ;
338+ assert ! ( levels. outputs[ 2 ] > 0.01 , "no signal on jack 2: {levels:?}" ) ;
339+ assert_eq ! ( levels. outputs[ 0 ] , 0.0 ) ;
340+
341+ // and the jack is wired to graph output port 2
342+ let ( j, o) = ( node_id ( & app, out2) , node_id ( & app, output) ) ;
343+ assert ! ( edges( & mut app) . contains( & ( j, 0 , o, 2 ) ) ) ;
344+ }
345+
286346#[ test]
287347fn unwired_nodes_are_not_auto_connected ( ) {
288348 let mut app = test_app ( 2 ) ;
@@ -341,6 +401,33 @@ fn startup_opens_the_stream() {
341401 assert_eq ! ( world. resource:: <AudioDeviceState >( ) . want_input, None ) ;
342402}
343403
404+ #[ test]
405+ fn restart_keeps_the_patch ( ) {
406+ let mut app = test_app ( 8 ) ;
407+ let world = app. world_mut ( ) ;
408+ let input = graph:: graph_input ( world) . unwrap ( ) ;
409+ let output = graph:: graph_output ( world) . unwrap ( ) ;
410+ let filter = graph:: node_create ( In ( ( AudioNodeKind :: Lowpass , 1 ) ) , world) . unwrap ( ) ;
411+ graph:: connect ( In ( ( input, filter, vec ! [ ( 1 , 0 ) ] ) ) , world) . unwrap ( ) ;
412+ graph:: connect ( In ( ( filter, output, vec ! [ ( 0 , 3 ) ] ) ) , world) . unwrap ( ) ;
413+ app. update ( ) ;
414+ let before = edges ( & mut app) ;
415+ assert_eq ! ( before. len( ) , 2 ) ;
416+
417+ stop_mock_stream ( app. world_mut ( ) ) ;
418+ // the graph can still be edited while the stream is down
419+ let world = app. world_mut ( ) ;
420+ let extra = graph:: node_create ( In ( ( AudioNodeKind :: Meter , 1 ) ) , world) . unwrap ( ) ;
421+ graph:: connect ( In ( ( input, extra, vec ! [ ( 2 , 0 ) ] ) ) , world) . unwrap ( ) ;
422+ app. update ( ) ;
423+
424+ start_mock_stream ( app. world_mut ( ) ) ;
425+ app. update ( ) ;
426+ let after = edges ( & mut app) ;
427+ assert_eq ! ( after. len( ) , 3 ) ;
428+ assert ! ( before. iter( ) . all( |e| after. contains( e) ) ) ;
429+ }
430+
344431fn heartbeat ( app : & mut App ) -> i64 {
345432 app. world_mut ( )
346433 . resource_mut :: < AudioContext > ( )
@@ -362,6 +449,12 @@ fn assert_processing(app: &mut App, context: &str) {
362449#[ test]
363450fn restart_with_new_block_sizes_keeps_running ( ) {
364451 let mut app = test_app ( 2 ) ;
452+ let world = app. world_mut ( ) ;
453+ let noise = graph:: node_create ( In ( ( AudioNodeKind :: WhiteNoise , 1 ) ) , world) . unwrap ( ) ;
454+ let filter = graph:: node_create ( In ( ( AudioNodeKind :: Lowpass , 1 ) ) , world) . unwrap ( ) ;
455+ let meter = graph:: node_create ( In ( ( AudioNodeKind :: Meter , 1 ) ) , world) . unwrap ( ) ;
456+ graph:: connect ( In ( ( noise, filter, vec ! [ ( 0 , 0 ) ] ) ) , world) . unwrap ( ) ;
457+ graph:: connect ( In ( ( filter, meter, vec ! [ ( 0 , 0 ) ] ) ) , world) . unwrap ( ) ;
365458 app. update ( ) ;
366459 assert_processing ( & mut app, "before any restart" ) ;
367460
@@ -374,4 +467,13 @@ fn restart_with_new_block_sizes_keeps_running() {
374467 assert_processing ( & mut app, & format ! ( "after restarting at {frames} frames" ) ) ;
375468 app. update ( ) ;
376469 }
470+
471+ let began = Instant :: now ( ) ;
472+ let mut level = 0.0 ;
473+ while level == 0.0 && began. elapsed ( ) < Duration :: from_secs ( 3 ) {
474+ app. update ( ) ;
475+ std:: thread:: sleep ( Duration :: from_millis ( 10 ) ) ;
476+ level = graph:: meter_level ( In ( meter) , app. world_mut ( ) ) . unwrap ( ) ;
477+ }
478+ assert ! ( level > 0.01 , "patch lost across restarts (level {level})" ) ;
377479}
0 commit comments