libfluidsynth  2.1.8
example.c

Example producing short random music with FluidSynth

/*
An example of how to use FluidSynth.
To compile it on Linux:
$ gcc -o example example.c `pkg-config fluidsynth --libs`
To compile it on Windows:
...
Author: Peter Hanappe.
This code is in the public domain. Use it as you like.
*/
#include <fluidsynth.h>
#if defined(WIN32)
#include <windows.h>
#define sleep(_t) Sleep(_t * 1000)
#else
#include <stdlib.h>
#include <unistd.h>
#endif
int main(int argc, char **argv)
{
fluid_settings_t *settings;
fluid_synth_t *synth;
int sfont_id;
int i, key;
/* Create the settings. */
settings = new_fluid_settings();
/* Change the settings if necessary*/
/* Create the synthesizer. */
synth = new_fluid_synth(settings);
/* Create the audio driver. The synthesizer starts playing as soon
as the driver is created. */
adriver = new_fluid_audio_driver(settings, synth);
/* Load a SoundFont and reset presets (so that new instruments
* get used from the SoundFont) */
sfont_id = fluid_synth_sfload(synth, "example.sf2", 1);
if(sfont_id == FLUID_FAILED)
{
puts("Loading the SoundFont failed!");
goto err;
}
/* Initialize the random number generator */
srand(getpid());
for(i = 0; i < 12; i++)
{
/* Generate a random key */
key = 60 + (int)(12.0f * rand() / (float) RAND_MAX);
/* Play a note */
fluid_synth_noteon(synth, 0, key, 80);
/* Sleep for 1 second */
sleep(1);
/* Stop the note */
fluid_synth_noteoff(synth, 0, key);
}
err:
/* Clean up */
return 0;
}
delete_fluid_synth
FLUIDSYNTH_API void delete_fluid_synth(fluid_synth_t *synth)
Delete a FluidSynth instance.
Definition: fluid_synth.c:1003
fluid_synth_noteon
FLUIDSYNTH_API int fluid_synth_noteon(fluid_synth_t *synth, int chan, int key, int vel)
Send a note-on event to a FluidSynth object.
Definition: fluid_synth.c:1233
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:386
fluid_audio_driver_t
struct _fluid_audio_driver_t fluid_audio_driver_t
Audio driver instance.
Definition: types.h:44
fluid_synth_noteoff
FLUIDSYNTH_API int fluid_synth_noteoff(fluid_synth_t *synth, int chan, int key)
Sends a note-off event to a FluidSynth object.
Definition: fluid_synth.c:1317
new_fluid_settings
FLUIDSYNTH_API fluid_settings_t * new_fluid_settings(void)
Create a new settings object.
Definition: fluid_settings.c:261
fluid_synth_t
struct _fluid_synth_t fluid_synth_t
Synthesizer instance.
Definition: types.h:37
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:4770
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:308
fluid_settings_t
struct _fluid_hashtable_t fluid_settings_t
Configuration settings instance.
Definition: types.h:36
FLUID_FAILED
#define FLUID_FAILED
Value that indicates failure, used by most libfluidsynth functions.
Definition: misc.h:60
delete_fluid_settings
FLUIDSYNTH_API void delete_fluid_settings(fluid_settings_t *settings)
Delete the provided settings object.
Definition: fluid_settings.c:284
new_fluid_synth
FLUIDSYNTH_API fluid_synth_t * new_fluid_synth(fluid_settings_t *settings)
Create new FluidSynth instance.
Definition: fluid_synth.c:611

Generated for libfluidsynth by doxygen 1.8.20