Add count-in
This commit is contained in:
@@ -27,7 +27,7 @@ pub struct Engine {
|
||||
project: Project,
|
||||
audio_pool: AudioClipPool,
|
||||
buffer_pool: BufferPool,
|
||||
playhead: u64, // Playhead position in samples
|
||||
playhead: i64, // Playhead position in samples (may be negative during count-in pre-roll)
|
||||
sample_rate: u32,
|
||||
playing: bool,
|
||||
channels: u32,
|
||||
@@ -74,6 +74,10 @@ pub struct Engine {
|
||||
// MIDI recording state
|
||||
midi_recording_state: Option<MidiRecordingState>,
|
||||
|
||||
// Currently held MIDI notes per track (note -> velocity), updated on NoteOn/NoteOff
|
||||
// Used to inject held notes when recording starts mid-press (e.g. after count-in)
|
||||
midi_held_notes: HashMap<TrackId, HashMap<u8, u8>>,
|
||||
|
||||
// MIDI input manager for external MIDI devices
|
||||
midi_input_manager: Option<MidiInputManager>,
|
||||
|
||||
@@ -160,6 +164,7 @@ impl Engine {
|
||||
recording_mirror_tx: None,
|
||||
recording_progress_counter: 0,
|
||||
midi_recording_state: None,
|
||||
midi_held_notes: HashMap::new(),
|
||||
midi_input_manager: None,
|
||||
metronome: Metronome::new(sample_rate),
|
||||
recording_sample_buffer: Vec::with_capacity(4096),
|
||||
@@ -396,17 +401,18 @@ impl Engine {
|
||||
);
|
||||
|
||||
// Update playhead (convert total samples to frames)
|
||||
self.playhead += (output.len() / self.channels as usize) as u64;
|
||||
self.playhead += (output.len() / self.channels as usize) as i64;
|
||||
|
||||
// Update atomic playhead for UI reads
|
||||
// Update atomic playhead for UI reads (clamped to 0; negative = count-in pre-roll)
|
||||
self.playhead_atomic
|
||||
.store(self.playhead, Ordering::Relaxed);
|
||||
.store(self.playhead.max(0) as u64, Ordering::Relaxed);
|
||||
|
||||
// Send periodic position updates
|
||||
self.frames_since_last_event += output.len() / self.channels as usize;
|
||||
if self.frames_since_last_event >= self.event_interval_frames / self.channels as usize
|
||||
{
|
||||
let position_seconds = self.playhead as f64 / self.sample_rate as f64;
|
||||
// Clamp to 0 during count-in pre-roll (negative playhead = before project start)
|
||||
let position_seconds = self.playhead.max(0) as f64 / self.sample_rate as f64;
|
||||
let _ = self
|
||||
.event_tx
|
||||
.push(AudioEvent::PlaybackPosition(position_seconds));
|
||||
@@ -692,17 +698,17 @@ impl Engine {
|
||||
self.project.stop_all_notes();
|
||||
}
|
||||
Command::Seek(seconds) => {
|
||||
let frames = (seconds * self.sample_rate as f64) as u64;
|
||||
self.playhead = frames;
|
||||
self.playhead_atomic
|
||||
.store(self.playhead, Ordering::Relaxed);
|
||||
self.playhead = (seconds * self.sample_rate as f64) as i64;
|
||||
// Clamp to 0 for atomic/disk-reader; negative = count-in pre-roll (no disk reads needed)
|
||||
let clamped = self.playhead.max(0) as u64;
|
||||
self.playhead_atomic.store(clamped, Ordering::Relaxed);
|
||||
// Stop all MIDI notes when seeking to prevent stuck notes
|
||||
self.project.stop_all_notes();
|
||||
// Reset all node graphs to clear effect buffers (echo, reverb, etc.)
|
||||
self.project.reset_all_graphs();
|
||||
// Notify disk reader to refill buffers from new position
|
||||
if let Some(ref mut dr) = self.disk_reader {
|
||||
dr.send(crate::audio::disk_reader::DiskReaderCommand::Seek { frame: frames });
|
||||
dr.send(crate::audio::disk_reader::DiskReaderCommand::Seek { frame: clamped });
|
||||
}
|
||||
}
|
||||
Command::SetTrackVolume(track_id, volume) => {
|
||||
@@ -1212,6 +1218,9 @@ impl Engine {
|
||||
// Emit event to UI for visual feedback
|
||||
let _ = self.event_tx.push(AudioEvent::NoteOn(note, velocity));
|
||||
|
||||
// Track held notes so count-in recording can inject them at start_time
|
||||
self.midi_held_notes.entry(track_id).or_default().insert(note, velocity);
|
||||
|
||||
// If MIDI recording is active on this track, capture the event
|
||||
if let Some(recording) = &mut self.midi_recording_state {
|
||||
if recording.track_id == track_id {
|
||||
@@ -1230,6 +1239,11 @@ impl Engine {
|
||||
// Emit event to UI for visual feedback
|
||||
let _ = self.event_tx.push(AudioEvent::NoteOff(note));
|
||||
|
||||
// Remove from held notes tracking
|
||||
if let Some(track_notes) = self.midi_held_notes.get_mut(&track_id) {
|
||||
track_notes.remove(¬e);
|
||||
}
|
||||
|
||||
// If MIDI recording is active on this track, capture the event
|
||||
if let Some(recording) = &mut self.midi_recording_state {
|
||||
if recording.track_id == track_id {
|
||||
@@ -3038,7 +3052,17 @@ impl Engine {
|
||||
// Check if track exists and is a MIDI track
|
||||
if let Some(crate::audio::track::TrackNode::Midi(_)) = self.project.get_track_mut(track_id) {
|
||||
// Create MIDI recording state
|
||||
let recording_state = MidiRecordingState::new(track_id, clip_id, start_time);
|
||||
let mut recording_state = MidiRecordingState::new(track_id, clip_id, start_time);
|
||||
|
||||
// Inject any notes currently held on this track (pressed during count-in pre-roll)
|
||||
// so they start at t=0 of the recording rather than being lost
|
||||
if let Some(held) = self.midi_held_notes.get(&track_id) {
|
||||
for (¬e, &velocity) in held {
|
||||
eprintln!("[MIDI_RECORDING] Injecting held note {} vel {} at start_time {:.3}s", note, velocity, start_time);
|
||||
recording_state.note_on(note, velocity, start_time);
|
||||
}
|
||||
}
|
||||
|
||||
self.midi_recording_state = Some(recording_state);
|
||||
|
||||
eprintln!("[MIDI_RECORDING] Started MIDI recording on track {} for clip {}", track_id, clip_id);
|
||||
|
||||
@@ -107,7 +107,7 @@ impl Metronome {
|
||||
pub fn process(
|
||||
&mut self,
|
||||
output: &mut [f32],
|
||||
playhead_samples: u64,
|
||||
playhead_samples: i64,
|
||||
playing: bool,
|
||||
sample_rate: u32,
|
||||
channels: u32,
|
||||
@@ -120,20 +120,21 @@ impl Metronome {
|
||||
let frames = output.len() / channels as usize;
|
||||
|
||||
for frame in 0..frames {
|
||||
let current_sample = playhead_samples + frame as u64;
|
||||
let current_sample = playhead_samples + frame as i64;
|
||||
|
||||
// Calculate current beat number
|
||||
let current_time_seconds = current_sample as f64 / sample_rate as f64;
|
||||
let beats_per_second = self.bpm as f64 / 60.0;
|
||||
let current_beat = (current_time_seconds * beats_per_second).floor() as i64;
|
||||
|
||||
// Check if we crossed a beat boundary
|
||||
if current_beat != self.last_beat && current_beat >= 0 {
|
||||
// Check if we crossed a beat boundary (including negative beats during count-in pre-roll)
|
||||
if current_beat != self.last_beat {
|
||||
self.last_beat = current_beat;
|
||||
|
||||
// Determine which click to play.
|
||||
// Beat 0 of each measure gets the accent (high click).
|
||||
let beat_in_measure = (current_beat as u32 % self.time_signature_numerator) as usize;
|
||||
// Use rem_euclid so negative beat numbers map correctly (e.g. -4 % 4 = 0).
|
||||
let beat_in_measure = current_beat.rem_euclid(self.time_signature_numerator as i64) as usize;
|
||||
self.playing_high_click = beat_in_measure == 0;
|
||||
self.click_position = 0; // Start from beginning of click
|
||||
}
|
||||
|
||||
@@ -213,7 +213,6 @@ impl MidiRecordingState {
|
||||
|
||||
/// Handle a MIDI note on event
|
||||
pub fn note_on(&mut self, note: u8, velocity: u8, absolute_time: f64) {
|
||||
// Store this note as active
|
||||
self.active_notes.insert(note, ActiveMidiNote {
|
||||
note,
|
||||
velocity,
|
||||
@@ -225,14 +224,21 @@ impl MidiRecordingState {
|
||||
pub fn note_off(&mut self, note: u8, absolute_time: f64) {
|
||||
// Find the matching noteOn
|
||||
if let Some(active_note) = self.active_notes.remove(¬e) {
|
||||
// Calculate relative time offset and duration
|
||||
let time_offset = active_note.start_time - self.start_time;
|
||||
let duration = absolute_time - active_note.start_time;
|
||||
// If the note was fully released before the recording start (e.g. during count-in
|
||||
// pre-roll), discard it — only notes still held at the clip start are kept.
|
||||
if absolute_time <= self.start_time {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clamp note start to clip start: notes held across the recording boundary
|
||||
// are treated as starting at the clip position.
|
||||
let note_start = active_note.start_time.max(self.start_time);
|
||||
let time_offset = note_start - self.start_time;
|
||||
let duration = absolute_time - note_start;
|
||||
|
||||
eprintln!("[MIDI_RECORDING_STATE] Completing note {}: note_start={:.3}s, note_end={:.3}s, recording_start={:.3}s, time_offset={:.3}s, duration={:.3}s",
|
||||
note, active_note.start_time, absolute_time, self.start_time, time_offset, duration);
|
||||
note, note_start, absolute_time, self.start_time, time_offset, duration);
|
||||
|
||||
// Add to completed notes
|
||||
self.completed_notes.push((
|
||||
time_offset,
|
||||
active_note.note,
|
||||
@@ -258,8 +264,9 @@ impl MidiRecordingState {
|
||||
pub fn get_notes_with_active(&self, current_time: f64) -> Vec<(f64, u8, u8, f64)> {
|
||||
let mut notes = self.completed_notes.clone();
|
||||
for active in self.active_notes.values() {
|
||||
let time_offset = active.start_time - self.start_time;
|
||||
let provisional_dur = (current_time - active.start_time).max(0.0);
|
||||
let note_start = active.start_time.max(self.start_time);
|
||||
let time_offset = note_start - self.start_time;
|
||||
let provisional_dur = (current_time - note_start).max(0.0);
|
||||
notes.push((time_offset, active.note, active.velocity, provisional_dur));
|
||||
}
|
||||
notes
|
||||
@@ -273,15 +280,13 @@ impl MidiRecordingState {
|
||||
/// 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) {
|
||||
// Collect all active notes and close them
|
||||
let active_notes: Vec<_> = self.active_notes.drain().collect();
|
||||
|
||||
for (_note_num, active_note) in active_notes {
|
||||
// Calculate relative time offset and duration
|
||||
let time_offset = active_note.start_time - self.start_time;
|
||||
let duration = end_time - active_note.start_time;
|
||||
let note_start = active_note.start_time.max(self.start_time);
|
||||
let time_offset = note_start - self.start_time;
|
||||
let duration = end_time - note_start;
|
||||
|
||||
// Add to completed notes
|
||||
self.completed_notes.push((
|
||||
time_offset,
|
||||
active_note.note,
|
||||
|
||||
Reference in New Issue
Block a user