Midi recording
This commit is contained in:
@@ -3482,13 +3482,103 @@ impl eframe::App for EditorApp {
|
||||
self.recording_layer_id = None;
|
||||
ctx.request_repaint();
|
||||
}
|
||||
AudioEvent::MidiRecordingProgress(_track_id, _clip_id, duration, _notes) => {
|
||||
println!("🎹 MIDI recording progress: {:.2}s", duration);
|
||||
AudioEvent::MidiRecordingProgress(_track_id, clip_id, duration, notes) => {
|
||||
// Update clip duration in document (so timeline bar grows)
|
||||
if let Some(layer_id) = self.recording_layer_id {
|
||||
let doc_clip_id = {
|
||||
let document = self.action_executor.document();
|
||||
document.root.children.iter()
|
||||
.find(|l| l.id() == layer_id)
|
||||
.and_then(|layer| {
|
||||
if let lightningbeam_core::layer::AnyLayer::Audio(audio_layer) = layer {
|
||||
audio_layer.clip_instances.last().map(|i| i.clip_id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
if let Some(doc_clip_id) = doc_clip_id {
|
||||
if let Some(clip) = self.action_executor.document_mut().audio_clips.get_mut(&doc_clip_id) {
|
||||
clip.duration = duration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update midi_event_cache with notes captured so far
|
||||
// (inlined instead of calling rebuild_midi_cache_entry to avoid
|
||||
// conflicting &mut self borrow with event_rx loop)
|
||||
{
|
||||
let mut events: Vec<(f64, u8, u8, bool)> = Vec::with_capacity(notes.len() * 2);
|
||||
for &(start_time, note, velocity, dur) in ¬es {
|
||||
events.push((start_time, note, velocity, true));
|
||||
events.push((start_time + dur, note, velocity, false));
|
||||
}
|
||||
events.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
|
||||
self.midi_event_cache.insert(clip_id, events);
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
AudioEvent::MidiRecordingStopped(track_id, clip_id, note_count) => {
|
||||
println!("🎹 MIDI recording stopped: track={:?}, clip_id={}, {} notes",
|
||||
track_id, clip_id, note_count);
|
||||
|
||||
// Query backend for the definitive final note data
|
||||
if let Some(ref controller_arc) = self.audio_controller {
|
||||
let mut controller = controller_arc.lock().unwrap();
|
||||
match controller.query_midi_clip(track_id, clip_id) {
|
||||
Ok(midi_clip_data) => {
|
||||
// Convert backend MidiEvent format to cache format
|
||||
let cache_events: Vec<(f64, u8, u8, bool)> = midi_clip_data.events.iter()
|
||||
.filter_map(|event| {
|
||||
let status_type = event.status & 0xF0;
|
||||
if status_type == 0x90 || status_type == 0x80 {
|
||||
let is_note_on = status_type == 0x90 && event.data2 > 0;
|
||||
Some((event.timestamp, event.data1, event.data2, is_note_on))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
drop(controller);
|
||||
self.midi_event_cache.insert(clip_id, cache_events);
|
||||
|
||||
// Update document clip with final duration and name
|
||||
if let Some(layer_id) = self.recording_layer_id {
|
||||
let doc_clip_id = {
|
||||
let document = self.action_executor.document();
|
||||
document.root.children.iter()
|
||||
.find(|l| l.id() == layer_id)
|
||||
.and_then(|layer| {
|
||||
if let lightningbeam_core::layer::AnyLayer::Audio(audio_layer) = layer {
|
||||
audio_layer.clip_instances.last().map(|i| i.clip_id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
};
|
||||
if let Some(doc_clip_id) = doc_clip_id {
|
||||
if let Some(clip) = self.action_executor.document_mut().audio_clips.get_mut(&doc_clip_id) {
|
||||
clip.duration = midi_clip_data.duration;
|
||||
clip.name = format!("MIDI Recording {}", clip_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("✅ Finalized MIDI recording: {} notes, {:.2}s",
|
||||
note_count, midi_clip_data.duration);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to query MIDI clip data after recording: {}", e);
|
||||
// Cache was already populated by last MidiRecordingProgress event
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Store clip_instance_to_backend_map entry for this MIDI clip.
|
||||
// The backend created the instance in create_midi_clip(), but doesn't
|
||||
// report the instance_id back. Needed for move/trim operations later.
|
||||
|
||||
// Clear recording state
|
||||
self.is_recording = false;
|
||||
self.recording_clips.clear();
|
||||
|
||||
@@ -150,21 +150,25 @@ impl TimelinePane {
|
||||
|
||||
/// Start recording on the active audio layer
|
||||
fn start_recording(&mut self, shared: &mut SharedPaneState) {
|
||||
use lightningbeam_core::clip::{AudioClip, ClipInstance};
|
||||
|
||||
let Some(active_layer_id) = *shared.active_layer_id else {
|
||||
println!("⚠️ No active layer selected for recording");
|
||||
return;
|
||||
};
|
||||
|
||||
// Get the active layer and check if it's an audio layer
|
||||
let document = shared.action_executor.document();
|
||||
let Some(layer) = document.root.children.iter().find(|l| l.id() == active_layer_id) else {
|
||||
println!("⚠️ Active layer not found in document");
|
||||
return;
|
||||
};
|
||||
|
||||
let AnyLayer::Audio(audio_layer) = layer else {
|
||||
println!("⚠️ Active layer is not an audio layer - cannot record");
|
||||
return;
|
||||
// Get layer type (copy it so we can drop the document borrow before mutating)
|
||||
let layer_type = {
|
||||
let document = shared.action_executor.document();
|
||||
let Some(layer) = document.root.children.iter().find(|l| l.id() == active_layer_id) else {
|
||||
println!("⚠️ Active layer not found in document");
|
||||
return;
|
||||
};
|
||||
let AnyLayer::Audio(audio_layer) = layer else {
|
||||
println!("⚠️ Active layer is not an audio layer - cannot record");
|
||||
return;
|
||||
};
|
||||
audio_layer.audio_layer_type
|
||||
};
|
||||
|
||||
// Get the backend track ID for this layer
|
||||
@@ -179,31 +183,53 @@ impl TimelinePane {
|
||||
if let Some(controller_arc) = shared.audio_controller {
|
||||
let mut controller = controller_arc.lock().unwrap();
|
||||
|
||||
match audio_layer.audio_layer_type {
|
||||
match layer_type {
|
||||
AudioLayerType::Midi => {
|
||||
// For MIDI recording, we need to create a clip first
|
||||
// The backend will emit MidiRecordingStarted with the clip_id
|
||||
// Create backend MIDI clip and start recording
|
||||
let clip_id = controller.create_midi_clip(track_id, start_time, 4.0);
|
||||
controller.start_midi_recording(track_id, clip_id, start_time);
|
||||
shared.recording_clips.insert(active_layer_id, clip_id);
|
||||
println!("🎹 Started MIDI recording on track {:?} at {:.2}s, clip_id={}",
|
||||
track_id, start_time, clip_id);
|
||||
|
||||
// Drop controller lock before document mutation
|
||||
drop(controller);
|
||||
|
||||
// Create document clip + clip instance immediately (clip_id is known synchronously)
|
||||
let doc_clip = AudioClip::new_midi("Recording...", clip_id, 4.0);
|
||||
let doc_clip_id = shared.action_executor.document_mut().add_audio_clip(doc_clip);
|
||||
|
||||
let clip_instance = ClipInstance::new(doc_clip_id)
|
||||
.with_timeline_start(start_time);
|
||||
|
||||
if let Some(layer) = shared.action_executor.document_mut().root.children.iter_mut()
|
||||
.find(|l| l.id() == active_layer_id)
|
||||
{
|
||||
if let lightningbeam_core::layer::AnyLayer::Audio(audio_layer) = layer {
|
||||
audio_layer.clip_instances.push(clip_instance);
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize empty cache entry for this clip
|
||||
shared.midi_event_cache.insert(clip_id, Vec::new());
|
||||
}
|
||||
AudioLayerType::Sampled => {
|
||||
// For audio recording, backend creates the clip
|
||||
controller.start_recording(track_id, start_time);
|
||||
println!("🎤 Started audio recording on track {:?} at {:.2}s", track_id, start_time);
|
||||
drop(controller);
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-start playback if not already playing
|
||||
// Re-acquire lock for playback start
|
||||
if !*shared.is_playing {
|
||||
let mut controller = controller_arc.lock().unwrap();
|
||||
controller.play();
|
||||
*shared.is_playing = true;
|
||||
println!("▶ Auto-started playback for recording");
|
||||
}
|
||||
|
||||
// Store recording state for clip creation when RecordingStarted event arrives
|
||||
// Store recording state
|
||||
*shared.is_recording = true;
|
||||
*shared.recording_start_time = start_time;
|
||||
*shared.recording_layer_id = Some(active_layer_id);
|
||||
|
||||
Reference in New Issue
Block a user