Subtractive synth regression tests
examples/tutorials/tests/subtractive.rs · 153 source lines
Read the chapters in source order. Select a numbered source line to expand its explanation; the line numbers and code match this snapshot exactly.
cargo test -p ravet-examples --test subtractive --no-default-features --lockedChapters
Build short deterministic instrument blocks
Helpers construct validated MIDI events, a rate-matched synth patch and a no-input output block. Fixed arrays make the measured processing region independent of heap-owned fixture construction.
1use ravet_examples::synth::{DEFAULT_VALUES, SubtractiveSynth, SynthPatch};
Bring these module/type names into scope for “Build short deterministic instrument blocks”; the surrounding chapter explains their responsibilities.
Link to line 12use ravet_foundation::{
Bring these module/type names into scope for “Build short deterministic instrument blocks”; the surrounding chapter explains their responsibilities.
Link to line 23 AudioBuffer, AudioLayout, Event, EventData, MidiMessage, ProcessBlock, ProcessConfig,
Supply these names as the remaining imports, arguments or fields of the surrounding declaration.
Link to line 34 ProcessStatus, Processor, Transport,
Supply these names as the remaining imports, arguments or fields of the surrounding declaration.
Link to line 45};
Close the surrounding scope, value or argument list in “Build short deterministic instrument blocks”.
Link to line 56fn event(frame_offset: usize, bytes: &[u8]) -> Event {
Define event. Helpers construct validated MIDI events, a rate-matched synth patch and a no-input output block. Fixed arrays make the measured processing region independent of heap-owned fixture construction.
Link to line 67 Event {
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Build short deterministic instrument blocks” explanation above.
Link to line 78 frame_offset,
Supply these names as the remaining imports, arguments or fields of the surrounding declaration.
Link to line 89 data: EventData::Midi(MidiMessage::from_bytes(bytes).unwrap()),
Typed event payload; timing is kept separately in frame_offset.
Link to line 910 }
Close the surrounding scope, value or argument list in “Build short deterministic instrument blocks”.
Link to line 1011}
Close the surrounding scope, value or argument list in “Build short deterministic instrument blocks”.
Link to line 1112fn synth(values: [f32; 9]) -> SubtractiveSynth {
Define synth. Helpers construct validated MIDI events, a rate-matched synth patch and a no-input output block. Fixed arrays make the measured processing region independent of heap-owned fixture construction.
Link to line 1213 let mut synth = SubtractiveSynth::new();
Bind mutable synth to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 1314 synth
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Build short deterministic instrument blocks” explanation above.
Link to line 1415 .set_patch(&SynthPatch::new(48_000., values).unwrap())
Call the displayed operation with these arguments as the next step in “Build short deterministic instrument blocks”. The chapter describes its role and validation.
Link to line 1516 .unwrap();
Call the displayed operation with these arguments as the next step in “Build short deterministic instrument blocks”. The chapter describes its role and validation.
Link to line 1617 synth
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Build short deterministic instrument blocks” explanation above.
Link to line 1718 .prepare(ProcessConfig::new(48_000., 1024, 128, AudioLayout::MONO_INSTRUMENT).unwrap())
Call the displayed operation with these arguments as the next step in “Build short deterministic instrument blocks”. The chapter describes its role and validation.
Link to line 1819 .unwrap();
Call the displayed operation with these arguments as the next step in “Build short deterministic instrument blocks”. The chapter describes its role and validation.
Link to line 1920 synth
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Build short deterministic instrument blocks” explanation above.
Link to line 2021}
Close the surrounding scope, value or argument list in “Build short deterministic instrument blocks”.
Link to line 2122fn process(synth: &mut SubtractiveSynth, output: &mut [f32], events: &[Event]) -> ProcessStatus {
Define process. Helpers construct validated MIDI events, a rate-matched synth patch and a no-input output block. Fixed arrays make the measured processing region independent of heap-owned fixture construction.
Link to line 2223 let frames = output.len();
Bind frames to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 2324 let mut views = [output];
Bind mutable views to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 2425 let audio = AudioBuffer::separate(&[], &mut views, frames).unwrap();
Borrow distinct input/output slices. An instrument supplies an empty input slice; output storage already belongs to the caller.
Link to line 2526 synth.process(&mut ProcessBlock::new(audio, events, Transport::default()))
Combine borrowed audio, the active input-event slice and explicit transport into one processing view.
Link to line 2627}
Close the surrounding scope, value or argument list in “Build short deterministic instrument blocks”.
Link to line 2728#[test]
Apply this Rust attribute to the following item. cfg selects which target/feature/test build contains it; derive generates the named trait implementations.
Link to line 28Check envelope targets and release completion
Use known timings to test attack/decay/sustain and release, then ensure the voice is finished when its envelope reaches Idle. One shared envelope drives both VCA and filter excursion.
29fn shared_adsr_hits_targets_and_release_terminates_audio() {
Define shared_adsr_hits_targets_and_release_terminates_audio. Use known timings to test attack/decay/sustain and release, then ensure the voice is finished when its envelope reaches Idle. One shared envelope drives both VCA and filter excursion.
Link to line 2930 let mut values = DEFAULT_VALUES;
Bind mutable values to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 3031 values[5] = 1.;
Assign the computed value to values[5]. The right-hand expression supplies the new value for this step.
Link to line 3132 values[6] = 1.;
Assign the computed value to values[6]. The right-hand expression supplies the new value for this step.
Link to line 3233 values[7] = 0.5;
Assign the computed value to values[7]. The right-hand expression supplies the new value for this step.
Link to line 3334 values[8] = 1.;
Assign the computed value to values[8]. The right-hand expression supplies the new value for this step.
Link to line 3435 let mut synth = synth(values);
Bind mutable synth to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 3536 let mut output = [0.; 48];
Bind mutable output to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 3637 assert_eq!(
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 3738 process(&mut synth, &mut output, &[event(0, &[0x90, 69, 127])]),
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Check envelope targets and release completion” explanation above.
Link to line 3839 ProcessStatus::Continue
Represent a successfully processed block. The offline renderer treats any different status as a failed rendering session.
Link to line 3940 );
Close the current expression or scope; no additional processing operation occurs on this line.
Link to line 4041 assert_eq!(synth.envelope_levels()[0], 1.);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 4142 process(&mut synth, &mut output, &[]);
Call the displayed operation with these arguments as the next step in “Check envelope targets and release completion”. The chapter describes its role and validation.
Link to line 4243 assert_eq!(synth.envelope_levels()[0], 0.5);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 4344 process(&mut synth, &mut output, &[event(0, &[0x80, 69, 0])]);
Call the displayed operation with these arguments as the next step in “Check envelope targets and release completion”. The chapter describes its role and validation.
Link to line 4445 assert_eq!(synth.envelope_levels()[0], 0.);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 4546 assert_eq!(output[47], 0.);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 4647 output.fill(9.);
Call the displayed operation with these arguments as the next step in “Check envelope targets and release completion”. The chapter describes its role and validation.
Link to line 4748 process(&mut synth, &mut output, &[]);
Call the displayed operation with these arguments as the next step in “Check envelope targets and release completion”. The chapter describes its role and validation.
Link to line 4849 assert_eq!(output, [0.; 48]);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 4950}
Close the surrounding scope, value or argument list in “Check envelope targets and release completion”.
Link to line 5051#[test]
Apply this Rust attribute to the following item. cfg selects which target/feature/test build contains it; derive generates the named trait implementations.
Link to line 51Compare waveforms and low-pass behavior
Exercise all four waveform selections and compare measured energy for cutoff choices. The triangle may alias; these offline energy checks do not establish an anti-aliasing or headroom guarantee.
52fn all_waveforms_sound_and_vcf_cutoff_attenuates_high_frequency() {
Define all_waveforms_sound_and_vcf_cutoff_attenuates_high_frequency. Exercise all four waveform selections and compare measured energy for cutoff choices. The triangle may alias; these offline energy checks do not establish an anti-aliasing or headroom guarantee.
Link to line 5253 let mut outputs = Vec::new();
Bind mutable outputs to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 5354 for wave in 0..4 {
Evaluate this condition or visit this range in source order. The statements in the following scope implement “Compare waveforms and low-pass behavior”.
Link to line 5455 let mut values = DEFAULT_VALUES;
Bind mutable values to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 5556 values[0] = wave as f32;
Assign the computed value to values[0]. The right-hand expression supplies the new value for this step.
Link to line 5657 values[2] = 18_000.;
Assign the computed value to values[2]. The right-hand expression supplies the new value for this step.
Link to line 5758 values[4] = 0.;
Disable the envelope’s positive filter excursion for this fixture. Its VCA envelope still controls amplitude.
Link to line 5859 values[5] = 0.;
Assign the computed value to values[5]. The right-hand expression supplies the new value for this step.
Link to line 5960 values[6] = 0.;
Assign the computed value to values[6]. The right-hand expression supplies the new value for this step.
Link to line 6061 values[7] = 1.;
Assign the computed value to values[7]. The right-hand expression supplies the new value for this step.
Link to line 6162 let mut s = synth(values);
Bind mutable s to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 6263 let mut output = [0.; 1024];
Bind mutable output to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 6364 process(&mut s, &mut output, &[event(0, &[0x90, 93, 127])]);
Call the displayed operation with these arguments as the next step in “Compare waveforms and low-pass behavior”. The chapter describes its role and validation.
Link to line 6465 assert!(output.iter().all(|v| v.is_finite()));
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 6566 assert!(output.iter().any(|v| v.abs() > 0.01));
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 6667 outputs.push(output);
Append the supplied item to this owned collection. UI/fixture collections grow outside the audio callback.
Link to line 6768 }
Close the surrounding scope, value or argument list in “Compare waveforms and low-pass behavior”.
Link to line 6869 for i in 0..4 {
Evaluate this condition or visit this range in source order. The statements in the following scope implement “Compare waveforms and low-pass behavior”.
Link to line 6970 for j in i + 1..4 {
Evaluate this condition or visit this range in source order. The statements in the following scope implement “Compare waveforms and low-pass behavior”.
Link to line 7071 assert!(
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 7172 outputs[i]
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Compare waveforms and low-pass behavior” explanation above.
Link to line 7273 .iter()
Borrow collection entries for iteration without moving or copying the whole collection.
Link to line 7374 .zip(outputs[j])
Pair each entry with the corresponding entry in the second sequence. Iteration ends when either sequence ends.
Link to line 7475 .any(|(a, b)| (a - b).abs() > 0.01)
Return true if any entry satisfies this predicate; stop at the first match.
Link to line 7576 );
Close the current expression or scope; no additional processing operation occurs on this line.
Link to line 7677 }
Close the surrounding scope, value or argument list in “Compare waveforms and low-pass behavior”.
Link to line 7778 }
Close the surrounding scope, value or argument list in “Compare waveforms and low-pass behavior”.
Link to line 7879 let energy = |cutoff: f32| {
Bind energy to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 7980 let mut values = DEFAULT_VALUES;
Bind mutable values to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 8081 values[0] = 0.;
Select the sine waveform for this deterministic rendering fixture. The live default patch selects saw.
Link to line 8182 values[2] = cutoff;
Assign the computed value to values[2]. The right-hand expression supplies the new value for this step.
Link to line 8283 values[4] = 0.;
Disable the envelope’s positive filter excursion for this fixture. Its VCA envelope still controls amplitude.
Link to line 8384 values[5] = 0.;
Assign the computed value to values[5]. The right-hand expression supplies the new value for this step.
Link to line 8485 values[6] = 0.;
Assign the computed value to values[6]. The right-hand expression supplies the new value for this step.
Link to line 8586 values[7] = 1.;
Assign the computed value to values[7]. The right-hand expression supplies the new value for this step.
Link to line 8687 let mut s = synth(values);
Bind mutable s to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 8788 let mut out = [0.; 1024];
Bind mutable out to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 8889 process(&mut s, &mut out, &[event(0, &[0x90, 93, 127])]);
Call the displayed operation with these arguments as the next step in “Compare waveforms and low-pass behavior”. The chapter describes its role and validation.
Link to line 8990 for _ in 0..4 {
Evaluate this condition or visit this range in source order. The statements in the following scope implement “Compare waveforms and low-pass behavior”.
Link to line 9091 process(&mut s, &mut out, &[]);
Call the displayed operation with these arguments as the next step in “Compare waveforms and low-pass behavior”. The chapter describes its role and validation.
Link to line 9192 }
Close the surrounding scope, value or argument list in “Compare waveforms and low-pass behavior”.
Link to line 9293 out.iter().map(|v| f64::from(*v).powi(2)).sum::<f64>()
Borrow collection entries for iteration without moving or copying the whole collection.
Link to line 9394 };
Close the surrounding scope, value or argument list in “Compare waveforms and low-pass behavior”.
Link to line 9495 assert!(energy(100.) < energy(10_000.) * 0.001);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 9596}
Close the surrounding scope, value or argument list in “Compare waveforms and low-pass behavior”.
Link to line 9697#[test]
Apply this Rust attribute to the following item. cfg selects which target/feature/test build contains it; derive generates the named trait implementations.
Link to line 97Check patch validation and state preservation
Reject invalid controls and active-rate mismatches before mutation. Check note-on latching for ADSR and complete invalid-block rejection, ensuring a fault does not advance the existing voice history.
98fn patch_validation_latching_and_whole_block_fault_preserve_history() {
Define patch_validation_latching_and_whole_block_fault_preserve_history. Reject invalid controls and active-rate mismatches before mutation. Check note-on latching for ADSR and complete invalid-block rejection, ensuring a fault does not advance the existing voice history.
Link to line 9899 let mut values = DEFAULT_VALUES;
Bind mutable values to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 99100 values[5] = 10.;
Assign the computed value to values[5]. The right-hand expression supplies the new value for this step.
Link to line 100101 values[6] = 0.;
Assign the computed value to values[6]. The right-hand expression supplies the new value for this step.
Link to line 101102 values[7] = 1.;
Assign the computed value to values[7]. The right-hand expression supplies the new value for this step.
Link to line 102103 let mut s = synth(values);
Bind mutable s to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 103104 let mut out = [0.; 48];
Bind mutable out to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 104105 process(&mut s, &mut out, &[event(0, &[0x90, 69, 100])]);
Call the displayed operation with these arguments as the next step in “Check patch validation and state preservation”. The chapter describes its role and validation.
Link to line 105106 let level = s.envelope_levels()[0];
Bind level to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 106107 assert!(level > 0.09 && level < 0.11);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 107108 let mut changed = values;
Bind mutable changed to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 108109 changed[0] = 2.;
Assign the computed value to changed[0]. The right-hand expression supplies the new value for this step.
Link to line 109110 changed[5] = 0.;
Assign the computed value to changed[5]. The right-hand expression supplies the new value for this step.
Link to line 110111 s.set_patch(&SynthPatch::new(48_000., changed).unwrap())
Call the displayed operation with these arguments as the next step in “Check patch validation and state preservation”. The chapter describes its role and validation.
Link to line 111112 .unwrap();
Call the displayed operation with these arguments as the next step in “Check patch validation and state preservation”. The chapter describes its role and validation.
Link to line 112113 process(&mut s, &mut out, &[]);
Call the displayed operation with these arguments as the next step in “Check patch validation and state preservation”. The chapter describes its role and validation.
Link to line 113114 assert!(s.envelope_levels()[0] > 0.19 && s.envelope_levels()[0] < 0.21);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 114115 let before = s.envelope_levels();
Bind before to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 115116 out.fill(8.);
Call the displayed operation with these arguments as the next step in “Check patch validation and state preservation”. The chapter describes its role and validation.
Link to line 116117 assert!(matches!(
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 117118 process(&mut s, &mut out, &[event(48, &[0x90, 60, 100])]),
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Check patch validation and state preservation” explanation above.
Link to line 118119 ProcessStatus::Fault(_)
Define or initialize ProcessStatus with the displayed type/value. Its ownership and use are described in “Check patch validation and state preservation”.
Link to line 119120 ));
Close the current expression or scope; no additional processing operation occurs on this line.
Link to line 120121 assert_eq!(out, [0.; 48]);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 121122 assert_eq!(s.envelope_levels(), before);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 122123 process(&mut s, &mut out, &[event(0, &[0x90, 72, 100])]);
Call the displayed operation with these arguments as the next step in “Check patch validation and state preservation”. The chapter describes its role and validation.
Link to line 123124 assert_eq!(s.envelope_levels()[1], 1.);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 124125 assert!(
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 125126 s.set_patch(&SynthPatch::new(96_000., values).unwrap())
Call the displayed operation with these arguments as the next step in “Check patch validation and state preservation”. The chapter describes its role and validation.
Link to line 126127 .is_err()
Call the displayed operation with these arguments as the next step in “Check patch validation and state preservation”. The chapter describes its role and validation.
Link to line 127128 );
Close the current expression or scope; no additional processing operation occurs on this line.
Link to line 128129 for (index, value) in [(0, 0.5), (2, f32::NAN), (3, 9.), (5, -1.)] {
Evaluate this condition or visit this range in source order. The statements in the following scope implement “Check patch validation and state preservation”.
Link to line 129130 let mut bad = values;
Bind mutable bad to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 130131 bad[index] = value;
Assign the computed value to bad[index]. The right-hand expression supplies the new value for this step.
Link to line 131132 assert!(SynthPatch::new(48_000., bad).is_err());
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 132133 }
Close the surrounding scope, value or argument list in “Check patch validation and state preservation”.
Link to line 133134}
Close the surrounding scope, value or argument list in “Check patch validation and state preservation”.
Link to line 134135#[test]
Apply this Rust attribute to the following item. cfg selects which target/feature/test build contains it; derive generates the named trait implementations.
Link to line 135Measure callbacks and fixed-patch changes
Prepare patch/voice resources before measurement. Repeated processing, set_patch and reset run in the measured region and must cause no heap operations. The test deliberately includes held-note state changes.
136fn prepared_synthesis_patch_changes_and_reset_have_no_heap_operations() {
Define prepared_synthesis_patch_changes_and_reset_have_no_heap_operations. Prepare patch/voice resources before measurement. Repeated processing, set_patch and reset run in the measured region and must cause no heap operations. The test deliberately includes held-note state changes.
Link to line 136137 let mut s = synth(DEFAULT_VALUES);
Bind mutable s to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 137138 let patch = SynthPatch::new(48_000., DEFAULT_VALUES).unwrap();
Bind patch to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 138139 let mut out = [0.; 512];
Bind mutable out to the expression on this and any following continuation lines. The chapter above explains how this intermediate value is used.
Link to line 139140 let counts = allocation_counter::measure(|| {
Measure heap activity only in the enclosed processing/changes/reset region; preparation and assertions are outside it.
Link to line 140141 for _ in 0..1000 {
Evaluate this condition or visit this range in source order. The statements in the following scope implement “Measure callbacks and fixed-patch changes”.
Link to line 141142 s.set_patch(&patch).unwrap();
Call the displayed operation with these arguments as the next step in “Measure callbacks and fixed-patch changes”. The chapter describes its role and validation.
Link to line 142143 process(
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Measure callbacks and fixed-patch changes” explanation above.
Link to line 143144 &mut s,
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Measure callbacks and fixed-patch changes” explanation above.
Link to line 144145 &mut out,
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Measure callbacks and fixed-patch changes” explanation above.
Link to line 145146 &[event(0, &[0x90, 60, 100]), event(256, &[0x80, 60, 0])],
Continue the surrounding value or expression with this exact operand. Read it together with the preceding statement and the “Measure callbacks and fixed-patch changes” explanation above.
Link to line 146147 );
Close the current expression or scope; no additional processing operation occurs on this line.
Link to line 147148 s.reset();
Call the displayed operation with these arguments as the next step in “Measure callbacks and fixed-patch changes”. The chapter describes its role and validation.
Link to line 148149 }
Close the surrounding scope, value or argument list in “Measure callbacks and fixed-patch changes”.
Link to line 149150 });
Close the current expression or scope; no additional processing operation occurs on this line.
Link to line 150151 assert_eq!(counts.count_total, 0);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 151152 assert_eq!(counts.count_current, 0);
Verify the fixture’s expected result. A mismatch fails the test rather than becoming a successful support claim.
Link to line 152153}
Close the surrounding scope, value or argument list in “Measure callbacks and fixed-patch changes”.
Link to line 153