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            }
236            AudioEvent::Stop { .. } => {
237                // Stop events are control events, not exported to MIDI
238            }
239        }
240    }
241
242    // Sort by start time
243    midi_notes.sort_by(|a, b| a.start.partial_cmp(&b.start).unwrap());
244
245    // Convert events to MIDI messages with proper delta timing
246    let mut midi_messages: Vec<MidiEventTimed> = Vec::new();
247
248    for note in &midi_notes {
249        let start_ticks = time_to_ticks(note.start, bpm, ticks_per_beat);
250        let end_ticks = time_to_ticks(note.start + note.duration, bpm, ticks_per_beat);
251
252        // Note On
253        midi_messages.push(MidiEventTimed {
254            ticks: start_ticks,
255            message: MidiMessage::NoteOn {
256                key: note.note.into(),
257                vel: ((note.velocity * 127.0) as u8).into(),
258            },
259        });
260
261        // Note Off
262        midi_messages.push(MidiEventTimed {
263            ticks: end_ticks,
264            message: MidiMessage::NoteOff {
265                key: note.note.into(),
266                vel: 0.into(),
267            },
268        });
269    }
270
271    // Sort all messages by time
272    midi_messages.sort_by_key(|msg| msg.ticks);
273
274    // Convert to delta times and create TrackEvents
275    let mut last_ticks = 0u32;
276
277    for msg in midi_messages {
278        let delta = msg.ticks.saturating_sub(last_ticks);
279
280        track_events.push(TrackEvent {
281            delta: delta.into(),
282            kind: TrackEventKind::Midi {
283                channel: 0.into(),
284                message: msg.message,
285            },
286        });
287
288        last_ticks = msg.ticks;
289    }
290
291    // End of track marker
292    track_events.push(TrackEvent {
293        delta: 0.into(),
294        kind: TrackEventKind::Meta(MetaMessage::EndOfTrack),
295    });
296
297    // Create SMF and write to memory buffer
298    let track = Track::from(track_events);
299    let mut smf = Smf::new(header);
300    smf.tracks.push(track);
301
302    // Write to memory buffer
303    let mut buffer = Vec::new();
304    smf.write(&mut buffer)
305        .map_err(|e| anyhow!("Failed to write MIDI bytes: {}", e))?;
306
307    Ok(buffer)
308}
309
310/// Export AudioEvents to a standard MIDI file
311#[cfg(feature = "cli")]
312pub fn export_midi_file(events: &[AudioEvent], output_path: &Path, bpm: f32) -> Result<()> {
313    if events.is_empty() {
314        return Err(anyhow!("No events to export"));
315    }
316
317    // Create MIDI header (single track format)
318    let ticks_per_beat = 480; // Standard MIDI resolution
319    let header = Header::new(Format::SingleTrack, Timing::Metrical(ticks_per_beat.into()));
320
321    // Create track events list
322    let mut track_events = Vec::new();
323
324    // Add tempo meta event at start
325    let tempo_us_per_quarter = (60_000_000.0 / bpm) as u32;
326    track_events.push(TrackEvent {
327        delta: 0.into(),
328        kind: TrackEventKind::Meta(MetaMessage::Tempo(tempo_us_per_quarter.into())),
329    });
330
331    // Collect all note events (expand chords to individual notes)
332    let mut midi_notes = Vec::new();
333
334    for event in events {
335        match event {
336            AudioEvent::Note {
337                midi,
338                start_time,
339                duration,
340                velocity,
341                ..
342            } => {
343                midi_notes.push(MidiNote {
344                    note: *midi,
345                    start: *start_time,
346                    duration: *duration,
347                    velocity: *velocity,
348                });
349            }
350            AudioEvent::Chord {
351                midis,
352                start_time,
353                duration,
354                velocity,
355                ..
356            } => {
357                // Expand chord into individual notes
358                for &note in midis {
359                    midi_notes.push(MidiNote {
360                        note,
361                        start: *start_time,
362                        duration: *duration,
363                        velocity: *velocity,
364                    });
365                }
366            }
367            AudioEvent::Sample { .. } => {
368                // Samples are not exported to MIDI
369            }
370            AudioEvent::Stop { .. } => {
371                // Stop events are control events, not exported to MIDI
372            }
373        }
374    }
375
376    // Sort by start time
377    midi_notes.sort_by(|a, b| a.start.partial_cmp(&b.start).unwrap());
378
379    // Convert events to MIDI messages with proper delta timing
380    let mut midi_messages: Vec<MidiEventTimed> = Vec::new();
381
382    for note in &midi_notes {
383        let start_ticks = time_to_ticks(note.start, bpm, ticks_per_beat);
384        let end_ticks = time_to_ticks(note.start + note.duration, bpm, ticks_per_beat);
385
386        // Note On
387        midi_messages.push(MidiEventTimed {
388            ticks: start_ticks,
389            message: MidiMessage::NoteOn {
390                key: note.note.into(),
391                vel: ((note.velocity * 127.0) as u8).into(),
392            },
393        });
394
395        // Note Off
396        midi_messages.push(MidiEventTimed {
397            ticks: end_ticks,
398            message: MidiMessage::NoteOff {
399                key: note.note.into(),
400                vel: 0.into(),
401            },
402        });
403    }
404
405    // Sort all messages by time
406    midi_messages.sort_by_key(|msg| msg.ticks);
407
408    // Convert to delta times and create TrackEvents
409    let mut last_ticks = 0u32;
410
411    for msg in midi_messages {
412        let delta = msg.ticks.saturating_sub(last_ticks);
413
414        track_events.push(TrackEvent {
415            delta: delta.into(),
416            kind: TrackEventKind::Midi {
417                channel: 0.into(),
418                message: msg.message,
419            },
420        });
421
422        last_ticks = msg.ticks;
423    }
424
425    // End of track marker
426    track_events.push(TrackEvent {
427        delta: 0.into(),
428        kind: TrackEventKind::Meta(MetaMessage::EndOfTrack),
429    });
430
431    // Create SMF and write to file
432    let track = Track::from(track_events);
433    let mut smf = Smf::new(header);
434    smf.tracks.push(track);
435
436    // Write to file directly (midly 0.5 API)
437    smf.save(output_path)
438        .map_err(|e| anyhow!("Failed to write MIDI file {}: {}", output_path.display(), e))?;
439
440    println!(
441        "✅ MIDI exported: {} ({} events in, {} notes written)",
442        output_path.display(),
443        events.len(),
444        midi_notes.len()
445    );
446    Ok(())
447}
448
449#[cfg(not(feature = "cli"))]
450pub fn export_midi_file(_events: &[AudioEvent], _output_path: &Path, _bpm: f32) -> Result<()> {
451    Err(anyhow!("MIDI export not available without 'cli' feature"))
452}
453
454// Helper structures for MIDI export
455#[derive(Debug, Clone)]
456struct MidiNote {
457    note: u8,
458    start: f32,
459    duration: f32,
460    velocity: f32,
461}
462
463#[derive(Debug, Clone)]
464struct MidiEventTimed {
465    ticks: u32,
466    message: MidiMessage,
467}
468
469/// Convert time in seconds to MIDI ticks
470fn time_to_ticks(time_seconds: f32, bpm: f32, ticks_per_beat: u16) -> u32 {
471    let beats = time_seconds * (bpm / 60.0);
472    (beats * ticks_per_beat as f32) as u32
473}