3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public License
5 * as published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 /* this file originally taken from FluidSynth - A Software Synthesizer
20 * Copyright (C) 2003 Peter Hanappe and others.
22 * \note fixed some track names causing segfault
25 #include "aubio_priv.h"
27 #include "midi_event.h"
28 #include "midi_track.h"
29 #include "midi_player.h"
30 #include "midi_file.h"
33 /** aubio_midi_file */
34 struct _aubio_midi_file_t{
42 unsigned int smpte_fps;
43 unsigned int smpte_res;
44 unsigned int division; /* If uses_SMPTE == 0 then division is
45 ticks per beat (quarter-note) */
46 double tempo; /* Beats per second (SI rules =) */
53 /***************************************************************
58 /** new_aubio_midi_file */
59 aubio_midi_file_t * new_aubio_midi_file(char* filename)
61 aubio_midi_file_t* mf;
63 mf = AUBIO_NEW(aubio_midi_file_t);
65 AUBIO_ERR( "Out of memory");
68 AUBIO_MEMSET(mf, 0, sizeof(aubio_midi_file_t));
71 mf->running_status = -1;
72 mf->fp = AUBIO_FOPEN(filename, "rb");
75 AUBIO_ERR( "Couldn't open the MIDI file !\n");
80 if (aubio_midi_file_read_mthd(mf) != AUBIO_OK) {
87 /** del_aubio_midi_file */
88 void del_aubio_midi_file(aubio_midi_file_t* mf)
100 /** aubio_midi_file_getc */
101 int aubio_midi_file_getc(aubio_midi_file_t* mf)
109 n = AUBIO_FREAD(&c, 1, 1, mf->fp);
115 /** aubio_midi_file_push */
116 int aubio_midi_file_push(aubio_midi_file_t* mf, int c)
122 /** aubio_midi_file_read */
123 int aubio_midi_file_read(aubio_midi_file_t* mf, void* buf, int len)
125 int num = AUBIO_FREAD(buf, 1, len, mf->fp);
129 AUBIO_DBG( "Coulnd't read the requested number of bytes");
132 return (num != len)? AUBIO_FAIL : AUBIO_OK;
135 /** aubio_midi_file_skip */
136 int aubio_midi_file_skip(aubio_midi_file_t* mf, int skip)
138 int err = AUBIO_FSEEK(mf->fp, skip, SEEK_CUR);
140 AUBIO_ERR( "FAIL to seek position in file");
146 /** aubio_midi_file_read_mthd */
147 int aubio_midi_file_read_mthd(aubio_midi_file_t* mf)
150 if (aubio_midi_file_read(mf, mthd, 14) != AUBIO_OK) {
153 if ((AUBIO_STRNCMP(mthd, "MThd", 4) != 0) || (mthd[7] != 6) || (mthd[9] > 2)) {
154 AUBIO_ERR( "Doesn't look like a MIDI file: invalid MThd header");
158 mf->ntracks = (unsigned) mthd[11];
159 mf->ntracks += (unsigned int) (mthd[10]) << 16;
160 /** \bug: smpte timing not yet implemented */
161 if((int)(mthd[12]) < 0){
162 /*if((mthd[12]) < 0){*/
164 mf->smpte_fps = -mthd[12];
165 mf->smpte_res = (unsigned) mthd[13];
166 AUBIO_ERR( "File uses SMPTE timing -- Not implemented yet");
170 mf->division = (mthd[12] << 8) | (mthd[13] & 0xff);
175 /** aubio_midi_file_load_tracks */
176 int aubio_midi_file_load_tracks(aubio_midi_file_t* mf, aubio_midi_player_t* player)
179 for (i = 0; i < mf->ntracks; i++) {
180 if (aubio_midi_file_read_track(mf, player, i) != AUBIO_OK) {
187 /** aubio_midi_file_read_tracklen */
188 int aubio_midi_file_read_tracklen(aubio_midi_file_t* mf)
190 unsigned char length[5];
191 if (aubio_midi_file_read(mf, length, 4) != AUBIO_OK) {
194 mf->tracklen = aubio_getlength(length);
200 /** aubio_midi_file_eot */
201 int aubio_midi_file_eot(aubio_midi_file_t* mf)
204 if (mf->trackpos > mf->tracklen) {
205 printf("track overrun: %d > %d\n", mf->trackpos, mf->tracklen);
208 return mf->eot || (mf->trackpos >= mf->tracklen);
211 /** aubio_midi_file_read_track */
212 int aubio_midi_file_read_track(aubio_midi_file_t* mf, aubio_midi_player_t* player, int num)
214 aubio_track_t* track;
215 unsigned char id[5], length[5];
219 AUBIO_DBG("Loading track %d\n",num);
220 if (aubio_midi_file_read(mf, id, 4) != AUBIO_OK) {
221 AUBIO_DBG("Failed loading track %d\n",num);
227 while (!found_track){
229 if (aubio_isasciistring((char*) id) == 0) {
230 AUBIO_ERR( "An non-ascii track header found, currupt file");
233 } else if (strcmp((char*) id, "MTrk") == 0) {
237 if (aubio_midi_file_read_tracklen(mf) != AUBIO_OK) {
241 track = new_aubio_track(num);
243 AUBIO_ERR( "Out of memory");
247 while (!aubio_midi_file_eot(mf)) {
248 if (aubio_midi_file_read_event(mf, track) != AUBIO_OK) {
253 aubio_midi_player_add_track(player, track);
256 if (aubio_midi_file_read(mf, length, 4) != AUBIO_OK) {
259 skip = aubio_getlength(length);
260 /* fseek(mf->fp, skip, SEEK_CUR); */
261 if (aubio_midi_file_skip(mf, skip) != AUBIO_OK) {
268 AUBIO_ERR( "Unexpected end of file");
271 AUBIO_DBG("Loaded track %d\n",num);
275 /** aubio_midi_file_read_varlen */
276 int aubio_midi_file_read_varlen(aubio_midi_file_t* mf)
283 AUBIO_ERR( "Invalid variable length number");
286 c = aubio_midi_file_getc(mf);
288 AUBIO_ERR( "Unexpected end of file");
292 mf->varlen |= (int) (c & 0x7F);
302 /** aubio_midi_file_read_event */
303 int aubio_midi_file_read_event(aubio_midi_file_t* mf, aubio_track_t* track)
309 unsigned char* metadata = NULL;
310 unsigned char* dyn_buf = NULL;
311 unsigned char static_buf[256];
312 int nominator, denominator, clocks, notes, sf, mi;
313 aubio_midi_event_t* evt;
319 /* read the delta-time of the event */
320 if (aubio_midi_file_read_varlen(mf) != AUBIO_OK) {
325 /* read the status byte */
326 status = aubio_midi_file_getc(mf);
328 AUBIO_ERR( "Unexpected end of file");
332 /* not a valid status byte: use the running status instead */
333 if ((status & 0x80) == 0) {
334 if ((mf->running_status & 0x80) == 0) {
335 AUBIO_ERR( "Undefined status and invalid running status");
338 aubio_midi_file_push(mf, status);
339 status = mf->running_status;
342 /* check what message we have */
344 mf->running_status = status;
346 if ((status == MIDI_SYSEX) || (status == MIDI_EOX)) { /* system exclusif */
347 /** \bug Sysex messages are not handled yet */
348 /* read the length of the message */
349 if (aubio_midi_file_read_varlen(mf) != AUBIO_OK) {
353 if (mf->varlen < 255) {
354 metadata = &static_buf[0];
356 AUBIO_DBG( "%s: %d: alloc metadata, len = %d", __FILE__, __LINE__, mf->varlen);
357 dyn_buf = AUBIO_MALLOC(mf->varlen + 1);
358 if (dyn_buf == NULL) {
359 //AUBIO_LOG(AUBIO_PANIC, "Out of memory");
360 AUBIO_ERR("Out of memory");
366 /* read the data of the message */
369 if (aubio_midi_file_read(mf, metadata, mf->varlen) != AUBIO_OK) {
377 AUBIO_DBG( "%s: %d: free metadata", __FILE__, __LINE__);
384 } else if (status == MIDI_META_EVENT) { /* meta events */
386 int result = AUBIO_OK;
388 /* get the type of the meta message */
389 type = aubio_midi_file_getc(mf);
391 AUBIO_ERR( "Unexpected end of file");
395 /* get the length of the data part */
396 if (aubio_midi_file_read_varlen(mf) != AUBIO_OK) {
402 if (mf->varlen < 255) {
403 metadata = &static_buf[0];
405 AUBIO_DBG( "%s: %d: alloc metadata, len = %d", __FILE__, __LINE__, mf->varlen);
406 dyn_buf = AUBIO_MALLOC(mf->varlen + 1);
407 if (dyn_buf == NULL) {
408 AUBIO_ERR("Out of memory");
415 if (aubio_midi_file_read(mf, metadata, mf->varlen) != AUBIO_OK) {
423 /* handle meta data */
427 metadata[mf->varlen] = 0;
430 case MIDI_TRACK_NAME:
431 if (metadata != NULL) /* avoids crashes on empty tracks */
432 metadata[mf->varlen] = 0;
433 aubio_track_set_name(track, (char*) metadata);
437 metadata[mf->varlen] = 0;
447 break; /* don't care much for text events */
450 if (mf->varlen != 0) {
451 AUBIO_ERR("Invalid length for EndOfTrack event");
459 if (mf->varlen != 3) {
460 AUBIO_ERR("Invalid length for SetTempo meta event");
464 tempo = (metadata[0] << 16) + (metadata[1] << 8) + metadata[2];
465 evt = new_aubio_midi_event();
467 AUBIO_ERR( "Out of memory");
472 evt->type = MIDI_SET_TEMPO;
476 aubio_track_add_event(track, evt);
479 case MIDI_SMPTE_OFFSET:
480 if (mf->varlen != 5) {
481 AUBIO_ERR("Invalid length for SMPTE Offset meta event");
485 break; /* we don't use smtp */
487 case MIDI_TIME_SIGNATURE:
488 if (mf->varlen != 4) {
489 AUBIO_ERR("Invalid length for TimeSignature meta event");
493 nominator = metadata[0];
494 denominator = pow(2.0, (double) metadata[1]);
495 clocks = metadata[2];
498 AUBIO_DBG("signature=%d/%d, metronome=%d, 32nd-notes=%d\n",
499 nominator, denominator, clocks, notes);
503 case MIDI_KEY_SIGNATURE:
504 if (mf->varlen != 2) {
505 AUBIO_ERR( "Invalid length for KeySignature meta event");
513 case MIDI_SEQUENCER_EVENT:
514 AUBIO_DBG("Sequencer event ignored\n");
522 AUBIO_DBG( "%s: %d: free metadata", __FILE__, __LINE__);
528 } else { /* channel messages */
530 type = status & 0xf0;
531 channel = status & 0x0f;
533 /* all channel message have at least 1 byte of associated data */
534 if ((param1 = aubio_midi_file_getc(mf)) < 0) {
535 AUBIO_ERR( "Unexpected end of file");
542 if ((param2 = aubio_midi_file_getc(mf)) < 0) {
543 AUBIO_ERR( "Unexpected end of file");
549 if ((param2 = aubio_midi_file_getc(mf)) < 0) {
550 AUBIO_ERR( "Unexpected end of file");
556 if ((param2 = aubio_midi_file_getc(mf)) < 0) {
557 AUBIO_ERR( "Unexpected end of file");
563 if ((param2 = aubio_midi_file_getc(mf)) < 0) {
564 AUBIO_ERR( "Unexpected end of file");
572 case CHANNEL_PRESSURE:
576 if ((param2 = aubio_midi_file_getc(mf)) < 0) {
577 AUBIO_ERR( "Unexpected end of file");
581 param1 = ((param2 & 0x7f) << 7) | (param1 & 0x7f);
586 /* Can't possibly happen !? */
587 AUBIO_ERR( "Unrecognized MIDI event");
590 evt = new_aubio_midi_event();
592 AUBIO_ERR( "Out of memory");
597 evt->channel = channel;
598 evt->param1 = param1;
599 evt->param2 = param2;
600 aubio_track_add_event(track, evt);
606 /** aubio_midi_file_get_division */
607 int aubio_midi_file_get_division(aubio_midi_file_t* midifile)
609 return midifile->division;
613 /** aubio_isasciistring */
614 int aubio_isasciistring(char* s)
617 int len = (int) AUBIO_STRLEN(s);
618 for (i = 0; i < len; i++) {
619 if (!aubio_isascii(s[i])) {
626 /** aubio_getlength */
627 long aubio_getlength(unsigned char *s)
630 i = s[3] | (s[2]<<8) | (s[1]<<16) | (s[0]<<24);