Midi recording
This commit is contained in:
@@ -2431,6 +2431,12 @@ impl Engine {
|
||||
fn handle_stop_midi_recording(&mut self) {
|
||||
eprintln!("[MIDI_RECORDING] handle_stop_midi_recording called");
|
||||
if let Some(mut recording) = self.midi_recording_state.take() {
|
||||
// Send note-off to the synth for any notes still held, so they don't get stuck
|
||||
let track_id_for_noteoff = recording.track_id;
|
||||
for note_num in recording.active_note_numbers() {
|
||||
self.project.send_midi_note_off(track_id_for_noteoff, note_num);
|
||||
}
|
||||
|
||||
// Close out any active notes at the current playhead position
|
||||
let end_time = self.playhead as f64 / self.sample_rate as f64;
|
||||
eprintln!("[MIDI_RECORDING] Closing active notes at time {}", end_time);
|
||||
|
||||
@@ -256,7 +256,8 @@ impl MidiClipInstance {
|
||||
// Get events from the clip that fall within the internal range
|
||||
for event in &clip.events {
|
||||
// Skip events outside the trimmed region
|
||||
if event.timestamp < self.internal_start || event.timestamp >= self.internal_end {
|
||||
// Use > (not >=) for internal_end so note-offs at the clip boundary are included
|
||||
if event.timestamp < self.internal_start || event.timestamp > self.internal_end {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -265,9 +266,10 @@ impl MidiClipInstance {
|
||||
let timeline_time = self.external_start + loop_offset + relative_content_time;
|
||||
|
||||
// Check if within current buffer range and instance bounds
|
||||
// Use <= for external_end so note-offs at the clip boundary are included
|
||||
if timeline_time >= range_start_seconds
|
||||
&& timeline_time < range_end_seconds
|
||||
&& timeline_time < external_end
|
||||
&& timeline_time <= external_end
|
||||
{
|
||||
let mut adjusted_event = *event;
|
||||
adjusted_event.timestamp = timeline_time;
|
||||
|
||||
@@ -253,6 +253,11 @@ impl MidiRecordingState {
|
||||
self.completed_notes.len()
|
||||
}
|
||||
|
||||
/// Get the note numbers of all currently held (active) notes
|
||||
pub fn active_note_numbers(&self) -> Vec<u8> {
|
||||
self.active_notes.keys().copied().collect()
|
||||
}
|
||||
|
||||
/// Close out all active notes at the given time
|
||||
/// This should be called when stopping recording to end any held notes
|
||||
pub fn close_active_notes(&mut self, end_time: f64) {
|
||||
|
||||
@@ -7,7 +7,7 @@ use super::node_graph::nodes::{AudioInputNode, AudioOutputNode};
|
||||
use super::node_graph::preset::GraphPreset;
|
||||
use super::pool::AudioClipPool;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// Track ID type
|
||||
pub type TrackId = u32;
|
||||
@@ -334,6 +334,10 @@ pub struct MidiTrack {
|
||||
/// Queue for live MIDI input (virtual keyboard, MIDI controllers)
|
||||
#[serde(skip)]
|
||||
live_midi_queue: Vec<MidiEvent>,
|
||||
/// Clip instances that were active (overlapping playhead) in the previous render buffer.
|
||||
/// Used to detect when the playhead exits a clip, so we can send all-notes-off.
|
||||
#[serde(skip)]
|
||||
prev_active_instances: HashSet<MidiClipInstanceId>,
|
||||
}
|
||||
|
||||
impl Clone for MidiTrack {
|
||||
@@ -350,6 +354,7 @@ impl Clone for MidiTrack {
|
||||
automation_lanes: self.automation_lanes.clone(),
|
||||
next_automation_id: self.next_automation_id,
|
||||
live_midi_queue: Vec::new(), // Don't clone live MIDI queue
|
||||
prev_active_instances: HashSet::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -372,6 +377,7 @@ impl MidiTrack {
|
||||
automation_lanes: HashMap::new(),
|
||||
next_automation_id: 0,
|
||||
live_midi_queue: Vec::new(),
|
||||
prev_active_instances: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -505,7 +511,11 @@ impl MidiTrack {
|
||||
|
||||
// Collect MIDI events from all clip instances that overlap with current time range
|
||||
let mut midi_events = Vec::new();
|
||||
let mut currently_active = HashSet::new();
|
||||
for instance in &self.clip_instances {
|
||||
if instance.overlaps_range(playhead_seconds, buffer_end_seconds) {
|
||||
currently_active.insert(instance.id);
|
||||
}
|
||||
// Get the clip content from the pool
|
||||
if let Some(clip) = midi_pool.get_clip(instance.clip_id) {
|
||||
let events = instance.get_events_in_range(
|
||||
@@ -517,6 +527,18 @@ impl MidiTrack {
|
||||
}
|
||||
}
|
||||
|
||||
// Send all-notes-off for clip instances that just became inactive
|
||||
// (playhead exited the clip). This prevents stuck notes from malformed clips.
|
||||
for prev_id in &self.prev_active_instances {
|
||||
if !currently_active.contains(prev_id) {
|
||||
for note in 0..128u8 {
|
||||
midi_events.push(MidiEvent::note_off(playhead_seconds, 0, note, 0));
|
||||
}
|
||||
break; // One round of all-notes-off is enough
|
||||
}
|
||||
}
|
||||
self.prev_active_instances = currently_active;
|
||||
|
||||
// Add live MIDI events (from virtual keyboard or MIDI controllers)
|
||||
// This allows real-time input to be heard during playback/recording
|
||||
midi_events.extend(self.live_midi_queue.drain(..));
|
||||
|
||||
Reference in New Issue
Block a user