mirror of
https://github.com/ZDoom/fluidsynth.git
synced 2024-11-13 00:04:10 +00:00
Added an example of how to use FluidSynth
This commit is contained in:
parent
e7484820a0
commit
115b101a6f
2 changed files with 77 additions and 0 deletions
77
fluidsynth/doc/example.c
Normal file
77
fluidsynth/doc/example.c
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
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>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
fluid_settings_t* settings;
|
||||||
|
fluid_synth_t* synth;
|
||||||
|
fluid_audio_driver_t* adriver;
|
||||||
|
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*/
|
||||||
|
sfont_id = fluid_synth_sfload(synth, "example.sf2", 0);
|
||||||
|
|
||||||
|
/* Select bank 0 and preset 0 in the SoundFont we just loaded on
|
||||||
|
channel 0 */
|
||||||
|
fluid_synth_program_select(synth, 0, sfont_id, 0, 0);
|
||||||
|
|
||||||
|
/* Initialze 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Clean up */
|
||||||
|
delete_fluid_audio_driver(adriver);
|
||||||
|
delete_fluid_synth(synth);
|
||||||
|
delete_fluid_settings(settings);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
fluidsynth/doc/example.sf2
Normal file
BIN
fluidsynth/doc/example.sf2
Normal file
Binary file not shown.
Loading…
Reference in a new issue