Esempio n. 1
0
/**
 * Open a MIDI file and return a new MIDI file handle.
 * @internal
 * @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* mf;

	mf = FLUID_NEW(fluid_midi_file);
	if (mf == NULL) {
		FLUID_LOG(FLUID_ERR, "Out of memory");
		return NULL;
	}
	FLUID_MEMSET(mf, 0, sizeof(fluid_midi_file));

	mf->c = -1;
	mf->running_status = -1;
	mf->fp = FLUID_FOPEN(filename, "rb");

	if (mf->fp == NULL) {
		FLUID_LOG(FLUID_ERR, "Couldn't open the MIDI file");
		FLUID_FREE(mf);
		return NULL;
	}

	if (fluid_midi_file_read_mthd(mf) != FLUID_OK) {
		FLUID_FREE(mf);
		return NULL;
	}
	return mf;
}
Esempio n. 2
0
/*
 * fluid_player_load
 */
int
fluid_player_load(fluid_player_t *player, fluid_playlist_item *item)
{
    fluid_midi_file *midifile;
    char* buffer;
    size_t buffer_length;
    int buffer_owned;

    if (item->filename != NULL)
    {
        fluid_file fp;
        /* This file is specified by filename; load the file from disk */
        FLUID_LOG(FLUID_DBG, "%s: %d: Loading midifile %s", __FILE__, __LINE__,
                item->filename);
        /* Read the entire contents of the file into the buffer */
        fp = FLUID_FOPEN(item->filename, "rb");
        if (fp == NULL) {
            FLUID_LOG(FLUID_ERR, "Couldn't open the MIDI file");
            return FLUID_FAILED;
        }
        buffer = fluid_file_read_full(fp, &buffer_length);
        if (buffer == NULL)
        {
            FLUID_FCLOSE(fp);
            return FLUID_FAILED;
        }
        buffer_owned = 1;
        FLUID_FCLOSE(fp);
    }
    else
    {
        /* This file is specified by a pre-loaded buffer; load from memory */
        FLUID_LOG(FLUID_DBG, "%s: %d: Loading midifile from memory (%p)",
                __FILE__, __LINE__, item->buffer);
        buffer = (char *) item->buffer;
        buffer_length = item->buffer_len;
        /* Do not free the buffer (it is owned by the playlist) */
        buffer_owned = 0;
    }

    midifile = new_fluid_midi_file(buffer, buffer_length);
    if (midifile == NULL) {
        if (buffer_owned) {
            FLUID_FREE(buffer);
        }
        return FLUID_FAILED;
    }
    player->division = fluid_midi_file_get_division(midifile);
    fluid_player_set_midi_tempo(player, player->miditempo); // Update deltatime
    /*FLUID_LOG(FLUID_DBG, "quarter note division=%d\n", player->division); */

    if (fluid_midi_file_load_tracks(midifile, player) != FLUID_OK) {
        if (buffer_owned) {
            FLUID_FREE(buffer);
        }
        delete_fluid_midi_file(midifile);
        return FLUID_FAILED;
    }
    delete_fluid_midi_file(midifile);
    if (buffer_owned) {
        FLUID_FREE(buffer);
    }
    return FLUID_OK;
}