mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-04-28 21:40:14 +02:00
122 lines
4 KiB
Rust
122 lines
4 KiB
Rust
use crate::*;
|
|
use super::*;
|
|
|
|
/// A sound sample.
|
|
#[derive(Default, Debug)]
|
|
pub struct Sample {
|
|
pub name: String,
|
|
pub start: usize,
|
|
pub end: usize,
|
|
pub channels: Vec<Vec<f32>>,
|
|
pub rate: Option<usize>,
|
|
pub gain: f32,
|
|
}
|
|
|
|
/// Load sample from WAV and assign to MIDI note.
|
|
#[macro_export] macro_rules! sample {
|
|
($note:expr, $name:expr, $src:expr) => {{
|
|
let (end, data) = read_sample_data($src)?;
|
|
(
|
|
u7::from_int_lossy($note).into(),
|
|
Sample::new($name, 0, end, data).into()
|
|
)
|
|
}};
|
|
}
|
|
|
|
impl Sample {
|
|
pub fn new (name: &str, start: usize, end: usize, channels: Vec<Vec<f32>>) -> Self {
|
|
Self { name: name.to_string(), start, end, channels, rate: None, gain: 1.0 }
|
|
}
|
|
pub fn play (sample: &Arc<RwLock<Self>>, after: usize, velocity: &u7) -> Voice {
|
|
Voice {
|
|
sample: sample.clone(),
|
|
after,
|
|
position: sample.read().unwrap().start,
|
|
velocity: velocity.as_int() as f32 / 127.0,
|
|
}
|
|
}
|
|
/// Read WAV from file
|
|
pub fn read_data (src: &str) -> Usually<(usize, Vec<Vec<f32>>)> {
|
|
let mut channels: Vec<wavers::Samples<f32>> = vec![];
|
|
for channel in wavers::Wav::from_path(src)?.channels() {
|
|
channels.push(channel);
|
|
}
|
|
let mut end = 0;
|
|
let mut data: Vec<Vec<f32>> = vec![];
|
|
for samples in channels.iter() {
|
|
let channel = Vec::from(samples.as_ref());
|
|
end = end.max(channel.len());
|
|
data.push(channel);
|
|
}
|
|
Ok((end, data))
|
|
}
|
|
pub fn from_file (path: &PathBuf) -> Usually<Self> {
|
|
let name = path.file_name().unwrap().to_string_lossy().into();
|
|
let mut sample = Self { name, ..Default::default() };
|
|
// Use file extension if present
|
|
let mut hint = Hint::new();
|
|
if let Some(ext) = path.extension() {
|
|
hint.with_extension(&ext.to_string_lossy());
|
|
}
|
|
let probed = symphonia::default::get_probe().format(
|
|
&hint,
|
|
MediaSourceStream::new(
|
|
Box::new(File::open(path)?),
|
|
Default::default(),
|
|
),
|
|
&Default::default(),
|
|
&Default::default()
|
|
)?;
|
|
let mut format = probed.format;
|
|
let params = &format.tracks().iter()
|
|
.find(|t| t.codec_params.codec != CODEC_TYPE_NULL)
|
|
.expect("no tracks found")
|
|
.codec_params;
|
|
let mut decoder = get_codecs().make(params, &Default::default())?;
|
|
loop {
|
|
match format.next_packet() {
|
|
Ok(packet) => sample.decode_packet(&mut decoder, packet)?,
|
|
Err(symphonia::core::errors::Error::IoError(_)) => break decoder.last_decoded(),
|
|
Err(err) => return Err(err.into()),
|
|
};
|
|
};
|
|
sample.end = sample.channels.iter().fold(0, |l, c|l + c.len());
|
|
Ok(sample)
|
|
}
|
|
fn decode_packet (
|
|
&mut self, decoder: &mut Box<dyn Decoder>, packet: Packet
|
|
) -> Usually<()> {
|
|
// Decode a packet
|
|
let decoded = decoder
|
|
.decode(&packet)
|
|
.map_err(|e|Box::<dyn crate::Error>::from(e))?;
|
|
// Determine sample rate
|
|
let spec = *decoded.spec();
|
|
if let Some(rate) = self.rate {
|
|
if rate != spec.rate as usize {
|
|
panic!("sample rate changed");
|
|
}
|
|
} else {
|
|
self.rate = Some(spec.rate as usize);
|
|
}
|
|
// Determine channel count
|
|
while self.channels.len() < spec.channels.count() {
|
|
self.channels.push(vec![]);
|
|
}
|
|
// Load sample
|
|
let mut samples = SampleBuffer::new(
|
|
decoded.frames() as u64,
|
|
spec
|
|
);
|
|
if samples.capacity() > 0 {
|
|
samples.copy_interleaved_ref(decoded);
|
|
for frame in samples.samples().chunks(spec.channels.count()) {
|
|
for (chan, frame) in frame.iter().enumerate() {
|
|
self.channels[chan].push(*frame)
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|