///|
/// Create a new AudioBuffer with zeroed samples.
pub fn new_audio_buffer(
channels : Int32,
sample_rate : Int32,
frames : Int32,
) -> AudioBuffer {
AudioBuffer::{
channels,
sample_rate,
data: FixedArray::make(frames * channels, 0.0),
}
}
///|
/// Number of frames in the buffer.
pub fn frame_count(buf : AudioBuffer) -> Int32 {
buf.data.length() / buf.channels
}
///|
/// Duration of the buffer in seconds.
pub fn duration_secs(buf : AudioBuffer) -> Float {
Float::from_int(frame_count(buf)) / Float::from_int(buf.sample_rate)
}
///|
/// Get sample value at (frame, channel). Interleaved layout.
pub fn get_sample(buf : AudioBuffer, frame : Int32, channel : Int32) -> Float {
buf.data[frame * buf.channels + channel]
}
///|
/// Set sample value at (frame, channel). Interleaved layout.
pub fn set_sample(
buf : AudioBuffer,
frame : Int32,
channel : Int32,
value : Float,
) -> Unit {
buf.data[frame * buf.channels + channel] = value
}