mirror of
https://github.com/ZDoom/fluidsynth.git
synced 2025-06-01 17:42:11 +00:00
* fix for bug "Undefined behavior parsing a MIDI file which unexpectedly ends" reported by Matt Giuca, ticket #92
* reformat fluid_midi.c source according to the coding style.
This commit is contained in:
parent
05ee2306b1
commit
068a7534ef
1 changed files with 1062 additions and 992 deletions
|
@ -38,7 +38,8 @@ static int fluid_midi_event_length(unsigned char event);
|
|||
* @param filename Path of file to open.
|
||||
* @return New MIDI file handle or NULL on error.
|
||||
*/
|
||||
fluid_midi_file* new_fluid_midi_file(char* filename)
|
||||
fluid_midi_file *
|
||||
new_fluid_midi_file(char *filename)
|
||||
{
|
||||
fluid_midi_file *mf;
|
||||
|
||||
|
@ -71,7 +72,8 @@ fluid_midi_file* new_fluid_midi_file(char* filename)
|
|||
* @internal
|
||||
* @param mf MIDI file handle to close and free.
|
||||
*/
|
||||
void delete_fluid_midi_file(fluid_midi_file* mf)
|
||||
void
|
||||
delete_fluid_midi_file (fluid_midi_file *mf)
|
||||
{
|
||||
if (mf == NULL) {
|
||||
return;
|
||||
|
@ -84,9 +86,12 @@ void delete_fluid_midi_file(fluid_midi_file* mf)
|
|||
}
|
||||
|
||||
/*
|
||||
* Get the next byte in a MIDI file.
|
||||
* Gets the next byte in a MIDI file, taking into account previous running status.
|
||||
*
|
||||
* returns FLUID_FAILED if EOF or read error
|
||||
*/
|
||||
int fluid_midi_file_getc(fluid_midi_file* mf)
|
||||
int
|
||||
fluid_midi_file_getc (fluid_midi_file *mf)
|
||||
{
|
||||
unsigned char c;
|
||||
int n;
|
||||
|
@ -95,15 +100,19 @@ int fluid_midi_file_getc(fluid_midi_file* mf)
|
|||
mf->c = -1;
|
||||
} else {
|
||||
n = FLUID_FREAD(&c, 1, 1, mf->fp);
|
||||
if (n != 1)
|
||||
return FLUID_FAILED;
|
||||
mf->trackpos++;
|
||||
}
|
||||
return (int) c;
|
||||
}
|
||||
|
||||
/*
|
||||
* fluid_midi_file_push
|
||||
* Saves a byte to be returned the next time fluid_midi_file_getc() is called,
|
||||
* when it is necessary according to running status.
|
||||
*/
|
||||
int fluid_midi_file_push(fluid_midi_file* mf, int c)
|
||||
int
|
||||
fluid_midi_file_push(fluid_midi_file *mf, int c)
|
||||
{
|
||||
mf->c = c;
|
||||
return FLUID_OK;
|
||||
|
@ -112,14 +121,15 @@ int fluid_midi_file_push(fluid_midi_file* mf, int c)
|
|||
/*
|
||||
* fluid_midi_file_read
|
||||
*/
|
||||
int fluid_midi_file_read(fluid_midi_file* mf, void* buf, int len)
|
||||
int
|
||||
fluid_midi_file_read(fluid_midi_file *mf, void *buf, int len)
|
||||
{
|
||||
int num = FLUID_FREAD(buf, 1, len, mf->fp);
|
||||
if (num == len)
|
||||
mf->trackpos += num;
|
||||
#if DEBUG
|
||||
if (num != len) {
|
||||
FLUID_LOG(FLUID_DBG, "Coulnd't read the requested number of bytes");
|
||||
}
|
||||
else
|
||||
FLUID_LOG(FLUID_DBG, "Could not read the requested number of bytes");
|
||||
#endif
|
||||
return (num != len) ? FLUID_FAILED : FLUID_OK;
|
||||
}
|
||||
|
@ -127,7 +137,8 @@ int fluid_midi_file_read(fluid_midi_file* mf, void* buf, int len)
|
|||
/*
|
||||
* fluid_midi_file_skip
|
||||
*/
|
||||
int fluid_midi_file_skip(fluid_midi_file* mf, int skip)
|
||||
int
|
||||
fluid_midi_file_skip (fluid_midi_file *mf, int skip)
|
||||
{
|
||||
int err = FLUID_FSEEK(mf->fp, skip, SEEK_CUR);
|
||||
if (err) {
|
||||
|
@ -140,14 +151,17 @@ int fluid_midi_file_skip(fluid_midi_file* mf, int skip)
|
|||
/*
|
||||
* fluid_midi_file_read_mthd
|
||||
*/
|
||||
int fluid_midi_file_read_mthd(fluid_midi_file* mf)
|
||||
int
|
||||
fluid_midi_file_read_mthd(fluid_midi_file *mf)
|
||||
{
|
||||
char mthd[15];
|
||||
if (fluid_midi_file_read(mf, mthd, 14) != FLUID_OK) {
|
||||
return FLUID_FAILED;
|
||||
}
|
||||
if ((FLUID_STRNCMP(mthd, "MThd", 4) != 0) || (mthd[7] != 6) || (mthd[9] > 2)) {
|
||||
FLUID_LOG(FLUID_ERR, "Doesn't look like a MIDI file: invalid MThd header");
|
||||
if ((FLUID_STRNCMP(mthd, "MThd", 4) != 0) || (mthd[7] != 6)
|
||||
|| (mthd[9] > 2)) {
|
||||
FLUID_LOG(FLUID_ERR,
|
||||
"Doesn't look like a MIDI file: invalid MThd header");
|
||||
return FLUID_FAILED;
|
||||
}
|
||||
mf->type = mthd[9];
|
||||
|
@ -170,7 +184,8 @@ int fluid_midi_file_read_mthd(fluid_midi_file* mf)
|
|||
/*
|
||||
* fluid_midi_file_load_tracks
|
||||
*/
|
||||
int fluid_midi_file_load_tracks(fluid_midi_file* mf, fluid_player_t* player)
|
||||
int
|
||||
fluid_midi_file_load_tracks(fluid_midi_file *mf, fluid_player_t *player)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < mf->ntracks; i++) {
|
||||
|
@ -184,7 +199,8 @@ int fluid_midi_file_load_tracks(fluid_midi_file* mf, fluid_player_t* player)
|
|||
/*
|
||||
* fluid_isasciistring
|
||||
*/
|
||||
int fluid_isasciistring(char* s)
|
||||
int
|
||||
fluid_isasciistring(char *s)
|
||||
{
|
||||
int i;
|
||||
int len = (int) FLUID_STRLEN(s);
|
||||
|
@ -199,7 +215,8 @@ int fluid_isasciistring(char* s)
|
|||
/*
|
||||
* fluid_getlength
|
||||
*/
|
||||
long fluid_getlength(unsigned char *s)
|
||||
long
|
||||
fluid_getlength(unsigned char *s)
|
||||
{
|
||||
long i = 0;
|
||||
i = s[3] | (s[2] << 8) | (s[1] << 16) | (s[0] << 24);
|
||||
|
@ -209,7 +226,8 @@ long fluid_getlength(unsigned char *s)
|
|||
/*
|
||||
* fluid_midi_file_read_tracklen
|
||||
*/
|
||||
int fluid_midi_file_read_tracklen(fluid_midi_file* mf)
|
||||
int
|
||||
fluid_midi_file_read_tracklen(fluid_midi_file *mf)
|
||||
{
|
||||
unsigned char length[5];
|
||||
if (fluid_midi_file_read(mf, length, 4) != FLUID_OK) {
|
||||
|
@ -224,7 +242,8 @@ int fluid_midi_file_read_tracklen(fluid_midi_file* mf)
|
|||
/*
|
||||
* fluid_midi_file_eot
|
||||
*/
|
||||
int fluid_midi_file_eot(fluid_midi_file* mf)
|
||||
int
|
||||
fluid_midi_file_eot(fluid_midi_file *mf)
|
||||
{
|
||||
#if DEBUG
|
||||
if (mf->trackpos > mf->tracklen) {
|
||||
|
@ -237,7 +256,8 @@ int fluid_midi_file_eot(fluid_midi_file* mf)
|
|||
/*
|
||||
* fluid_midi_file_read_track
|
||||
*/
|
||||
int fluid_midi_file_read_track(fluid_midi_file* mf, fluid_player_t* player, int num)
|
||||
int
|
||||
fluid_midi_file_read_track(fluid_midi_file *mf, fluid_player_t *player, int num)
|
||||
{
|
||||
fluid_track_t *track;
|
||||
unsigned char id[5], length[5];
|
||||
|
@ -253,7 +273,8 @@ int fluid_midi_file_read_track(fluid_midi_file* mf, fluid_player_t* player, int
|
|||
while (!found_track) {
|
||||
|
||||
if (fluid_isasciistring((char *) id) == 0) {
|
||||
FLUID_LOG(FLUID_ERR, "An non-ascii track header found, corrupt file");
|
||||
FLUID_LOG(FLUID_ERR,
|
||||
"An non-ascii track header found, corrupt file");
|
||||
return FLUID_FAILED;
|
||||
|
||||
} else if (strcmp((char *) id, "MTrk") == 0) {
|
||||
|
@ -304,7 +325,8 @@ int fluid_midi_file_read_track(fluid_midi_file* mf, fluid_player_t* player, int
|
|||
/*
|
||||
* fluid_midi_file_read_varlen
|
||||
*/
|
||||
int fluid_midi_file_read_varlen(fluid_midi_file* mf)
|
||||
int
|
||||
fluid_midi_file_read_varlen(fluid_midi_file *mf)
|
||||
{
|
||||
int i;
|
||||
int c;
|
||||
|
@ -333,7 +355,8 @@ int fluid_midi_file_read_varlen(fluid_midi_file* mf)
|
|||
/*
|
||||
* fluid_midi_file_read_event
|
||||
*/
|
||||
int fluid_midi_file_read_event(fluid_midi_file* mf, fluid_track_t* track)
|
||||
int
|
||||
fluid_midi_file_read_event(fluid_midi_file *mf, fluid_track_t *track)
|
||||
{
|
||||
int status;
|
||||
int type;
|
||||
|
@ -382,7 +405,8 @@ int fluid_midi_file_read_event(fluid_midi_file* mf, fluid_track_t* track)
|
|||
}
|
||||
|
||||
if (mf->varlen) {
|
||||
FLUID_LOG(FLUID_DBG, "%s: %d: alloc metadata, len = %d", __FILE__, __LINE__, mf->varlen);
|
||||
FLUID_LOG(FLUID_DBG, "%s: %d: alloc metadata, len = %d", __FILE__,
|
||||
__LINE__, mf->varlen);
|
||||
metadata = FLUID_MALLOC(mf->varlen + 1);
|
||||
|
||||
if (metadata == NULL) {
|
||||
|
@ -436,7 +460,8 @@ int fluid_midi_file_read_event(fluid_midi_file* mf, fluid_track_t* track)
|
|||
if (mf->varlen < 255) {
|
||||
metadata = &static_buf[0];
|
||||
} else {
|
||||
FLUID_LOG(FLUID_DBG, "%s: %d: alloc metadata, len = %d", __FILE__, __LINE__, mf->varlen);
|
||||
FLUID_LOG(FLUID_DBG, "%s: %d: alloc metadata, len = %d", __FILE__,
|
||||
__LINE__, mf->varlen);
|
||||
dyn_buf = FLUID_MALLOC(mf->varlen + 1);
|
||||
if (dyn_buf == NULL) {
|
||||
FLUID_LOG(FLUID_PANIC, "Out of memory");
|
||||
|
@ -446,8 +471,7 @@ int fluid_midi_file_read_event(fluid_midi_file* mf, fluid_track_t* track)
|
|||
}
|
||||
|
||||
/* read the data */
|
||||
if (mf->varlen)
|
||||
{
|
||||
if (mf->varlen) {
|
||||
if (fluid_midi_file_read(mf, metadata, mf->varlen) != FLUID_OK) {
|
||||
if (dyn_buf) {
|
||||
FLUID_FREE(dyn_buf);
|
||||
|
@ -492,7 +516,8 @@ int fluid_midi_file_read_event(fluid_midi_file* mf, fluid_track_t* track)
|
|||
|
||||
case MIDI_SET_TEMPO:
|
||||
if (mf->varlen != 3) {
|
||||
FLUID_LOG(FLUID_ERR, "Invalid length for SetTempo meta event");
|
||||
FLUID_LOG(FLUID_ERR,
|
||||
"Invalid length for SetTempo meta event");
|
||||
result = FLUID_FAILED;
|
||||
break;
|
||||
}
|
||||
|
@ -514,7 +539,8 @@ int fluid_midi_file_read_event(fluid_midi_file* mf, fluid_track_t* track)
|
|||
|
||||
case MIDI_SMPTE_OFFSET:
|
||||
if (mf->varlen != 5) {
|
||||
FLUID_LOG(FLUID_ERR, "Invalid length for SMPTE Offset meta event");
|
||||
FLUID_LOG(FLUID_ERR,
|
||||
"Invalid length for SMPTE Offset meta event");
|
||||
result = FLUID_FAILED;
|
||||
break;
|
||||
}
|
||||
|
@ -522,7 +548,8 @@ int fluid_midi_file_read_event(fluid_midi_file* mf, fluid_track_t* track)
|
|||
|
||||
case MIDI_TIME_SIGNATURE:
|
||||
if (mf->varlen != 4) {
|
||||
FLUID_LOG(FLUID_ERR, "Invalid length for TimeSignature meta event");
|
||||
FLUID_LOG(FLUID_ERR,
|
||||
"Invalid length for TimeSignature meta event");
|
||||
result = FLUID_FAILED;
|
||||
break;
|
||||
}
|
||||
|
@ -531,14 +558,16 @@ int fluid_midi_file_read_event(fluid_midi_file* mf, fluid_track_t* track)
|
|||
clocks = metadata[2];
|
||||
notes = metadata[3];
|
||||
|
||||
FLUID_LOG(FLUID_DBG, "signature=%d/%d, metronome=%d, 32nd-notes=%d",
|
||||
FLUID_LOG(FLUID_DBG,
|
||||
"signature=%d/%d, metronome=%d, 32nd-notes=%d",
|
||||
nominator, denominator, clocks, notes);
|
||||
|
||||
break;
|
||||
|
||||
case MIDI_KEY_SIGNATURE:
|
||||
if (mf->varlen != 2) {
|
||||
FLUID_LOG(FLUID_ERR, "Invalid length for KeySignature meta event");
|
||||
FLUID_LOG(FLUID_ERR,
|
||||
"Invalid length for KeySignature meta event");
|
||||
result = FLUID_FAILED;
|
||||
break;
|
||||
}
|
||||
|
@ -641,7 +670,8 @@ int fluid_midi_file_read_event(fluid_midi_file* mf, fluid_track_t* track)
|
|||
/*
|
||||
* fluid_midi_file_get_division
|
||||
*/
|
||||
int fluid_midi_file_get_division(fluid_midi_file* midifile)
|
||||
int
|
||||
fluid_midi_file_get_division(fluid_midi_file *midifile)
|
||||
{
|
||||
return midifile->division;
|
||||
}
|
||||
|
@ -655,7 +685,8 @@ int fluid_midi_file_get_division(fluid_midi_file* midifile)
|
|||
* Create a MIDI event structure.
|
||||
* @return New MIDI event structure or NULL when out of memory.
|
||||
*/
|
||||
fluid_midi_event_t* new_fluid_midi_event()
|
||||
fluid_midi_event_t *
|
||||
new_fluid_midi_event ()
|
||||
{
|
||||
fluid_midi_event_t* evt;
|
||||
evt = FLUID_NEW(fluid_midi_event_t);
|
||||
|
@ -678,12 +709,12 @@ fluid_midi_event_t* new_fluid_midi_event()
|
|||
* @param evt MIDI event structure
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int delete_fluid_midi_event(fluid_midi_event_t* evt)
|
||||
int
|
||||
delete_fluid_midi_event(fluid_midi_event_t *evt)
|
||||
{
|
||||
fluid_midi_event_t *temp;
|
||||
|
||||
while (evt)
|
||||
{
|
||||
while (evt) {
|
||||
temp = evt->next;
|
||||
|
||||
/* Dynamic SYSEX event? - free (param2 indicates if dynamic) */
|
||||
|
@ -701,7 +732,8 @@ int delete_fluid_midi_event(fluid_midi_event_t* evt)
|
|||
* @param evt MIDI event structure
|
||||
* @return Event type field (MIDI status byte without channel)
|
||||
*/
|
||||
int fluid_midi_event_get_type(fluid_midi_event_t* evt)
|
||||
int
|
||||
fluid_midi_event_get_type(fluid_midi_event_t *evt)
|
||||
{
|
||||
return evt->type;
|
||||
}
|
||||
|
@ -712,7 +744,8 @@ int fluid_midi_event_get_type(fluid_midi_event_t* evt)
|
|||
* @param type Event type field (MIDI status byte without channel)
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int fluid_midi_event_set_type(fluid_midi_event_t* evt, int type)
|
||||
int
|
||||
fluid_midi_event_set_type(fluid_midi_event_t *evt, int type)
|
||||
{
|
||||
evt->type = type;
|
||||
return FLUID_OK;
|
||||
|
@ -723,7 +756,8 @@ int fluid_midi_event_set_type(fluid_midi_event_t* evt, int type)
|
|||
* @param evt MIDI event structure
|
||||
* @return Channel field
|
||||
*/
|
||||
int fluid_midi_event_get_channel(fluid_midi_event_t* evt)
|
||||
int
|
||||
fluid_midi_event_get_channel(fluid_midi_event_t *evt)
|
||||
{
|
||||
return evt->channel;
|
||||
}
|
||||
|
@ -734,7 +768,8 @@ int fluid_midi_event_get_channel(fluid_midi_event_t* evt)
|
|||
* @param chan MIDI channel field
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int fluid_midi_event_set_channel(fluid_midi_event_t* evt, int chan)
|
||||
int
|
||||
fluid_midi_event_set_channel(fluid_midi_event_t *evt, int chan)
|
||||
{
|
||||
evt->channel = chan;
|
||||
return FLUID_OK;
|
||||
|
@ -745,7 +780,8 @@ int fluid_midi_event_set_channel(fluid_midi_event_t* evt, int chan)
|
|||
* @param evt MIDI event structure
|
||||
* @return MIDI note number (0-127)
|
||||
*/
|
||||
int fluid_midi_event_get_key(fluid_midi_event_t* evt)
|
||||
int
|
||||
fluid_midi_event_get_key(fluid_midi_event_t *evt)
|
||||
{
|
||||
return evt->param1;
|
||||
}
|
||||
|
@ -756,7 +792,8 @@ int fluid_midi_event_get_key(fluid_midi_event_t* evt)
|
|||
* @param v MIDI note number (0-127)
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int fluid_midi_event_set_key(fluid_midi_event_t* evt, int v)
|
||||
int
|
||||
fluid_midi_event_set_key(fluid_midi_event_t *evt, int v)
|
||||
{
|
||||
evt->param1 = v;
|
||||
return FLUID_OK;
|
||||
|
@ -767,7 +804,8 @@ int fluid_midi_event_set_key(fluid_midi_event_t* evt, int v)
|
|||
* @param evt MIDI event structure
|
||||
* @return MIDI velocity number (0-127)
|
||||
*/
|
||||
int fluid_midi_event_get_velocity(fluid_midi_event_t* evt)
|
||||
int
|
||||
fluid_midi_event_get_velocity(fluid_midi_event_t *evt)
|
||||
{
|
||||
return evt->param2;
|
||||
}
|
||||
|
@ -778,7 +816,8 @@ int fluid_midi_event_get_velocity(fluid_midi_event_t* evt)
|
|||
* @param v MIDI velocity value
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int fluid_midi_event_set_velocity(fluid_midi_event_t* evt, int v)
|
||||
int
|
||||
fluid_midi_event_set_velocity(fluid_midi_event_t *evt, int v)
|
||||
{
|
||||
evt->param2 = v;
|
||||
return FLUID_OK;
|
||||
|
@ -789,7 +828,8 @@ int fluid_midi_event_set_velocity(fluid_midi_event_t* evt, int v)
|
|||
* @param evt MIDI event structure
|
||||
* @return MIDI control number
|
||||
*/
|
||||
int fluid_midi_event_get_control(fluid_midi_event_t* evt)
|
||||
int
|
||||
fluid_midi_event_get_control(fluid_midi_event_t *evt)
|
||||
{
|
||||
return evt->param1;
|
||||
}
|
||||
|
@ -800,7 +840,8 @@ int fluid_midi_event_get_control(fluid_midi_event_t* evt)
|
|||
* @param v MIDI control number
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int fluid_midi_event_set_control(fluid_midi_event_t* evt, int v)
|
||||
int
|
||||
fluid_midi_event_set_control(fluid_midi_event_t *evt, int v)
|
||||
{
|
||||
evt->param1 = v;
|
||||
return FLUID_OK;
|
||||
|
@ -811,7 +852,8 @@ int fluid_midi_event_set_control(fluid_midi_event_t* evt, int v)
|
|||
* @param evt MIDI event structure
|
||||
* @return Value field
|
||||
*/
|
||||
int fluid_midi_event_get_value(fluid_midi_event_t* evt)
|
||||
int
|
||||
fluid_midi_event_get_value(fluid_midi_event_t *evt)
|
||||
{
|
||||
return evt->param2;
|
||||
}
|
||||
|
@ -822,7 +864,8 @@ int fluid_midi_event_get_value(fluid_midi_event_t* evt)
|
|||
* @param v Value to assign
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int fluid_midi_event_set_value(fluid_midi_event_t* evt, int v)
|
||||
int
|
||||
fluid_midi_event_set_value(fluid_midi_event_t *evt, int v)
|
||||
{
|
||||
evt->param2 = v;
|
||||
return FLUID_OK;
|
||||
|
@ -833,7 +876,8 @@ int fluid_midi_event_set_value(fluid_midi_event_t* evt, int v)
|
|||
* @param evt MIDI event structure
|
||||
* @return MIDI program number (0-127)
|
||||
*/
|
||||
int fluid_midi_event_get_program(fluid_midi_event_t* evt)
|
||||
int
|
||||
fluid_midi_event_get_program(fluid_midi_event_t *evt)
|
||||
{
|
||||
return evt->param1;
|
||||
}
|
||||
|
@ -844,7 +888,8 @@ int fluid_midi_event_get_program(fluid_midi_event_t* evt)
|
|||
* @param val MIDI program number (0-127)
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int fluid_midi_event_set_program(fluid_midi_event_t* evt, int val)
|
||||
int
|
||||
fluid_midi_event_set_program(fluid_midi_event_t *evt, int val)
|
||||
{
|
||||
evt->param1 = val;
|
||||
return FLUID_OK;
|
||||
|
@ -855,7 +900,8 @@ int fluid_midi_event_set_program(fluid_midi_event_t* evt, int val)
|
|||
* @param evt MIDI event structure
|
||||
* @return Pitch value (14 bit value, 0-16383, 8192 is center)
|
||||
*/
|
||||
int fluid_midi_event_get_pitch(fluid_midi_event_t* evt)
|
||||
int
|
||||
fluid_midi_event_get_pitch(fluid_midi_event_t *evt)
|
||||
{
|
||||
return evt->param1;
|
||||
}
|
||||
|
@ -866,7 +912,8 @@ int fluid_midi_event_get_pitch(fluid_midi_event_t* evt)
|
|||
* @param val Pitch value (14 bit value, 0-16383, 8192 is center)
|
||||
* @return Always returns FLUID_OK
|
||||
*/
|
||||
int fluid_midi_event_set_pitch(fluid_midi_event_t* evt, int val)
|
||||
int
|
||||
fluid_midi_event_set_pitch(fluid_midi_event_t *evt, int val)
|
||||
{
|
||||
evt->param1 = val;
|
||||
return FLUID_OK;
|
||||
|
@ -902,7 +949,8 @@ fluid_midi_event_set_sysex(fluid_midi_event_t* evt, void *data, int size, int dy
|
|||
/*
|
||||
* new_fluid_track
|
||||
*/
|
||||
fluid_track_t* new_fluid_track(int num)
|
||||
fluid_track_t *
|
||||
new_fluid_track(int num)
|
||||
{
|
||||
fluid_track_t *track;
|
||||
track = FLUID_NEW(fluid_track_t);
|
||||
|
@ -921,7 +969,8 @@ fluid_track_t* new_fluid_track(int num)
|
|||
/*
|
||||
* delete_fluid_track
|
||||
*/
|
||||
int delete_fluid_track(fluid_track_t* track)
|
||||
int
|
||||
delete_fluid_track(fluid_track_t *track)
|
||||
{
|
||||
if (track->name != NULL) {
|
||||
FLUID_FREE(track->name);
|
||||
|
@ -936,7 +985,8 @@ int delete_fluid_track(fluid_track_t* track)
|
|||
/*
|
||||
* fluid_track_set_name
|
||||
*/
|
||||
int fluid_track_set_name(fluid_track_t* track, char* name)
|
||||
int
|
||||
fluid_track_set_name(fluid_track_t *track, char *name)
|
||||
{
|
||||
int len;
|
||||
if (track->name != NULL) {
|
||||
|
@ -959,7 +1009,8 @@ int fluid_track_set_name(fluid_track_t* track, char* name)
|
|||
/*
|
||||
* fluid_track_get_name
|
||||
*/
|
||||
char* fluid_track_get_name(fluid_track_t* track)
|
||||
char *
|
||||
fluid_track_get_name(fluid_track_t *track)
|
||||
{
|
||||
return track->name;
|
||||
}
|
||||
|
@ -967,7 +1018,8 @@ char* fluid_track_get_name(fluid_track_t* track)
|
|||
/*
|
||||
* fluid_track_get_duration
|
||||
*/
|
||||
int fluid_track_get_duration(fluid_track_t* track)
|
||||
int
|
||||
fluid_track_get_duration(fluid_track_t *track)
|
||||
{
|
||||
int time = 0;
|
||||
fluid_midi_event_t *evt = track->first;
|
||||
|
@ -981,7 +1033,8 @@ int fluid_track_get_duration(fluid_track_t* track)
|
|||
/*
|
||||
* fluid_track_count_events
|
||||
*/
|
||||
int fluid_track_count_events(fluid_track_t* track, int* on, int* off)
|
||||
int
|
||||
fluid_track_count_events(fluid_track_t *track, int *on, int *off)
|
||||
{
|
||||
fluid_midi_event_t *evt = track->first;
|
||||
while (evt != NULL) {
|
||||
|
@ -998,7 +1051,8 @@ int fluid_track_count_events(fluid_track_t* track, int* on, int* off)
|
|||
/*
|
||||
* fluid_track_add_event
|
||||
*/
|
||||
int fluid_track_add_event(fluid_track_t* track, fluid_midi_event_t* evt)
|
||||
int
|
||||
fluid_track_add_event(fluid_track_t *track, fluid_midi_event_t *evt)
|
||||
{
|
||||
evt->next = NULL;
|
||||
if (track->first == NULL) {
|
||||
|
@ -1015,7 +1069,8 @@ int fluid_track_add_event(fluid_track_t* track, fluid_midi_event_t* evt)
|
|||
/*
|
||||
* fluid_track_first_event
|
||||
*/
|
||||
fluid_midi_event_t* fluid_track_first_event(fluid_track_t* track)
|
||||
fluid_midi_event_t *
|
||||
fluid_track_first_event(fluid_track_t *track)
|
||||
{
|
||||
track->cur = track->first;
|
||||
return track->cur;
|
||||
|
@ -1024,7 +1079,8 @@ fluid_midi_event_t* fluid_track_first_event(fluid_track_t* track)
|
|||
/*
|
||||
* fluid_track_next_event
|
||||
*/
|
||||
fluid_midi_event_t* fluid_track_next_event(fluid_track_t* track)
|
||||
fluid_midi_event_t *
|
||||
fluid_track_next_event(fluid_track_t *track)
|
||||
{
|
||||
if (track->cur != NULL) {
|
||||
track->cur = track->cur->next;
|
||||
|
@ -1073,12 +1129,12 @@ fluid_track_send_events(fluid_track_t* track,
|
|||
return status;
|
||||
}
|
||||
|
||||
|
||||
track->ticks += event->dtime;
|
||||
|
||||
if (event->type != MIDI_SET_TEMPO)
|
||||
fluid_synth_handle_midi_event(synth, event);
|
||||
else if (player) fluid_player_set_midi_tempo (player, event->param1);
|
||||
else if (player)
|
||||
fluid_player_set_midi_tempo(player, event->param1);
|
||||
|
||||
fluid_track_next_event(track);
|
||||
|
||||
|
@ -1096,7 +1152,8 @@ fluid_track_send_events(fluid_track_t* track,
|
|||
* @param synth Fluid synthesizer instance to create player for
|
||||
* @return New MIDI player instance or NULL on error (out of memory)
|
||||
*/
|
||||
fluid_player_t* new_fluid_player(fluid_synth_t* synth)
|
||||
fluid_player_t *
|
||||
new_fluid_player(fluid_synth_t *synth)
|
||||
{
|
||||
int i;
|
||||
fluid_player_t *player;
|
||||
|
@ -1123,8 +1180,8 @@ fluid_player_t* new_fluid_player(fluid_synth_t* synth)
|
|||
player->cur_msec = 0;
|
||||
player->cur_ticks = 0;
|
||||
|
||||
player->use_system_timer =
|
||||
fluid_settings_str_equal(synth->settings, "player.timing-source", "system");
|
||||
player->use_system_timer = fluid_settings_str_equal(synth->settings,
|
||||
"player.timing-source", "system");
|
||||
|
||||
fluid_settings_getint(synth->settings, "player.reset-synth", &i);
|
||||
player->reset_synth_between_songs = i;
|
||||
|
@ -1137,7 +1194,8 @@ fluid_player_t* new_fluid_player(fluid_synth_t* synth)
|
|||
* @param player MIDI player instance
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int delete_fluid_player(fluid_player_t* player)
|
||||
int
|
||||
delete_fluid_player(fluid_player_t *player)
|
||||
{
|
||||
fluid_list_t *q;
|
||||
|
||||
|
@ -1161,11 +1219,13 @@ int delete_fluid_player(fluid_player_t* player)
|
|||
/**
|
||||
* Registers settings related to the MIDI player
|
||||
*/
|
||||
void fluid_player_settings(fluid_settings_t* settings)
|
||||
void
|
||||
fluid_player_settings(fluid_settings_t *settings)
|
||||
{
|
||||
/* player.timing-source can be either "system" (use system timer)
|
||||
or "sample" (use timer based on number of written samples) */
|
||||
fluid_settings_register_str(settings, "player.timing-source", "sample", 0, NULL, NULL);
|
||||
fluid_settings_register_str(settings, "player.timing-source", "sample", 0,
|
||||
NULL, NULL);
|
||||
fluid_settings_add_option(settings, "player.timing-source", "sample");
|
||||
fluid_settings_add_option(settings, "player.timing-source", "system");
|
||||
|
||||
|
@ -1175,7 +1235,8 @@ void fluid_player_settings(fluid_settings_t* settings)
|
|||
}
|
||||
|
||||
|
||||
int fluid_player_reset(fluid_player_t* player)
|
||||
int
|
||||
fluid_player_reset(fluid_player_t *player)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
@ -1199,7 +1260,8 @@ int fluid_player_reset(fluid_player_t* player)
|
|||
/*
|
||||
* fluid_player_add_track
|
||||
*/
|
||||
int fluid_player_add_track(fluid_player_t* player, fluid_track_t* track)
|
||||
int
|
||||
fluid_player_add_track(fluid_player_t *player, fluid_track_t *track)
|
||||
{
|
||||
if (player->ntracks < MAX_NUMBER_OF_TRACKS) {
|
||||
player->track[player->ntracks++] = track;
|
||||
|
@ -1212,7 +1274,8 @@ int fluid_player_add_track(fluid_player_t* player, fluid_track_t* track)
|
|||
/*
|
||||
* fluid_player_count_tracks
|
||||
*/
|
||||
int fluid_player_count_tracks(fluid_player_t* player)
|
||||
int
|
||||
fluid_player_count_tracks(fluid_player_t *player)
|
||||
{
|
||||
return player->ntracks;
|
||||
}
|
||||
|
@ -1220,7 +1283,8 @@ int fluid_player_count_tracks(fluid_player_t* player)
|
|||
/*
|
||||
* fluid_player_get_track
|
||||
*/
|
||||
fluid_track_t* fluid_player_get_track(fluid_player_t* player, int i)
|
||||
fluid_track_t *
|
||||
fluid_player_get_track(fluid_player_t *player, int i)
|
||||
{
|
||||
if ((i >= 0) && (i < MAX_NUMBER_OF_TRACKS)) {
|
||||
return player->track[i];
|
||||
|
@ -1246,7 +1310,8 @@ fluid_player_add(fluid_player_t* player, const char* midifile)
|
|||
/*
|
||||
* fluid_player_load
|
||||
*/
|
||||
int fluid_player_load(fluid_player_t* player, char *filename)
|
||||
int
|
||||
fluid_player_load(fluid_player_t *player, char *filename)
|
||||
{
|
||||
fluid_midi_file *midifile;
|
||||
|
||||
|
@ -1265,7 +1330,8 @@ int fluid_player_load(fluid_player_t* player, char *filename)
|
|||
return FLUID_OK;
|
||||
}
|
||||
|
||||
void fluid_player_advancefile(fluid_player_t* player)
|
||||
void
|
||||
fluid_player_advancefile(fluid_player_t *player)
|
||||
{
|
||||
if (player->playlist == NULL) {
|
||||
return; /* No files to play */
|
||||
|
@ -1284,7 +1350,8 @@ void fluid_player_advancefile(fluid_player_t* player)
|
|||
}
|
||||
}
|
||||
|
||||
void fluid_player_playlist_load(fluid_player_t* player, unsigned int msec)
|
||||
void
|
||||
fluid_player_playlist_load(fluid_player_t *player, unsigned int msec)
|
||||
{
|
||||
char *current_filename;
|
||||
int i;
|
||||
|
@ -1299,7 +1366,8 @@ void fluid_player_playlist_load(fluid_player_t* player, unsigned int msec)
|
|||
|
||||
fluid_player_reset(player);
|
||||
current_filename = (char *) player->currentfile->data;
|
||||
FLUID_LOG(FLUID_DBG, "%s: %d: Loading midifile %s", __FILE__, __LINE__, current_filename);
|
||||
FLUID_LOG(FLUID_DBG, "%s: %d: Loading midifile %s", __FILE__, __LINE__,
|
||||
current_filename);
|
||||
} while (fluid_player_load(player, current_filename) != FLUID_OK);
|
||||
|
||||
/* Successfully loaded midi file */
|
||||
|
@ -1324,7 +1392,8 @@ void fluid_player_playlist_load(fluid_player_t* player, unsigned int msec)
|
|||
/*
|
||||
* fluid_player_callback
|
||||
*/
|
||||
int fluid_player_callback(void* data, unsigned int msec)
|
||||
int
|
||||
fluid_player_callback(void *data, unsigned int msec)
|
||||
{
|
||||
int i;
|
||||
int loadnextfile;
|
||||
|
@ -1345,21 +1414,23 @@ int fluid_player_callback(void* data, unsigned int msec)
|
|||
}
|
||||
|
||||
player->cur_msec = msec;
|
||||
player->cur_ticks = (player->start_ticks +
|
||||
(int) ((double) (player->cur_msec - player->start_msec) / player->deltatime));
|
||||
player->cur_ticks = (player->start_ticks
|
||||
+ (int) ((double) (player->cur_msec - player->start_msec)
|
||||
/ player->deltatime));
|
||||
|
||||
for (i = 0; i < player->ntracks; i++) {
|
||||
if (!fluid_track_eot(player->track[i])) {
|
||||
status = FLUID_PLAYER_PLAYING;
|
||||
if (fluid_track_send_events(player->track[i], synth, player, player->cur_ticks) != FLUID_OK) {
|
||||
if (fluid_track_send_events(player->track[i], synth, player,
|
||||
player->cur_ticks) != FLUID_OK) {
|
||||
/* */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (status == FLUID_PLAYER_DONE) {
|
||||
FLUID_LOG(FLUID_DBG, "%s: %d: Duration=%.3f sec",
|
||||
__FILE__, __LINE__, (msec - player->begin_msec) / 1000.0);
|
||||
FLUID_LOG(FLUID_DBG, "%s: %d: Duration=%.3f sec", __FILE__,
|
||||
__LINE__, (msec - player->begin_msec) / 1000.0);
|
||||
loadnextfile = 1;
|
||||
}
|
||||
} while (loadnextfile);
|
||||
|
@ -1374,7 +1445,8 @@ int fluid_player_callback(void* data, unsigned int msec)
|
|||
* @param player MIDI player instance
|
||||
* @return #FLUID_OK on success, #FLUID_FAILED otherwise
|
||||
*/
|
||||
int fluid_player_play(fluid_player_t* player)
|
||||
int
|
||||
fluid_player_play(fluid_player_t *player)
|
||||
{
|
||||
if (player->status == FLUID_PLAYER_PLAYING) {
|
||||
return FLUID_OK;
|
||||
|
@ -1387,14 +1459,14 @@ int fluid_player_play(fluid_player_t* player)
|
|||
player->status = FLUID_PLAYER_PLAYING;
|
||||
|
||||
if (player->use_system_timer) {
|
||||
player->system_timer = new_fluid_timer((int) player->deltatime, fluid_player_callback,
|
||||
(void*) player, TRUE, FALSE, TRUE);
|
||||
player->system_timer = new_fluid_timer((int) player->deltatime,
|
||||
fluid_player_callback, (void *) player, TRUE, FALSE, TRUE);
|
||||
if (player->system_timer == NULL) {
|
||||
return FLUID_FAILED;
|
||||
}
|
||||
} else {
|
||||
player->sample_timer = new_fluid_sample_timer(player->synth, fluid_player_callback,
|
||||
(void*) player);
|
||||
player->sample_timer = new_fluid_sample_timer(player->synth,
|
||||
fluid_player_callback, (void *) player);
|
||||
|
||||
if (player->sample_timer == NULL) {
|
||||
return FLUID_FAILED;
|
||||
|
@ -1408,7 +1480,8 @@ int fluid_player_play(fluid_player_t* player)
|
|||
* @param player MIDI player instance
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int fluid_player_stop(fluid_player_t* player)
|
||||
int
|
||||
fluid_player_stop(fluid_player_t *player)
|
||||
{
|
||||
if (player->system_timer != NULL) {
|
||||
delete_fluid_timer(player->system_timer);
|
||||
|
@ -1463,7 +1536,8 @@ int fluid_player_set_midi_tempo(fluid_player_t* player, int tempo)
|
|||
player->start_msec = player->cur_msec;
|
||||
player->start_ticks = player->cur_ticks;
|
||||
|
||||
FLUID_LOG(FLUID_DBG,"tempo=%d, tick time=%f msec, cur time=%d msec, cur tick=%d",
|
||||
FLUID_LOG(FLUID_DBG,
|
||||
"tempo=%d, tick time=%f msec, cur time=%d msec, cur tick=%d",
|
||||
tempo, player->deltatime, player->cur_msec, player->cur_ticks);
|
||||
|
||||
return FLUID_OK;
|
||||
|
@ -1475,7 +1549,8 @@ int fluid_player_set_midi_tempo(fluid_player_t* player, int tempo)
|
|||
* @param bpm Tempo in beats per minute
|
||||
* @return Always returns #FLUID_OK
|
||||
*/
|
||||
int fluid_player_set_bpm(fluid_player_t* player, int bpm)
|
||||
int
|
||||
fluid_player_set_bpm(fluid_player_t *player, int bpm)
|
||||
{
|
||||
return fluid_player_set_midi_tempo(player, (int) ((double) 60 * 1e6 / bpm));
|
||||
}
|
||||
|
@ -1485,7 +1560,8 @@ int fluid_player_set_bpm(fluid_player_t* player, int bpm)
|
|||
* @param player MIDI player instance
|
||||
* @return #FLUID_OK on success, #FLUID_FAILED otherwise
|
||||
*/
|
||||
int fluid_player_join(fluid_player_t* player)
|
||||
int
|
||||
fluid_player_join(fluid_player_t *player)
|
||||
{
|
||||
if (player->system_timer) {
|
||||
return fluid_timer_join(player->system_timer);
|
||||
|
@ -1510,7 +1586,8 @@ int fluid_player_join(fluid_player_t* player)
|
|||
/*
|
||||
* new_fluid_midi_parser
|
||||
*/
|
||||
fluid_midi_parser_t* new_fluid_midi_parser()
|
||||
fluid_midi_parser_t *
|
||||
new_fluid_midi_parser ()
|
||||
{
|
||||
fluid_midi_parser_t *parser;
|
||||
parser = FLUID_NEW(fluid_midi_parser_t);
|
||||
|
@ -1525,7 +1602,8 @@ fluid_midi_parser_t* new_fluid_midi_parser()
|
|||
/*
|
||||
* delete_fluid_midi_parser
|
||||
*/
|
||||
int delete_fluid_midi_parser(fluid_midi_parser_t* parser)
|
||||
int
|
||||
delete_fluid_midi_parser(fluid_midi_parser_t *parser)
|
||||
{
|
||||
FLUID_FREE(parser);
|
||||
return FLUID_OK;
|
||||
|
@ -1545,10 +1623,8 @@ fluid_midi_parser_parse(fluid_midi_parser_t* parser, unsigned char c)
|
|||
|
||||
/* Real-time messages (0xF8-0xFF) can occur anywhere, even in the middle
|
||||
* of another message. */
|
||||
if (c >= 0xF8)
|
||||
{
|
||||
if (c == MIDI_SYSTEM_RESET)
|
||||
{
|
||||
if (c >= 0xF8) {
|
||||
if (c == MIDI_SYSTEM_RESET) {
|
||||
parser->event.type = c;
|
||||
parser->status = 0; /* clear the status */
|
||||
return &parser->event;
|
||||
|
@ -1558,15 +1634,14 @@ fluid_midi_parser_parse(fluid_midi_parser_t* parser, unsigned char c)
|
|||
}
|
||||
|
||||
/* Status byte? - If previous message not yet complete, it is discarded (re-sync). */
|
||||
if (c & 0x80)
|
||||
{
|
||||
if (c & 0x80) {
|
||||
/* Any status byte terminates SYSEX messages (not just 0xF7) */
|
||||
if (parser->status == MIDI_SYSEX && parser->nr_bytes > 0)
|
||||
{
|
||||
if (parser->status == MIDI_SYSEX && parser->nr_bytes > 0) {
|
||||
event = &parser->event;
|
||||
fluid_midi_event_set_sysex (event, parser->data, parser->nr_bytes, FALSE);
|
||||
}
|
||||
else event = NULL;
|
||||
fluid_midi_event_set_sysex(event, parser->data, parser->nr_bytes,
|
||||
FALSE);
|
||||
} else
|
||||
event = NULL;
|
||||
|
||||
if (c < 0xF0) /* Voice category message? */
|
||||
{
|
||||
|
@ -1574,31 +1649,27 @@ fluid_midi_parser_parse(fluid_midi_parser_t* parser, unsigned char c)
|
|||
parser->status = c & 0xF0;
|
||||
|
||||
/* The event consumes x bytes of data... (subtract 1 for the status byte) */
|
||||
parser->nr_bytes_total = fluid_midi_event_length (parser->status) - 1;
|
||||
parser->nr_bytes_total = fluid_midi_event_length(parser->status)
|
||||
- 1;
|
||||
|
||||
parser->nr_bytes = 0; /* 0 bytes read so far */
|
||||
}
|
||||
else if (c == MIDI_SYSEX)
|
||||
{
|
||||
} else if (c == MIDI_SYSEX) {
|
||||
parser->status = MIDI_SYSEX;
|
||||
parser->nr_bytes = 0;
|
||||
}
|
||||
else parser->status = 0; /* Discard other system messages (0xF1-0xF7) */
|
||||
} else
|
||||
parser->status = 0; /* Discard other system messages (0xF1-0xF7) */
|
||||
|
||||
return event; /* Return SYSEX event or NULL */
|
||||
}
|
||||
|
||||
|
||||
/* Data/parameter byte */
|
||||
|
||||
|
||||
/* Discard data bytes for events we don't care about */
|
||||
if (parser->status == 0)
|
||||
return NULL;
|
||||
|
||||
/* Max data size exceeded? (SYSEX messages only really) */
|
||||
if (parser->nr_bytes == FLUID_MIDI_PARSER_MAX_DATA_SIZE)
|
||||
{
|
||||
if (parser->nr_bytes == FLUID_MIDI_PARSER_MAX_DATA_SIZE) {
|
||||
parser->status = 0; /* Discard the rest of the message */
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1616,8 +1687,7 @@ fluid_midi_parser_parse(fluid_midi_parser_t* parser, unsigned char c)
|
|||
parser->event.channel = parser->channel;
|
||||
parser->nr_bytes = 0; /* Reset data size, in case there are additional running status messages */
|
||||
|
||||
switch (parser->status)
|
||||
{
|
||||
switch (parser->status) {
|
||||
case NOTE_OFF:
|
||||
case NOTE_ON:
|
||||
case KEY_PRESSURE:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue