From 7a22eb87f670677894c0de61724b9b0cf6a870cb Mon Sep 17 00:00:00 2001 From: derselbst Date: Sun, 26 Nov 2017 10:07:08 +0100 Subject: [PATCH] add an example of how read a soundfont from memory --- doc/fluidsynth-v11-devdoc.txt | 7 ++- doc/fluidsynth_register_adriver.c | 2 +- doc/fluidsynth_sfload_mem.c | 95 +++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 doc/fluidsynth_sfload_mem.c diff --git a/doc/fluidsynth-v11-devdoc.txt b/doc/fluidsynth-v11-devdoc.txt index 6d64ffdf..349f4727 100644 --- a/doc/fluidsynth-v11-devdoc.txt +++ b/doc/fluidsynth-v11-devdoc.txt @@ -98,7 +98,7 @@ Changes in FluidSynth 2.0.0 concerning developers: - add individual reverb setters: fluid_synth_set_reverb_roomsize(), fluid_synth_set_reverb_damp(), fluid_synth_set_reverb_width(), fluid_synth_set_reverb_level() - add individual chorus setters: fluid_synth_set_chorus_nr(), fluid_synth_set_chorus_level(), fluid_synth_set_chorus_speed(), fluid_synth_set_chorus_depth(), fluid_synth_set_chorus_type() - introduce a separate data type for sequencer client IDs: #fluid_seq_id_t -- add file callback struct to _fluid_sfloader_t and expose new_fluid_defsfloader() to enable soundfont loading from memory +- add file callback struct to _fluid_sfloader_t and expose new_fluid_defsfloader() to enable soundfont loading from memory ( see fluid_sfload_mem.c ) \section NewIn1_1_9 Whats new in 1.1.9? @@ -797,3 +797,8 @@ Example of an arpeggio generated using the MIDI sequencer API \example fluidsynth_register_adriver.c Example of how to register audio drivers using fluid_audio_driver_register() (advanced users only) */ + +/*! +\example fluidsynth_sfload_mem.c +Example of how read a soundfont from memory (advanced users only) +*/ diff --git a/doc/fluidsynth_register_adriver.c b/doc/fluidsynth_register_adriver.c index b5a08b54..3ecc5924 100644 --- a/doc/fluidsynth_register_adriver.c +++ b/doc/fluidsynth_register_adriver.c @@ -1,4 +1,4 @@ -/** +/* * 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: diff --git a/doc/fluidsynth_sfload_mem.c b/doc/fluidsynth_sfload_mem.c new file mode 100644 index 00000000..b77955f4 --- /dev/null +++ b/doc/fluidsynth_sfload_mem.c @@ -0,0 +1,95 @@ +/* + * 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 +#include +#include + +void * my_open(const char * filename) +{ + void* p; + if(filename[0] != '&') + { + return NULL; + } + scanf("&%p", &p); + return p; +} + +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) +{ + // NYI + return FLUID_OK; +} + +int my_close(void * handle) +{ + // NYI + return FLUID_OK; +} + +long my_tell(void * handle) +{ + // NYI + return 0; +} + +fluid_file_callbacks_t my_cb = +{ + .fopen = my_open, + .fread = my_read, + .fseek = my_seek, + .fclose = my_close, + .ftell = my_tell +}; + +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); + my_sfloader->file_callbacks = &my_cb; + 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 */ + delete_fluid_synth(synth); + + delete_fluid_settings(settings); + + return err; +}