mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
wip: refactor into fewer crates
This commit is contained in:
parent
c367a0444e
commit
77703d83a5
105 changed files with 64 additions and 131 deletions
54
crates/device/src/sampler/sampler_midi.rs
Normal file
54
crates/device/src/sampler/sampler_midi.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
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 */ }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue