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#include <math.h> 22 23#include "fluid_synth.h" 24#include "fluid_sys.h" 25#include "fluid_chan.h" 26#include "fluid_tuning.h" 27#include "fluid_settings.h" 28#include "fluid_sfont.h" 29 30#ifdef TRAP_ON_FPE 31#define _GNU_SOURCE 32#include <fenv.h> 33 34/* seems to not be declared in fenv.h */ 35extern int feenableexcept (int excepts); 36#endif 37 38 39fluid_sfloader_t* new_fluid_defsfloader(void); 40 41/************************************************************************ 42 * 43 * These functions were added after the v1.0 API freeze. They are not 44 * in synth.h. They should be added as soon as a new development 45 * version is started. 46 * 47 ************************************************************************/ 48 49int fluid_synth_program_select2(fluid_synth_t* synth, 50 int chan, 51 char* sfont_name, 52 unsigned int bank_num, 53 unsigned int preset_num); 54 55fluid_sfont_t* fluid_synth_get_sfont_by_name(fluid_synth_t* synth, char *name); 56 57int fluid_synth_set_gen2(fluid_synth_t* synth, int chan, 58 int param, float value, 59 int absolute, int normalized); 60 61 62/*************************************************************** 63 * 64 * GLOBAL 65 */ 66 67/* has the synth module been initialized? */ 68static int fluid_synth_initialized = 0; 69static void fluid_synth_init(void); 70static void init_dither(void); 71 72/* default modulators 73 * SF2.01 page 52 ff: 74 * 75 * There is a set of predefined default modulators. They have to be 76 * explicitly overridden by the sound font in order to turn them off. 77 */ 78 79fluid_mod_t default_vel2att_mod; /* SF2.01 section 8.4.1 */ 80fluid_mod_t default_vel2filter_mod; /* SF2.01 section 8.4.2 */ 81fluid_mod_t default_at2viblfo_mod; /* SF2.01 section 8.4.3 */ 82fluid_mod_t default_mod2viblfo_mod; /* SF2.01 section 8.4.4 */ 83fluid_mod_t default_att_mod; /* SF2.01 section 8.4.5 */ 84fluid_mod_t default_pan_mod; /* SF2.01 section 8.4.6 */ 85fluid_mod_t default_expr_mod; /* SF2.01 section 8.4.7 */ 86fluid_mod_t default_reverb_mod; /* SF2.01 section 8.4.8 */ 87fluid_mod_t default_chorus_mod; /* SF2.01 section 8.4.9 */ 88fluid_mod_t default_pitch_bend_mod; /* SF2.01 section 8.4.10 */ 89 90/* reverb presets */ 91static fluid_revmodel_presets_t revmodel_preset[] = { 92 /* name */ /* roomsize */ /* damp */ /* width */ /* level */ 93 { "Test 1", 0.2f, 0.0f, 0.5f, 0.9f }, 94 { "Test 2", 0.4f, 0.2f, 0.5f, 0.8f }, 95 { "Test 3", 0.6f, 0.4f, 0.5f, 0.7f }, 96 { "Test 4", 0.8f, 0.7f, 0.5f, 0.6f }, 97 { "Test 5", 0.8f, 1.0f, 0.5f, 0.5f }, 98 { NULL, 0.0f, 0.0f, 0.0f, 0.0f } 99}; 100 101 102/*************************************************************** 103 * 104 * INITIALIZATION & UTILITIES 105 */ 106 107 108void fluid_synth_settings(fluid_settings_t* settings) 109{ 110 fluid_settings_register_str(settings, "synth.verbose", "no", 0, NULL, NULL); 111 fluid_settings_register_str(settings, "synth.dump", "no", 0, NULL, NULL); 112 fluid_settings_register_str(settings, "synth.reverb.active", "yes", 0, NULL, NULL); 113 fluid_settings_register_str(settings, "synth.chorus.active", "yes", 0, NULL, NULL); 114 fluid_settings_register_str(settings, "synth.ladspa.active", "no", 0, NULL, NULL); 115 fluid_settings_register_str(settings, "midi.portname", "", 0, NULL, NULL); 116 117 fluid_settings_register_int(settings, "synth.polyphony", 118 256, 16, 4096, 0, NULL, NULL); 119 fluid_settings_register_int(settings, "synth.midi-channels", 120 16, 16, 256, 0, NULL, NULL); 121 fluid_settings_register_num(settings, "synth.gain", 122 0.2f, 0.0f, 10.0f, 123 0, NULL, NULL); 124 fluid_settings_register_int(settings, "synth.audio-channels", 125 1, 1, 256, 0, NULL, NULL); 126 fluid_settings_register_int(settings, "synth.audio-groups", 127 1, 1, 256, 0, NULL, NULL); 128 fluid_settings_register_int(settings, "synth.effects-channels", 129 2, 2, 2, 0, NULL, NULL); 130 fluid_settings_register_num(settings, "synth.sample-rate", 131 44100.0f, 22050.0f, 96000.0f, 132 0, NULL, NULL); 133} 134 135/* 136 * fluid_version 137 */ 138void fluid_version(int *major, int *minor, int *micro) 139{ 140 *major = FLUIDSYNTH_VERSION_MAJOR; 141 *minor = FLUIDSYNTH_VERSION_MINOR; 142 *micro = FLUIDSYNTH_VERSION_MICRO; 143} 144 145/* 146 * fluid_version_str 147 */ 148char* fluid_version_str(void) 149{ 150 return FLUIDSYNTH_VERSION; 151} 152 153 154/* 155 * void fluid_synth_init 156 * 157 * Does all the initialization for this module. 158 */ 159static void 160fluid_synth_init() 161{ 162 fluid_synth_initialized++; 163 164#ifdef TRAP_ON_FPE 165 /* Turn on floating point exception traps */ 166 feenableexcept (FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID); 167#endif 168 169 fluid_conversion_config(); 170 171 fluid_dsp_float_config(); 172 173 fluid_sys_config(); 174 175 init_dither(); 176 177 178 /* SF2.01 page 53 section 8.4.1: MIDI Note-On Velocity to Initial Attenuation */ 179 fluid_mod_set_source1(&default_vel2att_mod, /* The modulator we are programming here */ 180 FLUID_MOD_VELOCITY, /* Source. VELOCITY corresponds to 'index=2'. */ 181 FLUID_MOD_GC /* Not a MIDI continuous controller */ 182 | FLUID_MOD_CONCAVE /* Curve shape. Corresponds to 'type=1' */ 183 | FLUID_MOD_UNIPOLAR /* Polarity. Corresponds to 'P=0' */ 184 | FLUID_MOD_NEGATIVE /* Direction. Corresponds to 'D=1' */ 185 ); 186 fluid_mod_set_source2(&default_vel2att_mod, 0, 0); /* No 2nd source */ 187 fluid_mod_set_dest(&default_vel2att_mod, GEN_ATTENUATION); /* Target: Initial attenuation */ 188 fluid_mod_set_amount(&default_vel2att_mod, 960.0); /* Modulation amount: 960 */ 189 190 191 192 /* SF2.01 page 53 section 8.4.2: MIDI Note-On Velocity to Filter Cutoff 193 * Have to make a design decision here. The specs don't make any sense this way or another. 194 * One sound font, 'Kingston Piano', which has been praised for its quality, tries to 195 * override this modulator with an amount of 0 and positive polarity (instead of what 196 * the specs say, D=1) for the secondary source. 197 * So if we change the polarity to 'positive', one of the best free sound fonts works... 198 */ 199 fluid_mod_set_source1(&default_vel2filter_mod, FLUID_MOD_VELOCITY, /* Index=2 */ 200 FLUID_MOD_GC /* CC=0 */ 201 | FLUID_MOD_LINEAR /* type=0 */ 202 | FLUID_MOD_UNIPOLAR /* P=0 */ 203 | FLUID_MOD_NEGATIVE /* D=1 */ 204 ); 205 fluid_mod_set_source2(&default_vel2filter_mod, FLUID_MOD_VELOCITY, /* Index=2 */ 206 FLUID_MOD_GC /* CC=0 */ 207 | FLUID_MOD_SWITCH /* type=3 */ 208 | FLUID_MOD_UNIPOLAR /* P=0 */ 209 // do not remove | FLUID_MOD_NEGATIVE /* D=1 */ 210 | FLUID_MOD_POSITIVE /* D=0 */ 211 ); 212 fluid_mod_set_dest(&default_vel2filter_mod, GEN_FILTERFC); /* Target: Initial filter cutoff */ 213 fluid_mod_set_amount(&default_vel2filter_mod, -2400); 214 215 216 217 /* SF2.01 page 53 section 8.4.3: MIDI Channel pressure to Vibrato LFO pitch depth */ 218 fluid_mod_set_source1(&default_at2viblfo_mod, FLUID_MOD_CHANNELPRESSURE, /* Index=13 */ 219 FLUID_MOD_GC /* CC=0 */ 220 | FLUID_MOD_LINEAR /* type=0 */ 221 | FLUID_MOD_UNIPOLAR /* P=0 */ 222 | FLUID_MOD_POSITIVE /* D=0 */ 223 ); 224 fluid_mod_set_source2(&default_at2viblfo_mod, 0,0); /* no second source */ 225 fluid_mod_set_dest(&default_at2viblfo_mod, GEN_VIBLFOTOPITCH); /* Target: Vib. LFO => pitch */ 226 fluid_mod_set_amount(&default_at2viblfo_mod, 50); 227 228 229 230 /* SF2.01 page 53 section 8.4.4: Mod wheel (Controller 1) to Vibrato LFO pitch depth */ 231 fluid_mod_set_source1(&default_mod2viblfo_mod, 1, /* Index=1 */ 232 FLUID_MOD_CC /* CC=1 */ 233 | FLUID_MOD_LINEAR /* type=0 */ 234 | FLUID_MOD_UNIPOLAR /* P=0 */ 235 | FLUID_MOD_POSITIVE /* D=0 */ 236 ); 237 fluid_mod_set_source2(&default_mod2viblfo_mod, 0,0); /* no second source */ 238 fluid_mod_set_dest(&default_mod2viblfo_mod, GEN_VIBLFOTOPITCH); /* Target: Vib. LFO => pitch */ 239 fluid_mod_set_amount(&default_mod2viblfo_mod, 50); 240 241 242 243 /* SF2.01 page 55 section 8.4.5: MIDI continuous controller 7 to initial attenuation*/ 244 fluid_mod_set_source1(&default_att_mod, 7, /* index=7 */ 245 FLUID_MOD_CC /* CC=1 */ 246 | FLUID_MOD_CONCAVE /* type=1 */ 247 | FLUID_MOD_UNIPOLAR /* P=0 */ 248 | FLUID_MOD_NEGATIVE /* D=1 */ 249 ); 250 fluid_mod_set_source2(&default_att_mod, 0, 0); /* No second source */ 251 fluid_mod_set_dest(&default_att_mod, GEN_ATTENUATION); /* Target: Initial attenuation */ 252 fluid_mod_set_amount(&default_att_mod, 960.0); /* Amount: 960 */ 253 254 255 256 /* SF2.01 page 55 section 8.4.6 MIDI continuous controller 10 to Pan Position */ 257 fluid_mod_set_source1(&default_pan_mod, 10, /* index=10 */ 258 FLUID_MOD_CC /* CC=1 */ 259 | FLUID_MOD_LINEAR /* type=0 */ 260 | FLUID_MOD_BIPOLAR /* P=1 */ 261 | FLUID_MOD_POSITIVE /* D=0 */ 262 ); 263 fluid_mod_set_source2(&default_pan_mod, 0, 0); /* No second source */ 264 fluid_mod_set_dest(&default_pan_mod, GEN_PAN); /* Target: pan */ 265 /* Amount: 500. The SF specs $8.4.6, p. 55 syas: "Amount = 1000 266 tenths of a percent". The center value (64) corresponds to 50%, 267 so it follows that amount = 50% x 1000/% = 500. */ 268 fluid_mod_set_amount(&default_pan_mod, 500.0); 269 270 271 /* SF2.01 page 55 section 8.4.7: MIDI continuous controller 11 to initial attenuation*/ 272 fluid_mod_set_source1(&default_expr_mod, 11, /* index=11 */ 273 FLUID_MOD_CC /* CC=1 */ 274 | FLUID_MOD_CONCAVE /* type=1 */ 275 | FLUID_MOD_UNIPOLAR /* P=0 */ 276 | FLUID_MOD_NEGATIVE /* D=1 */ 277 ); 278 fluid_mod_set_source2(&default_expr_mod, 0, 0); /* No second source */ 279 fluid_mod_set_dest(&default_expr_mod, GEN_ATTENUATION); /* Target: Initial attenuation */ 280 fluid_mod_set_amount(&default_expr_mod, 960.0); /* Amount: 960 */ 281 282 283 284 /* SF2.01 page 55 section 8.4.8: MIDI continuous controller 91 to Reverb send */ 285 fluid_mod_set_source1(&default_reverb_mod, 91, /* index=91 */ 286 FLUID_MOD_CC /* CC=1 */ 287 | FLUID_MOD_LINEAR /* type=0 */ 288 | FLUID_MOD_UNIPOLAR /* P=0 */ 289 | FLUID_MOD_POSITIVE /* D=0 */ 290 ); 291 fluid_mod_set_source2(&default_reverb_mod, 0, 0); /* No second source */ 292 fluid_mod_set_dest(&default_reverb_mod, GEN_REVERBSEND); /* Target: Reverb send */ 293 fluid_mod_set_amount(&default_reverb_mod, 200); /* Amount: 200 ('tenths of a percent') */ 294 295 296 297 /* SF2.01 page 55 section 8.4.9: MIDI continuous controller 93 to Reverb send */ 298 fluid_mod_set_source1(&default_chorus_mod, 93, /* index=93 */ 299 FLUID_MOD_CC /* CC=1 */ 300 | FLUID_MOD_LINEAR /* type=0 */ 301 | FLUID_MOD_UNIPOLAR /* P=0 */ 302 | FLUID_MOD_POSITIVE /* D=0 */ 303 ); 304 fluid_mod_set_source2(&default_chorus_mod, 0, 0); /* No second source */ 305 fluid_mod_set_dest(&default_chorus_mod, GEN_CHORUSSEND); /* Target: Chorus */ 306 fluid_mod_set_amount(&default_chorus_mod, 200); /* Amount: 200 ('tenths of a percent') */ 307 308 309 310 /* SF2.01 page 57 section 8.4.10 MIDI Pitch Wheel to Initial Pitch ... */ 311 fluid_mod_set_source1(&default_pitch_bend_mod, FLUID_MOD_PITCHWHEEL, /* Index=14 */ 312 FLUID_MOD_GC /* CC =0 */ 313 | FLUID_MOD_LINEAR /* type=0 */ 314 | FLUID_MOD_BIPOLAR /* P=1 */ 315 | FLUID_MOD_POSITIVE /* D=0 */ 316 ); 317 fluid_mod_set_source2(&default_pitch_bend_mod, FLUID_MOD_PITCHWHEELSENS, /* Index = 16 */ 318 FLUID_MOD_GC /* CC=0 */ 319 | FLUID_MOD_LINEAR /* type=0 */ 320 | FLUID_MOD_UNIPOLAR /* P=0 */ 321 | FLUID_MOD_POSITIVE /* D=0 */ 322 ); 323 fluid_mod_set_dest(&default_pitch_bend_mod, GEN_PITCH); /* Destination: Initial pitch */ 324 fluid_mod_set_amount(&default_pitch_bend_mod, 12700.0); /* Amount: 12700 cents */ 325} 326 327 328int fluid_synth_verify_settings(fluid_settings_t *settings) 329{ 330 return 0; 331} 332 333/*************************************************************** 334 * 335 * FLUID SYNTH 336 */ 337 338/* 339 * new_fluid_synth 340 */ 341fluid_synth_t* 342new_fluid_synth(fluid_settings_t *settings) 343{ 344 int i; 345 fluid_synth_t* synth; 346 fluid_sfloader_t* loader; 347 348 /* initialize all the conversion tables and other stuff */ 349 if (fluid_synth_initialized == 0) { 350 fluid_synth_init(); 351 } 352 353 fluid_synth_verify_settings(settings); 354 355 /* allocate a new synthesizer object */ 356 synth = FLUID_NEW(fluid_synth_t); 357 if (synth == NULL) { 358 FLUID_LOG(FLUID_ERR, "Out of memory"); 359 return NULL; 360 } 361 FLUID_MEMSET(synth, 0, sizeof(fluid_synth_t)); 362 363 fluid_mutex_init(synth->busy); 364 365 synth->settings = settings; 366 367 synth->with_reverb = fluid_settings_str_equal(settings, "synth.reverb.active", "yes"); 368 synth->with_chorus = fluid_settings_str_equal(settings, "synth.chorus.active", "yes"); 369 synth->verbose = fluid_settings_str_equal(settings, "synth.verbose", "yes"); 370 synth->dump = fluid_settings_str_equal(settings, "synth.dump", "yes"); 371 372 fluid_settings_getint(settings, "synth.polyphony", &synth->polyphony); 373 fluid_settings_getnum(settings, "synth.sample-rate", &synth->sample_rate); 374 fluid_settings_getint(settings, "synth.midi-channels", &synth->midi_channels); 375 fluid_settings_getint(settings, "synth.audio-channels", &synth->audio_channels); 376 fluid_settings_getint(settings, "synth.audio-groups", &synth->audio_groups); 377 fluid_settings_getint(settings, "synth.effects-channels", &synth->effects_channels); 378 fluid_settings_getnum(settings, "synth.gain", &synth->gain); 379 380 /* register the callbacks */ 381 fluid_settings_register_num(settings, "synth.gain", 382 0.2f, 0.0f, 10.0f, 0, 383 (fluid_num_update_t) fluid_synth_update_gain, synth); 384 fluid_settings_register_int(settings, "synth.polyphony", 385 synth->polyphony, 16, 4096, 0, 386 (fluid_int_update_t) fluid_synth_update_polyphony, 387 synth); 388 389 /* do some basic sanity checking on the settings */ 390 391 if (synth->midi_channels % 16 != 0) { 392 int n = synth->midi_channels / 16; 393 synth->midi_channels = (n + 1) * 16; 394 fluid_settings_setint(settings, "synth.midi-channels", synth->midi_channels); 395 FLUID_LOG(FLUID_WARN, "Requested number of MIDI channels is not a multiple of 16. " 396 "I'll increase the number of channels to the next multiple."); 397 } 398 399 if (synth->audio_channels < 1) { 400 FLUID_LOG(FLUID_WARN, "Requested number of audio channels is smaller than 1. " 401 "Changing this setting to 1."); 402 synth->audio_channels = 1; 403 } else if (synth->audio_channels > 128) { 404 FLUID_LOG(FLUID_WARN, "Requested number of audio channels is too big (%d). " 405 "Limiting this setting to 128.", synth->audio_channels); 406 synth->audio_channels = 128; 407 } 408 409 if (synth->audio_groups < 1) { 410 FLUID_LOG(FLUID_WARN, "Requested number of audio groups is smaller than 1. " 411 "Changing this setting to 1."); 412 synth->audio_groups = 1; 413 } else if (synth->audio_groups > 128) { 414 FLUID_LOG(FLUID_WARN, "Requested number of audio groups is too big (%d). " 415 "Limiting this setting to 128.", synth->audio_groups); 416 synth->audio_groups = 128; 417 } 418 419 if (synth->effects_channels != 2) { 420 FLUID_LOG(FLUID_WARN, "Invalid number of effects channels (%d)." 421 "Setting effects channels to 2.", synth->effects_channels); 422 synth->effects_channels = 2; 423 } 424 425 426 /* The number of buffers is determined by the higher number of nr 427 * groups / nr audio channels. If LADSPA is unused, they should be 428 * the same. */ 429 synth->nbuf = synth->audio_channels; 430 if (synth->audio_groups > synth->nbuf) { 431 synth->nbuf = synth->audio_groups; 432 } 433 434#ifdef LADSPA 435 /* Create and initialize the Fx unit.*/ 436 synth->LADSPA_FxUnit = new_fluid_LADSPA_FxUnit(synth); 437#endif 438 439 /* as soon as the synth is created it starts playing. */ 440 synth->state = FLUID_SYNTH_PLAYING; 441 synth->sfont = NULL; 442 synth->noteid = 0; 443 synth->ticks = 0; 444 synth->tuning = NULL; 445 446 /* allocate and add the default sfont loader */ 447 loader = new_fluid_defsfloader(); 448 449 if (loader == NULL) { 450 FLUID_LOG(FLUID_WARN, "Failed to create the default SoundFont loader"); 451 } else { 452 fluid_synth_add_sfloader(synth, loader); 453 } 454 455 /* allocate all channel objects */ 456 synth->channel = FLUID_ARRAY(fluid_channel_t*, synth->midi_channels); 457 if (synth->channel == NULL) { 458 FLUID_LOG(FLUID_ERR, "Out of memory"); 459 goto error_recovery; 460 } 461 for (i = 0; i < synth->midi_channels; i++) { 462 synth->channel[i] = new_fluid_channel(synth, i); 463 if (synth->channel[i] == NULL) { 464 goto error_recovery; 465 } 466 } 467 468 /* allocate all synthesis processes */ 469 synth->nvoice = synth->polyphony; 470 synth->voice = FLUID_ARRAY(fluid_voice_t*, synth->nvoice); 471 if (synth->voice == NULL) { 472 goto error_recovery; 473 } 474 for (i = 0; i < synth->nvoice; i++) { 475 synth->voice[i] = new_fluid_voice(synth->sample_rate); 476 if (synth->voice[i] == NULL) { 477 goto error_recovery; 478 } 479 } 480 481 /* Allocate the sample buffers */ 482 synth->left_buf = NULL; 483 synth->right_buf = NULL; 484 synth->fx_left_buf = NULL; 485 synth->fx_right_buf = NULL; 486 487 /* Left and right audio buffers */ 488 489 synth->left_buf = FLUID_ARRAY(fluid_real_t*, synth->nbuf); 490 synth->right_buf = FLUID_ARRAY(fluid_real_t*, synth->nbuf); 491 492 if ((synth->left_buf == NULL) || (synth->right_buf == NULL)) { 493 FLUID_LOG(FLUID_ERR, "Out of memory"); 494 goto error_recovery; 495 } 496 497 FLUID_MEMSET(synth->left_buf, 0, synth->nbuf * sizeof(fluid_real_t*)); 498 FLUID_MEMSET(synth->right_buf, 0, synth->nbuf * sizeof(fluid_real_t*)); 499 500 for (i = 0; i < synth->nbuf; i++) { 501 502 synth->left_buf[i] = FLUID_ARRAY(fluid_real_t, FLUID_BUFSIZE); 503 synth->right_buf[i] = FLUID_ARRAY(fluid_real_t, FLUID_BUFSIZE); 504 505 if ((synth->left_buf[i] == NULL) || (synth->right_buf[i] == NULL)) { 506 FLUID_LOG(FLUID_ERR, "Out of memory"); 507 goto error_recovery; 508 } 509 } 510 511 /* Effects audio buffers */ 512 513 synth->fx_left_buf = FLUID_ARRAY(fluid_real_t*, synth->effects_channels); 514 synth->fx_right_buf = FLUID_ARRAY(fluid_real_t*, synth->effects_channels); 515 516 if ((synth->fx_left_buf == NULL) || (synth->fx_right_buf == NULL)) { 517 FLUID_LOG(FLUID_ERR, "Out of memory"); 518 goto error_recovery; 519 } 520 521 FLUID_MEMSET(synth->fx_left_buf, 0, 2 * sizeof(fluid_real_t*)); 522 FLUID_MEMSET(synth->fx_right_buf, 0, 2 * sizeof(fluid_real_t*)); 523 524 for (i = 0; i < synth->effects_channels; i++) { 525 synth->fx_left_buf[i] = FLUID_ARRAY(fluid_real_t, FLUID_BUFSIZE); 526 synth->fx_right_buf[i] = FLUID_ARRAY(fluid_real_t, FLUID_BUFSIZE); 527 528 if ((synth->fx_left_buf[i] == NULL) || (synth->fx_right_buf[i] == NULL)) { 529 FLUID_LOG(FLUID_ERR, "Out of memory"); 530 goto error_recovery; 531 } 532 } 533 534 535 synth->cur = FLUID_BUFSIZE; 536 synth->dither_index = 0; 537 538 /* allocate the reverb module */ 539 synth->reverb = new_fluid_revmodel(); 540 if (synth->reverb == NULL) { 541 FLUID_LOG(FLUID_ERR, "Out of memory"); 542 goto error_recovery; 543 } 544 545 fluid_synth_set_reverb(synth, 546 FLUID_REVERB_DEFAULT_ROOMSIZE, 547 FLUID_REVERB_DEFAULT_DAMP, 548 FLUID_REVERB_DEFAULT_WIDTH, 549 FLUID_REVERB_DEFAULT_LEVEL); 550 551 /* allocate the chorus module */ 552 synth->chorus = new_fluid_chorus(synth->sample_rate); 553 if (synth->chorus == NULL) { 554 FLUID_LOG(FLUID_ERR, "Out of memory"); 555 goto error_recovery; 556 } 557 558 /* FIXME */ 559 synth->start = fluid_curtime(); 560 561 return synth; 562 563 error_recovery: 564 delete_fluid_synth(synth); 565 return NULL; 566} 567 568/* 569 * delete_fluid_synth 570 */ 571int 572delete_fluid_synth(fluid_synth_t* synth) 573{ 574 int i, k; 575 fluid_list_t *list; 576 fluid_sfont_t* sfont; 577 fluid_bank_offset_t* bank_offset; 578 fluid_sfloader_t* loader; 579 580 if (synth == NULL) { 581 return FLUID_OK; 582 } 583 584 fluid_profiling_print(); 585 586 synth->state = FLUID_SYNTH_STOPPED; 587 588 /* turn off all voices, needed to unload SoundFont data */ 589 if (synth->voice != NULL) { 590 for (i = 0; i < synth->nvoice; i++) { 591 if (synth->voice[i] && fluid_voice_is_playing (synth->voice[i])) 592 fluid_voice_off (synth->voice[i]); 593 } 594 } 595 596 /* delete all the SoundFonts */ 597 for (list = synth->sfont; list; list = fluid_list_next(list)) { 598 sfont = (fluid_sfont_t*) fluid_list_get(list); 599 delete_fluid_sfont(sfont); 600 } 601 602 delete_fluid_list(synth->sfont); 603 604 /* and the SoundFont offsets */ 605 for (list = synth->bank_offsets; list; list = fluid_list_next(list)) { 606 bank_offset = (fluid_bank_offset_t*) fluid_list_get(list); 607 FLUID_FREE(bank_offset); 608 } 609 610 delete_fluid_list(synth->bank_offsets); 611 612 613 /* delete all the SoundFont loaders */ 614 615 for (list = synth->loaders; list; list = fluid_list_next(list)) { 616 loader = (fluid_sfloader_t*) fluid_list_get(list); 617 fluid_sfloader_delete(loader); 618 } 619 620 delete_fluid_list(synth->loaders); 621 622 623 if (synth->channel != NULL) { 624 for (i = 0; i < synth->midi_channels; i++) { 625 if (synth->channel[i] != NULL) { 626 delete_fluid_channel(synth->channel[i]); 627 } 628 } 629 FLUID_FREE(synth->channel); 630 } 631 632 if (synth->voice != NULL) { 633 for (i = 0; i < synth->nvoice; i++) { 634 if (synth->voice[i] != NULL) { 635 delete_fluid_voice(synth->voice[i]); 636 } 637 } 638 FLUID_FREE(synth->voice); 639 } 640 641 /* free all the sample buffers */ 642 if (synth->left_buf != NULL) { 643 for (i = 0; i < synth->nbuf; i++) { 644 if (synth->left_buf[i] != NULL) { 645 FLUID_FREE(synth->left_buf[i]); 646 } 647 } 648 FLUID_FREE(synth->left_buf); 649 } 650 651 if (synth->right_buf != NULL) { 652 for (i = 0; i < synth->nbuf; i++) { 653 if (synth->right_buf[i] != NULL) { 654 FLUID_FREE(synth->right_buf[i]); 655 } 656 } 657 FLUID_FREE(synth->right_buf); 658 } 659 660 if (synth->fx_left_buf != NULL) { 661 for (i = 0; i < 2; i++) { 662 if (synth->fx_left_buf[i] != NULL) { 663 FLUID_FREE(synth->fx_left_buf[i]); 664 } 665 } 666 FLUID_FREE(synth->fx_left_buf); 667 } 668 669 if (synth->fx_right_buf != NULL) { 670 for (i = 0; i < 2; i++) { 671 if (synth->fx_right_buf[i] != NULL) { 672 FLUID_FREE(synth->fx_right_buf[i]); 673 } 674 } 675 FLUID_FREE(synth->fx_right_buf); 676 } 677 678 /* release the reverb module */ 679 if (synth->reverb != NULL) { 680 delete_fluid_revmodel(synth->reverb); 681 } 682 683 /* release the chorus module */ 684 if (synth->chorus != NULL) { 685 delete_fluid_chorus(synth->chorus); 686 } 687 688 /* free the tunings, if any */ 689 if (synth->tuning != NULL) { 690 for (i = 0; i < 128; i++) { 691 if (synth->tuning[i] != NULL) { 692 for (k = 0; k < 128; k++) { 693 if (synth->tuning[i][k] != NULL) { 694 FLUID_FREE(synth->tuning[i][k]); 695 } 696 } 697 FLUID_FREE(synth->tuning[i]); 698 } 699 } 700 FLUID_FREE(synth->tuning); 701 } 702 703#ifdef LADSPA 704 /* Release the LADSPA Fx unit */ 705 fluid_LADSPA_shutdown(synth->LADSPA_FxUnit); 706 FLUID_FREE(synth->LADSPA_FxUnit); 707#endif 708 709 fluid_mutex_destroy(synth->busy); 710 711 FLUID_FREE(synth); 712 713 return FLUID_OK; 714} 715 716/* 717 * fluid_synth_error 718 * 719 * The error messages are not thread-save, yet. They are still stored 720 * in a global message buffer (see fluid_sys.c). 721 * */ 722char* 723fluid_synth_error(fluid_synth_t* synth) 724{ 725 return fluid_error(); 726} 727 728/* 729 * fluid_synth_noteon 730 */ 731int 732fluid_synth_noteon(fluid_synth_t* synth, int chan, int key, int vel) 733{ 734 fluid_channel_t* channel; 735 int r = FLUID_FAILED; 736 737 /* check the ranges of the arguments */ 738 if ((chan < 0) || (chan >= synth->midi_channels)) { 739 FLUID_LOG(FLUID_WARN, "Channel out of range"); 740 return FLUID_FAILED; 741 } 742 743 /* notes with velocity zero go to noteoff */ 744 if (vel == 0) { 745 return fluid_synth_noteoff(synth, chan, key); 746 } 747 748 channel = synth->channel[chan]; 749 750 /* make sure this channel has a preset */ 751 if (channel->preset == NULL) { 752 if (synth->verbose) { 753 FLUID_LOG(FLUID_INFO, "noteon\t%d\t%d\t%d\t%05d\t%.3f\t%.3f\t%.3f\t%d\t%s", 754 chan, key, vel, 0, 755 (float) synth->ticks / 44100.0f, 756 (fluid_curtime() - synth->start) / 1000.0f, 757 0.0f, 0, "channel has no preset"); 758 } 759 return FLUID_FAILED; 760 } 761 762 /* If there is another voice process on the same channel and key, 763 advance it to the release phase. */ 764 fluid_synth_release_voice_on_same_note(synth, chan, key); 765 766 return fluid_synth_start(synth, synth->noteid++, channel->preset, 0, chan, key, vel); 767} 768 769/* 770 * fluid_synth_noteoff 771 */ 772int 773fluid_synth_noteoff(fluid_synth_t* synth, int chan, int key) 774{ 775 int i; 776 fluid_voice_t* voice; 777 int status = FLUID_FAILED; 778/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 779/* fluid_mutex_unlock(synth->busy); */ 780 781 for (i = 0; i < synth->polyphony; i++) { 782 voice = synth->voice[i]; 783 if (_ON(voice) && (voice->chan == chan) && (voice->key == key)) { 784 if (synth->verbose) { 785 int used_voices = 0; 786 int k; 787 for (k = 0; k < synth->polyphony; k++) { 788 if (!_AVAILABLE(synth->voice[k])) { 789 used_voices++; 790 } 791 } 792 FLUID_LOG(FLUID_INFO, "noteoff\t%d\t%d\t%d\t%05d\t%.3f\t%.3f\t%.3f\t%d", 793 voice->chan, voice->key, 0, voice->id, 794 (float) (voice->start_time + voice->ticks) / 44100.0f, 795 (fluid_curtime() - synth->start) / 1000.0f, 796 (float) voice->ticks / 44100.0f, 797 used_voices); 798 } /* if verbose */ 799 fluid_voice_noteoff(voice); 800 status = FLUID_OK; 801 } /* if voice on */ 802 } /* for all voices */ 803 return status; 804} 805 806/* 807 * fluid_synth_damp_voices 808 */ 809int 810fluid_synth_damp_voices(fluid_synth_t* synth, int chan) 811{ 812 int i; 813 fluid_voice_t* voice; 814 815/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 816/* fluid_mutex_unlock(synth->busy); */ 817 818 for (i = 0; i < synth->polyphony; i++) { 819 voice = synth->voice[i]; 820 if ((voice->chan == chan) && _SUSTAINED(voice)) { 821/* printf("turned off sustained note: chan=%d, key=%d, vel=%d\n", voice->chan, voice->key, voice->vel); */ 822 fluid_voice_noteoff(voice); 823 } 824 } 825 826 return FLUID_OK; 827} 828 829/* 830 * fluid_synth_cc 831 */ 832int 833fluid_synth_cc(fluid_synth_t* synth, int chan, int num, int val) 834{ 835/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 836/* fluid_mutex_unlock(synth->busy); */ 837 838 /* check the ranges of the arguments */ 839 if ((chan < 0) || (chan >= synth->midi_channels)) { 840 FLUID_LOG(FLUID_WARN, "Channel out of range"); 841 return FLUID_FAILED; 842 } 843 if ((num < 0) || (num >= 128)) { 844 FLUID_LOG(FLUID_WARN, "Ctrl out of range"); 845 return FLUID_FAILED; 846 } 847 if ((val < 0) || (val >= 128)) { 848 FLUID_LOG(FLUID_WARN, "Value out of range"); 849 return FLUID_FAILED; 850 } 851 852 if (synth->verbose) { 853 FLUID_LOG(FLUID_INFO, "cc\t%d\t%d\t%d", chan, num, val); 854 } 855 856 /* set the controller value in the channel */ 857 fluid_channel_cc(synth->channel[chan], num, val); 858 859 return FLUID_OK; 860} 861 862/* 863 * fluid_synth_cc 864 */ 865int 866fluid_synth_get_cc(fluid_synth_t* synth, int chan, int num, int* pval) 867{ 868 /* check the ranges of the arguments */ 869 if ((chan < 0) || (chan >= synth->midi_channels)) { 870 FLUID_LOG(FLUID_WARN, "Channel out of range"); 871 return FLUID_FAILED; 872 } 873 if ((num < 0) || (num >= 128)) { 874 FLUID_LOG(FLUID_WARN, "Ctrl out of range"); 875 return FLUID_FAILED; 876 } 877 878 *pval = synth->channel[chan]->cc[num]; 879 return FLUID_OK; 880} 881 882/* 883 * fluid_synth_all_notes_off 884 * 885 * put all notes on this channel into released state. 886 */ 887int 888fluid_synth_all_notes_off(fluid_synth_t* synth, int chan) 889{ 890 int i; 891 fluid_voice_t* voice; 892 893 for (i = 0; i < synth->polyphony; i++) { 894 voice = synth->voice[i]; 895 if (_PLAYING(voice) && (voice->chan == chan)) { 896 fluid_voice_noteoff(voice); 897 } 898 } 899 return FLUID_OK; 900} 901 902/* 903 * fluid_synth_all_sounds_off 904 * 905 * immediately stop all notes on this channel. 906 */ 907int 908fluid_synth_all_sounds_off(fluid_synth_t* synth, int chan) 909{ 910 int i; 911 fluid_voice_t* voice; 912 913 for (i = 0; i < synth->polyphony; i++) { 914 voice = synth->voice[i]; 915 if (_PLAYING(voice) && (voice->chan == chan)) { 916 fluid_voice_off(voice); 917 } 918 } 919 return FLUID_OK; 920} 921 922/* 923 * fluid_synth_system_reset 924 * 925 * Purpose: 926 * Respond to the MIDI command 'system reset' (0xFF, big red 'panic' button) 927 */ 928int 929fluid_synth_system_reset(fluid_synth_t* synth) 930{ 931 int i; 932 fluid_voice_t* voice; 933 934 for (i = 0; i < synth->polyphony; i++) { 935 voice = synth->voice[i]; 936 if (_PLAYING(voice)) { 937 fluid_voice_off(voice); 938 } 939 } 940 941 for (i = 0; i < synth->midi_channels; i++) { 942 fluid_channel_reset(synth->channel[i]); 943 } 944 945 fluid_chorus_reset(synth->chorus); 946 fluid_revmodel_reset(synth->reverb); 947 948 return FLUID_OK; 949} 950 951/* 952 * fluid_synth_modulate_voices 953 * 954 * tell all synthesis processes on this channel to update their 955 * synthesis parameters after a control change. 956 */ 957int 958fluid_synth_modulate_voices(fluid_synth_t* synth, int chan, int is_cc, int ctrl) 959{ 960 int i; 961 fluid_voice_t* voice; 962 963/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 964/* fluid_mutex_unlock(synth->busy); */ 965 966 for (i = 0; i < synth->polyphony; i++) { 967 voice = synth->voice[i]; 968 if (voice->chan == chan) { 969 fluid_voice_modulate(voice, is_cc, ctrl); 970 } 971 } 972 return FLUID_OK; 973} 974 975/* 976 * fluid_synth_modulate_voices_all 977 * 978 * Tell all synthesis processes on this channel to update their 979 * synthesis parameters after an all control off message (i.e. all 980 * controller have been reset to their default value). 981 */ 982int 983fluid_synth_modulate_voices_all(fluid_synth_t* synth, int chan) 984{ 985 int i; 986 fluid_voice_t* voice; 987 988/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 989/* fluid_mutex_unlock(synth->busy); */ 990 991 for (i = 0; i < synth->polyphony; i++) { 992 voice = synth->voice[i]; 993 if (voice->chan == chan) { 994 fluid_voice_modulate_all(voice); 995 } 996 } 997 return FLUID_OK; 998} 999 1000/** 1001 * Set the MIDI channel pressure controller value. 1002 * @param synth FluidSynth instance 1003 * @param chan MIDI channel number 1004 * @param val MIDI channel pressure value (7 bit, 0-127) 1005 * @return FLUID_OK on success 1006 * 1007 * Assign to the MIDI channel pressure controller value on a specific MIDI channel 1008 * in real time. 1009 */ 1010int 1011fluid_synth_channel_pressure(fluid_synth_t* synth, int chan, int val) 1012{ 1013 1014/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 1015/* fluid_mutex_unlock(synth->busy); */ 1016 1017 /* check the ranges of the arguments */ 1018 if ((chan < 0) || (chan >= synth->midi_channels)) { 1019 FLUID_LOG(FLUID_WARN, "Channel out of range"); 1020 return FLUID_FAILED; 1021 } 1022 1023 if (synth->verbose) { 1024 FLUID_LOG(FLUID_INFO, "channelpressure\t%d\t%d", chan, val); 1025 } 1026 1027 /* set the channel pressure value in the channel */ 1028 fluid_channel_pressure(synth->channel[chan], val); 1029 1030 return FLUID_OK; 1031} 1032 1033/** 1034 * Set the MIDI pitch bend controller value. 1035 * @param synth FluidSynth instance 1036 * @param chan MIDI channel number 1037 * @param val MIDI pitch bend value (14 bit, 0-16383 with 8192 being center) 1038 * @return FLUID_OK on success 1039 * 1040 * Assign to the MIDI pitch bend controller value on a specific MIDI channel 1041 * in real time. 1042 */ 1043int 1044fluid_synth_pitch_bend(fluid_synth_t* synth, int chan, int val) 1045{ 1046 1047/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 1048/* fluid_mutex_unlock(synth->busy); */ 1049 1050 /* check the ranges of the arguments */ 1051 if ((chan < 0) || (chan >= synth->midi_channels)) { 1052 FLUID_LOG(FLUID_WARN, "Channel out of range"); 1053 return FLUID_FAILED; 1054 } 1055 1056 if (synth->verbose) { 1057 FLUID_LOG(FLUID_INFO, "pitchb\t%d\t%d", chan, val); 1058 } 1059 1060 /* set the pitch-bend value in the channel */ 1061 fluid_channel_pitch_bend(synth->channel[chan], val); 1062 1063 return FLUID_OK; 1064} 1065 1066/* 1067 * fluid_synth_pitch_bend 1068 */ 1069int 1070fluid_synth_get_pitch_bend(fluid_synth_t* synth, int chan, int* ppitch_bend) 1071{ 1072 /* check the ranges of the arguments */ 1073 if ((chan < 0) || (chan >= synth->midi_channels)) { 1074 FLUID_LOG(FLUID_WARN, "Channel out of range"); 1075 return FLUID_FAILED; 1076 } 1077 1078 *ppitch_bend = synth->channel[chan]->pitch_bend; 1079 return FLUID_OK; 1080} 1081 1082/* 1083 * Fluid_synth_pitch_wheel_sens 1084 */ 1085int 1086fluid_synth_pitch_wheel_sens(fluid_synth_t* synth, int chan, int val) 1087{ 1088 1089 /* check the ranges of the arguments */ 1090 if ((chan < 0) || (chan >= synth->midi_channels)) { 1091 FLUID_LOG(FLUID_WARN, "Channel out of range"); 1092 return FLUID_FAILED; 1093 } 1094 1095 if (synth->verbose) { 1096 FLUID_LOG(FLUID_INFO, "pitchsens\t%d\t%d", chan, val); 1097 } 1098 1099 /* set the pitch-bend value in the channel */ 1100 fluid_channel_pitch_wheel_sens(synth->channel[chan], val); 1101 1102 return FLUID_OK; 1103} 1104 1105/* 1106 * fluid_synth_get_pitch_wheel_sens 1107 * 1108 * Note : this function was added after version 1.0 API freeze. 1109 * So its API is not in the synth.h file. It should be added in some later 1110 * version of fluidsynth. Maybe v2.0 ? -- Antoine Schmitt May 2003 1111 */ 1112 1113int 1114fluid_synth_get_pitch_wheel_sens(fluid_synth_t* synth, int chan, int* pval) 1115{ 1116 1117 // check the ranges of the arguments 1118 if ((chan < 0) || (chan >= synth->midi_channels)) { 1119 FLUID_LOG(FLUID_WARN, "Channel out of range"); 1120 return FLUID_FAILED; 1121 } 1122 1123 // get the pitch-bend value in the channel 1124 *pval = synth->channel[chan]->pitch_wheel_sensitivity; 1125 1126 return FLUID_OK; 1127} 1128 1129/* 1130 * fluid_synth_get_preset 1131 */ 1132fluid_preset_t* 1133fluid_synth_get_preset(fluid_synth_t* synth, unsigned int sfontnum, 1134 unsigned int banknum, unsigned int prognum) 1135{ 1136 fluid_preset_t* preset = NULL; 1137 fluid_sfont_t* sfont = NULL; 1138 fluid_list_t* list = synth->sfont; 1139 int offset; 1140 1141 sfont = fluid_synth_get_sfont_by_id(synth, sfontnum); 1142 1143 if (sfont != NULL) { 1144 offset = fluid_synth_get_bank_offset(synth, sfontnum); 1145 preset = fluid_sfont_get_preset(sfont, banknum - offset, prognum); 1146 if (preset != NULL) { 1147 return preset; 1148 } 1149 } 1150 return NULL; 1151} 1152 1153/* 1154 * fluid_synth_get_preset2 1155 */ 1156fluid_preset_t* 1157fluid_synth_get_preset2(fluid_synth_t* synth, char* sfont_name, 1158 unsigned int banknum, unsigned int prognum) 1159{ 1160 fluid_preset_t* preset = NULL; 1161 fluid_sfont_t* sfont = NULL; 1162 int offset; 1163 1164 sfont = fluid_synth_get_sfont_by_name(synth, sfont_name); 1165 1166 if (sfont != NULL) { 1167 offset = fluid_synth_get_bank_offset(synth, fluid_sfont_get_id(sfont)); 1168 preset = fluid_sfont_get_preset(sfont, banknum - offset, prognum); 1169 if (preset != NULL) { 1170 return preset; 1171 } 1172 } 1173 return NULL; 1174} 1175 1176fluid_preset_t* fluid_synth_find_preset(fluid_synth_t* synth, 1177 unsigned int banknum, 1178 unsigned int prognum) 1179{ 1180 fluid_preset_t* preset = NULL; 1181 fluid_sfont_t* sfont = NULL; 1182 fluid_list_t* list = synth->sfont; 1183 int offset; 1184 1185 while (list) { 1186 1187 sfont = (fluid_sfont_t*) fluid_list_get(list); 1188 offset = fluid_synth_get_bank_offset(synth, fluid_sfont_get_id(sfont)); 1189 preset = fluid_sfont_get_preset(sfont, banknum - offset, prognum); 1190 1191 if (preset != NULL) { 1192 preset->sfont = sfont; /* FIXME */ 1193 return preset; 1194 } 1195 1196 list = fluid_list_next(list); 1197 1198 } 1199 return NULL; 1200} 1201 1202 1203/* 1204 * fluid_synth_program_change 1205 */ 1206int 1207fluid_synth_program_change(fluid_synth_t* synth, int chan, int prognum) 1208{ 1209 fluid_preset_t* preset = NULL; 1210 fluid_channel_t* channel; 1211 unsigned int banknum; 1212 unsigned int sfont_id; 1213 int subst_bank, subst_prog; 1214 1215 if ((prognum < 0) || (prognum >= FLUID_NUM_PROGRAMS) || 1216 (chan < 0) || (chan >= synth->midi_channels)) 1217 { 1218 FLUID_LOG(FLUID_ERR, "Index out of range (chan=%d, prog=%d)", chan, prognum); 1219 return FLUID_FAILED; 1220 } 1221 1222 channel = synth->channel[chan]; 1223 banknum = fluid_channel_get_banknum(channel); 1224 1225 /* inform the channel of the new program number */ 1226 fluid_channel_set_prognum(channel, prognum); 1227 1228 if (synth->verbose) 1229 FLUID_LOG(FLUID_INFO, "prog\t%d\t%d\t%d", chan, banknum, prognum); 1230 1231 /* Special handling of channel 10 (or 9 counting from 0). channel 1232 * 10 is the percussion channel. 1233 * 1234 * FIXME - Shouldn't hard code bank selection for channel 10. I think this 1235 * is a hack for MIDI files that do bank changes in GM mode. Proper way to 1236 * handle this would probably be to ignore bank changes when in GM mode. 1237 */ 1238 if (channel->channum == 9) 1239 preset = fluid_synth_find_preset(synth, DRUM_INST_BANK, prognum); 1240 else preset = fluid_synth_find_preset(synth, banknum, prognum); 1241 1242 /* Fallback to another preset if not found */ 1243 if (!preset) 1244 { 1245 subst_bank = banknum; 1246 subst_prog = prognum; 1247 1248 /* Melodic instrument? */ 1249 if (channel->channum != 9 && banknum != DRUM_INST_BANK) 1250 { 1251 subst_bank = 0; 1252 1253 /* Fallback first to bank 0:prognum */ 1254 preset = fluid_synth_find_preset(synth, 0, prognum); 1255 1256 /* Fallback to first preset in bank 0 */ 1257 if (!preset && prognum != 0) 1258 { 1259 preset = fluid_synth_find_preset(synth, 0, 0); 1260 subst_prog = 0; 1261 } 1262 } 1263 else /* Percussion: Fallback to preset 0 in percussion bank */ 1264 { 1265 preset = fluid_synth_find_preset(synth, DRUM_INST_BANK, 0); 1266 subst_prog = 0; 1267 } 1268 1269 if (preset) 1270 FLUID_LOG(FLUID_WARN, "Instrument not found on channel %d [bank=%d prog=%d], substituted [bank=%d prog=%d]", 1271 chan, banknum, prognum, subst_bank, subst_prog); 1272 } 1273 1274 sfont_id = preset? fluid_sfont_get_id(preset->sfont) : 0; 1275 fluid_channel_set_sfontnum(channel, sfont_id); 1276 fluid_channel_set_preset(channel, preset); 1277 1278 return FLUID_OK; 1279} 1280 1281/* 1282 * fluid_synth_bank_select 1283 */ 1284int fluid_synth_bank_select(fluid_synth_t* synth, int chan, unsigned int bank) 1285{ 1286 if ((chan >= 0) && (chan < synth->midi_channels)) { 1287 fluid_channel_set_banknum(synth->channel[chan], bank); 1288 return FLUID_OK; 1289 } 1290 return FLUID_FAILED; 1291} 1292 1293 1294/* 1295 * fluid_synth_sfont_select 1296 */ 1297int fluid_synth_sfont_select(fluid_synth_t* synth, int chan, unsigned int sfont_id) 1298{ 1299 if ((chan >= 0) && (chan < synth->midi_channels)) { 1300 fluid_channel_set_sfontnum(synth->channel[chan], sfont_id); 1301 return FLUID_OK; 1302 } 1303 return FLUID_FAILED; 1304} 1305 1306/* 1307 * fluid_synth_get_program 1308 */ 1309int 1310fluid_synth_get_program(fluid_synth_t* synth, int chan, 1311 unsigned int* sfont_id, unsigned int* bank_num, unsigned int* preset_num) 1312{ 1313 fluid_channel_t* channel; 1314 if ((chan >= 0) && (chan < synth->midi_channels)) { 1315 channel = synth->channel[chan]; 1316 *sfont_id = fluid_channel_get_sfontnum(channel); 1317 *bank_num = fluid_channel_get_banknum(channel); 1318 *preset_num = fluid_channel_get_prognum(channel); 1319 return FLUID_OK; 1320 } 1321 return FLUID_FAILED; 1322} 1323 1324/* 1325 * fluid_synth_program_select 1326 */ 1327int fluid_synth_program_select(fluid_synth_t* synth, 1328 int chan, 1329 unsigned int sfont_id, 1330 unsigned int bank_num, 1331 unsigned int preset_num) 1332{ 1333 fluid_preset_t* preset = NULL; 1334 fluid_channel_t* channel; 1335 1336 if ((chan < 0) || (chan >= synth->midi_channels)) { 1337 FLUID_LOG(FLUID_ERR, "Channel number out of range (chan=%d)", chan); 1338 return FLUID_FAILED; 1339 } 1340 channel = synth->channel[chan]; 1341 1342 preset = fluid_synth_get_preset(synth, sfont_id, bank_num, preset_num); 1343 if (preset == NULL) { 1344 FLUID_LOG(FLUID_ERR, 1345 "There is no preset with bank number %d and preset number %d in SoundFont %d", 1346 bank_num, preset_num, sfont_id); 1347 return FLUID_FAILED; 1348 } 1349 1350 /* inform the channel of the new bank and program number */ 1351 fluid_channel_set_sfontnum(channel, sfont_id); 1352 fluid_channel_set_banknum(channel, bank_num); 1353 fluid_channel_set_prognum(channel, preset_num); 1354 1355 fluid_channel_set_preset(channel, preset); 1356 1357 return FLUID_OK; 1358} 1359 1360/* 1361 * fluid_synth_program_select2 1362 */ 1363int fluid_synth_program_select2(fluid_synth_t* synth, 1364 int chan, 1365 char* sfont_name, 1366 unsigned int bank_num, 1367 unsigned int preset_num) 1368{ 1369 fluid_preset_t* preset = NULL; 1370 fluid_channel_t* channel; 1371 fluid_sfont_t* sfont = NULL; 1372 int offset; 1373 1374 if ((chan < 0) || (chan >= synth->midi_channels)) { 1375 FLUID_LOG(FLUID_ERR, "Channel number out of range (chan=%d)", chan); 1376 return FLUID_FAILED; 1377 } 1378 channel = synth->channel[chan]; 1379 1380 sfont = fluid_synth_get_sfont_by_name(synth, sfont_name); 1381 if (sfont == NULL) { 1382 FLUID_LOG(FLUID_ERR, "Could not find SoundFont %s", sfont_name); 1383 return FLUID_FAILED; 1384 } 1385 1386 offset = fluid_synth_get_bank_offset(synth, fluid_sfont_get_id(sfont)); 1387 preset = fluid_sfont_get_preset(sfont, bank_num - offset, preset_num); 1388 if (preset == NULL) { 1389 FLUID_LOG(FLUID_ERR, 1390 "There is no preset with bank number %d and preset number %d in SoundFont %s", 1391 bank_num, preset_num, sfont_name); 1392 return FLUID_FAILED; 1393 } 1394 1395 /* inform the channel of the new bank and program number */ 1396 fluid_channel_set_sfontnum(channel, fluid_sfont_get_id(sfont)); 1397 fluid_channel_set_banknum(channel, bank_num); 1398 fluid_channel_set_prognum(channel, preset_num); 1399 1400 fluid_channel_set_preset(channel, preset); 1401 1402 return FLUID_OK; 1403} 1404 1405/* 1406 * fluid_synth_update_presets 1407 */ 1408void fluid_synth_update_presets(fluid_synth_t* synth) 1409{ 1410 int chan; 1411 fluid_channel_t* channel; 1412 1413 for (chan = 0; chan < synth->midi_channels; chan++) { 1414 channel = synth->channel[chan]; 1415 fluid_channel_set_preset(channel, 1416 fluid_synth_get_preset(synth, 1417 fluid_channel_get_sfontnum(channel), 1418 fluid_channel_get_banknum(channel), 1419 fluid_channel_get_prognum(channel))); 1420 } 1421} 1422 1423 1424/* 1425 * fluid_synth_update_gain 1426 */ 1427int fluid_synth_update_gain(fluid_synth_t* synth, char* name, double value) 1428{ 1429 fluid_synth_set_gain(synth, (float) value); 1430 return 0; 1431} 1432 1433/* 1434 * fluid_synth_set_gain 1435 */ 1436void fluid_synth_set_gain(fluid_synth_t* synth, float gain) 1437{ 1438 int i; 1439 1440 fluid_clip(gain, 0.0f, 10.0f); 1441 synth->gain = gain; 1442 1443 for (i = 0; i < synth->polyphony; i++) { 1444 fluid_voice_t* voice = synth->voice[i]; 1445 if (_PLAYING(voice)) { 1446 fluid_voice_set_gain(voice, gain); 1447 } 1448 } 1449} 1450 1451/* 1452 * fluid_synth_get_gain 1453 */ 1454float fluid_synth_get_gain(fluid_synth_t* synth) 1455{ 1456 return synth->gain; 1457} 1458 1459/* 1460 * fluid_synth_update_polyphony 1461 */ 1462int fluid_synth_update_polyphony(fluid_synth_t* synth, char* name, int value) 1463{ 1464 fluid_synth_set_polyphony(synth, value); 1465 return 0; 1466} 1467 1468/* 1469 * fluid_synth_set_polyphony 1470 */ 1471int fluid_synth_set_polyphony(fluid_synth_t* synth, int polyphony) 1472{ 1473 int i; 1474 1475 if (polyphony < 1 || polyphony > synth->nvoice) { 1476 return FLUID_FAILED; 1477 } 1478 1479 /* turn off any voices above the new limit */ 1480 for (i = polyphony; i < synth->nvoice; i++) { 1481 fluid_voice_t* voice = synth->voice[i]; 1482 if (_PLAYING(voice)) { 1483 fluid_voice_off(voice); 1484 } 1485 } 1486 1487 synth->polyphony = polyphony; 1488 1489 return FLUID_OK; 1490} 1491 1492/* 1493 * fluid_synth_get_polyphony 1494 */ 1495int fluid_synth_get_polyphony(fluid_synth_t* synth) 1496{ 1497 return synth->polyphony; 1498} 1499 1500/* 1501 * fluid_synth_get_internal_buffer_size 1502 */ 1503int fluid_synth_get_internal_bufsize(fluid_synth_t* synth) 1504{ 1505 return FLUID_BUFSIZE; 1506} 1507 1508/* 1509 * fluid_synth_program_reset 1510 * 1511 * Resend a bank select and a program change for every channel. This 1512 * function is called mainly after a SoundFont has been loaded, 1513 * unloaded or reloaded. */ 1514int 1515fluid_synth_program_reset(fluid_synth_t* synth) 1516{ 1517 int i; 1518 /* try to set the correct presets */ 1519 for (i = 0; i < synth->midi_channels; i++){ 1520 fluid_synth_program_change(synth, i, fluid_channel_get_prognum(synth->channel[i])); 1521 } 1522 return FLUID_OK; 1523} 1524 1525/* 1526 * fluid_synth_set_reverb_preset 1527 */ 1528int fluid_synth_set_reverb_preset(fluid_synth_t* synth, int num) 1529{ 1530 int i = 0; 1531 while (revmodel_preset[i].name != NULL) { 1532 if (i == num) { 1533 fluid_revmodel_setroomsize(synth->reverb, revmodel_preset[i].roomsize); 1534 fluid_revmodel_setdamp(synth->reverb, revmodel_preset[i].damp); 1535 fluid_revmodel_setwidth(synth->reverb, revmodel_preset[i].width); 1536 fluid_revmodel_setlevel(synth->reverb, revmodel_preset[i].level); 1537 return FLUID_OK; 1538 } 1539 i++; 1540 } 1541 return FLUID_FAILED; 1542} 1543 1544/* 1545 * fluid_synth_set_reverb 1546 */ 1547void fluid_synth_set_reverb(fluid_synth_t* synth, double roomsize, double damping, 1548 double width, double level) 1549{ 1550/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 1551/* fluid_mutex_unlock(synth->busy); */ 1552 1553 fluid_revmodel_setroomsize(synth->reverb, roomsize); 1554 fluid_revmodel_setdamp(synth->reverb, damping); 1555 fluid_revmodel_setwidth(synth->reverb, width); 1556 fluid_revmodel_setlevel(synth->reverb, level); 1557} 1558 1559/* 1560 * fluid_synth_set_chorus 1561 */ 1562void fluid_synth_set_chorus(fluid_synth_t* synth, int nr, double level, 1563 double speed, double depth_ms, int type) 1564{ 1565/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 1566/* fluid_mutex_unlock(synth->busy); */ 1567 1568 fluid_chorus_set_nr(synth->chorus, nr); 1569 fluid_chorus_set_level(synth->chorus, (fluid_real_t)level); 1570 fluid_chorus_set_speed_Hz(synth->chorus, (fluid_real_t)speed); 1571 fluid_chorus_set_depth_ms(synth->chorus, (fluid_real_t)depth_ms); 1572 fluid_chorus_set_type(synth->chorus, type); 1573 fluid_chorus_update(synth->chorus); 1574} 1575 1576/****************************************************** 1577 1578#define COMPRESS 1 1579#define COMPRESS_X1 4.0 1580#define COMPRESS_Y1 0.6 1581#define COMPRESS_X2 10.0 1582#define COMPRESS_Y2 1.0 1583 1584 len2 = 2 * len; 1585 alpha1 = COMPRESS_Y1 / COMPRESS_X1; 1586 alpha2 = (COMPRESS_Y2 - COMPRESS_Y1) / (COMPRESS_X2 - COMPRESS_X1); 1587 if (COMPRESS_X1 == COMPRESS_Y1) { 1588 for (j = 0; j < len2; j++) { 1589 if (buf[j] > COMPRESS_X1) { 1590 if (buf[j] > COMPRESS_X2) { 1591 buf[j] = COMPRESS_Y2; 1592 } else { 1593 buf[j] = COMPRESS_Y1 + alpha2 * (buf[j] - COMPRESS_X1); 1594 } 1595 } else if (buf[j] < -COMPRESS_X1) { 1596 if (buf[j] < -COMPRESS_X2) { 1597 buf[j] = -COMPRESS_Y2; 1598 } else { 1599 buf[j] = -COMPRESS_Y1 + alpha2 * (buf[j] + COMPRESS_X1); 1600 } 1601 } 1602 } 1603 } else { 1604 for (j = 0; j < len2; j++) { 1605 if ((buf[j] >= -COMPRESS_X1) && (buf[j] <= COMPRESS_X1)) { 1606 buf[j] *= alpha1; 1607 } else if (buf[j] > COMPRESS_X1) { 1608 if (buf[j] > COMPRESS_X2) { 1609 buf[j] = COMPRESS_Y2; 1610 } else { 1611 buf[j] = COMPRESS_Y1 + alpha2 * (buf[j] - COMPRESS_X1); 1612 } 1613 } else { 1614 if (buf[j] < -COMPRESS_X2) { 1615 buf[j] = -COMPRESS_Y2; 1616 } else { 1617 buf[j] = -COMPRESS_Y1 + alpha2 * (buf[j] + COMPRESS_X1); 1618 } 1619 } 1620 } 1621 } 1622 1623***************************************************/ 1624 1625/* 1626 * fluid_synth_nwrite_float 1627 */ 1628int 1629fluid_synth_nwrite_float(fluid_synth_t* synth, int len, 1630 float** left, float** right, 1631 float** fx_left, float** fx_right) 1632{ 1633 fluid_real_t** left_in = synth->left_buf; 1634 fluid_real_t** right_in = synth->right_buf; 1635 double time = fluid_utime(); 1636 int i, num, available, count, bytes; 1637 1638 /* make sure we're playing */ 1639 if (synth->state != FLUID_SYNTH_PLAYING) { 1640 return 0; 1641 } 1642 1643 /* First, take what's still available in the buffer */ 1644 count = 0; 1645 num = synth->cur; 1646 if (synth->cur < FLUID_BUFSIZE) { 1647 available = FLUID_BUFSIZE - synth->cur; 1648 1649 num = (available > len)? len : available; 1650 bytes = num * sizeof(float); 1651 1652 for (i = 0; i < synth->audio_channels; i++) { 1653 FLUID_MEMCPY(left[i], left_in[i] + synth->cur, bytes); 1654 FLUID_MEMCPY(right[i], right_in[i] + synth->cur, bytes); 1655 } 1656 count += num; 1657 num += synth->cur; /* if we're now done, num becomes the new synth->cur below */ 1658 } 1659 1660 /* Then, run one_block() and copy till we have 'len' samples */ 1661 while (count < len) { 1662 fluid_synth_one_block(synth, 1); 1663 1664 num = (FLUID_BUFSIZE > len - count)? len - count : FLUID_BUFSIZE; 1665 bytes = num * sizeof(float); 1666 1667 for (i = 0; i < synth->audio_channels; i++) { 1668 FLUID_MEMCPY(left[i] + count, left_in[i], bytes); 1669 FLUID_MEMCPY(right[i] + count, right_in[i], bytes); 1670 } 1671 1672 count += num; 1673 } 1674 1675 synth->cur = num; 1676 1677 time = fluid_utime() - time; 1678 synth->cpu_load = 0.5 * (synth->cpu_load + 1679 time * synth->sample_rate / len / 10000.0); 1680 1681/* printf("CPU: %.2f\n", synth->cpu_load); */ 1682 1683 return 0; 1684} 1685 1686 1687int fluid_synth_process(fluid_synth_t* synth, int len, 1688 int nin, float** in, 1689 int nout, float** out) 1690{ 1691 if (nout==2) { 1692 return fluid_synth_write_float(synth, len, out[0], 0, 1, out[1], 0, 1); 1693 } 1694 else { 1695 float **left, **right; 1696 int i; 1697 left = FLUID_ARRAY(float*, nout/2); 1698 right = FLUID_ARRAY(float*, nout/2); 1699 for(i=0; i<nout/2; i++) { 1700 left[i] = out[2*i]; 1701 right[i] = out[2*i+1]; 1702 } 1703 fluid_synth_nwrite_float(synth, len, left, right, NULL, NULL); 1704 FLUID_FREE(left); 1705 FLUID_FREE(right); 1706 return 0; 1707 } 1708} 1709 1710 1711/* 1712 * fluid_synth_write_float 1713 */ 1714int 1715fluid_synth_write_float(fluid_synth_t* synth, int len, 1716 void* lout, int loff, int lincr, 1717 void* rout, int roff, int rincr) 1718{ 1719 int i, j, k, l; 1720 float* left_out = (float*) lout; 1721 float* right_out = (float*) rout; 1722 fluid_real_t* left_in = synth->left_buf[0]; 1723 fluid_real_t* right_in = synth->right_buf[0]; 1724 double time = fluid_utime(); 1725 1726 /* make sure we're playing */ 1727 if (synth->state != FLUID_SYNTH_PLAYING) { 1728 return 0; 1729 } 1730 1731 l = synth->cur; 1732 1733 for (i = 0, j = loff, k = roff; i < len; i++, l++, j += lincr, k += rincr) { 1734 /* fill up the buffers as needed */ 1735 if (l == FLUID_BUFSIZE) { 1736 fluid_synth_one_block(synth, 0); 1737 l = 0; 1738 } 1739 1740 left_out[j] = (float) left_in[l]; 1741 right_out[k] = (float) right_in[l]; 1742 } 1743 1744 synth->cur = l; 1745 1746 time = fluid_utime() - time; 1747 synth->cpu_load = 0.5 * (synth->cpu_load + 1748 time * synth->sample_rate / len / 10000.0); 1749 1750/* printf("CPU: %.2f\n", synth->cpu_load); */ 1751 1752 return 0; 1753} 1754 1755#define DITHER_SIZE 48000 1756#define DITHER_CHANNELS 2 1757 1758static float rand_table[DITHER_CHANNELS][DITHER_SIZE]; 1759 1760static void init_dither(void) 1761{ 1762 float d, dp; 1763 int c, i; 1764 1765 for (c = 0; c < DITHER_CHANNELS; c++) { 1766 dp = 0; 1767 for (i = 0; i < DITHER_SIZE-1; i++) { 1768 d = rand() / (float)RAND_MAX - 0.5f; 1769 rand_table[c][i] = d - dp; 1770 dp = d; 1771 } 1772 rand_table[c][DITHER_SIZE-1] = 0 - dp; 1773 } 1774} 1775 1776/* A portable replacement for roundf(), seems it may actually be faster too! */ 1777static inline int 1778roundi (float x) 1779{ 1780 if (x >= 0.0f) 1781 return (int)(x+0.5f); 1782 else 1783 return (int)(x-0.5f); 1784} 1785 1786/* 1787 * fluid_synth_write_s16 1788 */ 1789int 1790fluid_synth_write_s16(fluid_synth_t* synth, int len, 1791 void* lout, int loff, int lincr, 1792 void* rout, int roff, int rincr) 1793{ 1794 int i, j, k, cur; 1795 signed short* left_out = (signed short*) lout; 1796 signed short* right_out = (signed short*) rout; 1797 fluid_real_t* left_in = synth->left_buf[0]; 1798 fluid_real_t* right_in = synth->right_buf[0]; 1799 double prof_ref = fluid_profile_ref(); 1800 fluid_real_t left_sample; 1801 fluid_real_t right_sample; 1802 double time = fluid_utime(); 1803 int di = synth->dither_index; 1804 double prof_ref_on_block; 1805 1806 /* make sure we're playing */ 1807 if (synth->state != FLUID_SYNTH_PLAYING) { 1808 return 0; 1809 } 1810 1811 cur = synth->cur; 1812 1813 for (i = 0, j = loff, k = roff; i < len; i++, cur++, j += lincr, k += rincr) { 1814 1815 /* fill up the buffers as needed */ 1816 if (cur == FLUID_BUFSIZE) { 1817 prof_ref_on_block = fluid_profile_ref(); 1818 1819 fluid_synth_one_block(synth, 0); 1820 cur = 0; 1821 1822 fluid_profile(FLUID_PROF_ONE_BLOCK, prof_ref_on_block); 1823 } 1824 1825 left_sample = roundi (left_in[cur] * 32766.0f + rand_table[0][di]); 1826 right_sample = roundi (right_in[cur] * 32766.0f + rand_table[1][di]); 1827 1828 di++; 1829 if (di >= DITHER_SIZE) di = 0; 1830 1831 /* digital clipping */ 1832 if (left_sample > 32767.0f) left_sample = 32767.0f; 1833 if (left_sample < -32768.0f) left_sample = -32768.0f; 1834 if (right_sample > 32767.0f) right_sample = 32767.0f; 1835 if (right_sample < -32768.0f) right_sample = -32768.0f; 1836 1837 left_out[j] = (signed short) left_sample; 1838 right_out[k] = (signed short) right_sample; 1839 } 1840 1841 synth->cur = cur; 1842 synth->dither_index = di; /* keep dither buffer continous */ 1843 1844 fluid_profile(FLUID_PROF_WRITE_S16, prof_ref); 1845 1846 1847 time = fluid_utime() - time; 1848 synth->cpu_load = 0.5 * (synth->cpu_load + 1849 time * synth->sample_rate / len / 10000.0); 1850 1851/* printf("CPU: %.2f\n", synth->cpu_load); */ 1852 1853 return 0; 1854} 1855 1856/* 1857 * fluid_synth_dither_s16 1858 * Converts stereo floating point sample data to signed 16 bit data with 1859 * dithering. 'dither_index' parameter is a caller supplied pointer to an 1860 * integer which should be initialized to 0 before the first call and passed 1861 * unmodified to additional calls which are part of the same synthesis output. 1862 * Only used internally currently. 1863 */ 1864void 1865fluid_synth_dither_s16(int *dither_index, int len, float* lin, float* rin, 1866 void* lout, int loff, int lincr, 1867 void* rout, int roff, int rincr) 1868{ 1869 int i, j, k; 1870 signed short* left_out = (signed short*) lout; 1871 signed short* right_out = (signed short*) rout; 1872 double prof_ref = fluid_profile_ref(); 1873 fluid_real_t left_sample; 1874 fluid_real_t right_sample; 1875 int di = *dither_index; 1876 1877 for (i = 0, j = loff, k = roff; i < len; i++, j += lincr, k += rincr) { 1878 1879 left_sample = roundi (lin[i] * 32766.0f + rand_table[0][di]); 1880 right_sample = roundi (rin[i] * 32766.0f + rand_table[1][di]); 1881 1882 di++; 1883 if (di >= DITHER_SIZE) di = 0; 1884 1885 /* digital clipping */ 1886 if (left_sample > 32767.0f) left_sample = 32767.0f; 1887 if (left_sample < -32768.0f) left_sample = -32768.0f; 1888 if (right_sample > 32767.0f) right_sample = 32767.0f; 1889 if (right_sample < -32768.0f) right_sample = -32768.0f; 1890 1891 left_out[j] = (signed short) left_sample; 1892 right_out[k] = (signed short) right_sample; 1893 } 1894 1895 *dither_index = di; /* keep dither buffer continous */ 1896 1897 fluid_profile(FLUID_PROF_WRITE_S16, prof_ref); 1898} 1899 1900/* 1901 * fluid_synth_one_block 1902 */ 1903int 1904fluid_synth_one_block(fluid_synth_t* synth, int do_not_mix_fx_to_out) 1905{ 1906 int i, auchan; 1907 fluid_voice_t* voice; 1908 fluid_real_t* left_buf; 1909 fluid_real_t* right_buf; 1910 fluid_real_t* reverb_buf; 1911 fluid_real_t* chorus_buf; 1912 int byte_size = FLUID_BUFSIZE * sizeof(fluid_real_t); 1913 double prof_ref = fluid_profile_ref(); 1914 1915/* fluid_mutex_lock(synth->busy); /\* Here comes the audio thread. Lock the synth. *\/ */ 1916 1917 fluid_check_fpe("??? Just starting up ???"); 1918 1919 /* clean the audio buffers */ 1920 for (i = 0; i < synth->nbuf; i++) { 1921 FLUID_MEMSET(synth->left_buf[i], 0, byte_size); 1922 FLUID_MEMSET(synth->right_buf[i], 0, byte_size); 1923 } 1924 1925 for (i = 0; i < synth->effects_channels; i++) { 1926 FLUID_MEMSET(synth->fx_left_buf[i], 0, byte_size); 1927 FLUID_MEMSET(synth->fx_right_buf[i], 0, byte_size); 1928 } 1929 1930 /* Set up the reverb / chorus buffers only, when the effect is 1931 * enabled on synth level. Nonexisting buffers are detected in the 1932 * DSP loop. Not sending the reverb / chorus signal saves some time 1933 * in that case. */ 1934 reverb_buf = synth->with_reverb ? synth->fx_left_buf[0] : NULL; 1935 chorus_buf = synth->with_chorus ? synth->fx_left_buf[1] : NULL; 1936 1937 fluid_profile(FLUID_PROF_ONE_BLOCK_CLEAR, prof_ref); 1938 1939 /* call all playing synthesis processes */ 1940 for (i = 0; i < synth->polyphony; i++) { 1941 voice = synth->voice[i]; 1942 1943 if (_PLAYING(voice)) { 1944 double prof_ref_voice = fluid_profile_ref(); 1945 1946 /* The output associated with a MIDI channel is wrapped around 1947 * using the number of audio groups as modulo divider. This is 1948 * typically the number of output channels on the 'sound card', 1949 * as long as the LADSPA Fx unit is not used. In case of LADSPA 1950 * unit, think of it as subgroups on a mixer. 1951 * 1952 * For example: Assume that the number of groups is set to 2. 1953 * Then MIDI channel 1, 3, 5, 7 etc. go to output 1, channels 2, 1954 * 4, 6, 8 etc to output 2. Or assume 3 groups: Then MIDI 1955 * channels 1, 4, 7, 10 etc go to output 1; 2, 5, 8, 11 etc to 1956 * output 2, 3, 6, 9, 12 etc to output 3. 1957 */ 1958 auchan = fluid_channel_get_num(fluid_voice_get_channel(voice)); 1959 auchan %= synth->audio_groups; 1960 left_buf = synth->left_buf[auchan]; 1961 right_buf = synth->right_buf[auchan]; 1962 1963 fluid_voice_write(voice, left_buf, right_buf, reverb_buf, chorus_buf); 1964 1965 fluid_profile(FLUID_PROF_ONE_BLOCK_VOICE, prof_ref_voice); 1966 } 1967 } 1968 1969 fluid_check_fpe("Synthesis processes"); 1970 1971 fluid_profile(FLUID_PROF_ONE_BLOCK_VOICES, prof_ref); 1972 1973 /* if multi channel output, don't mix the output of the chorus and 1974 reverb in the final output. The effects outputs are send 1975 separately. */ 1976 1977 if (do_not_mix_fx_to_out) { 1978 1979 /* send to reverb */ 1980 if (reverb_buf) { 1981 fluid_revmodel_processreplace(synth->reverb, reverb_buf, 1982 synth->fx_left_buf[0], synth->fx_right_buf[0]); 1983 fluid_check_fpe("Reverb"); 1984 } 1985 1986 fluid_profile(FLUID_PROF_ONE_BLOCK_REVERB, prof_ref); 1987 1988 /* send to chorus */ 1989 if (chorus_buf) { 1990 fluid_chorus_processreplace(synth->chorus, chorus_buf, 1991 synth->fx_left_buf[1], synth->fx_right_buf[1]); 1992 fluid_check_fpe("Chorus"); 1993 } 1994 1995 } else { 1996 1997 /* send to reverb */ 1998 if (reverb_buf) { 1999 fluid_revmodel_processmix(synth->reverb, reverb_buf, 2000 synth->left_buf[0], synth->right_buf[0]); 2001 fluid_check_fpe("Reverb"); 2002 } 2003 2004 fluid_profile(FLUID_PROF_ONE_BLOCK_REVERB, prof_ref); 2005 2006 /* send to chorus */ 2007 if (chorus_buf) { 2008 fluid_chorus_processmix(synth->chorus, chorus_buf, 2009 synth->left_buf[0], synth->right_buf[0]); 2010 fluid_check_fpe("Chorus"); 2011 } 2012 } 2013 2014 fluid_profile(FLUID_PROF_ONE_BLOCK_CHORUS, prof_ref); 2015 2016#ifdef LADSPA 2017 /* Run the signal through the LADSPA Fx unit */ 2018 fluid_LADSPA_run(synth->LADSPA_FxUnit, synth->left_buf, synth->right_buf, synth->fx_left_buf, synth->fx_right_buf); 2019 fluid_check_fpe("LADSPA"); 2020#endif 2021 2022 synth->ticks += FLUID_BUFSIZE; 2023 2024 /* Testcase, that provokes a denormal floating point error */ 2025#if 0 2026 {float num=1;while (num != 0){num*=0.5;};}; 2027#endif 2028 fluid_check_fpe("??? Remainder of synth_one_block ???"); 2029 2030/* fluid_mutex_unlock(synth->busy); /\* Allow other threads to touch the synth *\/ */ 2031 2032 return 0; 2033} 2034 2035 2036/* 2037 * fluid_synth_free_voice_by_kill 2038 * 2039 * selects a voice for killing. the selection algorithm is a refinement 2040 * of the algorithm previously in fluid_synth_alloc_voice. 2041 */ 2042fluid_voice_t* 2043fluid_synth_free_voice_by_kill(fluid_synth_t* synth) 2044{ 2045 int i; 2046 fluid_real_t best_prio = 999999.; 2047 fluid_real_t this_voice_prio; 2048 fluid_voice_t* voice; 2049 int best_voice_index=-1; 2050 2051/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 2052/* fluid_mutex_unlock(synth->busy); */ 2053 2054 for (i = 0; i < synth->polyphony; i++) { 2055 2056 voice = synth->voice[i]; 2057 2058 /* safeguard against an available voice. */ 2059 if (_AVAILABLE(voice)) { 2060 return voice; 2061 } 2062 2063 /* Determine, how 'important' a voice is. 2064 * Start with an arbitrary number */ 2065 this_voice_prio = 10000.; 2066 2067 /* Is this voice on the drum channel? 2068 * Then it is very important. 2069 * Also, forget about the released-note condition: 2070 * Typically, drum notes are triggered only very briefly, they run most 2071 * of the time in release phase. 2072 */ 2073 if (voice->chan == 9){ 2074 this_voice_prio += 4000; 2075 2076 } else if (_RELEASED(voice)){ 2077 /* The key for this voice has been released. Consider it much less important 2078 * than a voice, which is still held. 2079 */ 2080 this_voice_prio -= 2000.; 2081 } 2082 2083 if (_SUSTAINED(voice)){ 2084 /* The sustain pedal is held down on this channel. 2085 * Consider it less important than non-sustained channels. 2086 * This decision is somehow subjective. But usually the sustain pedal 2087 * is used to play 'more-voices-than-fingers', so it shouldn't hurt 2088 * if we kill one voice. 2089 */ 2090 this_voice_prio -= 1000; 2091 } 2092 2093 /* We are not enthusiastic about releasing voices, which have just been started. 2094 * Otherwise hitting a chord may result in killing notes belonging to that very same 2095 * chord. 2096 * So subtract the age of the voice from the priority - an older voice is just a little 2097 * bit less important than a younger voice. 2098 * This is a number between roughly 0 and 100.*/ 2099 this_voice_prio -= (synth->noteid - fluid_voice_get_id(voice)); 2100 2101 /* take a rough estimate of loudness into account. Louder voices are more important. */ 2102 if (voice->volenv_section != FLUID_VOICE_ENVATTACK){ 2103 this_voice_prio += voice->volenv_val * 1000.; 2104 } 2105 2106 /* check if this voice has less priority than the previous candidate. */ 2107 if (this_voice_prio < best_prio) 2108 best_voice_index = i, 2109 best_prio = this_voice_prio; 2110 } 2111 2112 if (best_voice_index < 0) { 2113 return NULL; 2114 } 2115 2116 voice = synth->voice[best_voice_index]; 2117 fluid_voice_off(voice); 2118 2119 return voice; 2120} 2121 2122/* 2123 * fluid_synth_alloc_voice 2124 */ 2125fluid_voice_t* 2126fluid_synth_alloc_voice(fluid_synth_t* synth, fluid_sample_t* sample, int chan, int key, int vel) 2127{ 2128 int i, k; 2129 fluid_voice_t* voice = NULL; 2130 fluid_channel_t* channel = NULL; 2131 2132/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 2133/* fluid_mutex_unlock(synth->busy); */ 2134 2135 /* check if there's an available synthesis process */ 2136 for (i = 0; i < synth->polyphony; i++) { 2137 if (_AVAILABLE(synth->voice[i])) { 2138 voice = synth->voice[i]; 2139 break; 2140 } 2141 } 2142 2143 /* No success yet? Then stop a running voice. */ 2144 if (voice == NULL) { 2145 voice = fluid_synth_free_voice_by_kill(synth); 2146 } 2147 2148 if (voice == NULL) { 2149 FLUID_LOG(FLUID_WARN, "Failed to allocate a synthesis process. (chan=%d,key=%d)", chan, key); 2150 return NULL; 2151 } 2152 2153 if (synth->verbose) { 2154 k = 0; 2155 for (i = 0; i < synth->polyphony; i++) { 2156 if (!_AVAILABLE(synth->voice[i])) { 2157 k++; 2158 } 2159 } 2160 2161 FLUID_LOG(FLUID_INFO, "noteon\t%d\t%d\t%d\t%05d\t%.3f\t%.3f\t%.3f\t%d", 2162 chan, key, vel, synth->storeid, 2163 (float) synth->ticks / 44100.0f, 2164 (fluid_curtime() - synth->start) / 1000.0f, 2165 0.0f, 2166 k); 2167 } 2168 2169 if (chan >= 0) { 2170 channel = synth->channel[chan]; 2171 } 2172 2173 if (fluid_voice_init(voice, sample, channel, key, vel, 2174 synth->storeid, synth->ticks, synth->gain) != FLUID_OK) { 2175 FLUID_LOG(FLUID_WARN, "Failed to initialize voice"); 2176 return NULL; 2177 } 2178 2179 /* add the default modulators to the synthesis process. */ 2180 fluid_voice_add_mod(voice, &default_vel2att_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.1 */ 2181 fluid_voice_add_mod(voice, &default_vel2filter_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.2 */ 2182 fluid_voice_add_mod(voice, &default_at2viblfo_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.3 */ 2183 fluid_voice_add_mod(voice, &default_mod2viblfo_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.4 */ 2184 fluid_voice_add_mod(voice, &default_att_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.5 */ 2185 fluid_voice_add_mod(voice, &default_pan_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.6 */ 2186 fluid_voice_add_mod(voice, &default_expr_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.7 */ 2187 fluid_voice_add_mod(voice, &default_reverb_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.8 */ 2188 fluid_voice_add_mod(voice, &default_chorus_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.9 */ 2189 fluid_voice_add_mod(voice, &default_pitch_bend_mod, FLUID_VOICE_DEFAULT); /* SF2.01 $8.4.10 */ 2190 2191 return voice; 2192} 2193 2194/* 2195 * fluid_synth_kill_by_exclusive_class 2196 */ 2197void fluid_synth_kill_by_exclusive_class(fluid_synth_t* synth, fluid_voice_t* new_voice) 2198{ 2199 /** Kill all voices on a given channel, which belong into 2200 excl_class. This function is called by a SoundFont's preset in 2201 response to a noteon event. If one noteon event results in 2202 several voice processes (stereo samples), ignore_ID must name 2203 the voice ID of the first generated voice (so that it is not 2204 stopped). The first voice uses ignore_ID=-1, which will 2205 terminate all voices on a channel belonging into the exclusive 2206 class excl_class. 2207 */ 2208 2209 int i; 2210 int excl_class = _GEN(new_voice,GEN_EXCLUSIVECLASS); 2211 2212 /* Check if the voice belongs to an exclusive class. In that case, 2213 previous notes from the same class are released. */ 2214 2215 /* Excl. class 0: No exclusive class */ 2216 if (excl_class == 0) { 2217 return; 2218 } 2219 2220 // FLUID_LOG(FLUID_INFO, "Voice belongs to exclusive class (class=%d, ignore_id=%d)", excl_class, ignore_ID); 2221 2222 /* Kill all notes on the same channel with the same exclusive class */ 2223 2224 for (i = 0; i < synth->polyphony; i++) { 2225 fluid_voice_t* existing_voice = synth->voice[i]; 2226 2227 /* Existing voice does not play? Leave it alone. */ 2228 if (!_PLAYING(existing_voice)) { 2229 continue; 2230 } 2231 2232 /* An exclusive class is valid for a whole channel (or preset). 2233 * Is the voice on a different channel? Leave it alone. */ 2234 if (existing_voice->chan != new_voice->chan) { 2235 continue; 2236 } 2237 2238 /* Existing voice has a different (or no) exclusive class? Leave it alone. */ 2239 if ((int)_GEN(existing_voice, GEN_EXCLUSIVECLASS) != excl_class) { 2240 continue; 2241 } 2242 2243 /* Existing voice is a voice process belonging to this noteon 2244 * event (for example: stereo sample)? Leave it alone. */ 2245 if (fluid_voice_get_id(existing_voice) == fluid_voice_get_id(new_voice)) { 2246 continue; 2247 } 2248 2249 // FLUID_LOG(FLUID_INFO, "Releasing previous voice of exclusive class (class=%d, id=%d)", 2250 // (int)_GEN(existing_voice, GEN_EXCLUSIVECLASS), (int)fluid_voice_get_id(existing_voice)); 2251 2252 fluid_voice_kill_excl(existing_voice); 2253 }; 2254}; 2255 2256/* 2257 * fluid_synth_start_voice 2258 */ 2259void fluid_synth_start_voice(fluid_synth_t* synth, fluid_voice_t* voice) 2260{ 2261/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 2262/* fluid_mutex_unlock(synth->busy); */ 2263 2264 /* Find the exclusive class of this voice. If set, kill all voices 2265 * that match the exclusive class and are younger than the first 2266 * voice process created by this noteon event. */ 2267 fluid_synth_kill_by_exclusive_class(synth, voice); 2268 2269 /* Start the new voice */ 2270 2271 fluid_voice_start(voice); 2272} 2273 2274/* 2275 * fluid_synth_add_sfloader 2276 */ 2277void fluid_synth_add_sfloader(fluid_synth_t* synth, fluid_sfloader_t* loader) 2278{ 2279 synth->loaders = fluid_list_prepend(synth->loaders, loader); 2280} 2281 2282 2283/* 2284 * fluid_synth_sfload 2285 */ 2286int 2287fluid_synth_sfload(fluid_synth_t* synth, const char* filename, int reset_presets) 2288{ 2289 fluid_sfont_t* sfont; 2290 fluid_list_t* list; 2291 fluid_sfloader_t* loader; 2292 2293#if defined(MACOS9) 2294 fluid_synth_sfunload_macos9(synth); 2295#endif 2296 2297 if (filename == NULL) { 2298 FLUID_LOG(FLUID_ERR, "Invalid filename"); 2299 return FLUID_FAILED; 2300 } 2301 2302 for (list = synth->loaders; list; list = fluid_list_next(list)) { 2303 loader = (fluid_sfloader_t*) fluid_list_get(list); 2304 2305 sfont = fluid_sfloader_load(loader, filename); 2306 2307 if (sfont != NULL) { 2308 2309 sfont->id = ++synth->sfont_id; 2310 2311 /* insert the sfont as the first one on the list */ 2312 synth->sfont = fluid_list_prepend(synth->sfont, sfont); 2313 2314 /* reset the presets for all channels */ 2315 if (reset_presets) { 2316 fluid_synth_program_reset(synth); 2317 } 2318 2319 return (int) sfont->id; 2320 } 2321 } 2322 2323 FLUID_LOG(FLUID_ERR, "Failed to load SoundFont \"%s\"", filename); 2324 return -1; 2325} 2326 2327/* 2328 * fluid_synth_sfunload_callback 2329 */ 2330static int fluid_synth_sfunload_callback(void* data, unsigned int msec) 2331{ 2332 fluid_sfont_t* sfont = (fluid_sfont_t*) data; 2333 int r = delete_fluid_sfont(sfont); 2334 if (r == 0) { 2335 FLUID_LOG(FLUID_DBG,"Unloaded SoundFont"); 2336 } 2337 return r != 0; 2338} 2339 2340/* 2341 * fluid_synth_sfunload_macos9 2342 */ 2343void fluid_synth_sfunload_macos9(fluid_synth_t* synth) 2344{ 2345#if defined(MACOS9) 2346 fluid_list_t *list, *next; 2347 fluid_sfont_t* sfont; 2348 2349 list = synth->unloading; 2350 while (list) { 2351 next = fluid_list_next(list); 2352 sfont = (fluid_sfont_t*) fluid_list_get(list); 2353 if (delete_fluid_sfont(sfont) == 0) { 2354 synth->unloading = fluid_list_remove(synth->unloading, sfont); 2355 } 2356 list = next; 2357 } 2358#endif 2359} 2360 2361/* 2362 * fluid_synth_sfunload 2363 */ 2364int 2365fluid_synth_sfunload(fluid_synth_t* synth, unsigned int id, int reset_presets) 2366{ 2367 fluid_sfont_t* sfont = fluid_synth_get_sfont_by_id(synth, id); 2368 2369#if defined(MACOS9) 2370 fluid_synth_sfunload_macos9(synth); 2371#endif 2372 2373 if (!sfont) { 2374 FLUID_LOG(FLUID_ERR, "No SoundFont with id = %d", id); 2375 return FLUID_FAILED; 2376 } 2377 2378 /* remove the SoundFont from the list */ 2379 synth->sfont = fluid_list_remove(synth->sfont, sfont); 2380 2381 /* reset the presets for all channels */ 2382 if (reset_presets) { 2383 fluid_synth_program_reset(synth); 2384 } else { 2385 fluid_synth_update_presets(synth); 2386 } 2387 2388 if (delete_fluid_sfont(sfont) != 0) { 2389#if defined(MACOS9) 2390 synth->unloading = fluid_list_prepend(synth->unloading, sfont); 2391#else 2392 /* spin off a timer thread to unload the sfont later */ 2393 new_fluid_timer(100, fluid_synth_sfunload_callback, sfont, 1, 1); 2394#endif 2395 } 2396 2397 return FLUID_OK; 2398} 2399 2400/* fluid_synth_sfreload 2401 * 2402 */ 2403int fluid_synth_sfreload(fluid_synth_t* synth, unsigned int id) 2404{ 2405 char filename[1024]; 2406 fluid_sfont_t* sfont; 2407 int index = 0; 2408 fluid_list_t *list; 2409 fluid_sfloader_t* loader; 2410 2411 2412 sfont = fluid_synth_get_sfont_by_id(synth, id); 2413 if (!sfont) { 2414 FLUID_LOG(FLUID_ERR, "No SoundFont with id = %d", id); 2415 return FLUID_FAILED; 2416 } 2417 2418 /* find the index of the SoundFont */ 2419 list = synth->sfont; 2420 while (list) { 2421 if (sfont == (fluid_sfont_t*) fluid_list_get(list)) { 2422 break; 2423 } 2424 list = fluid_list_next(list); 2425 index++; 2426 } 2427 2428 /* keep a copy of the SoundFont's filename */ 2429 FLUID_STRCPY(filename, fluid_sfont_get_name(sfont)); 2430 2431 if (fluid_synth_sfunload(synth, id, 0) != FLUID_OK) { 2432 return FLUID_FAILED; 2433 } 2434 2435 for (list = synth->loaders; list; list = fluid_list_next(list)) { 2436 loader = (fluid_sfloader_t*) fluid_list_get(list); 2437 2438 sfont = fluid_sfloader_load(loader, filename); 2439 2440 if (sfont != NULL) { 2441 2442 sfont->id = id; 2443 2444 /* insert the sfont at the same index */ 2445 synth->sfont = fluid_list_insert_at(synth->sfont, index, sfont); 2446 2447 /* reset the presets for all channels */ 2448 fluid_synth_update_presets(synth); 2449 2450 return sfont->id; 2451 } 2452 } 2453 2454 FLUID_LOG(FLUID_ERR, "Failed to load SoundFont \"%s\"", filename); 2455 return -1; 2456} 2457 2458 2459/* 2460 * fluid_synth_add_sfont 2461 */ 2462int fluid_synth_add_sfont(fluid_synth_t* synth, fluid_sfont_t* sfont) 2463{ 2464 sfont->id = ++synth->sfont_id; 2465 2466 /* insert the sfont as the first one on the list */ 2467 synth->sfont = fluid_list_prepend(synth->sfont, sfont); 2468 2469 /* reset the presets for all channels */ 2470 fluid_synth_program_reset(synth); 2471 2472 return sfont->id; 2473} 2474 2475 2476/* 2477 * fluid_synth_remove_sfont 2478 */ 2479void fluid_synth_remove_sfont(fluid_synth_t* synth, fluid_sfont_t* sfont) 2480{ 2481 int sfont_id = fluid_sfont_get_id(sfont); 2482 2483 synth->sfont = fluid_list_remove(synth->sfont, sfont); 2484 2485 /* remove a possible bank offset */ 2486 fluid_synth_remove_bank_offset(synth, sfont_id); 2487 2488 /* reset the presets for all channels */ 2489 fluid_synth_program_reset(synth); 2490} 2491 2492 2493/* fluid_synth_sfcount 2494 * 2495 * Returns the number of loaded SoundFonts 2496 */ 2497int 2498fluid_synth_sfcount(fluid_synth_t* synth) 2499{ 2500 return fluid_list_size(synth->sfont); 2501} 2502 2503/* fluid_synth_get_sfont 2504 * 2505 * Returns SoundFont num 2506 */ 2507fluid_sfont_t* 2508fluid_synth_get_sfont(fluid_synth_t* synth, unsigned int num) 2509{ 2510 return (fluid_sfont_t*) fluid_list_get(fluid_list_nth(synth->sfont, num)); 2511} 2512 2513/* fluid_synth_get_sfont_by_id 2514 * 2515 */ 2516fluid_sfont_t* fluid_synth_get_sfont_by_id(fluid_synth_t* synth, unsigned int id) 2517{ 2518 fluid_list_t* list = synth->sfont; 2519 fluid_sfont_t* sfont; 2520 2521 while (list) { 2522 sfont = (fluid_sfont_t*) fluid_list_get(list); 2523 if (fluid_sfont_get_id(sfont) == id) { 2524 return sfont; 2525 } 2526 list = fluid_list_next(list); 2527 } 2528 return NULL; 2529} 2530 2531/* fluid_synth_get_sfont_by_name 2532 * 2533 */ 2534fluid_sfont_t* fluid_synth_get_sfont_by_name(fluid_synth_t* synth, char *name) 2535{ 2536 fluid_list_t* list = synth->sfont; 2537 fluid_sfont_t* sfont; 2538 2539 while (list) { 2540 sfont = (fluid_sfont_t*) fluid_list_get(list); 2541 if (FLUID_STRCMP(fluid_sfont_get_name(sfont), name) == 0) { 2542 return sfont; 2543 } 2544 list = fluid_list_next(list); 2545 } 2546 return NULL; 2547} 2548 2549/* 2550 * fluid_synth_get_channel_preset 2551 */ 2552fluid_preset_t* 2553fluid_synth_get_channel_preset(fluid_synth_t* synth, int chan) 2554{ 2555 if ((chan >= 0) && (chan < synth->midi_channels)) { 2556 return fluid_channel_get_preset(synth->channel[chan]); 2557 } 2558 2559 return NULL; 2560} 2561 2562/* 2563 * fluid_synth_get_voicelist 2564 */ 2565void 2566fluid_synth_get_voicelist(fluid_synth_t* synth, fluid_voice_t* buf[], int bufsize, int ID) 2567{ 2568 int i; 2569 int count = 0; 2570 for (i = 0; i < synth->polyphony; i++) { 2571 fluid_voice_t* voice = synth->voice[i]; 2572 if (count >= bufsize) { 2573 return; 2574 } 2575 2576 if (_PLAYING(voice) && ((int)voice->id == ID || ID < 0)) { 2577 buf[count++] = voice; 2578 } 2579 } 2580 if (count >= bufsize) { 2581 return; 2582 } 2583 buf[count++] = NULL; 2584} 2585 2586/* Purpose: 2587 * Turns on / off the reverb unit in the synth */ 2588void fluid_synth_set_reverb_on(fluid_synth_t* synth, int on) 2589{ 2590 synth->with_reverb = on; 2591} 2592 2593/* Purpose: 2594 * Turns on / off the chorus unit in the synth */ 2595void fluid_synth_set_chorus_on(fluid_synth_t* synth, int on) 2596{ 2597 synth->with_chorus = on; 2598} 2599 2600/* Purpose: 2601 * Reports the current setting of the chorus unit. */ 2602int fluid_synth_get_chorus_nr(fluid_synth_t* synth) 2603{ 2604 return fluid_chorus_get_nr(synth->chorus); 2605} 2606 2607double fluid_synth_get_chorus_level(fluid_synth_t* synth) 2608{ 2609 return (double)fluid_chorus_get_level(synth->chorus); 2610} 2611 2612double fluid_synth_get_chorus_speed_Hz(fluid_synth_t* synth) 2613{ 2614 return (double)fluid_chorus_get_speed_Hz(synth->chorus); 2615} 2616 2617double fluid_synth_get_chorus_depth_ms(fluid_synth_t* synth) 2618{ 2619 return (double)fluid_chorus_get_depth_ms(synth->chorus); 2620} 2621 2622int fluid_synth_get_chorus_type(fluid_synth_t* synth) 2623{ 2624 return fluid_chorus_get_type(synth->chorus); 2625} 2626 2627/* Purpose: 2628 * Returns the current settings_old of the reverb unit */ 2629double fluid_synth_get_reverb_roomsize(fluid_synth_t* synth) 2630{ 2631 return (double)fluid_revmodel_getroomsize(synth->reverb); 2632} 2633 2634double fluid_synth_get_reverb_damp(fluid_synth_t* synth) 2635{ 2636 return (double) fluid_revmodel_getdamp(synth->reverb); 2637} 2638 2639double fluid_synth_get_reverb_level(fluid_synth_t* synth) 2640{ 2641 return (double) fluid_revmodel_getlevel(synth->reverb); 2642} 2643 2644double fluid_synth_get_reverb_width(fluid_synth_t* synth) 2645{ 2646 return (double) fluid_revmodel_getwidth(synth->reverb); 2647} 2648 2649/* Purpose: 2650 * 2651 * If the same note is hit twice on the same channel, then the older 2652 * voice process is advanced to the release stage. Using a mechanical 2653 * MIDI controller, the only way this can happen is when the sustain 2654 * pedal is held. In this case the behaviour implemented here is 2655 * natural for many instruments. Note: One noteon event can trigger 2656 * several voice processes, for example a stereo sample. Don't 2657 * release those... 2658 */ 2659void fluid_synth_release_voice_on_same_note(fluid_synth_t* synth, int chan, int key){ 2660 int i; 2661 fluid_voice_t* voice; 2662 2663/* fluid_mutex_lock(synth->busy); /\* Don't interfere with the audio thread *\/ */ 2664/* fluid_mutex_unlock(synth->busy); */ 2665 2666 for (i = 0; i < synth->polyphony; i++) { 2667 voice = synth->voice[i]; 2668 if (_PLAYING(voice) 2669 && (voice->chan == chan) 2670 && (voice->key == key) 2671 && (fluid_voice_get_id(voice) != synth->noteid)) { 2672 fluid_voice_noteoff(voice); 2673 } 2674 } 2675} 2676 2677/* Purpose: 2678 * Sets the interpolation method to use on channel chan. 2679 * If chan is < 0, then set the interpolation method on all channels. 2680 */ 2681int fluid_synth_set_interp_method(fluid_synth_t* synth, int chan, int interp_method){ 2682 int i; 2683 for (i = 0; i < synth->midi_channels; i++) { 2684 if (synth->channel[i] == NULL){ 2685 FLUID_LOG(FLUID_ERR, "Channels don't exist (yet)!"); 2686 return FLUID_FAILED; 2687 }; 2688 if (chan < 0 || fluid_channel_get_num(synth->channel[i]) == chan){ 2689 fluid_channel_set_interp_method(synth->channel[i], interp_method); 2690 }; 2691 }; 2692 return FLUID_OK; 2693}; 2694 2695/* Purpose: 2696 * Returns the number of allocated midi channels 2697 */ 2698int 2699fluid_synth_count_midi_channels(fluid_synth_t* synth) 2700{ 2701 return synth->midi_channels; 2702} 2703 2704/* Purpose: 2705 * Returns the number of allocated audio channels 2706 */ 2707int 2708fluid_synth_count_audio_channels(fluid_synth_t* synth) 2709{ 2710 return synth->audio_channels; 2711} 2712 2713/* Purpose: 2714 * Returns the number of allocated audio channels 2715 */ 2716int 2717fluid_synth_count_audio_groups(fluid_synth_t* synth) 2718{ 2719 return synth->audio_groups; 2720} 2721 2722/* Purpose: 2723 * Returns the number of allocated effects channels 2724 */ 2725int 2726fluid_synth_count_effects_channels(fluid_synth_t* synth) 2727{ 2728 return synth->effects_channels; 2729} 2730 2731double fluid_synth_get_cpu_load(fluid_synth_t* synth) 2732{ 2733 return synth->cpu_load; 2734} 2735 2736static fluid_tuning_t* 2737fluid_synth_get_tuning(fluid_synth_t* synth, int bank, int prog) 2738{ 2739 if ((bank < 0) || (bank >= 128)) { 2740 FLUID_LOG(FLUID_WARN, "Bank number out of range"); 2741 return NULL; 2742 } 2743 if ((prog < 0) || (prog >= 128)) { 2744 FLUID_LOG(FLUID_WARN, "Program number out of range"); 2745 return NULL; 2746 } 2747 if ((synth->tuning == NULL) || 2748 (synth->tuning[bank] == NULL) || 2749 (synth->tuning[bank][prog] == NULL)) { 2750 FLUID_LOG(FLUID_WARN, "No tuning at bank %d, prog %d", bank, prog); 2751 return NULL; 2752 } 2753 return synth->tuning[bank][prog]; 2754} 2755 2756static fluid_tuning_t* 2757fluid_synth_create_tuning(fluid_synth_t* synth, int bank, int prog, char* name) 2758{ 2759 if ((bank < 0) || (bank >= 128)) { 2760 FLUID_LOG(FLUID_WARN, "Bank number out of range"); 2761 return NULL; 2762 } 2763 if ((prog < 0) || (prog >= 128)) { 2764 FLUID_LOG(FLUID_WARN, "Program number out of range"); 2765 return NULL; 2766 } 2767 if (synth->tuning == NULL) { 2768 synth->tuning = FLUID_ARRAY(fluid_tuning_t**, 128); 2769 if (synth->tuning == NULL) { 2770 FLUID_LOG(FLUID_PANIC, "Out of memory"); 2771 return NULL; 2772 } 2773 FLUID_MEMSET(synth->tuning, 0, 128 * sizeof(fluid_tuning_t**)); 2774 } 2775 2776 if (synth->tuning[bank] == NULL) { 2777 synth->tuning[bank] = FLUID_ARRAY(fluid_tuning_t*, 128); 2778 if (synth->tuning[bank] == NULL) { 2779 FLUID_LOG(FLUID_PANIC, "Out of memory"); 2780 return NULL; 2781 } 2782 FLUID_MEMSET(synth->tuning[bank], 0, 128 * sizeof(fluid_tuning_t*)); 2783 } 2784 2785 if (synth->tuning[bank][prog] == NULL) { 2786 synth->tuning[bank][prog] = new_fluid_tuning(name, bank, prog); 2787 if (synth->tuning[bank][prog] == NULL) { 2788 return NULL; 2789 } 2790 } 2791 2792 if ((fluid_tuning_get_name(synth->tuning[bank][prog]) == NULL) 2793 || (FLUID_STRCMP(fluid_tuning_get_name(synth->tuning[bank][prog]), name) != 0)) { 2794 fluid_tuning_set_name(synth->tuning[bank][prog], name); 2795 } 2796 2797 return synth->tuning[bank][prog]; 2798} 2799 2800int fluid_synth_create_key_tuning(fluid_synth_t* synth, 2801 int bank, int prog, 2802 char* name, double* pitch) 2803{ 2804 fluid_tuning_t* tuning = fluid_synth_create_tuning(synth, bank, prog, name); 2805 if (tuning == NULL) { 2806 return FLUID_FAILED; 2807 } 2808 if (pitch) { 2809 fluid_tuning_set_all(tuning, pitch); 2810 } 2811 return FLUID_OK; 2812} 2813 2814 2815int fluid_synth_create_octave_tuning(fluid_synth_t* synth, 2816 int bank, int prog, 2817 char* name, double* pitch) 2818{ 2819 fluid_tuning_t* tuning = fluid_synth_create_tuning(synth, bank, prog, name); 2820 if (tuning == NULL) { 2821 return FLUID_FAILED; 2822 } 2823 fluid_tuning_set_octave(tuning, pitch); 2824 return FLUID_OK; 2825} 2826 2827int fluid_synth_tune_notes(fluid_synth_t* synth, int bank, int prog, 2828 int len, int *key, double* pitch, int apply) 2829{ 2830 fluid_tuning_t* tuning = fluid_synth_get_tuning(synth, bank, prog); 2831 int i; 2832 2833 if (tuning == NULL) { 2834 return FLUID_FAILED; 2835 } 2836 2837 for (i = 0; i < len; i++) { 2838 fluid_tuning_set_pitch(tuning, key[i], pitch[i]); 2839 } 2840 2841 return FLUID_OK; 2842} 2843 2844int fluid_synth_select_tuning(fluid_synth_t* synth, int chan, 2845 int bank, int prog) 2846{ 2847 fluid_tuning_t* tuning = fluid_synth_get_tuning(synth, bank, prog); 2848 2849 if (tuning == NULL) { 2850 return FLUID_FAILED; 2851 } 2852 if ((chan < 0) || (chan >= synth->midi_channels)) { 2853 FLUID_LOG(FLUID_WARN, "Channel out of range"); 2854 return FLUID_FAILED; 2855 } 2856 2857 fluid_channel_set_tuning(synth->channel[chan], synth->tuning[bank][prog]); 2858 2859 return FLUID_OK; 2860} 2861 2862int fluid_synth_reset_tuning(fluid_synth_t* synth, int chan) 2863{ 2864 if ((chan < 0) || (chan >= synth->midi_channels)) { 2865 FLUID_LOG(FLUID_WARN, "Channel out of range"); 2866 return FLUID_FAILED; 2867 } 2868 2869 fluid_channel_set_tuning(synth->channel[chan], NULL); 2870 2871 return FLUID_OK; 2872} 2873 2874void fluid_synth_tuning_iteration_start(fluid_synth_t* synth) 2875{ 2876 synth->cur_tuning = NULL; 2877} 2878 2879int fluid_synth_tuning_iteration_next(fluid_synth_t* synth, int* bank, int* prog) 2880{ 2881 int b = 0, p = 0; 2882 2883 if (synth->tuning == NULL) { 2884 return 0; 2885 } 2886 2887 if (synth->cur_tuning != NULL) { 2888 /* get the next program number */ 2889 b = fluid_tuning_get_bank(synth->cur_tuning); 2890 p = 1 + fluid_tuning_get_prog(synth->cur_tuning); 2891 if (p >= 128) { 2892 p = 0; 2893 b++; 2894 } 2895 } 2896 2897 while (b < 128) { 2898 if (synth->tuning[b] != NULL) { 2899 while (p < 128) { 2900 if (synth->tuning[b][p] != NULL) { 2901 synth->cur_tuning = synth->tuning[b][p]; 2902 *bank = b; 2903 *prog = p; 2904 return 1; 2905 } 2906 p++; 2907 } 2908 } 2909 p = 0; 2910 b++; 2911 } 2912 2913 return 0; 2914} 2915 2916int fluid_synth_tuning_dump(fluid_synth_t* synth, int bank, int prog, 2917 char* name, int len, double* pitch) 2918{ 2919 fluid_tuning_t* tuning = fluid_synth_get_tuning(synth, bank, prog); 2920 2921 if (tuning == NULL) { 2922 return FLUID_FAILED; 2923 } 2924 2925 if (name) { 2926 snprintf(name, len - 1, "%s", fluid_tuning_get_name(tuning)); 2927 name[len - 1] = 0; /* make sure the string is null terminated */ 2928 } 2929 if (pitch) { 2930 FLUID_MEMCPY(pitch, fluid_tuning_get_all(tuning), 128 * sizeof(double)); 2931 } 2932 2933 return FLUID_OK; 2934} 2935 2936fluid_settings_t* fluid_synth_get_settings(fluid_synth_t* synth) 2937{ 2938 return synth->settings; 2939} 2940 2941int fluid_synth_setstr(fluid_synth_t* synth, char* name, char* str) 2942{ 2943 return fluid_settings_setstr(synth->settings, name, str); 2944} 2945 2946int fluid_synth_getstr(fluid_synth_t* synth, char* name, char** str) 2947{ 2948 return fluid_settings_getstr(synth->settings, name, str); 2949} 2950 2951int fluid_synth_setnum(fluid_synth_t* synth, char* name, double val) 2952{ 2953 return fluid_settings_setnum(synth->settings, name, val); 2954} 2955 2956int fluid_synth_getnum(fluid_synth_t* synth, char* name, double* val) 2957{ 2958 return fluid_settings_getnum(synth->settings, name, val); 2959} 2960 2961int fluid_synth_setint(fluid_synth_t* synth, char* name, int val) 2962{ 2963 return fluid_settings_setint(synth->settings, name, val); 2964} 2965 2966int fluid_synth_getint(fluid_synth_t* synth, char* name, int* val) 2967{ 2968 return fluid_settings_getint(synth->settings, name, val); 2969} 2970 2971int 2972fluid_synth_set_gen(fluid_synth_t* synth, int chan, int param, float value) 2973{ 2974 int i; 2975 fluid_voice_t* voice; 2976 2977 if ((chan < 0) || (chan >= synth->midi_channels)) { 2978 FLUID_LOG(FLUID_WARN, "Channel out of range"); 2979 return FLUID_FAILED; 2980 } 2981 2982 if ((param < 0) || (param >= GEN_LAST)) { 2983 FLUID_LOG(FLUID_WARN, "Parameter number out of range"); 2984 return FLUID_FAILED; 2985 } 2986 2987 fluid_channel_set_gen(synth->channel[chan], param, value, 0); 2988 2989 for (i = 0; i < synth->polyphony; i++) { 2990 voice = synth->voice[i]; 2991 if (voice->chan == chan) { 2992 fluid_voice_set_param(voice, param, value, 0); 2993 } 2994 } 2995 2996 return FLUID_OK; 2997} 2998 2999/** Change the value of a generator. This function allows to control 3000 all synthesis parameters in real-time. The changes are additive, 3001 i.e. they add up to the existing parameter value. This function is 3002 similar to sending an NRPN message to the synthesizer. The 3003 function accepts a float as the value of the parameter. The 3004 parameter numbers and ranges are described in the SoundFont 2.01 3005 specification, paragraph 8.1.3, page 48. See also 3006 'fluid_gen_type'. 3007 3008 Using the fluid_synth_set_gen2() function, it is possible to set 3009 the absolute value of a generator. This is an extension to the 3010 SoundFont standard. If 'absolute' is non-zero, the value of the 3011 generator specified in the SoundFont is completely ignored and the 3012 generator is fixed to the value passed as argument. To undo this 3013 behavior, you must call fluid_synth_set_gen2 again, with 3014 'absolute' set to 0 (and possibly 'value' set to zero). 3015 3016 If 'normalized' is non-zero, the value is supposed to be 3017 normalized between 0 and 1. Before applying the value, it will be 3018 scaled and shifted to the range defined in the SoundFont 3019 specifications. 3020 3021 */ 3022int 3023fluid_synth_set_gen2(fluid_synth_t* synth, int chan, int param, 3024 float value, int absolute, int normalized) 3025{ 3026 int i; 3027 fluid_voice_t* voice; 3028 float v; 3029 3030 if ((chan < 0) || (chan >= synth->midi_channels)) { 3031 FLUID_LOG(FLUID_WARN, "Channel out of range"); 3032 return FLUID_FAILED; 3033 } 3034 3035 if ((param < 0) || (param >= GEN_LAST)) { 3036 FLUID_LOG(FLUID_WARN, "Parameter number out of range"); 3037 return FLUID_FAILED; 3038 } 3039 3040 v = (normalized)? fluid_gen_scale(param, value) : value; 3041 3042 fluid_channel_set_gen(synth->channel[chan], param, v, absolute); 3043 3044 for (i = 0; i < synth->polyphony; i++) { 3045 voice = synth->voice[i]; 3046 if (voice->chan == chan) { 3047 fluid_voice_set_param(voice, param, v, absolute); 3048 } 3049 } 3050 3051 return FLUID_OK; 3052} 3053 3054float fluid_synth_get_gen(fluid_synth_t* synth, int chan, int param) 3055{ 3056 if ((chan < 0) || (chan >= synth->midi_channels)) { 3057 FLUID_LOG(FLUID_WARN, "Channel out of range"); 3058 return 0.0; 3059 } 3060 3061 if ((param < 0) || (param >= GEN_LAST)) { 3062 FLUID_LOG(FLUID_WARN, "Parameter number out of range"); 3063 return 0.0; 3064 } 3065 3066 return fluid_channel_get_gen(synth->channel[chan], param); 3067} 3068 3069/* The synth needs to know the router for the command line handlers (they only 3070 * supply the synth as argument) 3071 */ 3072void fluid_synth_set_midi_router(fluid_synth_t* synth, fluid_midi_router_t* router){ 3073 synth->midi_router=router; 3074}; 3075 3076/* Purpose: 3077 * Any MIDI event from the MIDI router arrives here and is handed 3078 * to the appropriate function. 3079 */ 3080int fluid_synth_handle_midi_event(void* data, fluid_midi_event_t* event) 3081{ 3082 fluid_synth_t* synth = (fluid_synth_t*) data; 3083 int type = fluid_midi_event_get_type(event); 3084 int chan = fluid_midi_event_get_channel(event); 3085 3086 switch(type) { 3087 case NOTE_ON: 3088 return fluid_synth_noteon(synth, chan, 3089 fluid_midi_event_get_key(event), 3090 fluid_midi_event_get_velocity(event)); 3091 3092 case NOTE_OFF: 3093 return fluid_synth_noteoff(synth, chan, fluid_midi_event_get_key(event)); 3094 3095 case CONTROL_CHANGE: 3096 return fluid_synth_cc(synth, chan, 3097 fluid_midi_event_get_control(event), 3098 fluid_midi_event_get_value(event)); 3099 3100 case PROGRAM_CHANGE: 3101 return fluid_synth_program_change(synth, chan, fluid_midi_event_get_program(event)); 3102 3103 case CHANNEL_PRESSURE: 3104 return fluid_synth_channel_pressure(synth, chan, fluid_midi_event_get_program(event)); 3105 3106 case PITCH_BEND: 3107 return fluid_synth_pitch_bend(synth, chan, fluid_midi_event_get_pitch(event)); 3108 3109 case MIDI_SYSTEM_RESET: 3110 return fluid_synth_system_reset(synth); 3111 } 3112 return FLUID_FAILED; 3113} 3114 3115 3116int fluid_synth_start(fluid_synth_t* synth, unsigned int id, fluid_preset_t* preset, 3117 int audio_chan, int midi_chan, int key, int vel) 3118{ 3119 int r; 3120 3121 /* check the ranges of the arguments */ 3122 if ((midi_chan < 0) || (midi_chan >= synth->midi_channels)) { 3123 FLUID_LOG(FLUID_WARN, "Channel out of range"); 3124 return FLUID_FAILED; 3125 } 3126 3127 if ((key < 0) || (key >= 128)) { 3128 FLUID_LOG(FLUID_WARN, "Key out of range"); 3129 return FLUID_FAILED; 3130 } 3131 3132 if ((vel <= 0) || (vel >= 128)) { 3133 FLUID_LOG(FLUID_WARN, "Velocity out of range"); 3134 return FLUID_FAILED; 3135 } 3136 3137 fluid_mutex_lock(synth->busy); /* One at a time, please */ 3138 3139 synth->storeid = id; 3140 r = fluid_preset_noteon(preset, synth, midi_chan, key, vel); 3141 3142 fluid_mutex_unlock(synth->busy); 3143 3144 return r; 3145} 3146 3147int fluid_synth_stop(fluid_synth_t* synth, unsigned int id) 3148{ 3149 int i; 3150 fluid_voice_t* voice; 3151 int status = FLUID_FAILED; 3152 int count = 0; 3153 3154 for (i = 0; i < synth->polyphony; i++) { 3155 3156 voice = synth->voice[i]; 3157 3158 if (_ON(voice) && (fluid_voice_get_id(voice) == id)) { 3159 count++; 3160 fluid_voice_noteoff(voice); 3161 status = FLUID_OK; 3162 } 3163 } 3164 3165 return status; 3166} 3167 3168fluid_bank_offset_t* 3169fluid_synth_get_bank_offset0(fluid_synth_t* synth, int sfont_id) 3170{ 3171 fluid_list_t* list = synth->bank_offsets; 3172 fluid_bank_offset_t* offset; 3173 3174 while (list) { 3175 3176 offset = (fluid_bank_offset_t*) fluid_list_get(list); 3177 if (offset->sfont_id == sfont_id) { 3178 return offset; 3179 } 3180 3181 list = fluid_list_next(list); 3182 } 3183 3184 return NULL; 3185} 3186 3187int 3188fluid_synth_set_bank_offset(fluid_synth_t* synth, int sfont_id, int offset) 3189{ 3190 fluid_bank_offset_t* bank_offset; 3191 3192 bank_offset = fluid_synth_get_bank_offset0(synth, sfont_id); 3193 3194 if (bank_offset == NULL) { 3195 bank_offset = FLUID_NEW(fluid_bank_offset_t); 3196 if (bank_offset == NULL) { 3197 return -1; 3198 } 3199 bank_offset->sfont_id = sfont_id; 3200 bank_offset->offset = offset; 3201 synth->bank_offsets = fluid_list_prepend(synth->bank_offsets, bank_offset); 3202 } else { 3203 bank_offset->offset = offset; 3204 } 3205 3206 return 0; 3207} 3208 3209int 3210fluid_synth_get_bank_offset(fluid_synth_t* synth, int sfont_id) 3211{ 3212 fluid_bank_offset_t* bank_offset; 3213 3214 bank_offset = fluid_synth_get_bank_offset0(synth, sfont_id); 3215 return (bank_offset == NULL)? 0 : bank_offset->offset; 3216} 3217 3218void 3219fluid_synth_remove_bank_offset(fluid_synth_t* synth, int sfont_id) 3220{ 3221 fluid_bank_offset_t* bank_offset; 3222 3223 bank_offset = fluid_synth_get_bank_offset0(synth, sfont_id); 3224 if (bank_offset != NULL) { 3225 synth->bank_offsets = fluid_list_remove(synth->bank_offsets, bank_offset); 3226 } 3227} 3228