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.
167
src/main.cpp
167
src/main.cpp
@ -1,97 +1,126 @@
|
|||||||
/* FluidSynth Simple - An example of using fluidsynth
|
/*
|
||||||
*
|
|
||||||
* This code is in the public domain.
|
An example of how to use FluidSynth.
|
||||||
*
|
|
||||||
* To compile:
|
To compile it on Linux:
|
||||||
* gcc -g -O -o fluidsynth_simple fluidsynth_simple.c -lfluidsynth
|
$ gcc -o example example.c `pkg-config fluidsynth --libs`
|
||||||
*
|
|
||||||
* To run
|
To compile it on Windows:
|
||||||
* fluidsynth_simple soundfont
|
...
|
||||||
*
|
|
||||||
* [Peter Hanappe]
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
Author: Peter Hanappe.
|
||||||
|
This code is in the public domain. Use it as you like.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
#include <fluidsynth.h>
|
#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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
fluid_settings_t *settings;
|
fluid_settings_t *settings = NULL;
|
||||||
fluid_synth_t *synth = NULL;
|
fluid_synth_t *synth = NULL;
|
||||||
fluid_audio_driver_t *adriver = 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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create the settings object. This example uses the default
|
// Enumerate the presets (programs) in the SoundFont
|
||||||
* values for the settings. */
|
|
||||||
settings = new_fluid_settings();
|
|
||||||
|
|
||||||
if(settings == NULL)
|
std::cout << "Programs in SoundFont " << argv[1] << ":" << std::endl;
|
||||||
{
|
fluid_sfont_iteration_start(sfont);
|
||||||
fprintf(stderr, "Failed to create the settings\n");
|
while ((preset = fluid_sfont_iteration_next(sfont)) != nullptr) {
|
||||||
err = 2;
|
std::cout << "Bank: " << fluid_preset_get_banknum(preset)
|
||||||
goto cleanup;
|
<< ", Program: " << fluid_preset_get_num(preset)
|
||||||
|
<< ", Name: " << fluid_preset_get_name(preset) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create the synthesizer */
|
fluid_synth_program_select(synth, 0, sfont_id, 0, 0);
|
||||||
synth = new_fluid_synth(settings);
|
|
||||||
|
|
||||||
if(synth == NULL)
|
/* Create the audio driver. The synthesizer starts playing as soon
|
||||||
{
|
as the driver is created. */
|
||||||
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);
|
adriver = new_fluid_audio_driver(settings, synth);
|
||||||
|
|
||||||
if(adriver == NULL)
|
if(adriver == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to create the audio driver\n");
|
puts("Failed to create the audio driver!");
|
||||||
err = 5;
|
goto err;
|
||||||
goto cleanup;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Play a note */
|
/* Initialize the random number generator */
|
||||||
fluid_synth_noteon(synth, 0, 60, 100);
|
srand(getpid());
|
||||||
|
|
||||||
printf("Press \"Enter\" to stop: ");
|
for(i = 0; i < 12; i++)
|
||||||
fgetc(stdin);
|
|
||||||
printf("done\n");
|
|
||||||
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
|
|
||||||
if(adriver)
|
|
||||||
{
|
{
|
||||||
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)
|
err:
|
||||||
{
|
/* Clean up */
|
||||||
delete_fluid_synth(synth);
|
delete_fluid_audio_driver(adriver);
|
||||||
}
|
delete_fluid_synth(synth);
|
||||||
|
delete_fluid_settings(settings);
|
||||||
|
|
||||||
if(settings)
|
return 0;
|
||||||
{
|
|
||||||
delete_fluid_settings(settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user