enforce coding style guide

using astyle
This commit is contained in:
derselbst 2018-06-24 13:01:31 +02:00
parent 90c2d76709
commit 9382edabd5
129 changed files with 33394 additions and 27137 deletions

View file

@ -6,7 +6,7 @@
$ gcc -o example example.c `pkg-config fluidsynth --libs`
To compile it on Windows:
...
...
Author: Peter Hanappe.
@ -23,52 +23,53 @@
#include <stdlib.h>
#endif
int main(int argc, char** argv)
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;
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();
/* Create the settings. */
settings = new_fluid_settings();
/* Change the settings if necessary*/
/* Change the settings if necessary*/
/* Create the synthesizer. */
synth = new_fluid_synth(settings);
/* 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);
/* 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 and reset presets (so that new instruments
* get used from the SoundFont) */
sfont_id = fluid_synth_sfload(synth, "example.sf2", 1);
/* Load a SoundFont and reset presets (so that new instruments
* get used from the SoundFont) */
sfont_id = fluid_synth_sfload(synth, "example.sf2", 1);
/* Initialize the random number generator */
srand(getpid());
/* Initialize the random number generator */
srand(getpid());
for (i = 0; i < 12; i++) {
for(i = 0; i < 12; i++)
{
/* Generate a random key */
key = 60 + (int) (12.0f * rand() / (float) RAND_MAX);
/* Generate a random key */
key = 60 + (int)(12.0f * rand() / (float) RAND_MAX);
/* Play a note */
fluid_synth_noteon(synth, 0, key, 80);
/* Play a note */
fluid_synth_noteon(synth, 0, key, 80);
/* Sleep for 1 second */
sleep(1);
/* Sleep for 1 second */
sleep(1);
/* Stop the note */
fluid_synth_noteoff(synth, 0, key);
}
/* Stop the note */
fluid_synth_noteoff(synth, 0, key);
}
/* Clean up */
delete_fluid_audio_driver(adriver);
delete_fluid_synth(synth);
delete_fluid_settings(settings);
/* Clean up */
delete_fluid_audio_driver(adriver);
delete_fluid_synth(synth);
delete_fluid_settings(settings);
return 0;
return 0;
}

View file

