{ "standard": "// Copyright (C) 2026 Yusuke Oda\n// SPDX-License-Identifier: AGPL-3.0-only\n//! 通常の演奏・音色・オートメーションを固定した性能回帰スイート。\n#![allow(dead_code)] // ABI・保存処理など、計測対象以外の既存定義も読み込む。\n#[path = \"../src/chorus.rs\"]\nmod chorus;\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// 設定はUIの初期値から独立させ、新パラメーターの追加時には明示的に見直す。\nconst BASE: [f64; 93] = [\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 20., 2., 0.7, 0., 0., 50.,\n 20., 2., 0.7, 0., 0., 50.,\n 20., 2., 0.7, 0., 0., 50.,\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 // ホスト相当の変更点から計算した現在値。計測前に用意する。\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のケースだけ先頭スロットを使う。\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)); // 同時刻はNote Off→Note On。\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 // 各ブロック末尾の既知点まで補間する。\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// 比較元に系列入口がない版では単発呼出しへ戻す。固有メソッドがあればそちらを使う。\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 // 演奏全体を最低1周処理して、測定区間で使う処理経路もウォームアップする。\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 // 最終回の音源準備もburn-inに含め、常に同じ演奏位置から計測する。\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//! 通常の演奏・音色・オートメーションを固定した性能回帰スイート。\n#![allow(dead_code)] // ABI・保存処理など、計測対象以外の既存定義も読み込む。\n#[path = \"../src/chorus.rs\"]\nmod chorus;\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// 設定はUIの初期値から独立させ、新パラメーターの追加時には明示的に見直す。\nconst BASE: [f64; 93] = [\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 20., 2., 0.7, 0., 0., 50.,\n 20., 2., 0.7, 0., 0., 50.,\n 20., 2., 0.7, 0., 0., 50.,\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 { name: \"chorus-pad\", phrase: Phrase::Chord, settings: PAD, extra: &[], automation: None },\n Case { name: \"flanger-lead\", phrase: Phrase::Mono, settings: LEAD, extra: &[(75, 2.5), (76, 0.5), (77, 0.25), (79, 50.)], automation: None },\n Case { name: \"chorus-three-slots\", phrase: Phrase::Chord, settings: CHORD, extra: &[(43, 4.), (44, 4.)], automation: None },\n ];\n for (name, index, low, high) in [\n (\"chorus-auto-time\", 75, 10., 30.),\n (\"chorus-auto-depth\", 76, 0.5, 4.),\n (\"chorus-auto-rate\", 77, 0.2, 1.5),\n (\"chorus-auto-detune\", 78, -0.1, 0.1),\n (\"chorus-auto-feedback\", 79, -50., 50.),\n (\"chorus-auto-mix\", 80, 10., 60.),\n ] {\n cases.push(Case { name, phrase: Phrase::Chord, settings: CHORD, extra: &[],\n automation: Some(Automation { index, low, high, period_seconds: 4. }) });\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 // ホスト相当の変更点から計算した現在値。計測前に用意する。\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] = 4.;\n if cfg!(bench_bypass) { for i in [42,43,44] { plain[i]=0.; } } // Distortionのケースだけ先頭スロットを使う。\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)); // 同時刻はNote Off→Note On。\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 // 各ブロック末尾の既知点まで補間する。\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// 比較元に系列入口がない版では単発呼出しへ戻す。固有メソッドがあればそちらを使う。\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 // 演奏全体を最低1周処理して、測定区間で使う処理経路もウォームアップする。\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 // 最終回の音源準備もburn-inに含め、常に同じ演奏位置から計測する。\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" }