Add self-contained SoundBanks and MusicBanks with per-sample PCM16 and IMA ADPCM storage, materialized to resident PCM16. Integrate instrument envelopes, automation, shared DSP and host lifecycle controls while preserving staged real-time execution.
45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
#[derive(Debug)]
|
|
pub struct Sample {
|
|
pub sample_rate: u32,
|
|
pub channels: u8,
|
|
pub data: Vec<i16>,
|
|
pub loop_start: Option<u32>,
|
|
pub loop_end: Option<u32>,
|
|
pub default_volume: u8,
|
|
pub default_pan: u8,
|
|
pub default_pitch: f64,
|
|
}
|
|
|
|
impl Sample {
|
|
pub fn new(sample_rate: u32, data: Vec<i16>) -> Self {
|
|
Self {
|
|
sample_rate,
|
|
channels: 1,
|
|
data,
|
|
loop_start: None,
|
|
loop_end: None,
|
|
default_volume: 255,
|
|
default_pan: 128,
|
|
default_pitch: 1.0,
|
|
}
|
|
}
|
|
|
|
pub fn with_loop(mut self, start: u32, end: u32) -> Self {
|
|
self.loop_start = Some(start);
|
|
self.loop_end = Some(end);
|
|
self
|
|
}
|
|
|
|
pub fn frames_len(&self) -> usize {
|
|
self.data.len() / self.channels.max(1) as usize
|
|
}
|
|
|
|
pub fn with_channels(mut self, channels: u8) -> Result<Self, String> {
|
|
if !(1..=2).contains(&channels) || !self.data.len().is_multiple_of(channels as usize) {
|
|
return Err("audio sample channels/frame count".into());
|
|
}
|
|
self.channels = channels;
|
|
Ok(self)
|
|
}
|
|
}
|