Add custom layouts, piano pane, midi file import

This commit is contained in:
Skyler Lehmkuhl
2025-10-23 21:15:17 -04:00
parent c46c28c9bb
commit 3de1b05fb3
16 changed files with 2047 additions and 29 deletions
+32 -2
View File
@@ -149,6 +149,12 @@ impl Engine {
}
}
/// Process live MIDI input from all MIDI tracks
fn process_live_midi(&mut self, output: &mut [f32]) {
// Process all MIDI tracks to handle live input
self.project.process_live_midi(output, self.sample_rate, self.channels);
}
/// Process audio callback - called from the audio thread
pub fn process(&mut self, output: &mut [f32]) {
// Process all pending commands
@@ -203,8 +209,8 @@ impl Engine {
self.frames_since_last_event = 0;
}
} else {
// Not playing, output silence
output.fill(0.0);
// Not playing, but process live MIDI input
self.process_live_midi(output);
}
// Process recording if active (independent of playback state)
@@ -263,9 +269,13 @@ impl Engine {
self.playing = false;
self.playhead = 0;
self.playhead_atomic.store(0, Ordering::Relaxed);
// Stop all MIDI notes when stopping playback
self.project.stop_all_notes();
}
Command::Pause => {
self.playing = false;
// Stop all MIDI notes when pausing playback
self.project.stop_all_notes();
}
Command::Seek(seconds) => {
let samples = (seconds * self.sample_rate as f64 * self.channels as f64) as u64;
@@ -673,6 +683,16 @@ impl Engine {
// Notify UI that reset is complete
let _ = self.event_tx.push(AudioEvent::ProjectReset);
}
Command::SendMidiNoteOn(track_id, note, velocity) => {
// Send a live MIDI note on event to the specified track's instrument
self.project.send_midi_note_on(track_id, note, velocity);
}
Command::SendMidiNoteOff(track_id, note) => {
// Send a live MIDI note off event to the specified track's instrument
self.project.send_midi_note_off(track_id, note);
}
}
}
@@ -1083,4 +1103,14 @@ impl EngineController {
pub fn reset(&mut self) {
let _ = self.command_tx.push(Command::Reset);
}
/// Send a live MIDI note on event to a track's instrument
pub fn send_midi_note_on(&mut self, track_id: TrackId, note: u8, velocity: u8) {
let _ = self.command_tx.push(Command::SendMidiNoteOn(track_id, note, velocity));
}
/// Send a live MIDI note off event to a track's instrument
pub fn send_midi_note_off(&mut self, track_id: TrackId, note: u8) {
let _ = self.command_tx.push(Command::SendMidiNoteOff(track_id, note));
}
}
+48
View File
@@ -406,6 +406,54 @@ impl Project {
None => {}
}
}
/// Stop all notes on all MIDI tracks
pub fn stop_all_notes(&mut self) {
for track in self.tracks.values_mut() {
if let TrackNode::Midi(midi_track) = track {
midi_track.stop_all_notes();
}
}
}
/// Process live MIDI input from all MIDI tracks (called even when not playing)
pub fn process_live_midi(&mut self, output: &mut [f32], sample_rate: u32, channels: u32) {
// Process all MIDI tracks to handle queued live input events
for track in self.tracks.values_mut() {
if let TrackNode::Midi(midi_track) = track {
// Process only queued live events, not clips
midi_track.process_live_input(output, sample_rate, channels);
}
}
}
/// Send a live MIDI note on event to a track's instrument
pub fn send_midi_note_on(&mut self, track_id: TrackId, note: u8, velocity: u8) {
if let Some(TrackNode::Midi(track)) = self.tracks.get_mut(&track_id) {
// Create a MIDI event and queue it to the instrument
let event = crate::audio::midi::MidiEvent {
timestamp: 0, // Immediate playback
status: 0x90, // Note on
data1: note,
data2: velocity,
};
track.instrument.queue_event(event);
}
}
/// Send a live MIDI note off event to a track's instrument
pub fn send_midi_note_off(&mut self, track_id: TrackId, note: u8) {
if let Some(TrackNode::Midi(track)) = self.tracks.get_mut(&track_id) {
// Create a MIDI event and queue it to the instrument
let event = crate::audio::midi::MidiEvent {
timestamp: 0, // Immediate playback
status: 0x80, // Note off
data1: note,
data2: 0,
};
track.instrument.queue_event(event);
}
}
}
impl Default for Project {
+27
View File
@@ -388,6 +388,33 @@ impl MidiTrack {
!self.muted && (!any_solo || self.solo)
}
/// Stop all currently playing notes on this track's instrument
pub fn stop_all_notes(&mut self) {
self.instrument.all_notes_off();
}
/// Process only live MIDI input (queued events) without rendering clips
/// This is used when playback is stopped but we want to hear live input
pub fn process_live_input(
&mut self,
output: &mut [f32],
sample_rate: u32,
channels: u32,
) {
// Generate audio from the instrument (which processes queued events)
self.instrument.process(output, channels as usize, sample_rate);
// Apply effect chain
for effect in &mut self.effects {
effect.process(output, channels as usize, sample_rate);
}
// Apply track volume (no automation during live input)
for sample in output.iter_mut() {
*sample *= self.volume;
}
}
/// Render this MIDI track into the output buffer
pub fn render(
&mut self,
+10
View File
@@ -108,6 +108,12 @@ pub enum Command {
// Project commands
/// Reset the entire project (remove all tracks, clear audio pool, reset state)
Reset,
// Live MIDI input commands
/// Send a live MIDI note on event to a track's instrument (track_id, note, velocity)
SendMidiNoteOn(TrackId, u8, u8),
/// Send a live MIDI note off event to a track's instrument (track_id, note)
SendMidiNoteOff(TrackId, u8),
}
/// Events sent from audio thread back to UI/control thread
@@ -139,4 +145,8 @@ pub enum AudioEvent {
RecordingError(String),
/// Project has been reset
ProjectReset,
/// MIDI note started playing (note, velocity)
NoteOn(u8, u8),
/// MIDI note stopped playing (note)
NoteOff(u8),
}
+10
View File
@@ -192,6 +192,16 @@ impl SimpleSynth {
self.pending_events.push(event);
}
/// Stop all currently playing notes immediately (no release envelope)
pub fn all_notes_off(&mut self) {
for voice in &mut self.voices {
voice.active = false;
voice.envelope_state = EnvelopeState::Off;
voice.envelope_level = 0.0;
}
self.pending_events.clear();
}
/// Process all queued events
fn process_events(&mut self) {
// Collect events first to avoid borrowing issues