2017-11-26 09:07:08 +00:00
|
|
|
/*
|
|
|
|
* This is a C99 program that demonstrates how to load a soundfont from memory.
|
2018-06-24 11:01:31 +00:00
|
|
|
*
|
2017-11-26 09:07:08 +00:00
|
|
|
* 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>
|
|
|
|
|
2018-06-24 11:01:31 +00:00
|
|
|
void *my_open(const char *filename)
|
2017-11-26 09:07:08 +00:00
|
|
|
{
|
2018-06-24 11:01:31 +00:00
|
|
|
void *p;
|
|
|
|
|
2017-11-26 09:07:08 +00:00
|
|
|
if(filename[0] != '&')
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-06-24 11:01:31 +00:00
|
|
|
|
2018-09-18 23:18:42 +00:00
|
|
|
sscanf(filename, "&%p", &p);
|
2017-11-26 09:07:08 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-06-24 11:01:31 +00:00
|
|
|
int my_read(void *buf, int count, void *handle)
|
2017-11-26 09:07:08 +00:00
|
|
|
{
|
2018-09-18 23:18:42 +00:00
|
|
|
// NYI
|
2017-11-26 09:07:08 +00:00
|
|
|
return FLUID_OK;
|
|
|
|
}
|
|
|
|
|
2018-06-24 11:01:31 +00:00
|
|
|
int my_seek(void *handle, long offset, int origin)
|
2017-11-26 09:07:08 +00:00
|
|
|
{
|
|
|
|
// NYI
|
|
|
|
return FLUID_OK;
|
|
|
|
}
|
|
|
|
|
2018-06-24 11:01:31 +00:00
|
|
|
int my_close(void *handle)
|
2017-11-26 09:07:08 +00:00
|
|
|
{
|
|
|
|
// NYI
|
|
|
|
return FLUID_OK;
|
|
|
|
}
|
|
|
|
|
2018-06-24 11:01:31 +00:00
|
|
|
long my_tell(void *handle)
|
2017-11-26 09:07:08 +00:00
|
|
|
{
|
|
|
|
// NYI
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
2018-06-24 11:01:31 +00:00
|
|
|
fluid_settings_t *settings = new_fluid_settings();
|
|
|
|
fluid_synth_t *synth = new_fluid_synth(settings);
|
|
|
|
|
|
|
|
fluid_sfloader_t *my_sfloader = new_fluid_defsfloader(settings);
|
2018-02-11 15:56:53 +00:00
|
|
|
fluid_sfloader_set_callbacks(my_sfloader,
|
|
|
|
my_open,
|
|
|
|
my_read,
|
|
|
|
my_seek,
|
|
|
|
my_tell,
|
|
|
|
my_close);
|
2017-11-26 09:07:08 +00:00
|
|
|
fluid_synth_add_sfloader(synth, my_sfloader);
|
2018-06-24 11:01:31 +00:00
|
|
|
|
|
|
|
|
2017-11-26 09:07:08 +00:00
|
|
|
char abused_filename[64];
|
2018-06-24 11:01:31 +00:00
|
|
|
const void *pointer_to_sf2_in_mem = 0x1234Beef; // some pointer to where the soundfont shall be loaded from
|
2017-11-26 09:07:08 +00:00
|
|
|
sprintf(abused_filename, "&%p", pointer_to_sf2_in_mem);
|
2018-06-24 11:01:31 +00:00
|
|
|
|
2017-11-26 09:07:08 +00:00
|
|
|
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 */
|
2018-06-24 11:01:31 +00:00
|
|
|
|
2017-11-26 09:07:08 +00:00
|
|
|
if(id == FLUID_FAILED)
|
|
|
|
{
|
|
|
|
puts("oops");
|
|
|
|
err = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ~~~ Do your daily business here ~~~
|
|
|
|
*/
|
2018-06-24 11:01:31 +00:00
|
|
|
|
|
|
|
cleanup:
|
2017-11-26 09:07:08 +00:00
|
|
|
/* deleting the synth also deletes my_sfloader */
|
|
|
|
delete_fluid_synth(synth);
|
2018-06-24 11:01:31 +00:00
|
|
|
|
2017-11-26 09:07:08 +00:00
|
|
|
delete_fluid_settings(settings);
|
2018-06-24 11:01:31 +00:00
|
|
|
|
2017-11-26 09:07:08 +00:00
|
|
|
return err;
|
|
|
|
}
|