mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 20:56:43 +01:00
56 lines
2 KiB
Rust
56 lines
2 KiB
Rust
use crate::*;
|
|
|
|
/// The sampler plugin plays sounds.
|
|
#[derive(Debug)]
|
|
pub struct Sampler {
|
|
pub jack: Arc<RwLock<JackClient>>,
|
|
pub name: String,
|
|
pub mapped: BTreeMap<u7, Arc<RwLock<Sample>>>,
|
|
pub unmapped: Vec<Arc<RwLock<Sample>>>,
|
|
pub voices: Arc<RwLock<Vec<Voice>>>,
|
|
pub midi_in: Port<MidiIn>,
|
|
pub audio_outs: Vec<Port<AudioOut>>,
|
|
pub buffer: Vec<Vec<f32>>,
|
|
pub output_gain: f32
|
|
}
|
|
|
|
impl Sampler {
|
|
pub fn from_edn <'e> (jack: &Arc<RwLock<JackClient>>, args: &[Edn<'e>]) -> Usually<Self> {
|
|
let mut name = String::new();
|
|
let mut dir = String::new();
|
|
let mut samples = BTreeMap::new();
|
|
edn!(edn in args {
|
|
Edn::Map(map) => {
|
|
if let Some(Edn::Str(n)) = map.get(&Edn::Key(":name")) {
|
|
name = String::from(*n);
|
|
}
|
|
if let Some(Edn::Str(n)) = map.get(&Edn::Key(":dir")) {
|
|
dir = String::from(*n);
|
|
}
|
|
},
|
|
Edn::List(args) => match args.get(0) {
|
|
Some(Edn::Symbol("sample")) => {
|
|
let (midi, sample) = Sample::from_edn(jack, &dir, &args[1..])?;
|
|
if let Some(midi) = midi {
|
|
samples.insert(midi, sample);
|
|
} else {
|
|
panic!("sample without midi binding: {}", sample.read().unwrap().name);
|
|
}
|
|
},
|
|
_ => panic!("unexpected in sampler {name}: {args:?}")
|
|
},
|
|
_ => panic!("unexpected in sampler {name}: {edn:?}")
|
|
});
|
|
Ok(Sampler {
|
|
jack: jack.clone(),
|
|
name: name.into(),
|
|
mapped: samples,
|
|
unmapped: Default::default(),
|
|
voices: Default::default(),
|
|
buffer: Default::default(),
|
|
midi_in: jack.read().unwrap().register_port("in", MidiIn::default())?,
|
|
audio_outs: vec![],
|
|
output_gain: 0.
|
|
})
|
|
}
|
|
}
|