1 /* FluidSynth - A Software Synthesizer
3 * Copyright (C) 2003 Peter Hanappe and others.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public License
7 * as published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "fluid_midi.h"
22 #include "fluid_sys.h"
23 #include "fluid_synth.h"
24 #include "fluid_settings.h"
27 static int fluid_midi_event_length(unsigned char event);
29 /* Read the entire contents of a file into memory, allocating enough memory
30 * for the file, and returning the length and the buffer.
31 * Note: This rewinds the file to the start before reading.
32 * Returns NULL if there was an error reading or allocating memory.
34 static char* fluid_file_read_full(fluid_file fp, size_t* length);
35 #define READ_FULL_INITIAL_BUFLEN 1024
38 /***************************************************************
44 * Return a new MIDI file handle for parsing an already-loaded MIDI file.
46 * @param buffer Pointer to full contents of MIDI file (borrows the pointer).
47 * The caller must not free buffer until after the fluid_midi_file is deleted.
48 * @param length Size of the buffer in bytes.
49 * @return New MIDI file handle or NULL on error.
52 new_fluid_midi_file(const char* buffer, size_t length)
56 mf = FLUID_NEW(fluid_midi_file);
58 FLUID_LOG(FLUID_ERR, "Out of memory");
61 FLUID_MEMSET(mf, 0, sizeof(fluid_midi_file));
64 mf->running_status = -1;
71 if (fluid_midi_file_read_mthd(mf) != FLUID_OK) {
79 fluid_file_read_full(fluid_file fp, size_t* length)
84 /* Work out the length of the file in advance */
85 if (FLUID_FSEEK(fp, 0, SEEK_END) != 0)
87 FLUID_LOG(FLUID_ERR, "File load: Could not seek within file");
91 if (FLUID_FSEEK(fp, 0, SEEK_SET) != 0)
93 FLUID_LOG(FLUID_ERR, "File load: Could not seek within file");
96 FLUID_LOG(FLUID_DBG, "File load: Allocating %d bytes", buflen);
97 buffer = FLUID_MALLOC(buflen);
99 FLUID_LOG(FLUID_PANIC, "Out of memory");
102 n = FLUID_FREAD(buffer, 1, buflen, fp);
104 FLUID_LOG(FLUID_ERR, "Only read %d bytes; expected %d", n,
114 * Delete a MIDI file handle.
116 * @param mf MIDI file handle to close and free.
119 delete_fluid_midi_file (fluid_midi_file *mf)
129 * Gets the next byte in a MIDI file, taking into account previous running status.
131 * returns FLUID_FAILED if EOF or read error
134 fluid_midi_file_getc (fluid_midi_file *mf)
141 if (mf->buf_pos >= mf->buf_len) {
145 c = mf->buffer[mf->buf_pos++];
152 * Saves a byte to be returned the next time fluid_midi_file_getc() is called,
153 * when it is necessary according to running status.
156 fluid_midi_file_push(fluid_midi_file *mf, int c)
163 * fluid_midi_file_read
166 fluid_midi_file_read(fluid_midi_file *mf, void *buf, int len)
168 int num = len < mf->buf_len - mf->buf_pos
169 ? len : mf->buf_len - mf->buf_pos;
176 /* Note: Read bytes, even if there aren't enough, but only increment
177 * trackpos if successful (emulates old behaviour of fluid_midi_file_read)
179 FLUID_MEMCPY(buf, mf->buffer+mf->buf_pos, num);
185 FLUID_LOG(FLUID_DBG, "Could not read the requested number of bytes");
187 return (num != len) ? FLUID_FAILED : FLUID_OK;
191 * fluid_midi_file_skip
194 fluid_midi_file_skip (fluid_midi_file *mf, int skip)
196 int new_pos = mf->buf_pos + skip;
197 /* Mimic the behaviour of fseek: Error to seek past the start of file, but
198 * OK to seek past end (this just puts it into the EOF state). */
200 FLUID_LOG(FLUID_ERR, "Failed to seek position in file");
203 /* Clear the EOF flag, even if moved past the end of the file (this is
204 * consistent with the behaviour of fseek). */
206 mf->buf_pos = new_pos;
211 * fluid_midi_file_eof
213 int fluid_midi_file_eof(fluid_midi_file* mf)
215 /* Note: This does not simply test whether the file read pointer is past
216 * the end of the file. It mimics the behaviour of feof by actually
217 * testing the stateful EOF condition, which is set to TRUE if getc or
218 * fread have attempted to read past the end (but not if they have
219 * precisely reached the end), but reset to FALSE upon a successful seek.
225 * fluid_midi_file_read_mthd
228 fluid_midi_file_read_mthd(fluid_midi_file *mf)
231 if (fluid_midi_file_read(mf, mthd, 14) != FLUID_OK) {
234 if ((FLUID_STRNCMP(mthd, "MThd", 4) != 0) || (mthd[7] != 6)
237 "Doesn't look like a MIDI file: invalid MThd header");
241 mf->ntracks = (unsigned) mthd[11];
242 mf->ntracks += (unsigned int) (mthd[10]) << 16;
243 if ((mthd[12]) < 0) {
245 mf->smpte_fps = -mthd[12];
246 mf->smpte_res = (unsigned) mthd[13];
247 FLUID_LOG(FLUID_ERR, "File uses SMPTE timing -- Not implemented yet");
251 mf->division = (mthd[12] << 8) | (mthd[13] & 0xff);
252 FLUID_LOG(FLUID_DBG, "Division=%d", mf->division);
258 * fluid_midi_file_load_tracks
261 fluid_midi_file_load_tracks(fluid_midi_file *mf, fluid_player_t *player)
264 for (i = 0; i < mf->ntracks; i++) {
265 if (fluid_midi_file_read_track(mf, player, i) != FLUID_OK) {
273 * fluid_isasciistring
276 fluid_isasciistring(char *s)
279 int len = (int) FLUID_STRLEN(s);
280 for (i = 0; i < len; i++) {
281 if (!fluid_isascii(s[i])) {
292 fluid_getlength(unsigned char *s)
295 i = s[3] | (s[2] << 8) | (s[1] << 16) | (s[0] << 24);
300 * fluid_midi_file_read_tracklen
303 fluid_midi_file_read_tracklen(fluid_midi_file *mf)
305 unsigned char length[5];
306 if (fluid_midi_file_read(mf, length, 4) != FLUID_OK) {
309 mf->tracklen = fluid_getlength(length);
316 * fluid_midi_file_eot
319 fluid_midi_file_eot(fluid_midi_file *mf)
322 if (mf->trackpos > mf->tracklen) {
323 printf("track overrun: %d > %d\n", mf->trackpos, mf->tracklen);
326 return mf->eot || (mf->trackpos >= mf->tracklen);
330 * fluid_midi_file_read_track
333 fluid_midi_file_read_track(fluid_midi_file *mf, fluid_player_t *player, int num)
335 fluid_track_t *track;
336 unsigned char id[5], length[5];
340 if (fluid_midi_file_read(mf, id, 4) != FLUID_OK) {
346 while (!found_track) {
348 if (fluid_isasciistring((char *) id) == 0) {
350 "An non-ascii track header found, corrupt file");
353 } else if (strcmp((char *) id, "MTrk") == 0) {
357 if (fluid_midi_file_read_tracklen(mf) != FLUID_OK) {
361 track = new_fluid_track(num);
363 FLUID_LOG(FLUID_ERR, "Out of memory");
367 while (!fluid_midi_file_eot(mf)) {
368 if (fluid_midi_file_read_event(mf, track) != FLUID_OK) {
369 delete_fluid_track(track);
374 /* Skip remaining track data, if any */
375 if (mf->trackpos < mf->tracklen)
376 fluid_midi_file_skip(mf, mf->tracklen - mf->trackpos);
378 fluid_player_add_track(player, track);
382 if (fluid_midi_file_read(mf, length, 4) != FLUID_OK) {
385 skip = fluid_getlength(length);
386 /* fseek(mf->fp, skip, SEEK_CUR); */
387 if (fluid_midi_file_skip(mf, skip) != FLUID_OK) {
392 if (fluid_midi_file_eof(mf)) {
393 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
400 * fluid_midi_file_read_varlen
403 fluid_midi_file_read_varlen(fluid_midi_file *mf)
410 FLUID_LOG(FLUID_ERR, "Invalid variable length number");
413 c = fluid_midi_file_getc(mf);
415 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
419 mf->varlen |= (int) (c & 0x7F);
430 * fluid_midi_file_read_event
433 fluid_midi_file_read_event(fluid_midi_file *mf, fluid_track_t *track)
438 unsigned char *metadata = NULL;
439 unsigned char *dyn_buf = NULL;
440 unsigned char static_buf[256];
441 int nominator, denominator, clocks, notes;
442 fluid_midi_event_t *evt;
448 /* read the delta-time of the event */
449 if (fluid_midi_file_read_varlen(mf) != FLUID_OK) {
452 mf->dtime += mf->varlen;
454 /* read the status byte */
455 status = fluid_midi_file_getc(mf);
457 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
461 /* not a valid status byte: use the running status instead */
462 if ((status & 0x80) == 0) {
463 if ((mf->running_status & 0x80) == 0) {
464 FLUID_LOG(FLUID_ERR, "Undefined status and invalid running status");
467 fluid_midi_file_push(mf, status);
468 status = mf->running_status;
471 /* check what message we have */
473 mf->running_status = status;
475 if ((status == MIDI_SYSEX)) { /* system exclusif */
476 /* read the length of the message */
477 if (fluid_midi_file_read_varlen(mf) != FLUID_OK) {
482 FLUID_LOG(FLUID_DBG, "%s: %d: alloc metadata, len = %d", __FILE__,
483 __LINE__, mf->varlen);
484 metadata = FLUID_MALLOC(mf->varlen + 1);
486 if (metadata == NULL) {
487 FLUID_LOG(FLUID_PANIC, "Out of memory");
491 /* read the data of the message */
492 if (fluid_midi_file_read(mf, metadata, mf->varlen) != FLUID_OK) {
493 FLUID_FREE (metadata);
497 evt = new_fluid_midi_event();
499 FLUID_LOG(FLUID_ERR, "Out of memory");
500 FLUID_FREE (metadata);
504 evt->dtime = mf->dtime;
507 if (metadata[mf->varlen - 1] == MIDI_EOX)
510 /* Add SYSEX event and indicate that its dynamically allocated and should be freed with event */
511 fluid_midi_event_set_sysex(evt, metadata, size, TRUE);
512 fluid_track_add_event(track, evt);
518 } else if (status == MIDI_META_EVENT) { /* meta events */
520 int result = FLUID_OK;
522 /* get the type of the meta message */
523 type = fluid_midi_file_getc(mf);
525 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
529 /* get the length of the data part */
530 if (fluid_midi_file_read_varlen(mf) != FLUID_OK) {
534 if (mf->varlen < 255) {
535 metadata = &static_buf[0];
537 FLUID_LOG(FLUID_DBG, "%s: %d: alloc metadata, len = %d", __FILE__,
538 __LINE__, mf->varlen);
539 dyn_buf = FLUID_MALLOC(mf->varlen + 1);
540 if (dyn_buf == NULL) {
541 FLUID_LOG(FLUID_PANIC, "Out of memory");
549 if (fluid_midi_file_read(mf, metadata, mf->varlen) != FLUID_OK) {
557 /* handle meta data */
561 metadata[mf->varlen] = 0;
564 case MIDI_TRACK_NAME:
565 metadata[mf->varlen] = 0;
566 fluid_track_set_name(track, (char *) metadata);
570 metadata[mf->varlen] = 0;
580 break; /* don't care much for text events */
583 if (mf->varlen != 0) {
584 FLUID_LOG(FLUID_ERR, "Invalid length for EndOfTrack event");
585 result = FLUID_FAILED;
589 evt = new_fluid_midi_event();
591 FLUID_LOG(FLUID_ERR, "Out of memory");
592 result = FLUID_FAILED;
595 evt->dtime = mf->dtime;
596 evt->type = MIDI_EOT;
597 fluid_track_add_event(track, evt);
602 if (mf->varlen != 3) {
604 "Invalid length for SetTempo meta event");
605 result = FLUID_FAILED;
608 tempo = (metadata[0] << 16) + (metadata[1] << 8) + metadata[2];
609 evt = new_fluid_midi_event();
611 FLUID_LOG(FLUID_ERR, "Out of memory");
612 result = FLUID_FAILED;
615 evt->dtime = mf->dtime;
616 evt->type = MIDI_SET_TEMPO;
620 fluid_track_add_event(track, evt);
624 case MIDI_SMPTE_OFFSET:
625 if (mf->varlen != 5) {
627 "Invalid length for SMPTE Offset meta event");
628 result = FLUID_FAILED;
631 break; /* we don't use smtp */
633 case MIDI_TIME_SIGNATURE:
634 if (mf->varlen != 4) {
636 "Invalid length for TimeSignature meta event");
637 result = FLUID_FAILED;
640 nominator = metadata[0];
641 denominator = pow(2.0, (double) metadata[1]);
642 clocks = metadata[2];
646 "signature=%d/%d, metronome=%d, 32nd-notes=%d",
647 nominator, denominator, clocks, notes);
651 case MIDI_KEY_SIGNATURE:
652 if (mf->varlen != 2) {
654 "Invalid length for KeySignature meta event");
655 result = FLUID_FAILED;
658 /* We don't care about key signatures anyway */
663 case MIDI_SEQUENCER_EVENT:
671 FLUID_LOG(FLUID_DBG, "%s: %d: free metadata", __FILE__, __LINE__);
677 } else { /* channel messages */
679 type = status & 0xf0;
680 channel = status & 0x0f;
682 /* all channel message have at least 1 byte of associated data */
683 if ((param1 = fluid_midi_file_getc(mf)) < 0) {
684 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
691 if ((param2 = fluid_midi_file_getc(mf)) < 0) {
692 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
698 if ((param2 = fluid_midi_file_getc(mf)) < 0) {
699 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
705 if ((param2 = fluid_midi_file_getc(mf)) < 0) {
706 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
712 if ((param2 = fluid_midi_file_getc(mf)) < 0) {
713 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
721 case CHANNEL_PRESSURE:
725 if ((param2 = fluid_midi_file_getc(mf)) < 0) {
726 FLUID_LOG(FLUID_ERR, "Unexpected end of file");
730 param1 = ((param2 & 0x7f) << 7) | (param1 & 0x7f);
735 /* Can't possibly happen !? */
736 FLUID_LOG(FLUID_ERR, "Unrecognized MIDI event");
739 evt = new_fluid_midi_event();
741 FLUID_LOG(FLUID_ERR, "Out of memory");
744 evt->dtime = mf->dtime;
746 evt->channel = channel;
747 evt->param1 = param1;
748 evt->param2 = param2;
749 fluid_track_add_event(track, evt);
756 * fluid_midi_file_get_division
759 fluid_midi_file_get_division(fluid_midi_file *midifile)
761 return midifile->division;
764 /******************************************************
770 * Create a MIDI event structure.
771 * @return New MIDI event structure or NULL when out of memory.
774 new_fluid_midi_event ()
776 fluid_midi_event_t* evt;
777 evt = FLUID_NEW(fluid_midi_event_t);
779 FLUID_LOG(FLUID_ERR, "Out of memory");
788 evt->paramptr = NULL;
793 * Delete MIDI event structure.
794 * @param evt MIDI event structure
795 * @return Always returns #FLUID_OK
798 delete_fluid_midi_event(fluid_midi_event_t *evt)
800 fluid_midi_event_t *temp;
805 /* Dynamic SYSEX event? - free (param2 indicates if dynamic) */
806 if (evt->type == MIDI_SYSEX && evt->paramptr && evt->param2)
807 FLUID_FREE (evt->paramptr);
816 * Get the event type field of a MIDI event structure.
817 * @param evt MIDI event structure
818 * @return Event type field (MIDI status byte without channel)
821 fluid_midi_event_get_type(fluid_midi_event_t *evt)
827 * Set the event type field of a MIDI event structure.
828 * @param evt MIDI event structure
829 * @param type Event type field (MIDI status byte without channel)
830 * @return Always returns #FLUID_OK
833 fluid_midi_event_set_type(fluid_midi_event_t *evt, int type)
840 * Get the channel field of a MIDI event structure.
841 * @param evt MIDI event structure
842 * @return Channel field
845 fluid_midi_event_get_channel(fluid_midi_event_t *evt)
851 * Set the channel field of a MIDI event structure.
852 * @param evt MIDI event structure
853 * @param chan MIDI channel field
854 * @return Always returns #FLUID_OK
857 fluid_midi_event_set_channel(fluid_midi_event_t *evt, int chan)
864 * Get the key field of a MIDI event structure.
865 * @param evt MIDI event structure
866 * @return MIDI note number (0-127)
869 fluid_midi_event_get_key(fluid_midi_event_t *evt)
875 * Set the key field of a MIDI event structure.
876 * @param evt MIDI event structure
877 * @param v MIDI note number (0-127)
878 * @return Always returns #FLUID_OK
881 fluid_midi_event_set_key(fluid_midi_event_t *evt, int v)
888 * Get the velocity field of a MIDI event structure.
889 * @param evt MIDI event structure
890 * @return MIDI velocity number (0-127)
893 fluid_midi_event_get_velocity(fluid_midi_event_t *evt)
899 * Set the velocity field of a MIDI event structure.
900 * @param evt MIDI event structure
901 * @param v MIDI velocity value
902 * @return Always returns #FLUID_OK
905 fluid_midi_event_set_velocity(fluid_midi_event_t *evt, int v)
912 * Get the control number of a MIDI event structure.
913 * @param evt MIDI event structure
914 * @return MIDI control number
917 fluid_midi_event_get_control(fluid_midi_event_t *evt)
923 * Set the control field of a MIDI event structure.
924 * @param evt MIDI event structure
925 * @param v MIDI control number
926 * @return Always returns #FLUID_OK
929 fluid_midi_event_set_control(fluid_midi_event_t *evt, int v)
936 * Get the value field from a MIDI event structure.
937 * @param evt MIDI event structure
938 * @return Value field
941 fluid_midi_event_get_value(fluid_midi_event_t *evt)
947 * Set the value field of a MIDI event structure.
948 * @param evt MIDI event structure
949 * @param v Value to assign
950 * @return Always returns #FLUID_OK
953 fluid_midi_event_set_value(fluid_midi_event_t *evt, int v)
960 * Get the program field of a MIDI event structure.
961 * @param evt MIDI event structure
962 * @return MIDI program number (0-127)
965 fluid_midi_event_get_program(fluid_midi_event_t *evt)
971 * Set the program field of a MIDI event structure.
972 * @param evt MIDI event structure
973 * @param val MIDI program number (0-127)
974 * @return Always returns #FLUID_OK
977 fluid_midi_event_set_program(fluid_midi_event_t *evt, int val)
984 * Get the pitch field of a MIDI event structure.
985 * @param evt MIDI event structure
986 * @return Pitch value (14 bit value, 0-16383, 8192 is center)
989 fluid_midi_event_get_pitch(fluid_midi_event_t *evt)
995 * Set the pitch field of a MIDI event structure.
996 * @param evt MIDI event structure
997 * @param val Pitch value (14 bit value, 0-16383, 8192 is center)
998 * @return Always returns FLUID_OK
1001 fluid_midi_event_set_pitch(fluid_midi_event_t *evt, int val)
1008 * Assign sysex data to a MIDI event structure.
1009 * @param evt MIDI event structure
1010 * @param data Pointer to SYSEX data
1011 * @param size Size of SYSEX data
1012 * @param dynamic TRUE if the SYSEX data has been dynamically allocated and
1013 * should be freed when the event is freed (only applies if event gets destroyed
1014 * with delete_fluid_midi_event())
1015 * @return Always returns #FLUID_OK
1017 * NOTE: Unlike the other event assignment functions, this one sets evt->type.
1020 fluid_midi_event_set_sysex(fluid_midi_event_t *evt, void *data, int size, int dynamic)
1022 evt->type = MIDI_SYSEX;
1023 evt->paramptr = data;
1025 evt->param2 = dynamic;
1029 /******************************************************
1038 new_fluid_track(int num)
1040 fluid_track_t *track;
1041 track = FLUID_NEW(fluid_track_t);
1042 if (track == NULL) {
1047 track->first = NULL;
1055 * delete_fluid_track
1058 delete_fluid_track(fluid_track_t *track)
1060 if (track->name != NULL) {
1061 FLUID_FREE(track->name);
1063 if (track->first != NULL) {
1064 delete_fluid_midi_event(track->first);
1071 * fluid_track_set_name
1074 fluid_track_set_name(fluid_track_t *track, char *name)
1077 if (track->name != NULL) {
1078 FLUID_FREE(track->name);
1084 len = FLUID_STRLEN(name);
1085 track->name = FLUID_MALLOC(len + 1);
1086 if (track->name == NULL) {
1087 FLUID_LOG(FLUID_ERR, "Out of memory");
1088 return FLUID_FAILED;
1090 FLUID_STRCPY(track->name, name);
1095 * fluid_track_get_name
1098 fluid_track_get_name(fluid_track_t *track)
1104 * fluid_track_get_duration
1107 fluid_track_get_duration(fluid_track_t *track)
1110 fluid_midi_event_t *evt = track->first;
1111 while (evt != NULL) {
1120 * fluid_track_count_events
1123 fluid_track_count_events(fluid_track_t *track, int *on, int *off)
1125 fluid_midi_event_t *evt = track->first;
1126 while (evt != NULL) {
1127 if (evt->type == NOTE_ON) {
1129 } else if (evt->type == NOTE_OFF) {
1139 * fluid_track_add_event
1142 fluid_track_add_event(fluid_track_t *track, fluid_midi_event_t *evt)
1145 if (track->first == NULL) {
1150 track->last->next = evt;
1157 * fluid_track_first_event
1159 fluid_midi_event_t *
1160 fluid_track_first_event(fluid_track_t *track)
1162 track->cur = track->first;
1167 * fluid_track_next_event
1169 fluid_midi_event_t *
1170 fluid_track_next_event(fluid_track_t *track)
1172 if (track->cur != NULL) {
1173 track->cur = track->cur->next;
1182 fluid_track_reset(fluid_track_t *track)
1185 track->cur = track->first;
1190 * fluid_track_send_events
1193 fluid_track_send_events(fluid_track_t *track,
1194 fluid_synth_t *synth,
1195 fluid_player_t *player,
1198 int status = FLUID_OK;
1199 fluid_midi_event_t *event;
1204 if (event == NULL) {
1208 /* printf("track=%02d\tticks=%05u\ttrack=%05u\tdtime=%05u\tnext=%05u\n", */
1213 /* track->ticks + event->dtime); */
1215 if (track->ticks + event->dtime > ticks) {
1219 track->ticks += event->dtime;
1221 if (!player || event->type == MIDI_EOT) {
1223 else if (event->type == MIDI_SET_TEMPO) {
1224 fluid_player_set_midi_tempo(player, event->param1);
1227 if (player->playback_callback)
1228 player->playback_callback(player->playback_userdata, event);
1231 fluid_track_next_event(track);
1237 /******************************************************
1243 * Create a new MIDI player.
1244 * @param synth Fluid synthesizer instance to create player for
1245 * @return New MIDI player instance or NULL on error (out of memory)
1248 new_fluid_player(fluid_synth_t *synth)
1251 fluid_player_t *player;
1252 player = FLUID_NEW(fluid_player_t);
1253 if (player == NULL) {
1254 FLUID_LOG(FLUID_ERR, "Out of memory");
1257 player->status = FLUID_PLAYER_READY;
1259 player->ntracks = 0;
1260 for (i = 0; i < MAX_NUMBER_OF_TRACKS; i++) {
1261 player->track[i] = NULL;
1263 player->synth = synth;
1264 player->system_timer = NULL;
1265 player->sample_timer = NULL;
1266 player->playlist = NULL;
1267 player->currentfile = NULL;
1268 player->division = 0;
1269 player->send_program_change = 1;
1270 player->miditempo = 480000;
1271 player->deltatime = 4.0;
1272 player->cur_msec = 0;
1273 player->cur_ticks = 0;
1274 fluid_player_set_playback_callback(player, fluid_synth_handle_midi_event, synth);
1276 player->use_system_timer = fluid_settings_str_equal(synth->settings,
1277 "player.timing-source", "system");
1279 fluid_settings_getint(synth->settings, "player.reset-synth", &i);
1280 player->reset_synth_between_songs = i;
1286 * Delete a MIDI player instance.
1287 * @param player MIDI player instance
1288 * @return Always returns #FLUID_OK
1291 delete_fluid_player(fluid_player_t *player)
1294 fluid_playlist_item* pi;
1296 if (player == NULL) {
1299 fluid_player_stop(player);
1300 fluid_player_reset(player);
1302 while (player->playlist != NULL) {
1303 q = player->playlist->next;
1304 pi = (fluid_playlist_item*) player->playlist->data;
1305 FLUID_FREE(pi->filename);
1306 FLUID_FREE(pi->buffer);
1308 delete1_fluid_list(player->playlist);
1309 player->playlist = q;
1317 * Registers settings related to the MIDI player
1320 fluid_player_settings(fluid_settings_t *settings)
1322 /* player.timing-source can be either "system" (use system timer)
1323 or "sample" (use timer based on number of written samples) */
1324 fluid_settings_register_str(settings, "player.timing-source", "sample", 0,
1326 fluid_settings_add_option(settings, "player.timing-source", "sample");
1327 fluid_settings_add_option(settings, "player.timing-source", "system");
1329 /* Selects whether the player should reset the synth between songs, or not. */
1330 fluid_settings_register_int(settings, "player.reset-synth", 1, 0, 1,
1331 FLUID_HINT_TOGGLED, NULL, NULL);
1336 fluid_player_reset(fluid_player_t *player)
1340 for (i = 0; i < MAX_NUMBER_OF_TRACKS; i++) {
1341 if (player->track[i] != NULL) {
1342 delete_fluid_track(player->track[i]);
1343 player->track[i] = NULL;
1346 /* player->current_file = NULL; */
1347 /* player->status = FLUID_PLAYER_READY; */
1348 /* player->loop = 1; */
1349 player->ntracks = 0;
1350 player->division = 0;
1351 player->send_program_change = 1;
1352 player->miditempo = 480000;
1353 player->deltatime = 4.0;
1358 * fluid_player_add_track
1361 fluid_player_add_track(fluid_player_t *player, fluid_track_t *track)
1363 if (player->ntracks < MAX_NUMBER_OF_TRACKS) {
1364 player->track[player->ntracks++] = track;
1367 return FLUID_FAILED;
1372 * fluid_player_count_tracks
1375 fluid_player_count_tracks(fluid_player_t *player)
1377 return player->ntracks;
1381 * fluid_player_get_track
1384 fluid_player_get_track(fluid_player_t *player, int i)
1386 if ((i >= 0) && (i < MAX_NUMBER_OF_TRACKS)) {
1387 return player->track[i];
1394 * Change the MIDI callback function. This is usually set to
1395 * fluid_synth_handle_midi_event, but can optionally be changed
1396 * to a user-defined function instead, for intercepting all MIDI
1397 * messages sent to the synth. You can also use a midi router as
1398 * the callback function to modify the MIDI messages before sending
1399 * them to the synth.
1400 * @param player MIDI player instance
1401 * @param handler Pointer to callback function
1402 * @param handler_data Parameter sent to the callback function
1407 fluid_player_set_playback_callback(fluid_player_t* player,
1408 handle_midi_event_func_t handler, void* handler_data)
1410 player->playback_callback = handler;
1411 player->playback_userdata = handler_data;
1416 * Add a MIDI file to a player queue.
1417 * @param player MIDI player instance
1418 * @param midifile File name of the MIDI file to add
1419 * @return #FLUID_OK or #FLUID_FAILED
1422 fluid_player_add(fluid_player_t *player, const char *midifile)
1424 fluid_playlist_item *pi = FLUID_MALLOC(sizeof(fluid_playlist_item));
1425 char* f = FLUID_STRDUP(midifile);
1429 FLUID_LOG(FLUID_PANIC, "Out of memory");
1430 return FLUID_FAILED;
1436 player->playlist = fluid_list_append(player->playlist, pi);
1441 * Add a MIDI file to a player queue, from a buffer in memory.
1442 * @param player MIDI player instance
1443 * @param buffer Pointer to memory containing the bytes of a complete MIDI
1444 * file. The data is copied, so the caller may free or modify it immediately
1445 * without affecting the playlist.
1446 * @param len Length of the buffer, in bytes.
1447 * @return #FLUID_OK or #FLUID_FAILED
1450 fluid_player_add_mem(fluid_player_t* player, const void *buffer, size_t len)
1452 /* Take a copy of the buffer, so the caller can free immediately. */
1453 fluid_playlist_item *pi = FLUID_MALLOC(sizeof(fluid_playlist_item));
1454 void *buf_copy = FLUID_MALLOC(len);
1455 if (!pi || !buf_copy) {
1457 FLUID_FREE(buf_copy);
1458 FLUID_LOG(FLUID_PANIC, "Out of memory");
1459 return FLUID_FAILED;
1462 FLUID_MEMCPY(buf_copy, buffer, len);
1463 pi->filename = NULL;
1464 pi->buffer = buf_copy;
1465 pi->buffer_len = len;
1466 player->playlist = fluid_list_append(player->playlist, pi);
1474 fluid_player_load(fluid_player_t *player, fluid_playlist_item *item)
1476 fluid_midi_file *midifile;
1478 size_t buffer_length;
1481 if (item->filename != NULL)
1484 /* This file is specified by filename; load the file from disk */
1485 FLUID_LOG(FLUID_DBG, "%s: %d: Loading midifile %s", __FILE__, __LINE__,
1487 /* Read the entire contents of the file into the buffer */
1488 fp = FLUID_FOPEN(item->filename, "rb");
1490 FLUID_LOG(FLUID_ERR, "Couldn't open the MIDI file");
1491 return FLUID_FAILED;
1493 buffer = fluid_file_read_full(fp, &buffer_length);
1497 return FLUID_FAILED;
1504 /* This file is specified by a pre-loaded buffer; load from memory */
1505 FLUID_LOG(FLUID_DBG, "%s: %d: Loading midifile from memory (%p)",
1506 __FILE__, __LINE__, item->buffer);
1507 buffer = (char *) item->buffer;
1508 buffer_length = item->buffer_len;
1509 /* Do not free the buffer (it is owned by the playlist) */
1513 midifile = new_fluid_midi_file(buffer, buffer_length);
1514 if (midifile == NULL) {
1518 return FLUID_FAILED;
1520 player->division = fluid_midi_file_get_division(midifile);
1521 fluid_player_set_midi_tempo(player, player->miditempo); // Update deltatime
1522 /*FLUID_LOG(FLUID_DBG, "quarter note division=%d\n", player->division); */
1524 if (fluid_midi_file_load_tracks(midifile, player) != FLUID_OK) {
1528 delete_fluid_midi_file(midifile);
1529 return FLUID_FAILED;
1531 delete_fluid_midi_file(midifile);
1539 fluid_player_advancefile(fluid_player_t *player)
1541 if (player->playlist == NULL) {
1542 return; /* No files to play */
1544 if (player->currentfile != NULL) {
1545 player->currentfile = fluid_list_next(player->currentfile);
1547 if (player->currentfile == NULL) {
1548 if (player->loop == 0) {
1549 return; /* We're done playing */
1551 if (player->loop > 0) {
1554 player->currentfile = player->playlist;
1559 fluid_player_playlist_load(fluid_player_t *player, unsigned int msec)
1561 fluid_playlist_item* current_playitem;
1565 fluid_player_advancefile(player);
1566 if (player->currentfile == NULL) {
1567 /* Failed to find next song, probably since we're finished */
1568 player->status = FLUID_PLAYER_DONE;
1572 fluid_player_reset(player);
1573 current_playitem = (fluid_playlist_item *) player->currentfile->data;
1574 } while (fluid_player_load(player, current_playitem) != FLUID_OK);
1576 /* Successfully loaded midi file */
1578 player->begin_msec = msec;
1579 player->start_msec = msec;
1580 player->start_ticks = 0;
1581 player->cur_ticks = 0;
1583 if (player->reset_synth_between_songs) {
1584 fluid_synth_system_reset(player->synth);
1587 for (i = 0; i < player->ntracks; i++) {
1588 if (player->track[i] != NULL) {
1589 fluid_track_reset(player->track[i]);
1596 * fluid_player_callback
1599 fluid_player_callback(void *data, unsigned int msec)
1603 int status = FLUID_PLAYER_DONE;
1604 fluid_player_t *player;
1605 fluid_synth_t *synth;
1606 player = (fluid_player_t *) data;
1607 synth = player->synth;
1609 loadnextfile = player->currentfile == NULL ? 1 : 0;
1613 fluid_player_playlist_load(player, msec);
1614 if (player->currentfile == NULL) {
1619 player->cur_msec = msec;
1620 player->cur_ticks = (player->start_ticks
1621 + (int) ((double) (player->cur_msec - player->start_msec)
1622 / player->deltatime));
1624 for (i = 0; i < player->ntracks; i++) {
1625 if (!fluid_track_eot(player->track[i])) {
1626 status = FLUID_PLAYER_PLAYING;
1627 if (fluid_track_send_events(player->track[i], synth, player,
1628 player->cur_ticks) != FLUID_OK) {
1634 if (status == FLUID_PLAYER_DONE) {
1635 FLUID_LOG(FLUID_DBG, "%s: %d: Duration=%.3f sec", __FILE__,
1636 __LINE__, (msec - player->begin_msec) / 1000.0);
1639 } while (loadnextfile);
1641 player->status = status;
1647 * Activates play mode for a MIDI player if not already playing.
1648 * @param player MIDI player instance
1649 * @return #FLUID_OK on success, #FLUID_FAILED otherwise
1652 fluid_player_play(fluid_player_t *player)
1654 if (player->status == FLUID_PLAYER_PLAYING) {
1658 if (player->playlist == NULL) {
1662 player->status = FLUID_PLAYER_PLAYING;
1664 if (player->use_system_timer) {
1665 player->system_timer = new_fluid_timer((int) player->deltatime,
1666 fluid_player_callback, (void *) player, TRUE, FALSE, TRUE);
1667 if (player->system_timer == NULL) {
1668 return FLUID_FAILED;
1671 player->sample_timer = new_fluid_sample_timer(player->synth,
1672 fluid_player_callback, (void *) player);
1674 if (player->sample_timer == NULL) {
1675 return FLUID_FAILED;
1682 * Stops a MIDI player.
1683 * @param player MIDI player instance
1684 * @return Always returns #FLUID_OK
1687 fluid_player_stop(fluid_player_t *player)
1689 if (player->system_timer != NULL) {
1690 delete_fluid_timer(player->system_timer);
1692 if (player->sample_timer != NULL) {
1693 delete_fluid_sample_timer(player->synth, player->sample_timer);
1695 player->status = FLUID_PLAYER_DONE;
1696 player->sample_timer = NULL;
1697 player->system_timer = NULL;
1702 * Get MIDI player status.
1703 * @param player MIDI player instance
1704 * @return Player status (#fluid_player_status)
1708 fluid_player_get_status(fluid_player_t *player)
1710 return player->status;
1714 * Enable looping of a MIDI player
1715 * @param player MIDI player instance
1716 * @param loop Times left to loop the playlist. -1 means loop infinitely.
1717 * @return Always returns #FLUID_OK
1720 * For example, if you want to loop the playlist twice, set loop to 2
1721 * and call this function before you start the player.
1723 int fluid_player_set_loop(fluid_player_t *player, int loop)
1725 player->loop = loop;
1730 * Set the tempo of a MIDI player.
1731 * @param player MIDI player instance
1732 * @param tempo Tempo to set playback speed to (in microseconds per quarter note, as per MIDI file spec)
1733 * @return Always returns #FLUID_OK
1735 int fluid_player_set_midi_tempo(fluid_player_t *player, int tempo)
1737 player->miditempo = tempo;
1738 player->deltatime = (double) tempo / player->division / 1000.0; /* in milliseconds */
1739 player->start_msec = player->cur_msec;
1740 player->start_ticks = player->cur_ticks;
1742 FLUID_LOG(FLUID_DBG,
1743 "tempo=%d, tick time=%f msec, cur time=%d msec, cur tick=%d",
1744 tempo, player->deltatime, player->cur_msec, player->cur_ticks);
1750 * Set the tempo of a MIDI player in beats per minute.
1751 * @param player MIDI player instance
1752 * @param bpm Tempo in beats per minute
1753 * @return Always returns #FLUID_OK
1756 fluid_player_set_bpm(fluid_player_t *player, int bpm)
1758 return fluid_player_set_midi_tempo(player, (int) ((double) 60 * 1e6 / bpm));
1762 * Wait for a MIDI player to terminate (when done playing).
1763 * @param player MIDI player instance
1764 * @return #FLUID_OK on success, #FLUID_FAILED otherwise
1767 fluid_player_join(fluid_player_t *player)
1769 if (player->system_timer) {
1770 return fluid_timer_join(player->system_timer);
1771 } else if (player->sample_timer) {
1772 /* Busy-wait loop, since there's no thread to wait for... */
1773 while (player->status != FLUID_PLAYER_DONE) {
1784 /************************************************************************
1790 * new_fluid_midi_parser
1792 fluid_midi_parser_t *
1793 new_fluid_midi_parser ()
1795 fluid_midi_parser_t *parser;
1796 parser = FLUID_NEW(fluid_midi_parser_t);
1797 if (parser == NULL) {
1798 FLUID_LOG(FLUID_ERR, "Out of memory");
1801 parser->status = 0; /* As long as the status is 0, the parser won't do anything -> no need to initialize all the fields. */
1806 * delete_fluid_midi_parser
1809 delete_fluid_midi_parser(fluid_midi_parser_t *parser)
1816 * Parse a MIDI stream one character at a time.
1817 * @param parser Parser instance
1818 * @param c Next character in MIDI stream
1819 * @return A parsed MIDI event or NULL if none. Event is internal and should
1820 * not be modified or freed and is only valid until next call to this function.
1822 fluid_midi_event_t *
1823 fluid_midi_parser_parse(fluid_midi_parser_t *parser, unsigned char c)
1825 fluid_midi_event_t *event;
1827 /* Real-time messages (0xF8-0xFF) can occur anywhere, even in the middle
1828 * of another message. */
1830 if (c == MIDI_SYSTEM_RESET) {
1831 parser->event.type = c;
1832 parser->status = 0; /* clear the status */
1833 return &parser->event;
1839 /* Status byte? - If previous message not yet complete, it is discarded (re-sync). */
1841 /* Any status byte terminates SYSEX messages (not just 0xF7) */
1842 if (parser->status == MIDI_SYSEX && parser->nr_bytes > 0) {
1843 event = &parser->event;
1844 fluid_midi_event_set_sysex(event, parser->data, parser->nr_bytes,
1849 if (c < 0xF0) /* Voice category message? */
1851 parser->channel = c & 0x0F;
1852 parser->status = c & 0xF0;
1854 /* The event consumes x bytes of data... (subtract 1 for the status byte) */
1855 parser->nr_bytes_total = fluid_midi_event_length(parser->status)
1858 parser->nr_bytes = 0; /* 0 bytes read so far */
1859 } else if (c == MIDI_SYSEX) {
1860 parser->status = MIDI_SYSEX;
1861 parser->nr_bytes = 0;
1863 parser->status = 0; /* Discard other system messages (0xF1-0xF7) */
1865 return event; /* Return SYSEX event or NULL */
1868 /* Data/parameter byte */
1870 /* Discard data bytes for events we don't care about */
1871 if (parser->status == 0)
1874 /* Max data size exceeded? (SYSEX messages only really) */
1875 if (parser->nr_bytes == FLUID_MIDI_PARSER_MAX_DATA_SIZE) {
1876 parser->status = 0; /* Discard the rest of the message */
1880 /* Store next byte */
1881 parser->data[parser->nr_bytes++] = c;
1883 /* Do we still need more data to get this event complete? */
1884 if (parser->nr_bytes < parser->nr_bytes_total)
1887 /* Event is complete, return it.
1888 * Running status byte MIDI feature is also handled here. */
1889 parser->event.type = parser->status;
1890 parser->event.channel = parser->channel;
1891 parser->nr_bytes = 0; /* Reset data size, in case there are additional running status messages */
1893 switch (parser->status) {
1897 case CONTROL_CHANGE:
1898 case PROGRAM_CHANGE:
1899 case CHANNEL_PRESSURE:
1900 parser->event.param1 = parser->data[0]; /* For example key number */
1901 parser->event.param2 = parser->data[1]; /* For example velocity */
1904 /* Pitch-bend is transmitted with 14-bit precision. */
1905 parser->event.param1 = (parser->data[1] << 7) | parser->data[0];
1907 default: /* Unlikely */
1911 return &parser->event;
1915 * Returns the length of a MIDI message. */
1917 fluid_midi_event_length(unsigned char event)
1919 switch (event & 0xF0) {
1923 case CONTROL_CHANGE:
1926 case PROGRAM_CHANGE:
1927 case CHANNEL_PRESSURE:
1931 case MIDI_TIME_CODE:
1932 case MIDI_SONG_SELECT:
1936 case MIDI_TUNE_REQUEST:
1938 case MIDI_SONG_POSITION: