mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
54 lines
1.8 KiB
Rust
54 lines
1.8 KiB
Rust
use crate::*;
|
|
|
|
pub type MidiSample = (Option<u7>, Arc<RwLock<crate::Sample>>);
|
|
|
|
impl Sampler {
|
|
|
|
/// Create [Voice]s from [Sample]s in response to MIDI input.
|
|
pub fn process_midi_in (&mut self, scope: &ProcessScope) {
|
|
let Sampler { midi_in, mapped, voices, .. } = self;
|
|
if let Some(ref midi_in) = midi_in {
|
|
for RawMidi { time, bytes } in midi_in.port().iter(scope) {
|
|
if let LiveEvent::Midi { message, .. } = LiveEvent::parse(bytes).unwrap() {
|
|
match message {
|
|
MidiMessage::NoteOn { ref key, ref vel } => {
|
|
if let Some(ref sample) = mapped[key.as_int() as usize] {
|
|
voices.write().unwrap().push(Sample::play(sample, time as usize, vel));
|
|
}
|
|
},
|
|
MidiMessage::Controller { controller: _, value: _ } => {
|
|
// TODO
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
impl Sample {
|
|
pub fn handle_cc (&mut self, controller: u7, value: u7) {
|
|
let percentage = value.as_int() as f64 / 127.;
|
|
match controller.as_int() {
|
|
20 => {
|
|
self.start = (percentage * self.end as f64) as usize;
|
|
},
|
|
21 => {
|
|
let length = self.channels[0].len();
|
|
self.end = length.min(
|
|
self.start + (percentage * (length as f64 - self.start as f64)) as usize
|
|
);
|
|
},
|
|
22 => { /*attack*/ },
|
|
23 => { /*decay*/ },
|
|
24 => {
|
|
self.gain = percentage as f32 * 2.0;
|
|
},
|
|
26 => { /* pan */ }
|
|
25 => { /* pitch */ }
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|