1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * Media source implementation that reads MIDI files.
5 * This file is part of monster
7 * Copyright (C) 2006,2007 Nedko Arnaudov <nedko@arnaudov.name>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 *****************************************************************************/
30 #define DISABLE_DEBUG_OUTPUT
32 #include "media_source.h"
33 #include "media_source_midi.h"
35 #include "midi_event.h"
38 * A MIDI event after being parsed/loaded from the file.
39 * There could be made a case for using snd_seq_event_t instead.
43 struct event
* next_ptr
; /* linked list */
45 struct midi_event event
;
46 unsigned char sysex
[0];
51 struct event
* first_event_ptr
; /* list of all events in this track */
52 int end_tick
; /* length of this track */
54 struct event
* current_event_ptr
;
57 struct media_source_midi
59 struct media_source
virtual;
62 int file_offset
; /* current offset in input file */
64 struct track
* tracks
;
67 struct event
* ppqn_event_ptr
;
68 struct event
* tempo_event_ptr
;
69 struct event
* event_ptr
;
73 static void cleanup_file_data(struct media_source_midi
* media_source_ptr
)
76 struct event
* event_ptr
;
78 if (media_source_ptr
->tracks
!= NULL
)
80 for (i
= 0; i
< media_source_ptr
->num_tracks
; ++i
)
82 event_ptr
= media_source_ptr
->tracks
[i
].first_event_ptr
;
83 while (event_ptr
!= NULL
)
85 struct event
* next_ptr
= event_ptr
->next_ptr
;
91 media_source_ptr
->num_tracks
= 0;
93 free(media_source_ptr
->tracks
);
96 if (media_source_ptr
->ppqn_event_ptr
!= NULL
)
98 free(media_source_ptr
->ppqn_event_ptr
);
101 if (media_source_ptr
->tempo_event_ptr
!= NULL
)
103 free(media_source_ptr
->tempo_event_ptr
);
106 media_source_ptr
->tracks
= NULL
;
109 #define media_source_ptr ((struct media_source_midi *)virtual_ptr)
111 void media_source_midi_destroy(struct media_source
* virtual_ptr
)
113 DEBUG_OUT("media_source_midi_destroy");
114 cleanup_file_data(media_source_ptr
);
115 fclose(media_source_ptr
->file_handle
);
116 free(media_source_ptr
);
119 int media_source_midi_format_query(struct media_source
* virtual_ptr
, unsigned int * format_ptr
)
121 DEBUG_OUT("media_source_midi_format_query");
122 *format_ptr
= MEDIA_FORMAT_MIDI_EVENT
;
126 int media_source_midi_get_frame_data(struct media_source
* virtual_ptr
, void ** buffer_ptr_ptr
)
128 DEBUG_OUT("media_source_midi_get_frame_data");
130 if (media_source_ptr
->counter
== 0)
132 if (media_source_ptr
->ppqn_event_ptr
== NULL
)
134 ERROR_OUT("No PPQN event!");
138 *buffer_ptr_ptr
= &media_source_ptr
->ppqn_event_ptr
->event
;
139 DEBUG_OUT("Returning the PPQN event");
143 if (media_source_ptr
->counter
== 1)
145 if (media_source_ptr
->tempo_event_ptr
== NULL
)
147 ERROR_OUT("No tempo event!");
151 *buffer_ptr_ptr
= &media_source_ptr
->tempo_event_ptr
->event
;
152 DEBUG_OUT("Returning the tempo event");
156 if (media_source_ptr
->event_ptr
== NULL
)
158 *buffer_ptr_ptr
= NULL
;
162 *buffer_ptr_ptr
= &media_source_ptr
->event_ptr
->event
;
168 media_source_midi_next_frame(struct media_source
* virtual_ptr
)
170 struct event
* event_ptr
;
171 struct track
* event_track_ptr
;
175 DEBUG_OUT("media_source_midi_next_frame");
177 media_source_ptr
->counter
++;
179 if (media_source_ptr
->counter
< 2)
185 event_track_ptr
= NULL
;
186 min_tick
= media_source_ptr
->max_tick
+ 1;
188 //DEBUG_OUT("Search next event, %d tracks, max_tick = %d, min_tick = %d", media_source_ptr->num_tracks, media_source_ptr->max_tick, min_tick);
190 /* search next event */
191 for (i
= 0; i
< media_source_ptr
->num_tracks
; i
++)
193 struct track
* track
= media_source_ptr
->tracks
+ i
;
194 struct event
* e2
= track
->current_event_ptr
;
196 //DEBUG_OUT("Event %08X, tick = %u", (unsigned int)e2, e2 ? e2->event.tick : 0);
197 if (e2
&& e2
->event
.tick
< min_tick
)
199 min_tick
= e2
->event
.tick
;
201 event_track_ptr
= track
;
205 if (event_ptr
!= NULL
) /* if end of song NOT reached */
207 /* advance pointer to next event */
208 event_track_ptr
->current_event_ptr
= event_ptr
->next_ptr
;
212 DEBUG_OUT("End of song");
215 media_source_ptr
->event_ptr
= event_ptr
;
218 #undef media_source_ptr
220 static int read_byte(struct media_source_midi
* media_source_ptr
)
222 media_source_ptr
->file_offset
++;
223 return getc(media_source_ptr
->file_handle
);
226 /* reads a little-endian 32-bit integer */
227 static int read_32_le(struct media_source_midi
* media_source_ptr
)
230 value
= read_byte(media_source_ptr
);
231 value
|= read_byte(media_source_ptr
) << 8;
232 value
|= read_byte(media_source_ptr
) << 16;
233 value
|= read_byte(media_source_ptr
) << 24;
234 return !feof(media_source_ptr
->file_handle
) ? value
: -1;
237 /* reads a 4-character identifier */
238 static int read_id(struct media_source_midi
* media_source_ptr
)
242 id
= read_32_le(media_source_ptr
);
245 "Chunk \"%c%c%c%c\"",
247 (char)((id
>> 8) & 0xFF),
248 (char)((id
>> 16) & 0xFF),
249 (char)((id
>> 24) & 0xFF));
254 #define MAKE_ID(c1, c2, c3, c4) ((c1) | ((c2) << 8) | ((c3) << 16) | ((c4) << 24))
256 /* reads a fixed-size big-endian number */
257 static int read_int(struct media_source_midi
* media_source_ptr
, int bytes
)
263 c
= read_byte(media_source_ptr
);
266 value
= (value
<< 8) | c
;
273 /* reads a variable-length number */
274 static int read_var(struct media_source_midi
* media_source_ptr
)
278 c
= read_byte(media_source_ptr
);
282 c
= read_byte(media_source_ptr
);
283 value
= (value
<< 7) | (c
& 0x7f);
286 c
= read_byte(media_source_ptr
);
287 value
= (value
<< 7) | (c
& 0x7f);
290 c
= read_byte(media_source_ptr
);
291 value
= (value
<< 7) | c
;
297 return !feof(media_source_ptr
->file_handle
) ? value
: -1;
300 /* allocates a new event */
303 new_event(struct track
* track_ptr
, int sysex_length
, struct event
** event_ptr_ptr
)
305 struct event
* event_ptr
;
307 event_ptr
= malloc(sizeof(struct event
) + sysex_length
);
308 if (event_ptr
== NULL
)
310 ERROR_OUT("malloc failed.");
314 event_ptr
->next_ptr
= NULL
;
316 /* append at the end of the track's linked list */
317 if (track_ptr
->current_event_ptr
!= NULL
)
319 track_ptr
->current_event_ptr
->next_ptr
= event_ptr
;
323 track_ptr
->first_event_ptr
= event_ptr
;
326 track_ptr
->current_event_ptr
= event_ptr
;
328 *event_ptr_ptr
= event_ptr
;
333 static void skip(struct media_source_midi
* media_source_ptr
, int bytes
)
336 read_byte(media_source_ptr
), --bytes
;
339 /* reads one complete track from the file */
340 static int read_track(struct media_source_midi
* media_source_ptr
, struct track
*track
, int track_end
)
344 unsigned char last_cmd
= 0;
346 /* the current file position is after the track ID and length */
347 while (media_source_ptr
->file_offset
< track_end
)
350 struct event
* event_ptr
;
351 int delta_ticks
, len
, c
;
353 delta_ticks
= read_var(media_source_ptr
);
358 c
= read_byte(media_source_ptr
);
372 ungetc(c
, media_source_ptr
->file_handle
);
373 media_source_ptr
->file_offset
--;
381 /* channel messages with 2 parameter bytes */
382 case 0x8: /* Note Off */
383 ret
= new_event(track
, 0, &event_ptr
);
389 event_ptr
->event
.type
= MIDI_EVENT_NOTE_OFF
;
390 event_ptr
->event
.tick
= tick
;
391 event_ptr
->event
.data
.note_off
.channel
= cmd
& 0x0f;
392 event_ptr
->event
.data
.note_off
.note
= read_byte(media_source_ptr
) & 0x7f;
393 event_ptr
->event
.data
.note_off
.velocity
= read_byte(media_source_ptr
) & 0x7f;
395 case 0x9: /* Note On */
396 ret
= new_event(track
, 0, &event_ptr
);
402 event_ptr
->event
.type
= MIDI_EVENT_NOTE_ON
;
403 event_ptr
->event
.tick
= tick
;
404 event_ptr
->event
.data
.note_on
.channel
= cmd
& 0x0f;
405 event_ptr
->event
.data
.note_on
.note
= read_byte(media_source_ptr
) & 0x7f;
406 event_ptr
->event
.data
.note_on
.velocity
= read_byte(media_source_ptr
) & 0x7f;
408 case 0xa: /* Polyphonic Key Pressure (Aftertouch) */
409 ret
= new_event(track
, 0, &event_ptr
);
415 event_ptr
->event
.type
= MIDI_EVENT_KEYPRESS
;
416 event_ptr
->event
.tick
= tick
;
417 event_ptr
->event
.data
.keypress
.channel
= cmd
& 0x0f;
418 event_ptr
->event
.data
.keypress
.note
= read_byte(media_source_ptr
) & 0x7f;
419 event_ptr
->event
.data
.keypress
.velocity
= read_byte(media_source_ptr
) & 0x7f;
421 case 0xb: /* Control Change */
422 ret
= new_event(track
, 0, &event_ptr
);
428 event_ptr
->event
.type
= MIDI_EVENT_CC
;
429 event_ptr
->event
.tick
= tick
;
430 event_ptr
->event
.data
.cc
.channel
= cmd
& 0x0f;
431 event_ptr
->event
.data
.cc
.controller
= read_byte(media_source_ptr
) & 0x7f;
432 event_ptr
->event
.data
.cc
.value
= read_byte(media_source_ptr
) & 0x7f;
434 case 0xe: /* Pitch Wheel Change */
435 ret
= new_event(track
, 0, &event_ptr
);
441 event_ptr
->event
.type
= MIDI_EVENT_CC
;
442 event_ptr
->event
.tick
= tick
;
443 event_ptr
->event
.data
.pitch
.channel
= cmd
& 0x0f;
444 event_ptr
->event
.data
.pitch
.value
= (read_byte(media_source_ptr
) & 0x7f) | ((read_byte(media_source_ptr
) & 0x7f) << 7);
447 /* channel messages with 1 parameter byte */
448 case 0xc: /* Program Change */
449 ret
= new_event(track
, 0, &event_ptr
);
455 event_ptr
->event
.type
= MIDI_EVENT_PGMCHANGE
;
456 event_ptr
->event
.tick
= tick
;
457 event_ptr
->event
.data
.pgmchange
.channel
= cmd
& 0x0f;
458 event_ptr
->event
.data
.pgmchange
.program
= read_byte(media_source_ptr
) & 0x7f;
460 case 0xd: /* Channel Pressure (After-touch) */
461 ret
= new_event(track
, 0, &event_ptr
);
467 event_ptr
->event
.type
= MIDI_EVENT_CHANPRESS
;
468 event_ptr
->event
.tick
= tick
;
469 event_ptr
->event
.data
.chanpress
.channel
= cmd
& 0x0f;
470 event_ptr
->event
.data
.chanpress
.value
= read_byte(media_source_ptr
) & 0x7f;
476 case 0xf0: /* sysex */
477 case 0xf7: /* continued sysex, or escaped commands */
478 len
= read_var(media_source_ptr
);
486 ret
= new_event(track
, len
, &event_ptr
);
492 event_ptr
->event
.type
= MIDI_EVENT_SYSEX
;
493 event_ptr
->event
.tick
= tick
;
494 event_ptr
->event
.data
.sysex
.size
= len
;
495 event_ptr
->event
.data
.sysex
.data
= event_ptr
->sysex
;
498 event_ptr
->sysex
[0] = 0xf0;
506 event_ptr
->sysex
[c
] = read_byte(media_source_ptr
);
509 case 0xff: /* meta event */
510 c
= read_byte(media_source_ptr
);
511 len
= read_var(media_source_ptr
);
517 case 0x21: /* port number */
522 port
= read_byte(media_source_ptr
);
523 DEBUG_OUT("port %u", (unsigned int)port
);
524 skip(media_source_ptr
, len
- 1);
528 case 0x2f: /* end of track */
529 track
->end_tick
= tick
;
530 skip(media_source_ptr
, track_end
- media_source_ptr
->file_offset
);
533 case 0x51: /* tempo */
536 if (media_source_ptr
->smpte_timing
)
538 /* SMPTE timing doesn't change */
539 skip(media_source_ptr
, len
);
543 ret
= new_event(track
, 0, &event_ptr
);
549 event_ptr
->event
.type
= MIDI_EVENT_TEMPO
;
550 event_ptr
->event
.tick
= tick
;
551 event_ptr
->event
.data
.tempo
.value
= read_byte(media_source_ptr
) << 16;
552 event_ptr
->event
.data
.tempo
.value
|= read_byte(media_source_ptr
) << 8;
553 event_ptr
->event
.data
.tempo
.value
|= read_byte(media_source_ptr
);
554 skip(media_source_ptr
, len
- 3);
558 default: /* ignore all other meta events */
559 skip(media_source_ptr
, len
);
564 default: /* invalid Fx command */
569 default: /* cannot happen */
574 ERROR_OUT("invalid MIDI data (offset %#x)", media_source_ptr
->file_offset
);
580 struct media_source_midi
* media_source_ptr
,
583 media_source_ptr
->ppqn_event_ptr
= malloc(sizeof(struct event
));
584 if (media_source_ptr
->ppqn_event_ptr
== NULL
)
586 ERROR_OUT("malloc failed.");
590 media_source_ptr
->ppqn_event_ptr
->event
.type
= MIDI_EVENT_PPQN
;
591 media_source_ptr
->ppqn_event_ptr
->event
.tick
= 0;
592 media_source_ptr
->ppqn_event_ptr
->event
.data
.ppqn
.value
= ppqn
;
599 struct media_source_midi
* media_source_ptr
,
602 media_source_ptr
->tempo_event_ptr
= malloc(sizeof(struct event
));
603 if (media_source_ptr
->tempo_event_ptr
== NULL
)
605 ERROR_OUT("malloc failed.");
610 media_source_ptr
->tempo_event_ptr
->event
.type
= MIDI_EVENT_TEMPO
;
611 media_source_ptr
->tempo_event_ptr
->event
.tick
= 0;
612 media_source_ptr
->tempo_event_ptr
->event
.data
.tempo
.value
= tempo
;
617 /* reads an entire MIDI file */
620 struct media_source_midi
* media_source_ptr
)
622 int header_len
, type
, time_division
, i
;
625 /* the curren position is immediately after the "MThd" id */
626 header_len
= read_int(media_source_ptr
, 4);
629 ERROR_OUT("MIDI parsing error (MThd check)");
633 type
= read_int(media_source_ptr
, 2);
635 DEBUG_OUT("Type %d", type
);
637 if (type
!= 0 && type
!= 1)
639 ERROR_OUT("MIDI type %d format is not supported", type
);
643 media_source_ptr
->num_tracks
= read_int(media_source_ptr
, 2);
644 if (media_source_ptr
->num_tracks
< 1 || media_source_ptr
->num_tracks
> 1000)
646 ERROR_OUT("invalid number of tracks (%d)", media_source_ptr
->num_tracks
);
647 media_source_ptr
->num_tracks
= 0;
651 DEBUG_OUT("Tracks count: %d", media_source_ptr
->num_tracks
);
653 media_source_ptr
->tracks
= calloc(media_source_ptr
->num_tracks
, sizeof(struct track
));
654 if (!media_source_ptr
->tracks
)
656 ERROR_OUT("out of memory");
657 media_source_ptr
->num_tracks
= 0;
661 time_division
= read_int(media_source_ptr
, 2);
662 if (time_division
< 0)
664 ERROR_OUT("MIDI parsing error (time division is negative)");
668 /* interpret and set tempo */
669 media_source_ptr
->smpte_timing
= !!(time_division
& 0x8000);
670 if (!media_source_ptr
->smpte_timing
)
672 DEBUG_OUT("PPQN: %d", time_division
);
673 DEBUG_OUT("Settings default tempo, 120 bpm");
674 /* time_division is ticks per quarter */
676 ret
= set_ppqn_event(media_source_ptr
, time_division
);
682 ret
= set_tempo_event(media_source_ptr
, 500000); /* default: 120 bpm */
690 /* upper byte is negative frames per second */
691 i
= 0x80 - ((time_division
>> 8) & 0x7f);
692 /* lower byte is ticks per frame */
693 time_division
&= 0xff;
694 /* now pretend that we have quarter-note based timing */
698 ret
= set_tempo_event(media_source_ptr
, 12 * time_division
);
704 ret
= set_ppqn_event(media_source_ptr
, 500000);
711 ret
= set_tempo_event(media_source_ptr
, 10 * time_division
);
717 ret
= set_ppqn_event(media_source_ptr
, 400000);
723 case 29: /* 30 drop-frame */
724 ret
= set_tempo_event(media_source_ptr
, 2997 * time_division
);
730 ret
= set_ppqn_event(media_source_ptr
, 100000000);
737 ret
= set_tempo_event(media_source_ptr
, 15 * time_division
);
743 ret
= set_ppqn_event(media_source_ptr
, 500000);
750 ERROR_OUT("invalid number of SMPTE frames per second (%d)", i
);
756 for (i
= 0; i
< media_source_ptr
->num_tracks
; ++i
)
760 /* search for MTrk chunk */
763 int id
= read_id(media_source_ptr
);
764 len
= read_int(media_source_ptr
, 4);
765 if (feof(media_source_ptr
->file_handle
))
767 ERROR_OUT("unexpected end of file");
770 if (len
< 0 || len
>= 0x10000000)
772 ERROR_OUT("invalid chunk length %d", len
);
775 if (id
== MAKE_ID('M', 'T', 'r', 'k'))
777 skip(media_source_ptr
, len
);
780 ret
= read_track(media_source_ptr
, &media_source_ptr
->tracks
[i
], media_source_ptr
->file_offset
+ len
);
789 media_source_midi_load(struct media_source_midi
* media_source_ptr
)
794 ret
= read_smf(media_source_ptr
);
800 /* calculate length of the entire file */
801 DEBUG_OUT("Finding max_tick....");
802 media_source_ptr
->max_tick
= -1;
803 for (i
= 0; i
< media_source_ptr
->num_tracks
; ++i
)
805 DEBUG_OUT("track i end_tick = %d", media_source_ptr
->tracks
[i
].end_tick
);
806 if (media_source_ptr
->tracks
[i
].end_tick
> media_source_ptr
->max_tick
)
808 media_source_ptr
->max_tick
= media_source_ptr
->tracks
[i
].end_tick
;
811 DEBUG_OUT("max_tick = %d", media_source_ptr
->max_tick
);
813 /* initialize current position in each track */
814 for (i
= 0; i
< media_source_ptr
->num_tracks
; ++i
)
816 media_source_ptr
->tracks
[i
].current_event_ptr
= media_source_ptr
->tracks
[i
].first_event_ptr
;
824 const char * filename
,
825 struct media_source
** media_source_ptr_ptr
)
827 struct media_source_midi
* media_source_ptr
;
830 DEBUG_OUT("media_source_midi");
832 media_source_ptr
= (struct media_source_midi
*)malloc(sizeof(struct media_source_midi
));
833 if (media_source_ptr
== NULL
)
835 ERROR_OUT("malloc() failed.");
840 media_source_ptr
->ppqn_event_ptr
= NULL
;
841 media_source_ptr
->tempo_event_ptr
= NULL
;
842 media_source_ptr
->tracks
= NULL
;
844 media_source_ptr
->file_handle
= fopen(filename
, "rb");
845 if (!media_source_ptr
->file_handle
)
847 ERROR_OUT("Cannot open %s - %s", filename
, strerror(errno
));
852 media_source_ptr
->file_offset
= 0;
854 if (read_id(media_source_ptr
) != MAKE_ID('M', 'T', 'h', 'd'))
856 ERROR_OUT("%s is not a Standard MIDI File", filename
);
861 media_source_ptr
->virtual.destroy
= media_source_midi_destroy
;
862 media_source_ptr
->virtual.format_query
= media_source_midi_format_query
;
863 media_source_ptr
->virtual.get_frame_data
= media_source_midi_get_frame_data
;
864 media_source_ptr
->virtual.next_frame
= media_source_midi_next_frame
;
866 ret
= media_source_midi_load(media_source_ptr
);
869 ERROR_OUT("media_source_midi_load() failed.");
873 media_source_ptr
->counter
= 0;
875 *media_source_ptr_ptr
= &media_source_ptr
->virtual;
881 cleanup_file_data(media_source_ptr
);