libfluidsynth  1.1.11
fluidsynth_metronome.c

Example of a simple metronome using the MIDI sequencer API

/* FluidSynth Metronome - Sequencer API example
*
* This code is in the public domain.
*
* To compile:
* gcc -o fluidsynth_metronome -lfluidsynth fluidsynth_metronome.c
*
* To run:
* fluidsynth_metronome soundfont [beats [tempo]]
*
* [Pedro Lopez-Cabanillas <plcl@users.sf.net>]
*/
#include <stdlib.h>
#include <stdio.h>
#include <fluidsynth.h>
fluid_audio_driver_t *audiodriver;
fluid_sequencer_t *sequencer;
short synth_destination, client_destination;
unsigned int time_marker;
/* default tempo, beats per minute */
#define TEMPO 120
unsigned int note_duration = 60000 / TEMPO;
/* metronome click/bell */
unsigned int weak_note = 33;
unsigned int strong_note = 34;
/* number of notes in one pattern */
unsigned int pattern_size = 4;
/* prototype */
void
sequencer_callback (unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data);
/* schedule a note on message */
void
schedule_noteon (int chan, short key, unsigned int ticks)
{
fluid_event_set_dest (ev, synth_destination);
fluid_event_noteon (ev, chan, key, 127);
fluid_sequencer_send_at (sequencer, ev, ticks, 1);
}
/* schedule a timer event (shall trigger the callback) */
void
schedule_timer_event ()
{
fluid_event_set_dest (ev, client_destination);
fluid_event_timer (ev, NULL);
fluid_sequencer_send_at (sequencer, ev, time_marker, 1);
}
/* schedule the metronome pattern */
void
schedule_pattern ()
{
int i, note_time;
note_time = time_marker;
for (i = 0; i < pattern_size; ++i) {
schedule_noteon (9, i ? weak_note : strong_note, note_time);
note_time += note_duration;
}
time_marker = note_time;
}
void
sequencer_callback (unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data)
{
schedule_timer_event ();
schedule_pattern ();
}
void
usage (char* prog_name)
{
printf ("Usage: %s soundfont.sf2 [beats [tempo]]\n", prog_name);
printf ("\t(optional) beats: number of pattern beats, default %d\n",
pattern_size);
printf ("\t(optional) tempo: BPM (Beats Per Minute), default %d\n", TEMPO);
}
int
main (int argc, char *argv[])
{
int n;
fluid_settings_t *settings;
settings = new_fluid_settings ();
if (argc < 2) {
usage (argv[0]);
} else {
/* create the synth, driver and sequencer instances */
synth = new_fluid_synth (settings);
audiodriver = new_fluid_audio_driver (settings, synth);
sequencer = new_fluid_sequencer ();
/* register the synth with the sequencer */
synth_destination = fluid_sequencer_register_fluidsynth (sequencer,
synth);
/* register the client name and callback */
client_destination = fluid_sequencer_register_client (sequencer,
"fluidsynth_metronome", sequencer_callback, NULL);
/* load a SoundFont */
n = fluid_synth_sfload (synth, argv[1], 1);
if (n != -1) {
if (argc > 2) {
n = atoi (argv[2]);
if (n > 0) pattern_size = n;
}
if (argc > 3) {
n = atoi (argv[3]);
if (n > 0) note_duration = 60000 / n;
}
/* get the current time in ticks */
time_marker = fluid_sequencer_get_tick (sequencer);
/* schedule patterns */
schedule_pattern ();
schedule_timer_event ();
schedule_pattern ();
/* wait for user input */
printf ("press <Enter> to stop\n");
n = getchar ();
}
/* clean and exit */
}
return 0;
}
new_fluid_sequencer
FLUIDSYNTH_API fluid_sequencer_t * new_fluid_sequencer(void)
Create a new sequencer object which uses the system timer.
Definition: fluid_seq.c:97
delete_fluid_audio_driver
FLUIDSYNTH_API void delete_fluid_audio_driver(fluid_audio_driver_t *driver)
Deletes an audio driver instance.
Definition: fluid_adriver.c:403
fluid_audio_driver_t
struct _fluid_audio_driver_t fluid_audio_driver_t
Audio driver instance.
Definition: types.h:45
fluid_sequencer_t
struct _fluid_sequencer_t fluid_sequencer_t
Sequencer instance.
Definition: types.h:56
new_fluid_event
FLUIDSYNTH_API fluid_event_t * new_fluid_event(void)
Create a new sequencer event structure.
Definition: fluid_event.c:57
delete_fluid_sequencer
FLUIDSYNTH_API void delete_fluid_sequencer(fluid_sequencer_t *seq)
Free a sequencer object.
Definition: fluid_seq.c:155
fluid_sequencer_register_client
FLUIDSYNTH_API 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:269
new_fluid_settings
FLUIDSYNTH_API fluid_settings_t * new_fluid_settings(void)
Create a new settings object.
Definition: fluid_settings.c:235
fluid_synth_t
struct _fluid_synth_t fluid_synth_t
Synthesizer instance.
Definition: types.h:37
fluid_event_set_source
FLUIDSYNTH_API void fluid_event_set_source(fluid_event_t *evt, fluid_seq_id_t src)
Set source of a sequencer event.
Definition: fluid_event.c:104
fluid_event_t
struct _fluid_event_t fluid_event_t
Sequencer event.
Definition: types.h:55
fluid_sequencer_send_at
FLUIDSYNTH_API 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:460
fluid_synth_sfload
FLUIDSYNTH_API 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:3250
new_fluid_audio_driver
FLUIDSYNTH_API 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:344
delete_fluid_event
FLUIDSYNTH_API void delete_fluid_event(fluid_event_t *evt)
Delete a sequencer event structure.
Definition: fluid_event.c:76
fluid_settings_t
struct _fluid_hashtable_t fluid_settings_t
Configuration settings instance.
Definition: types.h:36
fluid_event_set_dest
FLUIDSYNTH_API 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:115
fluid_sequencer_get_tick
FLUIDSYNTH_API unsigned int fluid_sequencer_get_tick(fluid_sequencer_t *seq)
Get the current tick of a sequencer.
Definition: fluid_seq.c:501
fluid_sequencer_register_fluidsynth
FLUIDSYNTH_API 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:81
fluid_event_noteon
FLUIDSYNTH_API 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:140
delete_fluid_settings
FLUIDSYNTH_API void delete_fluid_settings(fluid_settings_t *settings)
Delete the provided settings object.
Definition: fluid_settings.c:254
new_fluid_synth
FLUIDSYNTH_API fluid_synth_t * new_fluid_synth(fluid_settings_t *settings)
Create new FluidSynth instance.
Definition: fluid_synth.c:550
delete_fluid_synth
FLUIDSYNTH_API int delete_fluid_synth(fluid_synth_t *synth)
Delete a FluidSynth instance.
Definition: fluid_synth.c:779
fluid_event_timer
FLUIDSYNTH_API void fluid_event_timer(fluid_event_t *evt, void *data)
Set a sequencer event to be a timer event.
Definition: fluid_event.c:126

Generated for libfluidsynth by doxygen 1.8.17