add an example of how read a soundfont from memory

This commit is contained in:
derselbst 2017-11-26 10:07:08 +01:00
parent 332c221cfb
commit 7a22eb87f6
3 changed files with 102 additions and 2 deletions

View file

@ -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 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() - 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 - 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? \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 fluidsynth_register_adriver.c
Example of how to register audio drivers using fluid_audio_driver_register() (advanced users only) 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)
*/

View file

@ -1,4 +1,4 @@
/** /*
* This is a simple C99 program that demonstrates the usage of fluid_audio_driver_register() * 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: * There are 3 calls to fluid_audio_driver_register(), i.e. 3 iterations:

View file

@ -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 <stdio.h>
#include <string.h>
#include <fluidsynth.h>
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;
}