libfluidsynth  2.3.5
fluidsynth_sfload_mem.c

Example of how read a soundfont from memory (advanced users only)

/*
* 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.
*/
#include <stdio.h>
#include <string.h>
#include <fluidsynth.h>
void *my_open(const char *filename)
{
void *p;
if(filename[0] != '&')
{
return NULL;
}
sscanf(filename, "&%p", &p);
return p;
}
int my_read(void *buf, int count, void *handle)
{
// NYI
return FLUID_OK;
}
int my_seek(void *handle, long offset, int origin)
{
// NYI
return FLUID_OK;
}
int my_close(void *handle)
{
// NYI
return FLUID_OK;
}
long my_tell(void *handle)
{
// NYI
return 0;
}
int main()
{
int err = 0;
fluid_synth_t *synth = new_fluid_synth(settings);
fluid_sfloader_t *my_sfloader = new_fluid_defsfloader(settings);
my_open,
my_read,
my_seek,
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
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");
err = -1;
goto cleanup;
}
/*
* ~~~ Do your daily business here ~~~
*/
cleanup:
/* deleting the synth also deletes my_sfloader */
return err;
}
struct _fluid_hashtable_t fluid_settings_t
Configuration settings instance.
Definition: types.h:38
struct _fluid_sfloader_t fluid_sfloader_t
SoundFont loader plugin.
Definition: types.h:41
struct _fluid_synth_t fluid_synth_t
Synthesizer instance.
Definition: types.h:39
#define FLUID_FAILED
Value that indicates failure, used by most libfluidsynth functions.
Definition: misc.h:65
#define FLUID_OK
Value that indicates success, used by most libfluidsynth functions.
Definition: misc.h:56
fluid_settings_t * new_fluid_settings(void)
Create a new settings object.
Definition: fluid_settings.c:262
void delete_fluid_settings(fluid_settings_t *settings)
Delete the provided settings object.
Definition: fluid_settings.c:286
fluid_sfloader_t * new_fluid_defsfloader(fluid_settings_t *settings)
Creates a default soundfont2 loader that can be used with fluid_synth_add_sfloader().
Definition: fluid_defsfont.c:62
void fluid_synth_add_sfloader(fluid_synth_t *synth, fluid_sfloader_t *loader)
Add a SoundFont loader to the synth.
Definition: fluid_synth.c:5313
int fluid_sfloader_set_callbacks(fluid_sfloader_t *loader, fluid_sfloader_callback_open_t open, fluid_sfloader_callback_read_t read, fluid_sfloader_callback_seek_t seek, fluid_sfloader_callback_tell_t tell, fluid_sfloader_callback_close_t close)
Set custom callbacks to be used upon soundfont loading.
Definition: fluid_sfont.c:178
int fluid_synth_sfload(fluid_synth_t *synth, const char *filename, int reset_presets)
Load a SoundFont file (filename is interpreted by SoundFont loaders).
Definition: fluid_synth.c:5344
fluid_synth_t * new_fluid_synth(fluid_settings_t *settings)
Create new FluidSynth instance.
Definition: fluid_synth.c:628
void delete_fluid_synth(fluid_synth_t *synth)
Delete a FluidSynth instance.
Definition: fluid_synth.c:1023