mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-10-08 18:56:42 +02:00
30 lines
867 B
Rust
30 lines
867 B
Rust
use crate::*;
|
|
|
|
/// A currently playing instance of a sample.
|
|
#[derive(Default, Debug, Clone)] pub struct Voice {
|
|
pub sample: Arc<RwLock<Sample>>,
|
|
pub after: usize,
|
|
pub position: usize,
|
|
pub velocity: f32,
|
|
}
|
|
|
|
impl Iterator for Voice {
|
|
type Item = [f32;2];
|
|
fn next (&mut self) -> Option<Self::Item> {
|
|
if self.after > 0 {
|
|
self.after -= 1;
|
|
return Some([0.0, 0.0])
|
|
}
|
|
let sample = self.sample.try_read().unwrap();
|
|
if self.position < sample.end {
|
|
let position = self.position;
|
|
self.position += 1;
|
|
return sample.channels[0].get(position).map(|_amplitude|[
|
|
sample.channels[0][position] * self.velocity * sample.gain,
|
|
sample.channels[0][position] * self.velocity * sample.gain,
|
|
])
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|