From b9018c774bd34ae795502da268e04a7b1105f489 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 29 Jun 2024 23:51:46 +0300 Subject: [PATCH] sequencer/sampler plays in time --- src/device/chain/sampler.rs | 75 +++++++++++++++------------------ src/device/sequencer/process.rs | 6 +-- src/main.rs | 6 +-- 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/src/device/chain/sampler.rs b/src/device/chain/sampler.rs index 9363daa2..088e075a 100644 --- a/src/device/chain/sampler.rs +++ b/src/device/chain/sampler.rs @@ -2,16 +2,30 @@ use crate::prelude::*; pub struct Voice { sample: Arc, + after: usize, position: usize, } impl Voice { - fn chunk (&mut self, frames: usize) -> Option>> { - let mut chunk = vec![]; + fn chunk (&mut self, mut frames: usize) -> Option>> { + // Create output buffer for each channel + let mut chunk = vec![vec![];self.sample.channels.len()]; + // If it's not time to play yet, count down + if self.after >= frames { + self.after = self.after - frames; + return Some(chunk) + } + // If the voice will start playing within the current buffer, + // subtract the remaining number of wait frames. + if self.after > 0 && self.after < frames { + chunk = vec![vec![0.0;self.after];self.sample.channels.len()]; + frames = frames - self.after; + self.after = 0; + } if self.position < self.sample.end { let start = self.position.min(self.sample.end); let end = (self.position + frames).min(self.sample.end); - for channel in self.sample.channels.iter() { - chunk.push(channel[start..end].into()); + for (i, channel) in self.sample.channels.iter().enumerate() { + chunk[i].extend_from_slice(&channel[start..end]); }; self.position = self.position + frames; Some(chunk) @@ -31,8 +45,8 @@ impl Sample { pub fn new (name: &str, start: usize, end: usize, channels: Vec>) -> Arc { Arc::new(Self { name: name.to_string(), start, end, channels }) } - fn play (self: &Arc) -> Voice { - Voice { sample: self.clone(), position: self.start } + fn play (self: &Arc, after: usize) -> Voice { + Voice { sample: self.clone(), after, position: self.start } } } @@ -70,10 +84,19 @@ impl Sampler { } pub fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control { - // Output buffer. This will be copied to the audio outs. + // Output buffer: this will be copied to the audio outs. let channel_count = self.audio_outs.len(); let mut mixed = vec![vec![0.0;scope.n_frames() as usize];channel_count]; - + // Process MIDI input to add new voices. + for RawMidi { time, bytes } in self.midi_in.iter(scope) { + if let LiveEvent::Midi { message, .. } = LiveEvent::parse(bytes).unwrap() { + if let MidiMessage::NoteOn { ref key, .. } = message { + if let Some(sample) = self.samples.get(key) { + self.voices.push(sample.play(time as usize)); + } + } + } + } // Emit next chunk of each currently playing voice, // dropping voices that have reached their ends. let mut voices = vec![]; @@ -93,11 +116,7 @@ impl Sampler { self.voices.push(voice); } } - - // process midi in - // add new voices - // emit new voices starting from midi event frames - // + // Write output buffer to output ports. for (i, port) in self.audio_outs.iter_mut().enumerate() { let buffer = &mixed[i]; for (i, value) in port.as_mut_slice(scope).iter_mut().enumerate() { @@ -105,34 +124,6 @@ impl Sampler { } } Control::Continue - - //for event in self.midi_in.iter(scope) { - //let len = 3.min(event.bytes.len()); - //let mut data = [0; 3]; - //data[..len].copy_from_slice(&event.bytes[..len]); - //if (data[0] >> 4) == 0b1001 { // note on - //let channel = data[0] & 0b00001111; - //let note = data[1]; - //let velocity = data[2]; - //for sample in self.samples.iter_mut() { - //if sample.trigger.0 == channel && sample.trigger.1 == note { - //sample.play(velocity); - //} - //} - //} - //for sample in self.samples.iter_mut() { - //if let Some(playing) = sample.playing { - //for (index, value) in sample.port.as_mut_slice(scope).iter_mut().enumerate() { - //*value = *sample.data[0].get(playing + index).unwrap_or(&0f32); - //} - //if playing + scope.n_frames() as usize > sample.data[0].len() { - //sample.playing = None - //} else { - //sample.playing = Some(playing + scope.n_frames() as usize) - //} - //} - //} - //} } fn load_sample (&mut self, _path: &str) {} @@ -253,7 +244,7 @@ fn cursor_down (state: &mut Sampler) -> Usually { fn select (state: &mut Sampler) -> Usually { for (i, sample) in state.samples.values().enumerate() { if i == state.cursor.0 { - state.voices.push(sample.play()) + state.voices.push(sample.play(0)) } } Ok(true) diff --git a/src/device/sequencer/process.rs b/src/device/sequencer/process.rs index 286c3476..e790d4c9 100644 --- a/src/device/sequencer/process.rs +++ b/src/device/sequencer/process.rs @@ -125,10 +125,10 @@ fn monitor_and_record ( tick: u32, ) { for event in midi_in.iter(scope) { - let msg = midly::live::LiveEvent::parse(event.bytes).unwrap(); + let msg = LiveEvent::parse(event.bytes).unwrap(); match msg { - midly::live::LiveEvent::Midi { channel: _, message } => match message { - midly::MidiMessage::NoteOn { key, vel: _ } => { + LiveEvent::Midi { message, .. } => match message { + MidiMessage::NoteOn { key, vel: _ } => { notes_on[key.as_int() as usize] = true; if monitoring { let t = event.time as usize; diff --git a/src/main.rs b/src/main.rs index 32d86b6b..80122f36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,9 +60,9 @@ fn main () -> Result<(), Box> { ]), Some(vec![ Phrase::new("HelloKick", ppq * 4, Some(BTreeMap::from([ ( ppq * 0, vec![MidiMessage::NoteOn { key: 36.into(), vel: 100.into() }] ), - ( ppq * 1, vec![MidiMessage::NoteOn { key: 36.into(), vel: 100.into() }] ), - ( ppq * 2, vec![MidiMessage::NoteOn { key: 36.into(), vel: 100.into() }] ), - ( ppq * 3, vec![MidiMessage::NoteOn { key: 36.into(), vel: 100.into() }] ), + ( ppq * 1, vec![MidiMessage::NoteOn { key: 40.into(), vel: 100.into() }] ), + ( ppq * 5 / 2, vec![MidiMessage::NoteOn { key: 36.into(), vel: 100.into() }] ), + ( ppq * 3, vec![MidiMessage::NoteOn { key: 40.into(), vel: 100.into() }] ), ]))) ]))?,