From e441ed634f907fc8d77dd379df0e13eb0d268aa2 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Tue, 3 Dec 2024 15:52:30 +0100 Subject: [PATCH] clarify sample index calculation with `channel_count` --- 01-play-sine/src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/01-play-sine/src/main.rs b/01-play-sine/src/main.rs index be2a3bd..db3cce2 100644 --- a/01-play-sine/src/main.rs +++ b/01-play-sine/src/main.rs @@ -29,12 +29,12 @@ fn main() -> Result<(), Box> { .register(); let sample_rate = 48000; - let channel_count = 2; + let channel_count : usize = 2; let mut audio_info = AudioInfoRaw::new(); audio_info.set_format(AudioFormat::F32LE); audio_info.set_rate(sample_rate); - audio_info.set_channels(channel_count); + audio_info.set_channels(channel_count as u32); let param_bytes = PodSerializer::serialize( Cursor::new(vec![]), @@ -83,7 +83,7 @@ fn main() -> Result<(), Box> { } }; - let stride = std::mem::size_of::() * (channel_count as usize); + let stride = std::mem::size_of::() * channel_count; let datas = buffer.datas_mut(); let data = &mut datas[0]; @@ -104,9 +104,9 @@ fn main() -> Result<(), Box> { let phi = pi2 * time_secs * sine_freq; let value = 0.5 + 0.5 * f32::sin(phi); - slice[2 * i] = value; - slice[2 * i + 1] = value; - samples_written += 2; + slice[channel_count*i + 0] = value; + slice[channel_count*i + 1] = value; + samples_written += 1; } }