{ "standard": "// Copyright (C) 2026 Yusuke Oda\n// SPDX-License-Identifier: AGPL-3.0-only\n//! \u901a\u5e38\u306e\u6f14\u594f\u30fb\u97f3\u8272\u30fb\u30aa\u30fc\u30c8\u30e1\u30fc\u30b7\u30e7\u30f3\u3092\u56fa\u5b9a\u3057\u305f\u6027\u80fd\u56de\u5e30\u30b9\u30a4\u30fc\u30c8\u3002\n#![allow(dead_code)] // ABI\u30fb\u4fdd\u5b58\u51e6\u7406\u306a\u3069\u3001\u8a08\u6e2c\u5bfe\u8c61\u4ee5\u5916\u306e\u65e2\u5b58\u5b9a\u7fa9\u3082\u8aad\u307f\u8fbc\u3080\u3002\n#[path = \"../src/abi.rs\"]\nmod abi;\n#[path = \"../src/distortion.rs\"]\nmod distortion;\n#[path = \"../src/effects.rs\"]\nmod effects;\n#[path = \"../src/delay.rs\"]\nmod delay;\n#[path = \"../src/reverb.rs\"]\nmod reverb;\n#[path = \"../src/eq.rs\"]\nmod eq;\n#[path = \"../src/envelope.rs\"]\nmod envelope;\n#[path = \"../src/filter.rs\"]\nmod filter;\n#[path = \"../src/float_mode.rs\"]\nmod float_mode;\n#[path = \"../src/oscillator.rs\"]\nmod oscillator;\n#[path = \"../src/parameters.rs\"]\nmod parameters;\n#[path = \"../src/random.rs\"]\nmod random;\n#[path = \"../src/synth.rs\"]\nmod synth;\n#[path = \"../src/voice.rs\"]\nmod voice;\n#[path = \"../src/volume.rs\"]\nmod volume;\n\nuse std::{\n hint::black_box,\n time::{Duration, Instant},\n};\nuse voice::Note;\n\n// \u8a2d\u5b9a\u306fUI\u306e\u521d\u671f\u5024\u304b\u3089\u72ec\u7acb\u3055\u305b\u3001\u65b0\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306e\u8ffd\u52a0\u6642\u306b\u306f\u660e\u793a\u7684\u306b\u898b\u76f4\u3059\u3002\nconst BASE: &[f64] = &[\n 0.,\n 10.,\n 0.,\n 120.,\n 0.7,\n 100.,\n 1.,\n 0.,\n 1.,\n 0.,\n 3600.,\n 1.,\n 1.,\n -6.,\n 10.,\n 0.,\n 100.,\n 0.7,\n 100.,\n 0.,\n 0.,\n 0.,\n 0.,\n 0.,\n 0.,\n f64::NEG_INFINITY,\n 0.,\n 0.,\n f64::NEG_INFINITY,\n 0.,\n 0.,\n 0.,\n 0.,\n 100.,\n 0.0,\n 250.0,\n 0.0,\n 1000.0,\n 0.0,\n 1.0,\n 4000.0,\n 0.0,\n 0.0, 0.0, 0.0, // Off / Off / Off.\n 1.0, 0.0, 100.0, 1.0, 0.0, 100.0,\n 5.0, 35.0, 25.0, 5.0, 35.0, 25.0, 5.0, 35.0, 25.0, // Delay\n 0.0, 0.0, 0.0, // Stereo\n 2.0, 6000.0, 100.0, 25.0, 2.0, 6000.0, 100.0, 25.0, 2.0, 6000.0, 100.0, 25.0, // Reverb\n];\nconst BASS: &[(usize, f64)] = &[\n (0, 3.),\n (1, 5.),\n (3, 180.),\n (4, 0.45),\n (9, 2.),\n (10, 1200.),\n (11, 1.2),\n (14, 0.),\n (16, 250.),\n (17, 0.),\n (19, 2400.),\n];\nconst LEAD: &[(usize, f64)] = &[(0, 2.), (6, 2.), (7, 7.), (9, 2.), (10, 2400.), (11, 0.9)];\nconst CHORD: &[(usize, f64)] = &[\n (0, 3.),\n (1, 15.),\n (3, 200.),\n (4, 0.6),\n (5, 400.),\n (6, 3.),\n (7, 9.),\n (8, 8.),\n (9, 2.),\n (11, 0.8),\n (20, 60.),\n];\nconst PAD: &[(usize, f64)] = &[\n (0, 3.),\n (1, 300.),\n (3, 500.),\n (5, 800.),\n (6, 5.),\n (7, 12.),\n (8, 8.),\n (9, 2.),\n (10, 2400.),\n (14, 600.),\n (16, 500.),\n (19, 1200.),\n (20, 80.),\n];\nconst BELL: &[(usize, f64)] = &[\n (1, 2.),\n (3, 1000.),\n (4, 0.),\n (5, 300.),\n (8, 4.),\n (24, 1200.),\n (27, 1900.),\n (29, 30.),\n (30, 10.),\n];\n\n#[derive(Clone, Copy)]\nenum Phrase {\n Mono,\n Bass,\n Arpeggio,\n Chord,\n Idle,\n}\n#[derive(Clone, Copy)]\nstruct Automation {\n index: usize,\n low: f64,\n high: f64,\n period_seconds: f64,\n}\nstruct Case {\n name: &'static str,\n phrase: Phrase,\n settings: &'static [(usize, f64)],\n extra: &'static [(usize, f64)],\n automation: Option,\n}\n\nfn cases() -> Vec {\n let mut cases = vec![\n Case {\n name: \"sine-mono\",\n phrase: Phrase::Mono,\n settings: &[],\n extra: &[],\n automation: None,\n },\n Case {\n name: \"saw-bass\",\n phrase: Phrase::Bass,\n settings: BASS,\n extra: &[],\n automation: None,\n },\n Case {\n name: \"triangle-pluck\",\n phrase: Phrase::Arpeggio,\n settings: &[\n (0, 1.),\n (1, 2.),\n (3, 250.),\n (4, 0.),\n (5, 180.),\n (8, 4.),\n (9, 1.),\n ],\n extra: &[],\n automation: None,\n },\n Case {\n name: \"square-lead\",\n phrase: Phrase::Mono,\n settings: LEAD,\n extra: &[],\n automation: None,\n },\n Case {\n name: \"saw-chords\",\n phrase: Phrase::Chord,\n settings: CHORD,\n extra: &[],\n automation: None,\n },\n Case {\n name: \"slow-pad\",\n phrase: Phrase::Chord,\n settings: PAD,\n extra: &[],\n automation: None,\n },\n Case {\n name: \"three-osc-keys\",\n phrase: Phrase::Chord,\n settings: &[\n (0, 3.),\n (5, 400.),\n (8, 8.),\n (9, 1.),\n (22, -6.),\n (23, 1.),\n (24, -1200.),\n (25, -12.),\n (28, -18.),\n ],\n extra: &[],\n automation: None,\n },\n Case {\n name: \"phase-mod-bell\",\n phrase: Phrase::Arpeggio,\n settings: BELL,\n extra: &[],\n automation: None,\n },\n Case {\n name: \"soft-bass\",\n phrase: Phrase::Bass,\n settings: BASS,\n extra: &[(31, 1.), (32, 9.), (33, 40.)],\n automation: None,\n },\n Case {\n name: \"hard-lead\",\n phrase: Phrase::Mono,\n settings: LEAD,\n extra: &[(31, 2.), (32, 6.), (33, 25.)],\n automation: None,\n },\n Case {\n name: \"idle-pad\",\n phrase: Phrase::Idle,\n settings: PAD,\n extra: &[],\n automation: None,\n },\n ];\n for (name, index, low, high, seconds) in [\n (\"auto-cutoff\", 10, 1200., 4800., 4.),\n (\"auto-resonance\", 11, 0.7, 2.5, 4.),\n (\"auto-drive\", 32, 3., 15., 4.),\n (\"auto-mix\", 33, 10., 50., 4.),\n (\"auto-pan\", 21, -40., 40., 4.),\n (\"auto-volume\", 13, -12., 0., 4.),\n (\"auto-width\", 20, 20., 80., 4.),\n (\"auto-phase-mod\", 29, 10., 40., 4.),\n ] {\n let distortion = index == 32 || index == 33;\n cases.push(Case {\n name,\n phrase: if index == 29 {\n Phrase::Arpeggio\n } else {\n Phrase::Chord\n },\n settings: if index == 29 { BELL } else { CHORD },\n extra: if distortion {\n &[(31, 1.), (32, 9.), (33, 40.)]\n } else {\n &[]\n },\n automation: Some(Automation {\n index,\n low,\n high,\n period_seconds: seconds,\n }),\n });\n }\n cases\n}\n\nstruct Event {\n at: usize,\n note: Note,\n velocity: f32,\n on: bool,\n}\nstruct Scenario {\n parameters: [f64; parameters::COUNT],\n events: Vec,\n // \u30db\u30b9\u30c8\u76f8\u5f53\u306e\u5909\u66f4\u70b9\u304b\u3089\u8a08\u7b97\u3057\u305f\u73fe\u5728\u5024\u3002\u8a08\u6e2c\u524d\u306b\u7528\u610f\u3059\u308b\u3002\n automation: Vec,\n parameter: Option,\n}\nfn scenario(case: &Case, rate: usize, block: usize, length: usize) -> Scenario {\n let mut plain: [f64; parameters::COUNT] = std::array::from_fn(|i| BASE[i]);\n for &(i, v) in case.settings.iter().chain(case.extra) {\n plain[i] = v;\n }\n if let Some(a) = case.automation {\n plain[a.index] = a.low;\n }\n plain[42] = f64::from(plain[31] != 0.0); // Distortion\u306e\u30b1\u30fc\u30b9\u3060\u3051\u5148\u982d\u30b9\u30ed\u30c3\u30c8\u3092\u4f7f\u3046\u3002\n assert!(\n plain\n .iter()\n .enumerate()\n .all(|(i, &v)| parameters::valid_plain(i, v))\n );\n let parameters = std::array::from_fn(|i| parameters::to_normalized(i, plain[i]));\n let mut events = Vec::new();\n let (step_ms, gate_ms) = match case.phrase {\n Phrase::Mono => (500, 400),\n Phrase::Bass => (250, 190),\n Phrase::Arpeggio => (250, 160),\n Phrase::Chord => (2000, 1750),\n Phrase::Idle => (2000, 0),\n };\n if !matches!(case.phrase, Phrase::Idle) {\n for n in 0.. {\n let at = n * step_ms * rate / 1000;\n if at >= length {\n break;\n }\n let pitches: &[i16] = match case.phrase {\n Phrase::Mono => &[60, 64, 67, 65, 62, 60, 67, 64][n % 8..n % 8 + 1],\n Phrase::Bass => &[36, 36, 43, 39, 41, 41, 43, 39][n % 8..n % 8 + 1],\n Phrase::Arpeggio => &[60, 64, 67, 72, 67, 64, 62, 67][n % 8..n % 8 + 1],\n Phrase::Chord if n % 2 == 0 => &[48, 55, 60, 64],\n Phrase::Chord => &[53, 60, 65, 69],\n Phrase::Idle => unreachable!(),\n };\n for (i, &pitch) in pitches.iter().enumerate() {\n let note = Note {\n id: (n * 4 + i + 1) as i32,\n channel: 0,\n pitch,\n };\n let velocity = [0.8, 0.65, 0.75, 0.7][n % 4];\n events.push(Event {\n at,\n note,\n velocity,\n on: true,\n });\n events.push(Event {\n at: at + gate_ms * rate / 1000,\n note,\n velocity,\n on: false,\n });\n }\n }\n }\n events.sort_by_key(|e| (e.at, e.on)); // \u540c\u6642\u523b\u306fNote Off\u2192Note On\u3002\n let mut automation = Vec::new();\n if let Some(a) = case.automation {\n automation.reserve(length);\n let point = |n: usize| {\n let phase = (n as f64 / rate as f64 / a.period_seconds).fract();\n let triangle = 1.0 - (2.0 * phase - 1.0).abs();\n parameters::to_normalized(a.index, a.low + (a.high - a.low) * triangle)\n };\n for start in (0..length).step_by(block) {\n let count = block.min(length - start);\n let before = point(start);\n let after = point(start + count);\n let value = |i: usize| {\n if i == count {\n after\n } else if parameters::holds_between_points(a.index) {\n before\n } else {\n before + (after - before) * i as f64 / count as f64\n }\n };\n // \u5404\u30d6\u30ed\u30c3\u30af\u672b\u5c3e\u306e\u65e2\u77e5\u70b9\u307e\u3067\u88dc\u9593\u3059\u308b\u3002\n for i in 1..=count {\n automation.push(value(i));\n }\n }\n }\n Scenario {\n parameters,\n events,\n automation,\n parameter: case.automation.map(|a| a.index),\n }\n}\n\n// \u6bd4\u8f03\u5143\u306b\u7cfb\u5217\u5165\u53e3\u304c\u306a\u3044\u7248\u3067\u306f\u5358\u767a\u547c\u51fa\u3057\u3078\u623b\u3059\u3002\u56fa\u6709\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308c\u3070\u305d\u3061\u3089\u3092\u4f7f\u3046\u3002\ntrait GenerateSeries {\n fn generate_samples(&mut self, output: &mut [[f64; 2]]);\n}\nimpl GenerateSeries for synth::Synthesizer {\n fn generate_samples(&mut self, output: &mut [[f64; 2]]) {\n for value in output {\n *value = self.generate_sample();\n }\n }\n}\n\nstruct PlaybackState {\n synth: synth::Synthesizer,\n parameters: [f64; parameters::COUNT],\n event: usize,\n position: usize,\n}\nstruct Playback {\n state: PlaybackState,\n}\nimpl Playback {\n fn new(scenario: &Scenario, rate: usize) -> Self {\n let mut synth = synth::Synthesizer::new();\n synth.set_sample_rate(rate as f64);\n synth.set_parameters(scenario.parameters);\n Self {\n state: PlaybackState {\n synth,\n parameters: scenario.parameters,\n event: 0,\n position: 0,\n },\n }\n }\n fn generate_samples(&mut self, scenario: &Scenario, block: usize, output: &mut [[f64; 2]]) {\n let s = &mut self.state;\n for chunk in output.chunks_mut(block) {\n let _float_mode = float_mode::FloatMode::flush_subnormals();\n if scenario.parameter.is_none() {\n let mut at = 0;\n while at < chunk.len() {\n while let Some(event) = scenario.events.get(s.event) {\n if event.at != s.position {\n break;\n }\n if event.on {\n assert!(s.synth.note_on(event.note, 0., event.velocity));\n } else {\n s.synth.note_off(event.note);\n }\n s.event += 1;\n }\n let end = scenario\n .events\n .get(s.event)\n .map_or(s.position + chunk.len() - at, |e| {\n e.at.min(s.position + chunk.len() - at)\n });\n let count = end - s.position;\n black_box(&mut s.synth).generate_samples(&mut chunk[at..at + count]);\n at += count;\n s.position += count;\n }\n } else {\n for sample in chunk {\n if let Some(index) = scenario.parameter {\n s.parameters[index] = scenario.automation[s.position];\n s.synth.set_parameters(black_box(s.parameters));\n }\n while let Some(event) = scenario.events.get(s.event) {\n if event.at != s.position {\n break;\n }\n if event.on {\n assert!(s.synth.note_on(event.note, 0., event.velocity));\n } else {\n s.synth.note_off(event.note);\n }\n s.event += 1;\n }\n *sample = black_box(&mut s.synth).generate_sample();\n s.position += 1;\n }\n }\n }\n black_box(output);\n }\n}\n\nfn fingerprint(samples: &[[f64; 2]]) -> (u64, f64, f64) {\n let mut hash = 0xcbf29ce484222325_u64;\n let mut peak = 0.0_f64;\n let mut sum = 0.;\n for &x in samples.iter().flatten() {\n assert!(x.is_finite(), \"nonfinite audio\");\n hash = (hash ^ x.to_bits()).wrapping_mul(0x100000001b3);\n peak = peak.max(x.abs());\n sum += x * x;\n }\n (hash, peak, (sum / (samples.len() * 2) as f64).sqrt())\n}\nfn main() {\n let args: Vec<_> = std::env::args().collect();\n let cases = cases();\n if args.get(1).is_some_and(|a| a == \"--list\") {\n for case in &cases {\n println!(\"{}\", case.name);\n }\n return;\n }\n assert_eq!(args.len(), 6, \"usage: standard RATE BUFFER AUDIO_SECONDS CASE BURN_IN_MS\");\n let rate: usize = args[1].parse().unwrap();\n let block: usize = args[2].parse().unwrap();\n let seconds: usize = args[3].parse().unwrap();\n let burn_in_ms: u64 = args[5].parse().unwrap();\n assert!(\n [44100, 48000, 96000].contains(&rate)\n && (1..=2048).contains(&block)\n && (4..=60).contains(&seconds)\n && burn_in_ms >= 10\n );\n let case = cases.iter().find(|c| c.name == args[4]).expect(\"unknown case\");\n let warmup = 2 * rate;\n let length = seconds * rate;\n let scenario = scenario(case, rate, block, warmup + length);\n let mut warm = vec![[0.; 2]; warmup];\n let mut output = vec![[0.; 2]; length];\n // \u6f14\u594f\u5168\u4f53\u3092\u6700\u4f4e1\u5468\u51e6\u7406\u3057\u3066\u3001\u6e2c\u5b9a\u533a\u9593\u3067\u4f7f\u3046\u51e6\u7406\u7d4c\u8def\u3082\u30a6\u30a9\u30fc\u30e0\u30a2\u30c3\u30d7\u3059\u308b\u3002\n let burn_start = Instant::now();\n let mut burn_in_cycles = 0;\n let mut playback = loop {\n let mut playback = Playback::new(&scenario, rate);\n playback.generate_samples(&scenario, block, &mut warm);\n // \u6700\u7d42\u56de\u306e\u97f3\u6e90\u6e96\u5099\u3082burn-in\u306b\u542b\u3081\u3001\u5e38\u306b\u540c\u3058\u6f14\u594f\u4f4d\u7f6e\u304b\u3089\u8a08\u6e2c\u3059\u308b\u3002\n if burn_in_cycles > 0 && burn_start.elapsed() >= Duration::from_millis(burn_in_ms) {\n break playback;\n }\n playback.generate_samples(&scenario, block, &mut output);\n burn_in_cycles += 1;\n };\n let burn_in_ns = burn_start.elapsed().as_nanos();\n let start = Instant::now();\n playback.generate_samples(&scenario, block, &mut output);\n let measurement_ns = start.elapsed().as_nanos();\n let ns = measurement_ns as f64 / length as f64;\n let (hash, peak, rms) = fingerprint(&output);\n assert_eq!(matches!(case.phrase, Phrase::Idle), peak == 0.0, \"unexpected silence/nonzero\");\n println!(\"case,ns,burn_in_ns,burn_in_cycles,measurement_ns,fingerprint,peak,rms\");\n println!(\"{},{ns:.6},{burn_in_ns},{burn_in_cycles},{measurement_ns},{hash:016x},{peak:.12},{rms:.12}\", case.name);\n}\n", "active": "// Copyright (C) 2026 Yusuke Oda\n// SPDX-License-Identifier: AGPL-3.0-only\n//! \u901a\u5e38\u306e\u6f14\u594f\u30fb\u97f3\u8272\u30fb\u30aa\u30fc\u30c8\u30e1\u30fc\u30b7\u30e7\u30f3\u3092\u56fa\u5b9a\u3057\u305f\u6027\u80fd\u56de\u5e30\u30b9\u30a4\u30fc\u30c8\u3002\n#![allow(dead_code)] // ABI\u30fb\u4fdd\u5b58\u51e6\u7406\u306a\u3069\u3001\u8a08\u6e2c\u5bfe\u8c61\u4ee5\u5916\u306e\u65e2\u5b58\u5b9a\u7fa9\u3082\u8aad\u307f\u8fbc\u3080\u3002\n#[path = \"../src/abi.rs\"]\nmod abi;\n#[path = \"../src/distortion.rs\"]\nmod distortion;\n#[path = \"../src/effects.rs\"]\nmod effects;\n#[path = \"../src/delay.rs\"]\nmod delay;\n#[path = \"../src/reverb.rs\"]\nmod reverb;\n#[path = \"../src/eq.rs\"]\nmod eq;\n#[path = \"../src/envelope.rs\"]\nmod envelope;\n#[path = \"../src/filter.rs\"]\nmod filter;\n#[path = \"../src/float_mode.rs\"]\nmod float_mode;\n#[path = \"../src/oscillator.rs\"]\nmod oscillator;\n#[path = \"../src/parameters.rs\"]\nmod parameters;\n#[path = \"../src/random.rs\"]\nmod random;\n#[path = \"../src/synth.rs\"]\nmod synth;\n#[path = \"../src/voice.rs\"]\nmod voice;\n#[path = \"../src/volume.rs\"]\nmod volume;\n\nuse std::{\n hint::black_box,\n time::{Duration, Instant},\n};\nuse voice::Note;\n\n// \u8a2d\u5b9a\u306fUI\u306e\u521d\u671f\u5024\u304b\u3089\u72ec\u7acb\u3055\u305b\u3001\u65b0\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306e\u8ffd\u52a0\u6642\u306b\u306f\u660e\u793a\u7684\u306b\u898b\u76f4\u3059\u3002\nconst BASE: &[f64] = &[\n 0.,\n 10.,\n 0.,\n 120.,\n 0.7,\n 100.,\n 1.,\n 0.,\n 1.,\n 0.,\n 3600.,\n 1.,\n 1.,\n -6.,\n 10.,\n 0.,\n 100.,\n 0.7,\n 100.,\n 0.,\n 0.,\n 0.,\n 0.,\n 0.,\n 0.,\n f64::NEG_INFINITY,\n 0.,\n 0.,\n f64::NEG_INFINITY,\n 0.,\n 0.,\n 0.,\n 0.,\n 100.,\n 0.0,\n 250.0,\n 0.0,\n 1000.0,\n 0.0,\n 1.0,\n 4000.0,\n 0.0,\n 0.0, 0.0, 0.0, // Off / Off / Off.\n 1.0, 0.0, 100.0, 1.0, 0.0, 100.0,\n 5.0, 35.0, 25.0, 5.0, 35.0, 25.0, 5.0, 35.0, 25.0, // Delay\n 0.0, 0.0, 0.0, // Stereo\n 2.0, 6000.0, 100.0, 25.0, 2.0, 6000.0, 100.0, 25.0, 2.0, 6000.0, 100.0, 25.0, // Reverb\n];\nconst BASS: &[(usize, f64)] = &[\n (0, 3.),\n (1, 5.),\n (3, 180.),\n (4, 0.45),\n (9, 2.),\n (10, 1200.),\n (11, 1.2),\n (14, 0.),\n (16, 250.),\n (17, 0.),\n (19, 2400.),\n];\nconst LEAD: &[(usize, f64)] = &[(0, 2.), (6, 2.), (7, 7.), (9, 2.), (10, 2400.), (11, 0.9)];\nconst CHORD: &[(usize, f64)] = &[\n (0, 3.),\n (1, 15.),\n (3, 200.),\n (4, 0.6),\n (5, 400.),\n (6, 3.),\n (7, 9.),\n (8, 8.),\n (9, 2.),\n (11, 0.8),\n (20, 60.),\n];\nconst PAD: &[(usize, f64)] = &[\n (0, 3.),\n (1, 300.),\n (3, 500.),\n (5, 800.),\n (6, 5.),\n (7, 12.),\n (8, 8.),\n (9, 2.),\n (10, 2400.),\n (14, 600.),\n (16, 500.),\n (19, 1200.),\n (20, 80.),\n];\nconst BELL: &[(usize, f64)] = &[\n (1, 2.),\n (3, 1000.),\n (4, 0.),\n (5, 300.),\n (8, 4.),\n (24, 1200.),\n (27, 1900.),\n (29, 30.),\n (30, 10.),\n];\n\n#[derive(Clone, Copy)]\nenum Phrase {\n Mono,\n Bass,\n Arpeggio,\n Chord,\n Idle,\n}\n#[derive(Clone, Copy)]\nstruct Automation {\n index: usize,\n low: f64,\n high: f64,\n period_seconds: f64,\n}\nstruct Case {\n name: &'static str,\n phrase: Phrase,\n settings: &'static [(usize, f64)],\n extra: &'static [(usize, f64)],\n automation: Option,\n}\n\nfn cases() -> Vec {\n vec![\n Case { name:\"reverb-mono-1\",phrase:Phrase::Mono,settings:&[],extra:&[(42,3.)],automation:None },\n Case { name:\"reverb-chord-1\",phrase:Phrase::Chord,settings:CHORD,extra:&[(42,3.)],automation:None },\n Case { name:\"reverb-chord-3\",phrase:Phrase::Chord,settings:CHORD,extra:&[(42,3.),(43,3.),(44,3.)],automation:None },\n Case { name:\"reverb-mix\",phrase:Phrase::Chord,settings:CHORD,extra:&[(42,3.)],automation:Some(Automation{index:66,low:10.,high:40.,period_seconds:2.}) },\n Case { name:\"reverb-decay\",phrase:Phrase::Chord,settings:CHORD,extra:&[(42,3.)],automation:Some(Automation{index:63,low:1.,high:4.,period_seconds:4.}) },\n ]\n}\n\nstruct Event {\n at: usize,\n note: Note,\n velocity: f32,\n on: bool,\n}\nstruct Scenario {\n parameters: [f64; parameters::COUNT],\n events: Vec,\n // \u30db\u30b9\u30c8\u76f8\u5f53\u306e\u5909\u66f4\u70b9\u304b\u3089\u8a08\u7b97\u3057\u305f\u73fe\u5728\u5024\u3002\u8a08\u6e2c\u524d\u306b\u7528\u610f\u3059\u308b\u3002\n automation: Vec,\n parameter: Option,\n}\nfn scenario(case: &Case, rate: usize, block: usize, length: usize) -> Scenario {\n let mut plain: [f64; parameters::COUNT] = std::array::from_fn(|i| BASE[i]);\n for &(i, v) in case.settings.iter().chain(case.extra) {\n plain[i] = v;\n }\n if let Some(a) = case.automation {\n plain[a.index] = a.low;\n }\n if std::env::var_os(\"REVERB_OFF\").is_some() {\n for kind in [42,43,44] {plain[kind]=0.0;}\n }\n assert!(\n plain\n .iter()\n .enumerate()\n .all(|(i, &v)| parameters::valid_plain(i, v))\n );\n let parameters = std::array::from_fn(|i| parameters::to_normalized(i, plain[i]));\n let mut events = Vec::new();\n let (step_ms, gate_ms) = match case.phrase {\n Phrase::Mono => (500, 400),\n Phrase::Bass => (250, 190),\n Phrase::Arpeggio => (250, 160),\n Phrase::Chord => (2000, 1750),\n Phrase::Idle => (2000, 0),\n };\n if !matches!(case.phrase, Phrase::Idle) {\n for n in 0.. {\n let at = n * step_ms * rate / 1000;\n if at >= length {\n break;\n }\n let pitches: &[i16] = match case.phrase {\n Phrase::Mono => &[60, 64, 67, 65, 62, 60, 67, 64][n % 8..n % 8 + 1],\n Phrase::Bass => &[36, 36, 43, 39, 41, 41, 43, 39][n % 8..n % 8 + 1],\n Phrase::Arpeggio => &[60, 64, 67, 72, 67, 64, 62, 67][n % 8..n % 8 + 1],\n Phrase::Chord if n % 2 == 0 => &[48, 55, 60, 64],\n Phrase::Chord => &[53, 60, 65, 69],\n Phrase::Idle => unreachable!(),\n };\n for (i, &pitch) in pitches.iter().enumerate() {\n let note = Note {\n id: (n * 4 + i + 1) as i32,\n channel: 0,\n pitch,\n };\n let velocity = [0.8, 0.65, 0.75, 0.7][n % 4];\n events.push(Event {\n at,\n note,\n velocity,\n on: true,\n });\n events.push(Event {\n at: at + gate_ms * rate / 1000,\n note,\n velocity,\n on: false,\n });\n }\n }\n }\n events.sort_by_key(|e| (e.at, e.on)); // \u540c\u6642\u523b\u306fNote Off\u2192Note On\u3002\n let mut automation = Vec::new();\n if let Some(a) = case.automation {\n automation.reserve(length);\n let point = |n: usize| {\n let phase = (n as f64 / rate as f64 / a.period_seconds).fract();\n let triangle = 1.0 - (2.0 * phase - 1.0).abs();\n parameters::to_normalized(a.index, a.low + (a.high - a.low) * triangle)\n };\n for start in (0..length).step_by(block) {\n let count = block.min(length - start);\n let before = point(start);\n let after = point(start + count);\n let value = |i: usize| {\n if i == count {\n after\n } else if parameters::holds_between_points(a.index) {\n before\n } else {\n before + (after - before) * i as f64 / count as f64\n }\n };\n // \u5404\u30d6\u30ed\u30c3\u30af\u672b\u5c3e\u306e\u65e2\u77e5\u70b9\u307e\u3067\u88dc\u9593\u3059\u308b\u3002\n for i in 1..=count {\n automation.push(value(i));\n }\n }\n }\n Scenario {\n parameters,\n events,\n automation,\n parameter: case.automation.map(|a| a.index),\n }\n}\n\n// \u6bd4\u8f03\u5143\u306b\u7cfb\u5217\u5165\u53e3\u304c\u306a\u3044\u7248\u3067\u306f\u5358\u767a\u547c\u51fa\u3057\u3078\u623b\u3059\u3002\u56fa\u6709\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308c\u3070\u305d\u3061\u3089\u3092\u4f7f\u3046\u3002\ntrait GenerateSeries {\n fn generate_samples(&mut self, output: &mut [[f64; 2]]);\n}\nimpl GenerateSeries for synth::Synthesizer {\n fn generate_samples(&mut self, output: &mut [[f64; 2]]) {\n for value in output {\n *value = self.generate_sample();\n }\n }\n}\n\nstruct PlaybackState {\n synth: synth::Synthesizer,\n parameters: [f64; parameters::COUNT],\n event: usize,\n position: usize,\n}\nstruct Playback {\n state: PlaybackState,\n}\nimpl Playback {\n fn new(scenario: &Scenario, rate: usize) -> Self {\n let mut synth = synth::Synthesizer::new();\n synth.set_sample_rate(rate as f64);\n synth.set_parameters(scenario.parameters);\n Self {\n state: PlaybackState {\n synth,\n parameters: scenario.parameters,\n event: 0,\n position: 0,\n },\n }\n }\n fn generate_samples(&mut self, scenario: &Scenario, block: usize, output: &mut [[f64; 2]]) {\n let s = &mut self.state;\n for chunk in output.chunks_mut(block) {\n let _float_mode = float_mode::FloatMode::flush_subnormals();\n if scenario.parameter.is_none() {\n let mut at = 0;\n while at < chunk.len() {\n while let Some(event) = scenario.events.get(s.event) {\n if event.at != s.position {\n break;\n }\n if event.on {\n assert!(s.synth.note_on(event.note, 0., event.velocity));\n } else {\n s.synth.note_off(event.note);\n }\n s.event += 1;\n }\n let end = scenario\n .events\n .get(s.event)\n .map_or(s.position + chunk.len() - at, |e| {\n e.at.min(s.position + chunk.len() - at)\n });\n let count = end - s.position;\n black_box(&mut s.synth).generate_samples(&mut chunk[at..at + count]);\n at += count;\n s.position += count;\n }\n } else {\n for sample in chunk {\n if let Some(index) = scenario.parameter {\n s.parameters[index] = scenario.automation[s.position];\n s.synth.set_parameters(black_box(s.parameters));\n }\n while let Some(event) = scenario.events.get(s.event) {\n if event.at != s.position {\n break;\n }\n if event.on {\n assert!(s.synth.note_on(event.note, 0., event.velocity));\n } else {\n s.synth.note_off(event.note);\n }\n s.event += 1;\n }\n *sample = black_box(&mut s.synth).generate_sample();\n s.position += 1;\n }\n }\n }\n black_box(output);\n }\n}\n\nfn fingerprint(samples: &[[f64; 2]]) -> (u64, f64, f64) {\n let mut hash = 0xcbf29ce484222325_u64;\n let mut peak = 0.0_f64;\n let mut sum = 0.;\n for &x in samples.iter().flatten() {\n assert!(x.is_finite(), \"nonfinite audio\");\n hash = (hash ^ x.to_bits()).wrapping_mul(0x100000001b3);\n peak = peak.max(x.abs());\n sum += x * x;\n }\n (hash, peak, (sum / (samples.len() * 2) as f64).sqrt())\n}\nfn main() {\n let args: Vec<_> = std::env::args().collect();\n let cases = cases();\n if args.get(1).is_some_and(|a| a == \"--list\") {\n for case in &cases {\n println!(\"{}\", case.name);\n }\n return;\n }\n assert_eq!(args.len(), 6, \"usage: standard RATE BUFFER AUDIO_SECONDS CASE BURN_IN_MS\");\n let rate: usize = args[1].parse().unwrap();\n let block: usize = args[2].parse().unwrap();\n let seconds: usize = args[3].parse().unwrap();\n let burn_in_ms: u64 = args[5].parse().unwrap();\n assert!(\n [44100, 48000, 96000].contains(&rate)\n && (1..=2048).contains(&block)\n && (4..=60).contains(&seconds)\n && burn_in_ms >= 10\n );\n let case = cases.iter().find(|c| c.name == args[4]).expect(\"unknown case\");\n let warmup = 2 * rate;\n let length = seconds * rate;\n let scenario = scenario(case, rate, block, warmup + length);\n let mut warm = vec![[0.; 2]; warmup];\n let mut output = vec![[0.; 2]; length];\n // \u6f14\u594f\u5168\u4f53\u3092\u6700\u4f4e1\u5468\u51e6\u7406\u3057\u3066\u3001\u6e2c\u5b9a\u533a\u9593\u3067\u4f7f\u3046\u51e6\u7406\u7d4c\u8def\u3082\u30a6\u30a9\u30fc\u30e0\u30a2\u30c3\u30d7\u3059\u308b\u3002\n let burn_start = Instant::now();\n let mut burn_in_cycles = 0;\n let mut playback = loop {\n let mut playback = Playback::new(&scenario, rate);\n playback.generate_samples(&scenario, block, &mut warm);\n // \u6700\u7d42\u56de\u306e\u97f3\u6e90\u6e96\u5099\u3082burn-in\u306b\u542b\u3081\u3001\u5e38\u306b\u540c\u3058\u6f14\u594f\u4f4d\u7f6e\u304b\u3089\u8a08\u6e2c\u3059\u308b\u3002\n if burn_in_cycles > 0 && burn_start.elapsed() >= Duration::from_millis(burn_in_ms) {\n break playback;\n }\n playback.generate_samples(&scenario, block, &mut output);\n burn_in_cycles += 1;\n };\n let burn_in_ns = burn_start.elapsed().as_nanos();\n let start = Instant::now();\n playback.generate_samples(&scenario, block, &mut output);\n let measurement_ns = start.elapsed().as_nanos();\n let ns = measurement_ns as f64 / length as f64;\n let (hash, peak, rms) = fingerprint(&output);\n assert_eq!(matches!(case.phrase, Phrase::Idle), peak == 0.0, \"unexpected silence/nonzero\");\n println!(\"case,ns,burn_in_ns,burn_in_cycles,measurement_ns,fingerprint,peak,rms\");\n println!(\"{},{ns:.6},{burn_in_ns},{burn_in_cycles},{measurement_ns},{hash:016x},{peak:.12},{rms:.12}\", case.name);\n}\n" }