devalang_wasm/engine/audio/
midi.rs

1use crate::engine::audio::events::AudioEvent;
2use crate::language::syntax::ast::Value;
3/// MIDI file loading and parsing
4use anyhow::{Result, anyhow};
5use std::path::Path;
6
7#[cfg(any(feature = "cli", feature = "wasm"))]
8use midly::{
9    Format, Header, MetaMessage, MidiMessage, Smf, Timing, Track, TrackEvent, TrackEventKind,
10};
11
12#[cfg(all(target_arch = "wasm32", not(any(feature = "cli", feature = "wasm"))))]
13use crate::midly::{
14    Format, Header, MetaMessage, MidiMessage, Smf, Timing, Track, TrackEvent, TrackEventKind,
15};
16
17#[cfg(feature = "cli")]
18use std::collections::HashMap;
19
20/// Load a MIDI file and return a Value::Map representing the MIDI data
21#[cfg(feature = "cli")]
22pub fn load_midi_file(path: &Path) -> Result<Value> {
23    let bytes = std::fs::read(path)
24        .map_err(|e| anyhow!("Failed to read MIDI file {}: {}", path.display(), e))?;
25
26    let smf = Smf::parse(&bytes)
27        .map_err(|e| anyhow!("Failed to parse MIDI file {}: {}", path.display(), e))?;
28
29    // Convert MIDI data to a map
30    let mut midi_map = HashMap::new();
31
32    // Defaults
33    let mut bpm = 120.0f32;
34    let mut tempo_us_per_quarter: u32 = 500_000; // default 120 BPM
35    let mut notes: Vec<Value> = Vec::new();
36
37    // Determine ticks per beat from header timing
38    let ticks_per_beat: u32 = match smf.header.timing {
39        Timing::Metrical(t) => t.as_int() as u32,
40        _ => 480u32,
41    };
42
43    // Active note-on map to pair note-offs: key = (track, channel, key) -> Vec of indices in notes
44    let mut active: std::collections::HashMap<(usize, u8, u8), Vec<usize>> =
45        std::collections::HashMap::new();
46
47    // Process all tracks
48    for (track_idx, track) in smf.tracks.iter().enumerate() {
49        let mut current_ticks: u32 = 0;
50
51        for event in track {
52            current_ticks = current_ticks.wrapping_add(event.delta.as_int() as u32);
53
54            match event.kind {
55                TrackEventKind::Midi { channel, message } => {
56                    let chan = channel.as_int();
57                    match message {
58                        MidiMessage::NoteOn { key, vel } => {
59                            if vel.as_int() > 0 {
60                                // compute time in ms from ticks using current tempo
61                                let time_ms = (current_ticks as f32)
62                                    * (tempo_us_per_quarter as f32)
63                                    / (ticks_per_beat as f32)
64                                    / 1000.0;
65
66                                let mut note_map = HashMap::new();
67                                note_map.insert(
68                                    "tick".to_string(),
69                                    Value::Number(current_ticks as f32),
70                                );
71                                note_map.insert("time".to_string(), Value::Number(time_ms));
72                                // store beat position (useful to rescale when interpreter BPM changes)
73                                let beats = time_ms * (bpm as f32) / 60000.0; // time_ms / (60000/midi_bpm)
74                                note_map.insert("beat".to_string(), Value::Number(beats));
75                                note_map
76                                    .insert("note".to_string(), Value::Number(key.as_int() as f32));
77                                note_map.insert(
78                                    "velocity".to_string(),
79                                    Value::Number(vel.as_int() as f32),
80                                );
81                                note_map
82                                    .insert("track".to_string(), Value::Number(track_idx as f32));
83                                note_map.insert("channel".to_string(), Value::Number(chan as f32));
84
85                                notes.push(Value::Map(note_map));
86
87                                // record active index for pairing
88                                let idx = notes.len() - 1;
89                                active
90                                    .entry((track_idx, chan as u8, key.as_int() as u8))
91                                    .or_default()
92                                    .push(idx);
93                            }
94                        }
95                        MidiMessage::NoteOff { key, .. } => {
96                            // pair with most recent active note-on for same track/channel/key
97                            let key_tuple = (track_idx, channel.as_int() as u8, key.as_int() as u8);
98                            if let Some(vec_idxs) = active.get_mut(&key_tuple) {
99                                if let Some(on_idx) = vec_idxs.pop() {
100                                    // compute duration from ticks
101                                    // find onset tick stored in notes[on_idx]
102                                    if let Some(Value::Map(on_map)) = notes.get_mut(on_idx) {
103                                        if let Some(Value::Number(on_tick)) = on_map.get("tick") {
104                                            let onset_ticks = *on_tick as u32;
105                                            let dur_ticks =
106                                                current_ticks.saturating_sub(onset_ticks);
107                                            let duration_ms = (dur_ticks as f32)
108                                                * (tempo_us_per_quarter as f32)
109                                                / (ticks_per_beat as f32)
110                                                / 1000.0;
111                                            on_map.insert(
112                                                "duration".to_string(),
113                                                Value::Number(duration_ms),
114                                            );
115                                            // also store duration in beats for easier rescaling
116                                            let duration_beats =
117                                                duration_ms * (bpm as f32) / 60000.0;
118                                            on_map.insert(
119                                                "duration_beats".to_string(),
120                                                Value::Number(duration_beats),
121                                            );
122                                        }
123                                    }
124                                }
125                            }
126                        }
127                        _ => {}
128                    }
129                }
130                TrackEventKind::Meta(meta) => {
131                    // Extract tempo if present
132                    if let MetaMessage::Tempo(t) = meta {
133                        tempo_us_per_quarter = t.as_int();
134                        bpm = 60_000_000.0f32 / tempo_us_per_quarter as f32;
135                    }
136                }
137                _ => {}
138            }
139        }
140    }
141
142    // For any lingering active notes without note-off, set a default duration (500 ms)
143    for (_key, vec_idxs) in active.iter() {
144        for &idx in vec_idxs.iter() {
145            if let Some(Value::Map(m)) = notes.get_mut(idx) {
146                if !m.contains_key("duration") {
147                    m.insert("duration".to_string(), Value::Number(500.0));
148                    // default duration beats using current bpm
149                    let default_beats = 500.0 * (bpm as f32) / 60000.0;
150                    m.insert("duration_beats".to_string(), Value::Number(default_beats));
151                }
152            }
153        }
154    }
155
156    // Store in map
157    midi_map.insert("bpm".to_string(), Value::Number(bpm));
158    midi_map.insert(
159        "ticks_per_beat".to_string(),
160        Value::Number(ticks_per_beat as f32),
161    );
162    midi_map.insert("notes".to_string(), Value::Array(notes));
163    midi_map.insert("type".to_string(), Value::String("midi".to_string()));
164
165    Ok(Value::Map(midi_map))
166}
167
168#[cfg(not(feature = "cli"))]
169pub fn load_midi_file(_path: &Path) -> Result<Value> {
170    Err(anyhow!("MIDI loading not available without 'cli' feature"))
171}
172
173// ============================================================================
174// MIDI EXPORT
175// ============================================================================
176
177/// Export AudioEvents to MIDI bytes (for WASM)
178pub fn events_to_midi_bytes(events: &[AudioEvent], bpm: f32) -> Result<Vec<u8>> {
179    if events.is_empty() {
180        return Err(anyhow!("No events to export"));
181    }
182
183    // Create MIDI header (single track format)
184    let ticks_per_beat = 480; // Standard MIDI resolution
185    let header = Header::new(Format::SingleTrack, Timing::Metrical(ticks_per_beat.into()));
186
187    // Create track events list
188    let mut track_events = Vec::new();
189
190    // Add tempo meta event at start
191    let tempo_us_per_quarter = (60_000_000.0 / bpm) as u32;
192    track_events.push(TrackEvent {
193        delta: 0.into(),
194        kind: TrackEventKind::Meta(MetaMessage::Tempo(tempo_us_per_quarter.into())),
195    });
196
197    // Collect all note events (expand chords to individual notes)
198    let mut midi_notes = Vec::new();
199
200    for event in events {
201        match event {
202            AudioEvent::Note {
203                midi,
204                start_time,
205                duration,
206                velocity,
207                ..
208            } => {
209                midi_notes.push(MidiNote {
210                    note: *midi,
211                    start: *start_time,
212                    duration: *duration,
213                    velocity: *velocity,
214                });
215            }
216            AudioEvent::Chord {
217                midis,
218                start_time,
219                duration,
220                velocity,
221                ..
222            } => {
223                // Expand chord into individual notes
224                for &note in midis {
225                    midi_notes.push(MidiNote {
226                        note,
227                        start: *start_time,
228                        duration: *duration,
229                        velocity: *velocity,
230                    });
231                }
232            }
233            AudioEvent::Sample { .. } => {
234                // Samples are not exported to MIDI
235            } // Log messages are stored separately in AudioEventList.logs and are not part of AudioEvent
236        }
237    }
238
239    // Sort by start time
240    midi_notes.sort_by(|a, b| a.start.partial_cmp(&b.start).unwrap());
241
242    // Convert events to MIDI messages with proper delta timing
243    let mut midi_messages: Vec<MidiEventTimed> = Vec::new();
244
245    for note in &midi_notes {
246        let start_ticks = time_to_ticks(note.start, bpm, ticks_per_beat);
247        let end_ticks = time_to_ticks(note.start + note.duration, bpm, ticks_per_beat);
248
249        // Note On
250        midi_messages.push(MidiEventTimed {
251            ticks: start_ticks,
252            message: MidiMessage::NoteOn {
253                key: note.note.into(),
254                vel: ((note.velocity * 127.0) as u8).into(),
255            },
256        });
257
258        // Note Off
259        midi_messages.push(MidiEventTimed {
260            ticks: end_ticks,
261            message: MidiMessage::NoteOff {
262                key: note.note.into(),
263                vel: 0.into(),
264            },
265        });
266    }
267
268    // Sort all messages by time
269    midi_messages.sort_by_key(|msg| msg.ticks);
270
271    // Convert to delta times and create TrackEvents
272    let mut last_ticks = 0u32;
273
274    for msg in midi_messages {
275        let delta = msg.ticks.saturating_sub(last_ticks);
276
277        track_events.push(TrackEvent {
278            delta: delta.into(),
279            kind: TrackEventKind::Midi {
280                channel: 0.into(),
281                message: msg.message,
282            },
283        });
284
285        last_ticks = msg.ticks;
286    }
287
288    // End of track marker
289    track_events.push(TrackEvent {
290        delta: 0.into(),
291        kind: TrackEventKind::Meta(MetaMessage::EndOfTrack),
292    });
293
294    // Create SMF and write to memory buffer
295    let track = Track::from(track_events);
296    let mut smf = Smf::new(header);
297    smf.tracks.push(track);
298
299    // Write to memory buffer
300    let mut buffer = Vec::new();
301    smf.write(&mut buffer)
302        .map_err(|e| anyhow!("Failed to write MIDI bytes: {}", e))?;
303
304    Ok(buffer)
305}
306
307/// Export AudioEvents to a standard MIDI file
308#[cfg(feature = "cli")]
309pub fn export_midi_file(events: &[AudioEvent], output_path: &Path, bpm: f32) -> Result<()> {
310    if events.is_empty() {
311        return Err(anyhow!("No events to export"));
312    }
313
314    // Create MIDI header (single track format)
315    let ticks_per_beat = 480; // Standard MIDI resolution
316    let header = Header::new(Format::SingleTrack, Timing::Metrical(ticks_per_beat.into()));
317
318    // Create track events list
319    let mut track_events = Vec::new();
320
321    // Add tempo meta event at start
322    let tempo_us_per_quarter = (60_000_000.0 / bpm) as u32;
323    track_events.push(TrackEvent {
324        delta: 0.into(),
325        kind: TrackEventKind::Meta(MetaMessage::Tempo(tempo_us_per_quarter.into())),
326    });
327
328    // Collect all note events (expand chords to individual notes)
329    let mut midi_notes = Vec::new();
330
331    for event in events {
332        match event {
333            AudioEvent::Note {
334                midi,
335                start_time,
336                duration,
337                velocity,
338                ..
339            } => {
340                midi_notes.push(MidiNote {
341                    note: *midi,
342                    start: *start_time,
343                    duration: *duration,
344                    velocity: *velocity,
345                });
346            }
347            AudioEvent::Chord {
348                midis,
349                start_time,
350                duration,
351                velocity,
352                ..
353            } => {
354                // Expand chord into individual notes
355                for &note in midis {
356                    midi_notes.push(MidiNote {
357                        note,
358                        start: *start_time,
359                        duration: *duration,
360                        velocity: *velocity,
361                    });
362                }
363            }
364            AudioEvent::Sample { .. } => {
365                // Samples are not exported to MIDI
366            } // Note: Log messages are stored separately in AudioEventList.logs and are
367              // not part of the AudioEvent enum; they are ignored for MIDI export.
368        }
369    }
370
371    // Sort by start time
372    midi_notes.sort_by(|a, b| a.start.partial_cmp(&b.start).unwrap());
373
374    // Convert events to MIDI messages with proper delta timing
375    let mut midi_messages: Vec<MidiEventTimed> = Vec::new();
376
377    for note in &midi_notes {
378        let start_ticks = time_to_ticks(note.start, bpm, ticks_per_beat);
379        let end_ticks = time_to_ticks(note.start + note.duration, bpm, ticks_per_beat);
380
381        // Note On
382        midi_messages.push(MidiEventTimed {
383            ticks: start_ticks,
384            message: MidiMessage::NoteOn {
385                key: note.note.into(),
386                vel: ((note.velocity * 127.0) as u8).into(),
387            },
388        });
389
390        // Note Off
391        midi_messages.push(MidiEventTimed {
392            ticks: end_ticks,
393            message: MidiMessage::NoteOff {
394                key: note.note.into(),
395                vel: 0.into(),
396            },
397        });
398    }
399
400    // Sort all messages by time
401    midi_messages.sort_by_key(|msg| msg.ticks);
402
403    // Convert to delta times and create TrackEvents
404    let mut last_ticks = 0u32;
405
406    for msg in midi_messages {
407        let delta = msg.ticks.saturating_sub(last_ticks);
408
409        track_events.push(TrackEvent {
410            delta: delta.into(),
411            kind: TrackEventKind::Midi {
412                channel: 0.into(),
413                message: msg.message,
414            },
415        });
416
417        last_ticks = msg.ticks;
418    }
419
420    // End of track marker
421    track_events.push(TrackEvent {
422        delta: 0.into(),
423        kind: TrackEventKind::Meta(MetaMessage::EndOfTrack),
424    });
425
426    // Create SMF and write to file
427    let track = Track::from(track_events);
428    let mut smf = Smf::new(header);
429    smf.tracks.push(track);
430
431    // Write to file directly (midly 0.5 API)
432    smf.save(output_path)
433        .map_err(|e| anyhow!("Failed to write MIDI file {}: {}", output_path.display(), e))?;
434
435    println!(
436        "✅ MIDI exported: {} ({} events in, {} notes written)",
437        output_path.display(),
438        events.len(),
439        midi_notes.len()
440    );
441    Ok(())
442}
443
444#[cfg(not(feature = "cli"))]
445pub fn export_midi_file(_events: &[AudioEvent], _output_path: &Path, _bpm: f32) -> Result<()> {
446    Err(anyhow!("MIDI export not available without 'cli' feature"))
447}
448
449// Helper structures for MIDI export
450#[derive(Debug, Clone)]
451struct MidiNote {
452    note: u8,
453    start: f32,
454    duration: f32,
455    velocity: f32,
456}
457
458#[derive(Debug, Clone)]
459struct MidiEventTimed {
460    ticks: u32,
461    message: MidiMessage,
462}
463
464/// Convert time in seconds to MIDI ticks
465fn time_to_ticks(time_seconds: f32, bpm: f32, ticks_per_beat: u16) -> u32 {
466    let beats = time_seconds * (bpm / 60.0);
467    (beats * ticks_per_beat as f32) as u32
468}