|
4 | 4 |
|
5 | 5 | use crate::generation::{sum_all_waveforms, Normalizer}; |
6 | 6 | use crate::manager::resizeable_2d_vec::Resizeable2DVec; |
| 7 | +use rayon::prelude::*; |
7 | 8 | use weresocool_ast::follow::evaluate::EvaluateAction; |
8 | 9 | use weresocool_instrument::{Offset, RenderOp, StereoWaveform}; |
9 | 10 | use weresocool_instrument::renderable::render_voice::RenderVoice; |
10 | 11 | use weresocool_instrument::renderable::Renderable; |
11 | 12 | use weresocool_shared::{Settings, timing_print}; |
12 | 13 |
|
| 14 | +/// Voice counts at or above this threshold use the rayon parallel |
| 15 | +/// voice-render path. Below it, the per-iteration overhead of work- |
| 16 | +/// stealing exceeds the gain (heuristic confirmed by bench at 8 vs 100 |
| 17 | +/// voices). Tunable; the only correctness constraint is `>= 1`. |
| 18 | +const PARALLEL_VOICE_THRESHOLD: usize = 16; |
| 19 | + |
13 | 20 | #[derive(Debug)] |
14 | 21 | pub struct AudioEngine { |
15 | 22 | pub renders: [Option<Vec<RenderVoice>>; 2], |
@@ -83,58 +90,78 @@ impl AudioEngine { |
83 | 90 | let mut rendered_per_voice: Vec<StereoWaveform> = Vec::with_capacity(render_voices.len()); |
84 | 91 | let loop_play = !next_exists && Settings::global().loop_play; |
85 | 92 | let mut samples_rendered = 0usize; |
86 | | - |
87 | | - for (i, voice) in render_voices.iter_mut().enumerate() { |
88 | | - match voice.get_batch( |
89 | | - remaining_buffer_size, |
90 | | - None, |
91 | | - loop_play, |
92 | | - ) { |
93 | | - Some(batch) => { |
94 | | - any_data_rendered = true; |
95 | | - let batch_samples: usize = batch.iter().map(|op| op.samples).sum(); |
96 | | - samples_rendered = samples_rendered.max(batch_samples); |
97 | | - |
98 | | - // Split MIDI-directed ops from audio-directed |
99 | | - let (midi_batch, mut audio_batch): (Vec<_>, Vec<_>) = batch |
100 | | - .into_iter() |
101 | | - .partition(|op| !op.midi.is_empty()); |
102 | | - |
103 | | - midi_ops.extend(midi_batch.into_iter()); |
104 | | - |
105 | | - let voice_rendered = |
106 | | - audio_batch.render(&mut voice.oscillator, Some(&offset)); |
107 | | - rendered_per_voice.push(voice_rendered); |
108 | | - |
109 | | - if collect_viz_ops { |
110 | | - // Use 1,000,000 scale to support filter rates as low as 0.000001 |
111 | | - let vis_threshold = (Settings::global().vis_filter_rate * 1_000_000.0) as usize; |
112 | | - let b: Vec<_> = audio_batch |
113 | | - .iter() |
114 | | - .filter(|op| { |
115 | | - let hash = op.index.wrapping_mul(2654435761) % 1_000_000; |
116 | | - hash < vis_threshold |
117 | | - }) |
118 | | - .cloned() |
119 | | - .map(|mut op| { |
120 | | - let follow_offset = op.follows.eval_value( |
121 | | - offset.freq as f32, |
122 | | - offset.gain as f32, |
123 | | - ); |
124 | | - op.f *= follow_offset.0 as f64; |
125 | | - op.g = ( |
126 | | - op.g.0 * follow_offset.1 as f64, |
127 | | - op.g.1 * follow_offset.1 as f64, |
128 | | - ); |
129 | | - op |
130 | | - }) |
131 | | - .collect(); |
132 | | - |
133 | | - total_ops.extend_at(i, b); |
134 | | - } |
135 | | - } |
136 | | - None => { |
137 | | - // Voice has finished |
| 93 | + // Read once outside the (possibly parallel) inner closure. |
| 94 | + let vis_threshold = if collect_viz_ops { |
| 95 | + (Settings::global().vis_filter_rate * 1_000_000.0) as usize |
| 96 | + } else { |
| 97 | + 0 |
| 98 | + }; |
| 99 | + |
| 100 | + // Render each voice. Voices own independent state |
| 101 | + // (oscillator, sample_index, op_index) and the |
| 102 | + // per-voice work — get_batch + audio_batch.render — |
| 103 | + // doesn't touch any shared state, so we can run it |
| 104 | + // across the rayon pool when the voice count is |
| 105 | + // large enough to amortize work-stealing overhead. |
| 106 | + // |
| 107 | + // All collected outputs are merged serially after |
| 108 | + // the parallel section. Merge operations |
| 109 | + // (midi_ops.extend, rendered_per_voice.push, |
| 110 | + // total_ops.extend_at, samples_rendered.max) are |
| 111 | + // commutative, so iteration order doesn't matter |
| 112 | + // for correctness. |
| 113 | + let per_voice: Vec<PerVoiceOutput> = if render_voices.len() >= PARALLEL_VOICE_THRESHOLD { |
| 114 | + render_voices |
| 115 | + .par_iter_mut() |
| 116 | + .enumerate() |
| 117 | + .map(|(i, voice)| { |
| 118 | + render_one_voice( |
| 119 | + i, |
| 120 | + voice, |
| 121 | + remaining_buffer_size, |
| 122 | + loop_play, |
| 123 | + &offset, |
| 124 | + collect_viz_ops, |
| 125 | + vis_threshold, |
| 126 | + ) |
| 127 | + }) |
| 128 | + .collect() |
| 129 | + } else { |
| 130 | + render_voices |
| 131 | + .iter_mut() |
| 132 | + .enumerate() |
| 133 | + .map(|(i, voice)| { |
| 134 | + render_one_voice( |
| 135 | + i, |
| 136 | + voice, |
| 137 | + remaining_buffer_size, |
| 138 | + loop_play, |
| 139 | + &offset, |
| 140 | + collect_viz_ops, |
| 141 | + vis_threshold, |
| 142 | + ) |
| 143 | + }) |
| 144 | + .collect() |
| 145 | + }; |
| 146 | + |
| 147 | + for out in per_voice { |
| 148 | + let PerVoiceOutput { |
| 149 | + voice_index, |
| 150 | + rendered, |
| 151 | + } = out; |
| 152 | + if let Some(VoiceRenderData { |
| 153 | + batch_samples, |
| 154 | + midi_batch, |
| 155 | + voice_rendered, |
| 156 | + viz_ops, |
| 157 | + }) = rendered |
| 158 | + { |
| 159 | + any_data_rendered = true; |
| 160 | + samples_rendered = samples_rendered.max(batch_samples); |
| 161 | + midi_ops.extend(midi_batch); |
| 162 | + rendered_per_voice.push(voice_rendered); |
| 163 | + if collect_viz_ops { |
| 164 | + total_ops.extend_at(voice_index, viz_ops); |
138 | 165 | } |
139 | 166 | } |
140 | 167 | } |
@@ -261,3 +288,76 @@ impl Default for AudioEngine { |
261 | 288 | Self::new() |
262 | 289 | } |
263 | 290 | } |
| 291 | + |
| 292 | +/// Output of a single voice's per-batch work. Returned from the |
| 293 | +/// (possibly parallel) per-voice section so the orchestrator can merge |
| 294 | +/// results serially. |
| 295 | +struct PerVoiceOutput { |
| 296 | + voice_index: usize, |
| 297 | + /// `None` if the voice ran out of ops this batch. |
| 298 | + rendered: Option<VoiceRenderData>, |
| 299 | +} |
| 300 | + |
| 301 | +struct VoiceRenderData { |
| 302 | + batch_samples: usize, |
| 303 | + midi_batch: Vec<RenderOp>, |
| 304 | + voice_rendered: StereoWaveform, |
| 305 | + viz_ops: Vec<RenderOp>, |
| 306 | +} |
| 307 | + |
| 308 | +/// Pure (modulo each voice's own oscillator + indices) per-voice work. |
| 309 | +/// Safe to call in parallel because it only mutates the voice handed in. |
| 310 | +fn render_one_voice( |
| 311 | + voice_index: usize, |
| 312 | + voice: &mut RenderVoice, |
| 313 | + remaining_buffer_size: usize, |
| 314 | + loop_play: bool, |
| 315 | + offset: &Offset, |
| 316 | + collect_viz_ops: bool, |
| 317 | + vis_threshold: usize, |
| 318 | +) -> PerVoiceOutput { |
| 319 | + let Some(batch) = voice.get_batch(remaining_buffer_size, None, loop_play) else { |
| 320 | + return PerVoiceOutput { voice_index, rendered: None }; |
| 321 | + }; |
| 322 | + |
| 323 | + let batch_samples: usize = batch.iter().map(|op| op.samples).sum(); |
| 324 | + |
| 325 | + // Split MIDI-directed ops from audio-directed. |
| 326 | + let (midi_batch, mut audio_batch): (Vec<_>, Vec<_>) = batch |
| 327 | + .into_iter() |
| 328 | + .partition(|op| !op.midi.is_empty()); |
| 329 | + |
| 330 | + let voice_rendered = audio_batch.render(&mut voice.oscillator, Some(offset)); |
| 331 | + |
| 332 | + let viz_ops = if collect_viz_ops { |
| 333 | + audio_batch |
| 334 | + .iter() |
| 335 | + .filter(|op| { |
| 336 | + let hash = op.index.wrapping_mul(2654435761) % 1_000_000; |
| 337 | + hash < vis_threshold |
| 338 | + }) |
| 339 | + .cloned() |
| 340 | + .map(|mut op| { |
| 341 | + let follow_offset = op.follows.eval_value(offset.freq as f32, offset.gain as f32); |
| 342 | + op.f *= follow_offset.0 as f64; |
| 343 | + op.g = ( |
| 344 | + op.g.0 * follow_offset.1 as f64, |
| 345 | + op.g.1 * follow_offset.1 as f64, |
| 346 | + ); |
| 347 | + op |
| 348 | + }) |
| 349 | + .collect() |
| 350 | + } else { |
| 351 | + Vec::new() |
| 352 | + }; |
| 353 | + |
| 354 | + PerVoiceOutput { |
| 355 | + voice_index, |
| 356 | + rendered: Some(VoiceRenderData { |
| 357 | + batch_samples, |
| 358 | + midi_batch, |
| 359 | + voice_rendered, |
| 360 | + viz_ops, |
| 361 | + }), |
| 362 | + } |
| 363 | +} |
0 commit comments