sequencer/sampler plays in time

This commit is contained in:
🪞👃🪞 2024-06-29 23:51:46 +03:00
parent 82b5614040
commit b9018c774b
3 changed files with 39 additions and 48 deletions

View file

@ -2,16 +2,30 @@ use crate::prelude::*;
pub struct Voice {
sample: Arc<Sample>,
after: usize,
position: usize,
}
impl Voice {
fn chunk (&mut self, frames: usize) -> Option<Vec<Vec<f32>>> {
let mut chunk = vec![];
fn chunk (&mut self, mut frames: usize) -> Option<Vec<Vec<f32>>> {
// 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<Vec<f32>>) -> Arc<Self> {
Arc::new(Self { name: name.to_string(), start, end, channels })
}
fn play (self: &Arc<Self>) -> Voice {
Voice { sample: self.clone(), position: self.start }
fn play (self: &Arc<Self>, 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<bool> {
fn select (state: &mut Sampler) -> Usually<bool> {
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)

View file

@ -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;

View file

@ -60,9 +60,9 @@ fn main () -> Result<(), Box<dyn Error>> {
]), 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() }] ),
])))
]))?,