1 /* FluidSynth - A Software Synthesizer 2 * 3 * Copyright (C) 2003 Peter Hanappe and others. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public License 7 * as published by the Free Software Foundation; either version 2 of 8 * the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public 16 * License along with this library; if not, write to the Free 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 18 * 02111-1307, USA 19 */ 20 21 #ifndef _FLUIDSYNTH_SFONT_H 22 #define _FLUIDSYNTH_SFONT_H 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 29 30 /** 31 * 32 * SoundFont plugins 33 * 34 * It is possible to add new SoundFont loaders to the 35 * synthesizer. The API uses a couple of "interfaces" (structures 36 * with callback functions): fluid_sfloader_t, fluid_sfont_t, and 37 * fluid_preset_t. 38 * 39 * To add a new SoundFont loader to the synthesizer, call 40 * fluid_synth_add_sfloader() and pass a pointer to an 41 * fluid_sfloader_t structure. The important callback function in 42 * this structure is "load", which should try to load a file and 43 * returns a fluid_sfont_t structure, or NULL if it fails. 44 * 45 * The fluid_sfont_t structure contains a callback to obtain the 46 * name of the soundfont. It contains two functions to iterate 47 * though the contained presets, and one function to obtain a 48 * preset corresponding to a bank and preset number. This 49 * function should return an fluid_preset_t structure. 50 * 51 * The fluid_preset_t structure contains some functions to obtain 52 * information from the preset (name, bank, number). The most 53 * important callback is the noteon function. The noteon function 54 * should call fluid_synth_alloc_voice() for every sample that has 55 * to be played. fluid_synth_alloc_voice() expects a pointer to a 56 * fluid_sample_t structure and returns a pointer to the opaque 57 * fluid_voice_t structure. To set or increments the values of a 58 * generator, use fluid_voice_gen_{set,incr}. When you are 59 * finished initializing the voice call fluid_voice_start() to 60 * start playing the synthesis voice. 61 * */ 62 63 enum { 64 FLUID_PRESET_SELECTED, 65 FLUID_PRESET_UNSELECTED, 66 FLUID_SAMPLE_DONE 67 }; 68 69 70 /* 71 * fluid_sfloader_t 72 */ 73 74 struct _fluid_sfloader_t { 75 /** Private data */ 76 void* data; 77 78 /** The free must free the memory allocated for the loader in 79 * addition to any private data. It should return 0 if no error 80 * occured, non-zero otherwise.*/ 81 int (*free)(fluid_sfloader_t* loader); 82 83 /** Load a file. Returns NULL if an error occured. */ 84 fluid_sfont_t* (*load)(fluid_sfloader_t* loader, const char* filename); 85 }; 86 87 88 /* 89 * fluid_sfont_t 90 */ 91 92 struct _fluid_sfont_t { 93 void* data; 94 unsigned int id; 95 96 /** The 'free' callback function should return 0 when it was able to 97 free all resources. It should return a non-zero value if some of 98 the samples could not be freed because they are still in use. */ 99 int (*free)(fluid_sfont_t* sfont); 100 101 /** Return the name of the sfont */ 102 char* (*get_name)(fluid_sfont_t* sfont); 103 104 /** Return the preset with the specified bank and preset number. All 105 * the fields, including the 'sfont' field, should * be filled 106 * in. If the preset cannot be found, the function returns NULL. */ 107 fluid_preset_t* (*get_preset)(fluid_sfont_t* sfont, unsigned int bank, unsigned int prenum); 108 109 void (*iteration_start)(fluid_sfont_t* sfont); 110 111 /* return 0 when no more presets are available, 1 otherwise */ 112 int (*iteration_next)(fluid_sfont_t* sfont, fluid_preset_t* preset); 113 }; 114 115 #define fluid_sfont_get_id(_sf) ((_sf)->id) 116 117 118 /* 119 * fluid_preset_t 120 */ 121 122 struct _fluid_preset_t { 123 void* data; 124 fluid_sfont_t* sfont; 125 int (*free)(fluid_preset_t* preset); 126 char* (*get_name)(fluid_preset_t* preset); 127 int (*get_banknum)(fluid_preset_t* preset); 128 int (*get_num)(fluid_preset_t* preset); 129 130 /** handle a noteon event. Returns 0 if no error occured. */ 131 int (*noteon)(fluid_preset_t* preset, fluid_synth_t* synth, int chan, int key, int vel); 132 133 /** Implement this function if the preset needs to be notified about 134 preset select and unselect events. */ 135 int (*notify)(fluid_preset_t* preset, int reason, int chan); 136 }; 137 138 139 /* 140 * fluid_sample_t 141 */ 142 143 struct _fluid_sample_t 144 { 145 char name[21]; 146 unsigned int start; 147 unsigned int end; /* Note: Index of last valid sample point (contrary to SF spec) */ 148 unsigned int loopstart; 149 unsigned int loopend; /* Note: first point following the loop (superimposed on loopstart) */ 150 unsigned int samplerate; 151 int origpitch; 152 int pitchadj; 153 int sampletype; 154 int valid; 155 short* data; 156 157 /** The amplitude, that will lower the level of the sample's loop to 158 the noise floor. Needed for note turnoff optimization, will be 159 filled out automatically */ 160 /* Set this to zero, when submitting a new sample. */ 161 int amplitude_that_reaches_noise_floor_is_valid; 162 double amplitude_that_reaches_noise_floor; 163 164 /** Count the number of playing voices that use this sample. */ 165 unsigned int refcount; 166 167 /** Implement this function if the sample or SoundFont needs to be 168 notified when the sample is no longer used. */ 169 int (*notify)(fluid_sample_t* sample, int reason); 170 171 /** Pointer to SoundFont specific data */ 172 void* userdata; 173 }; 174 175 176 #define fluid_sample_refcount(_sample) ((_sample)->refcount) 177 178 179 /** Sample types */ 180 181 #define FLUID_SAMPLETYPE_MONO 1 182 #define FLUID_SAMPLETYPE_RIGHT 2 183 #define FLUID_SAMPLETYPE_LEFT 4 184 #define FLUID_SAMPLETYPE_LINKED 8 185 #define FLUID_SAMPLETYPE_OGG_VORBIS 0x10 /**< Flag for #fluid_sample_t \a sampletype field for Ogg Vorbis compressed samples */ 186 #define FLUID_SAMPLETYPE_OGG_VORBIS_UNPACKED 0x20 187 #define FLUID_SAMPLETYPE_ROM 0x8000 188 189 190 191 #ifdef __cplusplus 192 } 193 #endif 194 195 #endif /* _FLUIDSYNTH_SFONT_H */ 196