libfluidsynth  2.3.5
fluidsynth_simple.c

A basic example of using fluidsynth to play a single note

/* FluidSynth Simple - An example of using fluidsynth
*
* This code is in the public domain.
*
* To compile:
* gcc -g -O -o fluidsynth_simple fluidsynth_simple.c -lfluidsynth
*
* To run
* fluidsynth_simple soundfont
*
* [Peter Hanappe]
*/
#include <stdio.h>
#include <fluidsynth.h>
int main(int argc, char **argv)
{
fluid_settings_t *settings;
fluid_synth_t *synth = NULL;
fluid_audio_driver_t *adriver = NULL;
int err = 0;
if(argc != 2)
{
fprintf(stderr, "Usage: fluidsynth_simple [soundfont]\n");
return 1;
}
/* Create the settings object. This example uses the default
* values for the settings. */
settings = new_fluid_settings();
if(settings == NULL)
{
fprintf(stderr, "Failed to create the settings\n");
err = 2;
goto cleanup;
}
/* Create the synthesizer */
synth = new_fluid_synth(settings);
if(synth == NULL)
{
fprintf(stderr, "Failed to create the synthesizer\n");
err = 3;
goto cleanup;
}
/* Load the soundfont */
if(fluid_synth_sfload(synth, argv[1], 1) == -1)
{
fprintf(stderr, "Failed to load the SoundFont\n");
err = 4;
goto cleanup;
}
/* Create the audio driver. As soon as the audio driver is
* created, the synthesizer can be played. */
adriver = new_fluid_audio_driver(settings, synth);
if(adriver == NULL)
{
fprintf(stderr, "Failed to create the audio driver\n");
err = 5;
goto cleanup;
}
/* Play a note */
fluid_synth_noteon(synth, 0, 60, 100);
printf("Press \"Enter\" to stop: ");
fgetc(stdin);
printf("done\n");
cleanup:
if(adriver)
{
}
if(synth)
{
}
if(settings)
{
}
return err;
}
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
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_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:1253
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