Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(audio): fuse single-track stereo mixing and clamping

vedanth634548ec2845
diff --git a/crates/audio/examples/single-track-mixing-benchmark.rs b/crates/audio/examples/single-track-mixing-benchmark.rs
new file mode 100644
index 000000000..efbc696d5
--- /dev/null
+++ b/crates/audio/examples/single-track-mixing-benchmark.rs
@@ -0,0 +1,97 @@
+use cap_audio::{
+ AudioData, AudioRendererTrack, AudioSampleSource, DecodedAudio, StereoMode, render_audio,
+};
+use std::{
+ collections::hash_map::DefaultHasher,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ sync::Arc,
+ time::Instant,
+};
+
+struct Indexed(DecodedAudio);
+impl AudioSampleSource for Indexed {
+ fn channels(&self) -> u16 {
+ self.0.channels()
+ }
+ fn sample_count(&self) -> usize {
+ self.0.sample_count()
+ }
+ fn sample(&self, index: usize) -> Option<&f32> {
+ self.0.sample(index)
+ }
+}
+
+fn source(channels: u16, seed: usize) -> DecodedAudio {
+ DecodedAudio::from(Arc::new(AudioData::from_samples(
+ (0..192_000 * usize::from(channels))
+ .map(|index| (((index * 97 + seed) % 1009) as f32 - 504.0) / 600.0)
+ .collect(),
+ channels,
+ )))
+}
+
+fn measure<T: AudioSampleSource>(name: &str, data: &[T], gain: f32, offset: isize, mode: u8) {
+ let tracks = data
+ .iter()
+ .map(|data| AudioRendererTrack {
+ data,
+ gain,
+ offset,
+ stereo_mode: match mode {
+ 1 => StereoMode::MonoL,
+ 2 => StereoMode::MonoR,
+ _ => StereoMode::Stereo,
+ },
+ })
+ .collect::<Vec<_>>();
+ for (frames, iterations) in [(64, 4000), (1024, 500), (4096, 200)] {
+ let mut output = vec![13.0_f32; frames * 2 + 4];
+ for sample in 0..18 {
+ let start = Instant::now();
+ let mut rendered = 0;
+ for call in 0..iterations {
+ let cursor = 256 + (call * frames) % (192_000 - frames - 512);
+ rendered += black_box(render_audio(
+ black_box(&tracks),
+ black_box(cursor),
+ black_box(frames),
+ 2,
+ black_box(&mut output),
+ ));
+ }
+ let elapsed = start.elapsed().as_nanos();
+ assert_eq!(rendered, iterations * frames);
+ assert_eq!(&output[..2], &[13.0; 2]);
+ assert_eq!(&output[output.len() - 2..], &[13.0; 2]);
+ if sample >= 3 {
+ let mut hash = DefaultHasher::new();
+ for value in &output {
+ value.to_bits().hash(&mut hash);
+ }
+ println!(
+ "MIX_BENCH {}",
+ serde_json::json!({
+ "case": name, "frames": frames, "sample": sample-3,
+ "ns_per_call": elapsed as f64 / iterations as f64,
+ "fingerprint": hash.finish(),
+ })
+ );
+ }
+ }
+ }
+}
+
+fn main() {
+ let stereo = [source(2, 0), source(2, 7)];
+ let mono = [source(1, 0)];
+ measure("one_stereo_unity", &stereo[..1], 0.0, 0, 0);
+ measure("one_stereo_gain", &stereo[..1], 6.0, 0, 0);
+ measure("two_stereo", &stereo, -6.0, 0, 0);
+ measure("mono", &mono, -6.0, 0, 0);
+ measure("offset", &stereo[..1], -6.0, -64, 0);
+ measure("left", &stereo[..1], -6.0, 0, 1);
+ measure("muted", &stereo[..1], -30.0, 0, 0);
+ let indexed = [Indexed(source(2, 0))];
+ measure("indexed", &indexed, -6.0, 0, 0);
+}
diff --git a/crates/audio/src/renderer.rs b/crates/audio/src/renderer.rs
index f1fd08985..f2c7d16f0 100644
--- a/crates/audio/src/renderer.rs
+++ b/crates/audio/src/renderer.rs
@@ -68,6 +68,29 @@ pub fn render_audio<T: AudioSampleSource>(
return 0;
}
let out = &mut out[out_offset..out_offset + samples * 2];
+ if let [track] = tracks
+ && matches!(track.stereo_mode, StereoMode::Stereo)
+ && track.data.channels() == 2
+ {
+ let gain = gain_for_db(track.gain);
+ if gain == f32::NEG_INFINITY {
+ out.fill(0.0);
+ return samples;
+ }
+ if let Ok(first) = usize::try_from(offset as i128 + track.offset as i128)
+ && let Some(first) = first.checked_mul(2)
+ && let Some(end) = first.checked_add(out.len())
+ && let Some(input) = track.data.sample_slice(first..end)
+ && input.len() == out.len()
+ {
+ let mut index = 0;
+ while index < out.len() {
+ out[index] = (0.0 + input[index] * gain).clamp(-1.0, 1.0);
+ index += 1;
+ }
+ return samples;
+ }
+ }
out.fill(0.0);
for track in tracks {
@@ -258,6 +281,146 @@ mod tests {
}
}
+ #[test]
+ fn single_stereo_mixing_matches_indexed_samples_at_boundaries() {
+ let data = AudioData::from_raw_f32(
+ vec![
+ 0.0,
+ -0.0,
+ -2.0,
+ 2.0,
+ f32::NEG_INFINITY,
+ f32::INFINITY,
+ f32::from_bits(0x7fc0_1234),
+ f32::from_bits(1),
+ -f32::from_bits(1),
+ 0.25,
+ -0.25,
+ 1.0,
+ -1.0,
+ 0.5,
+ ],
+ 2,
+ );
+ for gain in [
+ f32::NEG_INFINITY,
+ -31.0,
+ -30.0,
+ -6.0,
+ 0.0,
+ 6.0,
+ f32::INFINITY,
+ f32::NAN,
+ ] {
+ for offset in [-3, 0, 2] {
+ for cursor in [0, 1, 4, 100] {
+ for count in [0, 1, 3, 16] {
+ for mode in [0, 1, 2] {
+ let render = |slices| {
+ let source = Source {
+ data: &data,
+ slices,
+ };
+ let track = AudioRendererTrack {
+ data: &source,
+ gain,
+ offset,
+ stereo_mode: match mode {
+ 1 => StereoMode::MonoL,
+ 2 => StereoMode::MonoR,
+ _ => StereoMode::Stereo,
+ },
+ };
+ let mut output = [13.0_f32; 36];
+ let rendered =
+ render_audio(&[track], cursor, count, 2, &mut output);
+ (rendered, output)
+ };
+ let (expected_count, expected) = render(false);
+ let (actual_count, actual) = render(true);
+ assert_eq!(actual_count, expected_count);
+ for (actual, expected) in actual.into_iter().zip(expected) {
+ if expected.is_nan() {
+ assert!(actual.is_nan());
+ } else {
+ assert_eq!(actual.to_bits(), expected.to_bits());
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn partial_stereo_slices_keep_the_unavailable_tail_silent() {
+ struct PartialSlice;
+ impl AudioSampleSource for PartialSlice {
+ fn channels(&self) -> u16 {
+ 2
+ }
+ fn sample_count(&self) -> usize {
+ 4
+ }
+ fn sample(&self, _index: usize) -> Option<&f32> {
+ None
+ }
+ fn sample_slice(&self, _range: Range<usize>) -> Option<&[f32]> {
+ Some(&[0.25, -0.5, 0.75, -2.0])
+ }
+ }
+ let tracks = [AudioRendererTrack {
+ data: &PartialSlice,
+ gain: 0.0,
+ offset: 0,
+ stereo_mode: StereoMode::Stereo,
+ }];
+ let mut output = [13.0_f32; 12];
+ assert_eq!(render_audio(&tracks, 0, 4, 2, &mut output), 4);
+ assert_eq!(
+ output,
+ [
+ 13.0, 13.0, 0.25, -0.5, 0.75, -1.0, 0.0, 0.0, 0.0, 0.0, 13.0, 13.0
+ ]
+ );
+ }
+
+ #[test]
+ fn muted_stereo_tracks_do_not_read_samples() {
+ struct Muted;
+ impl AudioSampleSource for Muted {
+ fn channels(&self) -> u16 {
+ 2
+ }
+ fn sample_count(&self) -> usize {
+ 4
+ }
+ fn sample(&self, _index: usize) -> Option<&f32> {
+ panic!("muted sample read")
+ }
+ fn sample_slice(&self, _range: Range<usize>) -> Option<&[f32]> {
+ panic!("muted slice read")
+ }
+ }
+ for gain in [f32::NEG_INFINITY, -31.0, -30.0] {
+ let tracks = [AudioRendererTrack {
+ data: &Muted,
+ gain,
+ offset: 0,
+ stereo_mode: StereoMode::Stereo,
+ }];
+ let mut output = [13.0_f32; 12];
+ assert_eq!(render_audio(&tracks, 0, 4, 2, &mut output), 4);
+ assert_eq!(
+ output,
+ [
+ 13.0, 13.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 13.0, 13.0
+ ]
+ );
+ }
+ }
+
#[test]
fn contiguous_and_indexed_mixing_preserve_sample_bits_and_output_bounds() {
let data = [