Skip to content

MIDI Input

MIDI Input Subsystem.

There are multiple ways to send MIDI events to the synthesizer. They can come from MIDI files, from external MIDI sequencers or raw MIDI event sources, can be modified via MIDI routers and also generated manually.

The interface connecting all sources and sinks of MIDI events in libfluidsynth is handle_midi_event_func_t.

Subgroups

Types

handle_midi_event_func_t

typedef int(* handle_midi_event_func_t) (void *data, fluid_midi_event_t *event);

Generic callback function for MIDI event handler.

Parameters:

Name Description
data User defined data pointer
event The MIDI event

Returns: Should return FLUID_OK on success, FLUID_FAILED otherwise

This callback is used to pass MIDI events - from MIDI File Player, MIDI Router or MIDI Driver - to MIDI Router via fluid_midi_router_handle_midi_event() - or to Synthesizer via fluid_synth_handle_midi_event().

Additionally, there is a translation layer to pass MIDI events to a MIDI Sequencer via fluid_sequencer_add_midi_event_to_buffer().

handle_midi_tick_func_t

typedef int(* handle_midi_tick_func_t) (void *data, int tick);

Generic callback function fired once by MIDI tick change.

Parameters:

Name Description
data User defined data pointer
tick The current (zero-based) tick, which triggered the callback

Returns: Should return FLUID_OK on success, FLUID_FAILED otherwise

This callback is fired at a constant rate depending on the current BPM and PPQ. e.g. for PPQ = 192 and BPM = 140 the callback is fired 192 * 140 times per minute (448/sec).

It can be used to sync external elements with the beat, or stop / loop the song on a given tick. Ticks being BPM-dependent, you can manipulate values such as bars or beats, without having to care about BPM.

For example, this callback loops the song whenever it reaches the 5th bar :

int handle_tick(void *data, int tick)
{
    fluid_player_t *player = (fluid_player_t *)data;
    int ppq = 192; // From MIDI header
    int beatsPerBar = 4; // From the song's time signature
    int loopBar = 5;
    int loopTick = (loopBar - 1) * ppq * beatsPerBar;

    if (tick == loopTick)
    {
        return fluid_player_seek(player, 0);
    }

    return FLUID_OK;
}

Functions

fluid_synth_handle_midi_event()

int fluid_synth_handle_midi_event(void *data, fluid_midi_event_t *event)

Handle MIDI event from MIDI router, used as a callback function.

Parameters:

Name Description
data FluidSynth instance
event MIDI event to handle

Returns: FLUID_OK on success, FLUID_FAILED otherwise