Added some more soundfonts, general faffery getting their instruments
This commit is contained in:
parent
e151fb7fe5
commit
2d5420b4c3
BIN
soundfonts/sine.sf2
Normal file
BIN
soundfonts/sine.sf2
Normal file
Binary file not shown.
BIN
soundfonts/square-equaltemperament.sf2
Normal file
BIN
soundfonts/square-equaltemperament.sf2
Normal file
Binary file not shown.
161
src/main.cpp
161
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 <stdio.h>
|
||||
Author: Peter Hanappe.
|
||||
This code is in the public domain. Use it as you like.
|
||||
|
||||
*/
|
||||
|
||||
#include <fluidsynth.h>
|
||||
#include <iostream>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#define sleep(_t) Sleep(_t * 1000)
|
||||
#include <process.h>
|
||||
#define getpid _getpid
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
/* 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, 60, 100);
|
||||
fluid_synth_noteon(synth, 0, key, 100);
|
||||
|
||||
printf("Press \"Enter\" to stop: ");
|
||||
fgetc(stdin);
|
||||
printf("done\n");
|
||||
/* Sleep for 1 second */
|
||||
sleep(1);
|
||||
|
||||
/* Stop the note */
|
||||
fluid_synth_noteoff(synth, 0, key);
|
||||
}
|
||||
|
||||
cleanup:
|
||||
|
||||
if(adriver)
|
||||
{
|
||||
err:
|
||||
/* Clean up */
|
||||
delete_fluid_audio_driver(adriver);
|
||||
}
|
||||
|
||||
if(synth)
|
||||
{
|
||||
delete_fluid_synth(synth);
|
||||
}
|
||||
|
||||
if(settings)
|
||||
{
|
||||
delete_fluid_settings(settings);
|
||||
}
|
||||
|
||||
return err;
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user