1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/// Metadata container for a loaded sample.
pub struct SampleBuffer<T> {
/// Audio data (mono: single channel interleaved; stereo: deinterleaved into two vecs).
pub data: Vec<T>,
/// Right-channel data (None for mono).
pub right: Option<Vec<T>>,
/// Original sample rate.
pub sample_rate: f32,
/// Number of channels (1 or 2).
pub channels: u16,
/// Display name.
pub name: String,
}
impl<T> SampleBuffer<T> {
/// Create a mono sample buffer.
pub fn mono(data: Vec<T>, sample_rate: f32, name: impl Into<String>) -> Self {
let channels = 1;
Self {
data,
right: None,
sample_rate,
channels,
name: name.into(),
}
}
/// Create a stereo sample buffer (deinterleaved).
pub fn stereo(left: Vec<T>, right: Vec<T>, sample_rate: f32, name: impl Into<String>) -> Self {
let channels = 2;
Self {
data: left,
right: Some(right),
sample_rate,
channels,
name: name.into(),
}
}
/// Length in samples (per channel).
pub fn len(&self) -> usize {
self.data.len()
}
/// Returns `true` if the sample buffer contains no samples.
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
}