This example demonstrates the basic lifecycle of using FluidLite: creating settings, initializing a synth, loading a SoundFont, triggering a note, and writing audio to a buffer.
Workflow:
- Create settings with
new_fluid_settings(). - Create a synth with
new_fluid_synth(settings). - Load a SoundFont using
fluid_synth_sfload(synth, "filename.sf2", bank). - Trigger notes with
fluid_synth_noteon(synth, channel, note, velocity). - Write audio data using
fluid_synth_write_float(...). - Clean up with
delete_fluid_synth(synth) and delete_fluid_settings(settings).
#include <stdlib.h>
#include <stdio.h>
#include "fluidlite.h"
#define SAMPLE_RATE 44100
#define SAMPLE_SIZE sizeof(float)
#define NUM_FRAMES SAMPLE_RATE
#define NUM_CHANNELS 2
#define NUM_SAMPLES (NUM_FRAMES * NUM_CHANNELS)
int main() {
fluid_settings_t* settings = new_fluid_settings();
fluid_synth_t* synth = new_fluid_synth(settings);
fluid_synth_sfload(synth, "soundfont.sf2", 1);
float* buffer = calloc(SAMPLE_SIZE, NUM_SAMPLES);
FILE* file = fopen("float32output.pcm", "wb");
fluid_synth_noteon(synth, 0, 60, 127);
fluid_synth_write_float(synth, NUM_FRAMES, buffer, 0, NUM_CHANNELS, buffer, 1, NUM_CHANNELS);
fwrite(buffer, SAMPLE_SIZE, NUM_SAMPLES, file);
fluid_synth_noteoff(synth, 0, 60);
fluid_synth_write_float(synth, NUM_FRAMES, buffer, 0, NUM_CHANNELS, buffer, 1, NUM_CHANNELS);
fwrite(buffer, SAMPLE_SIZE, NUM_SAMPLES, file);
fclose(file);
free(buffer);
delete_fluid_synth(synth);
delete_fluid_settings(settings);
return 0;
}