FluidSynth's sequencer can be used to play MIDI events in a more flexible way than using the MIDI file player, which expects the events to be stored as Standard MIDI Files. Using the sequencer, you can provide the events one by one, with an optional timestamp for scheduling.
The following example uses the fluidsynth sequencer to implement a sort of music box. FluidSynth internal clock is used to schedule repetitive sequences of notes. The next sequence is scheduled on advance before the end of the current one, using a timer event that triggers a callback function. The scheduling times are always absolute values, to avoid slippage.
#include "fluidsynth.h"
short synthSeqID, mySeqID;
unsigned int now;
unsigned int seqduration;
void createsynth()
{
seqduration = 1000;
}
void deletesynth()
{
}
void loadsoundfont()
{
int fluid_res;
}
void sendnoteon(int chan, short key, unsigned int date)
{
int fluid_res;
}
void schedule_next_callback()
{
int fluid_res;
unsigned int callbackdate = now + seqduration/2;
}
void schedule_next_sequence() {
now = now + seqduration;
sendnoteon(0, 60, now + seqduration/2);
sendnoteon(0, 60, now + seqduration);
sendnoteon(1, 45, now + seqduration/10);
sendnoteon(1, 50, now + 4*seqduration/10);
sendnoteon(1, 55, now + 8*seqduration/10);
schedule_next_callback();
}
schedule_next_sequence();
}
int main(void) {
createsynth();
loadsoundfont();
schedule_next_sequence();
sleep(100000);
deletesynth();
return 0;
}
struct _fluid_sequencer_t fluid_sequencer_t
Sequencer instance.
Definition: types.h:56
struct _fluid_hashtable_t fluid_settings_t
Configuration settings instance.
Definition: types.h:37
struct _fluid_audio_driver_t fluid_audio_driver_t
Audio driver instance.
Definition: types.h:45
struct _fluid_event_t fluid_event_t
Sequencer event.
Definition: types.h:55
struct _fluid_synth_t fluid_synth_t
Synthesizer instance.
Definition: types.h:38
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:349
void delete_fluid_audio_driver(fluid_audio_driver_t *driver)
Deletes an audio driver instance.
Definition: fluid_adriver.c:442
void fluid_event_set_source(fluid_event_t *evt, fluid_seq_id_t src)
Set source of a sequencer event.
Definition: fluid_event.c:111
void fluid_event_set_dest(fluid_event_t *evt, fluid_seq_id_t dest)
Set destination of this sequencer event, i.e.
Definition: fluid_event.c:122
void fluid_event_noteon(fluid_event_t *evt, int channel, short key, short vel)
Set a sequencer event to be a note on event.
Definition: fluid_event.c:149
fluid_event_t * new_fluid_event(void)
Create a new sequencer event structure.
Definition: fluid_event.c:58
void fluid_event_timer(fluid_event_t *evt, void *data)
Set a sequencer event to be a timer event.
Definition: fluid_event.c:133
void delete_fluid_event(fluid_event_t *evt)
Delete a sequencer event structure.
Definition: fluid_event.c:80
fluid_seq_id_t fluid_sequencer_register_client(fluid_sequencer_t *seq, const char *name, fluid_event_callback_t callback, void *data)
Register a sequencer client.
Definition: fluid_seq.c:209
fluid_sequencer_t * new_fluid_sequencer2(int use_system_timer)
Create a new sequencer object.
Definition: fluid_seq.c:112
int fluid_sequencer_send_at(fluid_sequencer_t *seq, fluid_event_t *evt, unsigned int time, int absolute)
Schedule an event for sending at a later time.
Definition: fluid_seq.c:475
fluid_seq_id_t fluid_sequencer_register_fluidsynth(fluid_sequencer_t *seq, fluid_synth_t *synth)
Registers a synthesizer as a destination client of the given sequencer.
Definition: fluid_seqbind.c:110
void delete_fluid_sequencer(fluid_sequencer_t *seq)
Free a sequencer object.
Definition: fluid_seq.c:156
unsigned int fluid_sequencer_get_tick(fluid_sequencer_t *seq)
Get the current tick of the sequencer scaled by the time scale currently set.
Definition: fluid_seq.c:546
fluid_settings_t * new_fluid_settings(void)
Create a new settings object.
Definition: fluid_settings.c:261
int fluid_settings_setint(fluid_settings_t *settings, const char *name, int val)
Set an integer value for a setting.
Definition: fluid_settings.c:1533
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:5439
fluid_synth_t * new_fluid_synth(fluid_settings_t *settings)
Create new FluidSynth instance.
Definition: fluid_synth.c:644
void delete_fluid_synth(fluid_synth_t *synth)
Delete a FluidSynth instance.
Definition: fluid_synth.c:1043