diff --git a/soundfonts/sine.sf2 b/soundfonts/sine.sf2 new file mode 100644 index 0000000..bf82f2d Binary files /dev/null and b/soundfonts/sine.sf2 differ diff --git a/soundfonts/square-equaltemperament.sf2 b/soundfonts/square-equaltemperament.sf2 new file mode 100644 index 0000000..9b271d6 Binary files /dev/null and b/soundfonts/square-equaltemperament.sf2 differ diff --git a/src/main.cpp b/src/main.cpp index b114fad..bddb1d2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,97 +1,126 @@ -/* 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] - */ +/* + + 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: + ... -#include + Author: Peter Hanappe. + This code is in the public domain. Use it as you like. + +*/ + #include +#include + +#if defined(_WIN32) +#include +#define sleep(_t) Sleep(_t * 1000) +#include +#define getpid _getpid +#else +#include +#include +#endif int main(int argc, char **argv) { - fluid_settings_t *settings; + fluid_settings_t *settings = NULL; fluid_synth_t *synth = NULL; fluid_audio_driver_t *adriver = NULL; - int err = 0; + int sfont_id; + int i, key; - if(argc != 2) + fluid_sfont_t* sfont = nullptr; + fluid_preset_t* preset = nullptr; + + /* Create the settings. */ + settings = new_fluid_settings(); + if(settings == NULL) { - fprintf(stderr, "Usage: fluidsynth_simple [soundfont]\n"); + puts("Failed to create the settings!"); + goto err; + } + + /* Change the settings if necessary*/ + + /* Create the synthesizer. */ + synth = new_fluid_synth(settings); + if(synth == NULL) + { + puts("Failed to create the synth!"); + goto err; + } + + /* Load a SoundFont and reset presets (so that new instruments + * get used from the SoundFont) + * Depending on the size of the SoundFont, this will take some time to complete... + */ + sfont_id = fluid_synth_sfload(synth, argv[1], 1); + if(sfont_id == FLUID_FAILED) + { + puts("Loading the SoundFont failed!"); + goto err; + } + + // Get the SoundFont + sfont = fluid_synth_get_sfont_by_id(synth, sfont_id); + + if (!sfont) { + std::cerr << "Failed to get SoundFont by ID" << std::endl; + delete_fluid_synth(synth); + delete_fluid_settings(settings); return 1; } - /* Create the settings object. This example uses the default - * values for the settings. */ - settings = new_fluid_settings(); + // Enumerate the presets (programs) in the SoundFont - if(settings == NULL) - { - fprintf(stderr, "Failed to create the settings\n"); - err = 2; - goto cleanup; + std::cout << "Programs in SoundFont " << argv[1] << ":" << std::endl; + fluid_sfont_iteration_start(sfont); + while ((preset = fluid_sfont_iteration_next(sfont)) != nullptr) { + std::cout << "Bank: " << fluid_preset_get_banknum(preset) + << ", Program: " << fluid_preset_get_num(preset) + << ", Name: " << fluid_preset_get_name(preset) << std::endl; } - /* Create the synthesizer */ - synth = new_fluid_synth(settings); + fluid_synth_program_select(synth, 0, sfont_id, 0, 0); - 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. */ + /* Create the audio driver. The synthesizer starts playing as soon + as the driver is created. */ adriver = new_fluid_audio_driver(settings, synth); - if(adriver == NULL) { - fprintf(stderr, "Failed to create the audio driver\n"); - err = 5; - goto cleanup; + puts("Failed to create the audio driver!"); + goto err; } - /* Play a note */ - fluid_synth_noteon(synth, 0, 60, 100); + /* Initialize the random number generator */ + srand(getpid()); - printf("Press \"Enter\" to stop: "); - fgetc(stdin); - printf("done\n"); - - -cleanup: - - if(adriver) + for(i = 0; i < 12; i++) { - delete_fluid_audio_driver(adriver); + /* Generate a random key */ + key = 60 + (int)(12.0f * rand() / (float) RAND_MAX); + + /* Play a note */ + fluid_synth_noteon(synth, 0, key, 100); + + /* Sleep for 1 second */ + sleep(1); + + /* Stop the note */ + fluid_synth_noteoff(synth, 0, key); } - if(synth) - { - delete_fluid_synth(synth); - } +err: + /* Clean up */ + delete_fluid_audio_driver(adriver); + delete_fluid_synth(synth); + delete_fluid_settings(settings); - if(settings) - { - delete_fluid_settings(settings); - } - - return err; + return 0; } \ No newline at end of file