jammable again - and autoconnects!

This commit is contained in:
🪞👃🪞 2024-07-05 01:28:27 +03:00
parent 3ed9ebddd4
commit 768c2337e7
5 changed files with 98 additions and 59 deletions

View file

@ -70,6 +70,7 @@ impl Sampler {
// dropping voices that have reached their ends.
let mut voices = vec![];
std::mem::swap(&mut voices, &mut self.voices);
let gain = 0.5;
loop {
if voices.len() < 1 {
break
@ -79,7 +80,7 @@ impl Sampler {
for (i, channel) in chunk.iter().enumerate() {
let buffer = &mut mixed[i % channel_count];
for (i, sample) in channel.iter().enumerate() {
buffer[i] += sample;
buffer[i] += sample * gain;
}
}
self.voices.push(voice);

View file

@ -61,7 +61,7 @@ impl Track {
frames: usize,
panic: bool,
) {
// Need to be borrowed outside the conditional
// Need to be borrowed outside the conditionals?
let recording = self.recording;
let monitoring = self.monitoring;
// Output buffer
@ -82,39 +82,42 @@ impl Track {
}
}
// Monitor and record input
let phrase = &mut self.phrase_mut();
for (time, event, bytes) in parse_midi_input(input) {
let pulse = timebase.frames_pulses((frame0 + time) as f64) as usize;
match event {
LiveEvent::Midi { message, .. } => {
if monitoring {
if let Some(Some(frame)) = output.get_mut(time) {
frame.push(bytes.into())
} else {
output[time] = Some(vec![bytes.into()]);
}
}
if recording {
if let Some(phrase) = phrase {
let contains = phrase.notes.contains_key(&pulse);
if contains {
phrase.notes.get_mut(&pulse).unwrap().push(message.clone());
if self.recording || self.monitoring {
let phrase = &mut self.phrase_mut();
for (time, event, bytes) in parse_midi_input(input) {
match event {
LiveEvent::Midi { message, .. } => {
if monitoring {
if let Some(Some(frame)) = output.get_mut(time) {
frame.push(bytes.into())
} else {
phrase.notes.insert(pulse, vec![message.clone()]);
output[time] = Some(vec![bytes.into()]);
}
};
}
match message {
MidiMessage::NoteOn { key, .. } => {
notes_on[key.as_int() as usize] = true;
}
MidiMessage::NoteOff { key, .. } => {
notes_on[key.as_int() as usize] = false;
},
_ => {}
}
},
_ => {}
if recording {
if let Some(phrase) = phrase {
let pulse = timebase.frames_pulses((frame0 + time) as f64) as usize;
let pulse = pulse % phrase.length;
let contains = phrase.notes.contains_key(&pulse);
if contains {
phrase.notes.get_mut(&pulse).unwrap().push(message.clone());
} else {
phrase.notes.insert(pulse, vec![message.clone()]);
}
};
}
match message {
MidiMessage::NoteOn { key, .. } => {
notes_on[key.as_int() as usize] = true;
}
MidiMessage::NoteOff { key, .. } => {
notes_on[key.as_int() as usize] = false;
},
_ => {}
}
},
_ => {}
}
}
}
self.notes_on = notes_on;
@ -171,4 +174,13 @@ impl Track {
let index = self.phrases.len() - 1;
&mut self.phrases[index]
}
pub fn toggle_monitor (&mut self) {
self.monitoring = !self.monitoring;
}
pub fn toggle_record (&mut self) {
self.recording = !self.recording;
}
pub fn toggle_overdub (&mut self) {
self.overdub = !self.overdub;
}
}