Skip to content

Commit be43f87

Browse files
committed
[add] playback scheduler and gain ramp
1 parent e85f790 commit be43f87

2 files changed

Lines changed: 535 additions & 0 deletions

File tree

crates/lambda-rs/src/audio/buffer.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,54 @@ impl SoundBuffer {
145145
});
146146
}
147147

148+
/// Construct a `SoundBuffer` from interleaved samples for unit tests.
149+
///
150+
/// # Arguments
151+
/// - `samples`: Interleaved samples, `frames * channels` in length.
152+
/// - `sample_rate`: Sample rate in Hz.
153+
/// - `channels`: Interleaved channel count.
154+
///
155+
/// # Returns
156+
/// A validated `SoundBuffer` constructed from the provided samples.
157+
///
158+
/// # Errors
159+
/// Returns [`AudioError::InvalidData`] when the metadata is invalid or when
160+
/// the sample vector length is not a multiple of `channels`.
161+
#[cfg(test)]
162+
pub(crate) fn from_interleaved_samples_for_test(
163+
samples: Vec<f32>,
164+
sample_rate: u32,
165+
channels: u16,
166+
) -> Result<Self, AudioError> {
167+
if sample_rate == 0 {
168+
return Err(AudioError::InvalidData {
169+
details: "test sound buffer sample rate was 0".to_string(),
170+
});
171+
}
172+
173+
if channels == 0 {
174+
return Err(AudioError::InvalidData {
175+
details: "test sound buffer channel count was 0".to_string(),
176+
});
177+
}
178+
179+
if samples.len() % channels as usize != 0 {
180+
return Err(AudioError::InvalidData {
181+
details: format!(
182+
"test sound buffer sample length was not divisible by channels (samples={}, channels={})",
183+
samples.len(),
184+
channels
185+
),
186+
});
187+
}
188+
189+
return Ok(Self {
190+
samples,
191+
sample_rate,
192+
channels,
193+
});
194+
}
195+
148196
/// Return the sample rate in Hz.
149197
///
150198
/// # Returns

0 commit comments

Comments
 (0)