libfluidsynth  2.3.5
Playing a MIDI file from memory

FluidSynth can be also play MIDI files directly from a buffer in memory. If you need to play a file from a stream (such as stdin, a network, or a high-level file interface), you can load the entire file into a buffer first, and then use this approach. Use the same technique as above, but rather than calling fluid_player_add(), load it into memory and call fluid_player_add_mem() instead. Once you have passed a buffer to fluid_player_add_mem(), it is copied, so you may use it again or free it immediately (it is your responsibility to free it if you allocated it).

#include <stdlib.h>
#include <string.h>
#include <fluidsynth.h>
/* An example midi file */
const char MIDIFILE[] = {
0x4d, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06,
0x00, 0x01, 0x00, 0x01, 0x01, 0xe0, 0x4d, 0x54,
0x72, 0x6b, 0x00, 0x00, 0x00, 0x20, 0x00, 0x90,
0x3c, 0x64, 0x87, 0x40, 0x80, 0x3c, 0x7f, 0x00,
0x90, 0x43, 0x64, 0x87, 0x40, 0x80, 0x43, 0x7f,
0x00, 0x90, 0x48, 0x64, 0x87, 0x40, 0x80, 0x48,
0x7f, 0x83, 0x60, 0xff, 0x2f, 0x00
};
int main(int argc, char** argv)
{
int i;
void* buffer;
size_t buffer_len;
fluid_settings_t* settings;
fluid_synth_t* synth;
fluid_player_t* player;
settings = new_fluid_settings();
synth = new_fluid_synth(settings);
player = new_fluid_player(synth);
adriver = new_fluid_audio_driver(settings, synth);
/* process command line arguments */
for (i = 1; i < argc; i++) {
if (fluid_is_soundfont(argv[i])) {
fluid_synth_sfload(synth, argv[1], 1);
}
}
/* queue up the in-memory midi file */
fluid_player_add_mem(player, MIDIFILE, sizeof(MIDIFILE));
/* play the midi file */
/* wait for playback termination */
/* cleanup */
return 0;
}
struct _fluid_hashtable_t fluid_settings_t
Configuration settings instance.
Definition: types.h:38
struct _fluid_audio_driver_t fluid_audio_driver_t
Audio driver instance.
Definition: types.h:46
struct _fluid_synth_t fluid_synth_t
Synthesizer instance.
Definition: types.h:39
struct _fluid_player_t fluid_player_t
MIDI player instance.
Definition: types.h:48
fluid_audio_driver_t * new_fluid_audio_driver(fluid_settings_t *settings, fluid_synth_t *synth)
Create a new audio driver.
Definition: fluid_adriver.c:330
void delete_fluid_audio_driver(fluid_audio_driver_t *driver)
Deletes an audio driver instance.
Definition: fluid_adriver.c:423
int fluid_player_add_mem(fluid_player_t *player, const void *buffer, size_t len)
Add a MIDI file to a player queue, from a buffer in memory.
Definition: fluid_midi.c:1933
int fluid_player_join(fluid_player_t *player)
Wait for a MIDI player until the playback has been stopped.
Definition: fluid_midi.c:2578
int fluid_player_play(fluid_player_t *player)
Activates play mode for a MIDI player if not already playing.
Definition: fluid_midi.c:2283
void delete_fluid_player(fluid_player_t *player)
Delete a MIDI player instance.
Definition: fluid_midi.c:1757
fluid_player_t * new_fluid_player(fluid_synth_t *synth)
Create a new MIDI player.
Definition: fluid_midi.c:1667
int fluid_is_soundfont(const char *filename)
Check if a file is a SoundFont file.
Definition: fluid_sffile.c:263
fluid_settings_t * new_fluid_settings(void)
Create a new settings object.
Definition: fluid_settings.c:262
void delete_fluid_settings(fluid_settings_t *settings)
Delete the provided settings object.
Definition: fluid_settings.c:286
int fluid_synth_sfload(fluid_synth_t *synth, const char *filename, int reset_presets)
Load a SoundFont file (filename is interpreted by SoundFont loaders).
Definition: fluid_synth.c:5344
fluid_synth_t * new_fluid_synth(fluid_settings_t *settings)
Create new FluidSynth instance.
Definition: fluid_synth.c:628
void delete_fluid_synth(fluid_synth_t *synth)
Delete a FluidSynth instance.
Definition: fluid_synth.c:1023