devalang_wasm/engine/audio/
midi.rs1use crate::engine::audio::events::AudioEvent;
2use crate::language::syntax::ast::Value;
3use 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#[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 let mut midi_map = HashMap::new();
31
32 let mut bpm = 120.0f32;
34 let mut tempo_us_per_quarter: u32 = 500_000; let mut notes: Vec<Value> = Vec::new();
36
37 let ticks_per_beat: u32 = match smf.header.timing {
39 Timing::Metrical(t) => t.as_int() as u32,
40 _ => 480u32,
41 };
42
43 let mut active: std::collections::HashMap<(usize, u8, u8), Vec<usize>> =
45 std::collections::HashMap::new();
46
47 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 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 let beats = time_ms * (bpm as f32) / 60000.0; 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 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 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 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 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 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 (_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 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 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
173pub 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 let ticks_per_beat = 480; let header = Header::new(Format::SingleTrack, Timing::Metrical(ticks_per_beat.into()));
186
187 let mut track_events = Vec::new();
189
190 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 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 for ¬e 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 }
236 }
237 }
238
239 midi_notes.sort_by(|a, b| a.start.partial_cmp(&b.start).unwrap());
241
242 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 midi_messages.push(MidiEventTimed {
251 ticks: start_ticks,
252 message: MidiMessage::NoteOn {
253 key: note.note.into(),
254 vel: (note.velocity as u8).into(),
255 },
256 });
257
258 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 midi_messages.sort_by_key(|msg| msg.ticks);
270
271 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 track_events.push(TrackEvent {
290 delta: 0.into(),
291 kind: TrackEventKind::Meta(MetaMessage::EndOfTrack),
292 });
293
294 let track = Track::from(track_events);
296 let mut smf = Smf::new(header);
297 smf.tracks.push(track);
298
299 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#[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 let ticks_per_beat = 480; let header = Header::new(Format::SingleTrack, Timing::Metrical(ticks_per_beat.into()));
317
318 let mut track_events = Vec::new();
320
321 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 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 for ¬e 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 }
367 }
368 }
369
370 midi_notes.sort_by(|a, b| a.start.partial_cmp(&b.start).unwrap());
372
373 let mut midi_messages: Vec<MidiEventTimed> = Vec::new();
375
376 for note in &midi_notes {
377 let start_ticks = time_to_ticks(note.start, bpm, ticks_per_beat);
378 let end_ticks = time_to_ticks(note.start + note.duration, bpm, ticks_per_beat);
379
380 midi_messages.push(MidiEventTimed {
382 ticks: start_ticks,
383 message: MidiMessage::NoteOn {
384 key: note.note.into(),
385 vel: (note.velocity as u8).into(),
386 },
387 });
388
389 midi_messages.push(MidiEventTimed {
391 ticks: end_ticks,
392 message: MidiMessage::NoteOff {
393 key: note.note.into(),
394 vel: 0.into(),
395 },
396 });
397 }
398
399 midi_messages.sort_by_key(|msg| msg.ticks);
401
402 let mut last_ticks = 0u32;
404
405 for msg in midi_messages {
406 let delta = msg.ticks.saturating_sub(last_ticks);
407
408 track_events.push(TrackEvent {
409 delta: delta.into(),
410 kind: TrackEventKind::Midi {
411 channel: 0.into(),
412 message: msg.message,
413 },
414 });
415
416 last_ticks = msg.ticks;
417 }
418
419 track_events.push(TrackEvent {
421 delta: 0.into(),
422 kind: TrackEventKind::Meta(MetaMessage::EndOfTrack),
423 });
424
425 let track = Track::from(track_events);
427 let mut smf = Smf::new(header);
428 smf.tracks.push(track);
429
430 smf.save(output_path)
432 .map_err(|e| anyhow!("Failed to write MIDI file {}: {}", output_path.display(), e))?;
433
434 println!(
435 "✅ MIDI exported: {} ({} events)",
436 output_path.display(),
437 events.len()
438 );
439 Ok(())
440}
441
442#[cfg(not(feature = "cli"))]
443pub fn export_midi_file(_events: &[AudioEvent], _output_path: &Path, _bpm: f32) -> Result<()> {
444 Err(anyhow!("MIDI export not available without 'cli' feature"))
445}
446
447#[derive(Debug, Clone)]
449struct MidiNote {
450 note: u8,
451 start: f32,
452 duration: f32,
453 velocity: f32,
454}
455
456#[derive(Debug, Clone)]
457struct MidiEventTimed {
458 ticks: u32,
459 message: MidiMessage,
460}
461
462fn time_to_ticks(time_seconds: f32, bpm: f32, ticks_per_beat: u16) -> u32 {
464 let beats = time_seconds * (bpm / 60.0);
465 (beats * ticks_per_beat as f32) as u32
466}