@ -28,124 +28,147 @@ unsigned int notes[] = { 60, 64, 67, 72, 76, 79, 84, 79, 76, 72, 67, 64 };
unsigned int pattern_size;
/* prototype */
void
sequencer_callback (unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data);
sequencer_callback(unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data);
/* schedule a note on message */
void
schedule_noteon (int chan, short key, unsigned int ticks)
schedule_noteon(int chan, short key, unsigned int ticks)
{
fluid_event_t *ev = new_fluid_event ();
fluid_event_set_source (ev, -1);
fluid_event_set_dest (ev, synth_destination);
fluid_event_noteon (ev, chan, key, 127);
fluid_sequencer_send_at (sequencer, ev, ticks, 1);
delete_fluid_event (ev);
fluid_event_t *ev = new_fluid_event();
fluid_event_set_source(ev, -1);
fluid_event_set_dest(ev, synth_destination);
fluid_event_noteon(ev, chan, key, 127);
fluid_sequencer_send_at(sequencer, ev, ticks, 1);
delete_fluid_event(ev);
}
/* schedule a note off message */
void
schedule_noteoff (int chan, short key, unsigned int ticks)
schedule_noteoff(int chan, short key, unsigned int ticks)
{
fluid_event_t *ev = new_fluid_event ();
fluid_event_set_source (ev, -1);
fluid_event_set_dest (ev, synth_destination);
fluid_event_noteoff (ev, chan, key);
fluid_sequencer_send_at (sequencer, ev, ticks, 1);
delete_fluid_event (ev);
fluid_event_t *ev = new_fluid_event();
fluid_event_set_source(ev, -1);
fluid_event_set_dest(ev, synth_destination);
fluid_event_noteoff(ev, chan, key);
fluid_sequencer_send_at(sequencer, ev, ticks, 1);
delete_fluid_event(ev);
}
/* schedule a timer event (shall trigger the callback) */
void
schedule_timer_event ()
schedule_timer_event()
{
fluid_event_t *ev = new_fluid_event ();
fluid_event_set_source (ev, -1);
fluid_event_set_dest (ev, client_destination);
fluid_event_timer (ev, NULL);
fluid_sequencer_send_at (sequencer, ev, time_marker, 1);
delete_fluid_event (ev);
fluid_event_t *ev = new_fluid_event();
fluid_event_set_source(ev, -1);
fluid_event_set_dest(ev, client_destination);
fluid_event_timer(ev, NULL);
fluid_sequencer_send_at(sequencer, ev, time_marker, 1);
delete_fluid_event(ev);
}
/* schedule the arpeggio's notes */
void
schedule_pattern ()
schedule_pattern()
{
int i, note_time, note_duration;
note_time = time_marker;
note_duration = duration / pattern_size;
for (i = 0; i < pattern_size; ++i) {
schedule_noteon (0, notes[i], note_time);
for(i = 0; i < pattern_size; ++i)
{
schedule_noteon(0, notes[i], note_time);
note_time += note_duration;
schedule_noteoff (0, notes[i], note_time);
schedule_noteoff(0, notes[i], note_time);
}
time_marker += duration;
}
void
sequencer_callback (unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data)
sequencer_callback(unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data)
{
schedule_timer_event ();
schedule_pattern ();
schedule_timer_event();
schedule_pattern();
}
void
usage (char* prog_name)
usage(char *prog_name)
{
printf ("Usage: %s soundfont.sf2 [steps [duration]]\n", prog_name);
printf ("\t(optional) steps: number of pattern notes, from 2 to %d\n",
pattern_size);
printf ("\t(optional) duration: of the pattern in ticks, default %d\n",
duration);
printf("Usage: %s soundfont.sf2 [steps [duration]]\n", prog_name);
printf("\t(optional) steps: number of pattern notes, from 2 to %d\n",
pattern_size);
printf("\t(optional) duration: of the pattern in ticks, default %d\n",
duration);
}
int
main (int argc, char* argv[])
main(int argc, char *argv[])
{
int n;
fluid_settings_t *settings;
settings = new_fluid_settings ();
settings = new_fluid_settings();
pattern_size = sizeof(notes) / sizeof(int);
if (argc < 2) {
usage (argv[0]);
} else {
/* create the synth, driver and sequencer instances */
synth = new_fluid_synth (settings);
audiodriver = new_fluid_audio_driver (settings, synth);
sequencer = new_fluid_sequencer ();
/* register the synth with the sequencer */
synth_destination = fluid_sequencer_register_fluidsynth (sequencer,
synth);
/* register the client name and callback */
client_destination = fluid_sequencer_register_client (sequencer,
"arpeggio", sequencer_callback, NULL);
/* load a SoundFont */
n = fluid_synth_sfload (synth, argv[1], 1);
if (n != -1) {
if (argc > 2) {
n = atoi (argv[2]);
if ((n > 1) && (n <= pattern_size)) pattern_size = n;
}
if (argc > 3) {
n = atoi (argv[3]);
if (n > 0) duration = n;
}
/* get the current time in ticks */
time_marker = fluid_sequencer_get_tick (sequencer);
/* schedule patterns */
schedule_pattern ();
schedule_timer_event ();
schedule_pattern ();
/* wait for user input */
printf ("press <Enter> to stop\n");
n = getchar ();
}
/* clean and exit */
delete_fluid_sequencer (sequencer);
delete_fluid_audio_driver (audiodriver);
delete_fluid_synth (synth);
if(argc < 2)
{
usage(argv[0]);
}
delete_fluid_settings (settings);
else
{
/* create the synth, driver and sequencer instances */
synth = new_fluid_synth(settings);
audiodriver = new_fluid_audio_driver(settings, synth);
sequencer = new_fluid_sequencer();
/* register the synth with the sequencer */
synth_destination = fluid_sequencer_register_fluidsynth(sequencer,
synth);
/* register the client name and callback */
client_destination = fluid_sequencer_register_client(sequencer,
"arpeggio", sequencer_callback, NULL);
/* load a SoundFont */
n = fluid_synth_sfload(synth, argv[1], 1);
if(n != -1)
{
if(argc > 2)
{
n = atoi(argv[2]);
if((n > 1) && (n <= pattern_size))
{
pattern_size = n;
}
}
if(argc > 3)
{
n = atoi(argv[3]);
if(n > 0)
{
duration = n;
}
}
/* get the current time in ticks */
time_marker = fluid_sequencer_get_tick(sequencer);
/* schedule patterns */
schedule_pattern();
schedule_timer_event();
schedule_pattern();
/* wait for user input */
printf("press <Enter> to stop\n");
n = getchar();
}
/* clean and exit */
delete_fluid_sequencer(sequencer);
delete_fluid_audio_driver(audiodriver);
delete_fluid_synth(synth);
}
delete_fluid_settings(settings);
return 0;
}

View file

@ -19,9 +19,10 @@
/* The structure with the effects data. This example simply applies a
* linear gain the to synthesizer output. */
struct fx_data_t {
fluid_synth_t* synth;
float gain;
struct fx_data_t
{
fluid_synth_t *synth;
float gain;
} fx_data_t;
/* This function implements the callback function of the audio driver
@ -37,103 +38,120 @@ struct fx_data_t {
* audio drivers currently handles audio input. Either 'nin' will be
* zero, or the buffers will be filled with zero samples.
*/
int fx_function(void* data, int len,
int nin, float** in,
int nout, float** out)
int fx_function(void *data, int len,
int nin, float **in,
int nout, float **out)
{
struct fx_data_t* fx_data = (struct fx_data_t*) data;
int i, k;
float* out_i;
struct fx_data_t *fx_data = (struct fx_data_t *) data;
int i, k;
float *out_i;
/* Call the synthesizer to fill the output buffers with its
* audio output. */
if (fluid_synth_process(fx_data->synth, len, nin, in, nout, out) != 0) {
/* Some error occured. Very unlikely to happen, though. */
return -1;
}
/* Call the synthesizer to fill the output buffers with its
* audio output. */
if(fluid_synth_process(fx_data->synth, len, nin, in, nout, out) != 0)
{
/* Some error occured. Very unlikely to happen, though. */
return -1;
}
/* Apply your effects here. In this example, the gain is
* applied to all the output buffers. */
for (i = 0; i < nout; i++) {
out_i = out[i];
for (k = 0; k < len; k++) {
out_i[k] *= fx_data->gain;
}
}
/* Apply your effects here. In this example, the gain is
* applied to all the output buffers. */
for(i = 0; i < nout; i++)
{
out_i = out[i];
return 0;
for(k = 0; k < len; k++)
{
out_i[k] *= fx_data->gain;
}
}
return 0;
}
int main(int argc, char** argv)
int main(int argc, char **argv)
{
fluid_settings_t* settings;
fluid_synth_t* synth = NULL;
fluid_audio_driver_t* adriver = NULL;
int err = 0;
struct fx_data_t fx_data;
fluid_settings_t *settings;
fluid_synth_t *synth = NULL;
fluid_audio_driver_t *adriver = NULL;
int err = 0;
struct fx_data_t fx_data;
if (argc != 3) {
fprintf(stderr, "Usage: fluidsynth_simple [soundfont] [gain]\n");
return 1;
}
if(argc != 3)
{
fprintf(stderr, "Usage: fluidsynth_simple [soundfont] [gain]\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;
}
/* Create the settings object. This example uses the default
* values for the settings. */
settings = new_fluid_settings();
/* Load the soundfont */
if (fluid_synth_sfload(synth, argv[1], 1) == -1) {
fprintf(stderr, "Failed to load the SoundFont\n");
err = 4;
goto cleanup;
}
if(settings == NULL)
{
fprintf(stderr, "Failed to create the settings\n");
err = 2;
goto cleanup;
}
/* Fill in the data of the effects unit */
fx_data.synth = synth;
fx_data.gain = atof(argv[2]);
/* Create the synthesizer */
synth = new_fluid_synth(settings);
/* Create the audio driver. As soon as the audio driver is
* created, the synthesizer can be played. */
adriver = new_fluid_audio_driver2(settings, fx_function, (void*) &fx_data);
if (adriver == NULL) {
fprintf(stderr, "Failed to create the audio driver\n");
err = 5;
goto cleanup;
}
if(synth == NULL)
{
fprintf(stderr, "Failed to create the synthesizer\n");
err = 3;
goto cleanup;
}
/* Play a note */
fluid_synth_noteon(synth, 0, 60, 100);
/* Load the soundfont */
if(fluid_synth_sfload(synth, argv[1], 1) == -1)
{
fprintf(stderr, "Failed to load the SoundFont\n");
err = 4;
goto cleanup;
}
printf("Press \"Enter\" to stop: ");
fgetc(stdin);
printf("done\n");
/* Fill in the data of the effects unit */
fx_data.synth = synth;
fx_data.gain = atof(argv[2]);
cleanup:
if (adriver) {
delete_fluid_audio_driver(adriver);
}
if (synth) {
delete_fluid_synth(synth);
}
if (settings) {
delete_fluid_settings(settings);
}
return err;
/* Create the audio driver. As soon as the audio driver is
* created, the synthesizer can be played. */
adriver = new_fluid_audio_driver2(settings, fx_function, (void *) &fx_data);
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)
{
delete_fluid_audio_driver(adriver);
}
if(synth)
{
delete_fluid_synth(synth);
}
if(settings)
{
delete_fluid_settings(settings);
}
return err;
}

View file

@ -30,108 +30,131 @@ unsigned int strong_note = 34;
unsigned int pattern_size = 4;
/* prototype */
void
sequencer_callback (unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data);
sequencer_callback(unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data);
/* schedule a note on message */
void
schedule_noteon (int chan, short key, unsigned int ticks)
schedule_noteon(int chan, short key, unsigned int ticks)
{
fluid_event_t *ev = new_fluid_event ();
fluid_event_set_source (ev, -1);
fluid_event_set_dest (ev, synth_destination);
fluid_event_noteon (ev, chan, key, 127);
fluid_sequencer_send_at (sequencer, ev, ticks, 1);
delete_fluid_event (ev);
fluid_event_t *ev = new_fluid_event();
fluid_event_set_source(ev, -1);
fluid_event_set_dest(ev, synth_destination);
fluid_event_noteon(ev, chan, key, 127);
fluid_sequencer_send_at(sequencer, ev, ticks, 1);
delete_fluid_event(ev);
}
/* schedule a timer event (shall trigger the callback) */
void
schedule_timer_event ()
schedule_timer_event()
{
fluid_event_t *ev = new_fluid_event ();
fluid_event_set_source (ev, -1);
fluid_event_set_dest (ev, client_destination);
fluid_event_timer (ev, NULL);
fluid_sequencer_send_at (sequencer, ev, time_marker, 1);
delete_fluid_event (ev);
fluid_event_t *ev = new_fluid_event();
fluid_event_set_source(ev, -1);
fluid_event_set_dest(ev, client_destination);
fluid_event_timer(ev, NULL);
fluid_sequencer_send_at(sequencer, ev, time_marker, 1);
delete_fluid_event(ev);
}
/* schedule the metronome pattern */
void
schedule_pattern ()
schedule_pattern()
{
int i, note_time;
note_time = time_marker;
for (i = 0; i < pattern_size; ++i) {
schedule_noteon (9, i ? weak_note : strong_note, note_time);
for(i = 0; i < pattern_size; ++i)
{
schedule_noteon(9, i ? weak_note : strong_note, note_time);
note_time += note_duration;
}
time_marker = note_time;
}
void
sequencer_callback (unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data)
sequencer_callback(unsigned int time, fluid_event_t *event,
fluid_sequencer_t *seq, void *data)
{
schedule_timer_event ();
schedule_pattern ();
schedule_timer_event();
schedule_pattern();
}
void
usage (char* prog_name)
usage(char *prog_name)
{
printf ("Usage: %s soundfont.sf2 [beats [tempo]]\n", prog_name);
printf ("\t(optional) beats: number of pattern beats, default %d\n",
pattern_size);
printf ("\t(optional) tempo: BPM (Beats Per Minute), default %d\n", TEMPO);
printf("Usage: %s soundfont.sf2 [beats [tempo]]\n", prog_name);
printf("\t(optional) beats: number of pattern beats, default %d\n",
pattern_size);
printf("\t(optional) tempo: BPM (Beats Per Minute), default %d\n", TEMPO);
}
int
main (int argc, char *argv[])
main(int argc, char *argv[])
{
int n;
fluid_settings_t *settings;
settings = new_fluid_settings ();
if (argc < 2) {
usage (argv[0]);
} else {
/* create the synth, driver and sequencer instances */
synth = new_fluid_synth (settings);
audiodriver = new_fluid_audio_driver (settings, synth);
sequencer = new_fluid_sequencer ();
/* register the synth with the sequencer */
synth_destination = fluid_sequencer_register_fluidsynth (sequencer,
synth);
/* register the client name and callback */
client_destination = fluid_sequencer_register_client (sequencer,
"fluidsynth_metronome", sequencer_callback, NULL);
/* load a SoundFont */
n = fluid_synth_sfload (synth, argv[1], 1);
if (n != -1) {
if (argc > 2) {
n = atoi (argv[2]);
if (n > 0) pattern_size = n;
}
if (argc > 3) {
n = atoi (argv[3]);
if (n > 0) note_duration = 60000 / n;
}
/* get the current time in ticks */
time_marker = fluid_sequencer_get_tick (sequencer);
/* schedule patterns */
schedule_pattern ();
schedule_timer_event ();
schedule_pattern ();
/* wait for user input */
printf ("press <Enter> to stop\n");
n = getchar ();
}
/* clean and exit */
delete_fluid_sequencer (sequencer);
delete_fluid_audio_driver (audiodriver);
delete_fluid_synth (synth);
settings = new_fluid_settings();
if(argc < 2)
{
usage(argv[0]);
}
delete_fluid_settings (settings);
else
{
/* create the synth, driver and sequencer instances */
synth = new_fluid_synth(settings);
audiodriver = new_fluid_audio_driver(settings, synth);
sequencer = new_fluid_sequencer();
/* register the synth with the sequencer */
synth_destination = fluid_sequencer_register_fluidsynth(sequencer,
synth);
/* register the client name and callback */
client_destination = fluid_sequencer_register_client(sequencer,
"fluidsynth_metronome", sequencer_callback, NULL);
/* load a SoundFont */
n = fluid_synth_sfload(synth, argv[1], 1);
if(n != -1)
{
if(argc > 2)
{
n = atoi(argv[2]);
if(n > 0)
{
pattern_size = n;
}
}
if(argc > 3)
{
n = atoi(argv[3]);
if(n > 0)
{
note_duration = 60000 / n;
}
}
/* get the current time in ticks */
time_marker = fluid_sequencer_get_tick(sequencer);
/* schedule patterns */
schedule_pattern();
schedule_timer_event();
schedule_pattern();
/* wait for user input */
printf("press <Enter> to stop\n");
n = getchar();
}
/* clean and exit */
delete_fluid_sequencer(sequencer);
delete_fluid_audio_driver(audiodriver);
delete_fluid_synth(synth);
}
delete_fluid_settings(settings);
return 0;
}

View file

@ -19,7 +19,7 @@ int main()
float left[SAMPLES], right[SAMPLES];
// array of buffers used to setup channel mapping
float *dry[1*2], *fx[1*2];
float *dry[1 * 2], *fx[1 * 2];
// first make sure to zero out the sample buffers
memset(left, 0, sizeof(left));
@ -31,32 +31,41 @@ int main()
// Setup channel mapping for a single stereo channel to which to render effects to.
// Just using the same sample buffers as for dry audio is fine here, as it will cause the effects to be mixed with dry output.
// Note: reverb and chorus together make up two stereo channels. Setting up only one stereo channel is sufficient
// Note: reverb and chorus together make up two stereo channels. Setting up only one stereo channel is sufficient
// as the channels warp around (i.e. chorus will be mixed with reverb channel).
fx[0] = left;
fx[1] = right;
int err = fluid_synth_process(synth, SAMPLES, 2, fx, 2, dry);
if(err == FLUID_FAILED)
{
puts(oops);
}
// USECASE2: only render dry audio and discard effects
// same as above, but call fluid_synth_process() like:
int err = fluid_synth_process(synth, SAMPLES, 0, NULL, 2, dry);
if(err == FLUID_FAILED)
{
puts(oops);
}
}
// USECASE3: render audio and discard all samples
{
int err = fluid_synth_process(synth, SAMPLES, 0, NULL, 0, NULL);
if(err == FLUID_FAILED)
{
puts(oops);
}
}
// USECASE4: multi-channel rendering, i.e. render all audio and effects channels to dedicated audio buffers
// ofc its not a good idea to allocate all the arrays on the stack
{
@ -79,7 +88,7 @@ int main()
// ...
// dry[i*2 + 0] = ith audio channel left
// dry[i*2 + 1] = ith audio channel right
for(int i=0; i<n_aud_chan*2; i++)
for(int i = 0; i < n_aud_chan * 2; i++)
{
dry[i] = &samp_buf[i * SAMPLES];
}
@ -90,17 +99,20 @@ int main()
// fx[1] = global reverb channel right
// fx[2] = global chorus channel left
// fx[3] = global chorus channel right
for(int i=0; i<n_fx_chan*2; i++)
for(int i = 0; i < n_fx_chan * 2; i++)
{
fx[i] = &samp_buf[n_aud_chan*2*SAMPLES + i * SAMPLES];
fx[i] = &samp_buf[n_aud_chan * 2 * SAMPLES + i * SAMPLES];
}
// dont forget to zero sample buffer(s) before each rendering
memset(samp_buf, 0, sizeof(samp_buf));
int err = fluid_synth_process(synth, SAMPLES, n_fx_chan*2, fx, n_aud_chan*2, dry);
int err = fluid_synth_process(synth, SAMPLES, n_fx_chan * 2, fx, n_aud_chan * 2, dry);
if(err == FLUID_FAILED)
{
puts(oops);
}
}
return 0;

View file

@ -1,9 +1,9 @@
/*
* This is a simple C99 program that demonstrates the usage of fluid_audio_driver_register()
*
*
* There are 3 calls to fluid_audio_driver_register(), i.e. 3 iterations:
* First the alsa driver is registered and created, followed by the jack and portaudio driver.
*
*
* The usual usecase would be to call fluid_audio_driver_register() only once providing the audio drivers needed during fluidsynth usage.
* If necessary however fluid_audio_driver_register() can be called multiple times as demonstrated here.
* Therefore the user must make sure to delete all fluid-instances of any kind before making the call to fluid_audio_driver_register().
@ -15,33 +15,35 @@
int main()
{
const char* DRV[] = { "alsa", "jack", "portaudio" };
const char* adrivers[2];
for(int i=0; i<sizeof(DRV)/sizeof(DRV[0]); i++)
const char *DRV[] = { "alsa", "jack", "portaudio" };
const char *adrivers[2];
for(int i = 0; i < sizeof(DRV) / sizeof(DRV[0]); i++)
{
adrivers[0] = DRV[i];
/* register any other driver you need
*
*
* adrivers[X] = "whatever";
*/
adrivers[1] = NULL; /* NULL terminate the array */
/* register those audio drivers. Note that at this time no fluidsynth objects are alive! */
int res = fluid_audio_driver_register(adrivers);
if(res != FLUID_OK)
{
puts("adriver reg err");
return -1;
}
fluid_settings_t* settings = new_fluid_settings();
fluid_settings_t *settings = new_fluid_settings();
res = fluid_settings_setstr(settings, "audio.driver", DRV[i]);
/* settings API will be refactored to return FLUID_OK|FAILED next major release
* returning TRUE or FALSE is deprecated
*/
#if FLUIDSYNTH_VERSION_MAJOR >= 2
if(res != FLUID_OK)
#else
if(res == 0)
@ -51,18 +53,19 @@ int main()
return -1;
}
fluid_synth_t* synth = new_fluid_synth(settings);
fluid_audio_driver_t* ad = new_fluid_audio_driver(settings, synth);
fluid_synth_t *synth = new_fluid_synth(settings);
fluid_audio_driver_t *ad = new_fluid_audio_driver(settings, synth);
/*
* ~~~ Do your daily business here ~~~
*/
delete_fluid_audio_driver(ad);
delete_fluid_synth(synth);
delete_fluid_settings(settings);
/* everything cleaned up, fluid_audio_driver_register() can be called again if needed */
}
return 0;
}

View file

@ -1,6 +1,6 @@
/*
* This is a C99 program that demonstrates how to load a soundfont from memory.
*
*
* It only gives a brief overview on how to achieve this with fluidsynth's API.
* Although it should compile, it's highly incomplete, as the details of it's
* implementation depend on the users needs.
@ -10,37 +10,39 @@
#include <string.h>
#include <fluidsynth.h>
void * my_open(const char * filename)
void *my_open(const char *filename)
{
void* p;
void *p;
if(filename[0] != '&')
{
return NULL;
}
scanf("&%p", &p);
return p;
}
int my_read(void *buf, int count, void * handle)
int my_read(void *buf, int count, void *handle)
{
// not yet implemented
memset(buf, 0, count);
return FLUID_OK;
}
int my_seek(void * handle, long offset, int origin)
int my_seek(void *handle, long offset, int origin)
{
// NYI
return FLUID_OK;
}
int my_close(void * handle)
int my_close(void *handle)
{
// NYI
return FLUID_OK;
}
long my_tell(void * handle)
long my_tell(void *handle)
{
// NYI
return 0;
@ -50,10 +52,10 @@ int main()
{
int err = 0;
fluid_settings_t* settings = new_fluid_settings();
fluid_synth_t* synth = new_fluid_synth(settings);
fluid_sfloader_t* my_sfloader = new_fluid_defsfloader(settings);
fluid_settings_t *settings = new_fluid_settings();
fluid_synth_t *synth = new_fluid_synth(settings);
fluid_sfloader_t *my_sfloader = new_fluid_defsfloader(settings);
fluid_sfloader_set_callbacks(my_sfloader,
my_open,
my_read,
@ -61,15 +63,15 @@ int main()
my_tell,
my_close);
fluid_synth_add_sfloader(synth, my_sfloader);
char abused_filename[64];
const void* pointer_to_sf2_in_mem = 0x1234Beef; // some pointer to where the soundfont shall be loaded from
const void *pointer_to_sf2_in_mem = 0x1234Beef; // some pointer to where the soundfont shall be loaded from
sprintf(abused_filename, "&%p", pointer_to_sf2_in_mem);
int id = fluid_synth_sfload(synth, abused_filename, 0);
/* now my_open() will be called with abused_filename and should have opened the memory region */
if(id == FLUID_FAILED)
{
puts("oops");
@ -80,12 +82,12 @@ int main()
/*
* ~~~ Do your daily business here ~~~
*/
cleanup:
cleanup:
/* deleting the synth also deletes my_sfloader */
delete_fluid_synth(synth);
delete_fluid_settings(settings);
return err;
}

View file

@ -15,70 +15,83 @@
#include <stdio.h>
#include <fluidsynth.h>
int main(int argc, char** argv)
int main(int argc, char **argv)
{
fluid_settings_t* settings;
fluid_synth_t* synth = NULL;
fluid_audio_driver_t* adriver = NULL;
int err = 0;
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;
}
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;
}
/* Create the settings object. This example uses the default
* values for the settings. */
settings = new_fluid_settings();
/* Load the soundfont */
if (fluid_synth_sfload(synth, argv[1], 1) == -1) {
fprintf(stderr, "Failed to load the SoundFont\n");
err = 4;
goto cleanup;
}
if(settings == NULL)
{
fprintf(stderr, "Failed to create the settings\n");
err = 2;
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;
}
/* Create the synthesizer */
synth = new_fluid_synth(settings);
/* Play a note */
fluid_synth_noteon(synth, 0, 60, 100);
if(synth == NULL)
{
fprintf(stderr, "Failed to create the synthesizer\n");
err = 3;
goto cleanup;
}
printf("Press \"Enter\" to stop: ");
fgetc(stdin);
printf("done\n");
/* Load the soundfont */
if(fluid_synth_sfload(synth, argv[1], 1) == -1)
{
fprintf(stderr, "Failed to load the SoundFont\n");
err = 4;
goto cleanup;
}
cleanup:
if (adriver) {
delete_fluid_audio_driver(adriver);
}
if (synth) {
delete_fluid_synth(synth);
}
if (settings) {
delete_fluid_settings(settings);
}
return err;
/* 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)
{
delete_fluid_audio_driver(adriver);
}
if(synth)
{
delete_fluid_synth(synth);
}
if(settings)
{
delete_fluid_settings(settings);
}
return err;
}