1 /* 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 22 #ifndef __FLUID_S_H__ 23 #define __FLUID_S_H__ 24 25 #include "audio/midi/synthesizer.h" 26 #include "audio/midi/midipatch.h" 27 28 namespace FluidS { 29 30 using namespace Ms; 31 32 class Voice; 33 class SFont; 34 class Preset; 35 class Sample; 36 class Channel; 37 struct Mod; 38 class Fluid; 39 40 #define FLUID_NUM_PROGRAMS 129 41 42 enum fluid_loop { 43 FLUID_UNLOOPED = 0, 44 FLUID_LOOP_DURING_RELEASE = 1, 45 FLUID_NOTUSED = 2, 46 FLUID_LOOP_UNTIL_RELEASE = 3 47 }; 48 49 enum fluid_synth_status { 50 FLUID_SYNTH_CLEAN, 51 FLUID_SYNTH_PLAYING, 52 FLUID_SYNTH_QUIET, 53 FLUID_SYNTH_STOPPED 54 }; 55 56 //--------------------------------------------------------- 57 // BankOffset 58 //--------------------------------------------------------- 59 60 struct BankOffset { 61 int sfont_id; 62 int offset; 63 }; 64 65 enum fluid_midi_control_change { 66 BANK_SELECT_MSB = 0x00, 67 MODULATION_MSB = 0x01, 68 BREATH_MSB = 0x02, 69 FOOT_MSB = 0x04, 70 PORTAMENTO_TIME_MSB = 0x05, 71 DATA_ENTRY_MSB = 0x06, 72 VOLUME_MSB = 0x07, 73 BALANCE_MSB = 0x08, 74 PAN_MSB = 0x0A, 75 EXPRESSION_MSB = 0x0B, 76 EFFECTS1_MSB = 0x0C, 77 EFFECTS2_MSB = 0x0D, 78 GPC1_MSB = 0x10, /* general purpose controller */ 79 GPC2_MSB = 0x11, 80 GPC3_MSB = 0x12, 81 GPC4_MSB = 0x13, 82 BANK_SELECT_LSB = 0x20, 83 MODULATION_WHEEL_LSB = 0x21, 84 BREATH_LSB = 0x22, 85 FOOT_LSB = 0x24, 86 PORTAMENTO_TIME_LSB = 0x25, 87 DATA_ENTRY_LSB = 0x26, 88 VOLUME_LSB = 0x27, 89 BALANCE_LSB = 0x28, 90 PAN_LSB = 0x2A, 91 EXPRESSION_LSB = 0x2B, 92 EFFECTS1_LSB = 0x2C, 93 EFFECTS2_LSB = 0x2D, 94 GPC1_LSB = 0x30, 95 GPC2_LSB = 0x31, 96 GPC3_LSB = 0x32, 97 GPC4_LSB = 0x33, 98 SUSTAIN_SWITCH = 0x40, 99 PORTAMENTO_SWITCH = 0x41, 100 SOSTENUTO_SWITCH = 0x42, 101 SOFT_PEDAL_SWITCH = 0x43, 102 LEGATO_SWITCH = 0x45, 103 HOLD2_SWITCH = 0x45, 104 SOUND_CTRL1 = 0x46, 105 SOUND_CTRL2 = 0x47, 106 SOUND_CTRL3 = 0x48, 107 SOUND_CTRL4 = 0x49, 108 SOUND_CTRL5 = 0x4A, 109 SOUND_CTRL6 = 0x4B, 110 SOUND_CTRL7 = 0x4C, 111 SOUND_CTRL8 = 0x4D, 112 SOUND_CTRL9 = 0x4E, 113 SOUND_CTRL10 = 0x4F, 114 GPC5 = 0x50, 115 GPC6 = 0x51, 116 GPC7 = 0x52, 117 GPC8 = 0x53, 118 PORTAMENTO_CTRL = 0x54, 119 EFFECTS_DEPTH1 = 0x5B, 120 EFFECTS_DEPTH2 = 0x5C, 121 EFFECTS_DEPTH3 = 0x5D, 122 EFFECTS_DEPTH4 = 0x5E, 123 EFFECTS_DEPTH5 = 0x5F, 124 DATA_ENTRY_INCR = 0x60, 125 DATA_ENTRY_DECR = 0x61, 126 NRPN_LSB = 0x62, 127 NRPN_MSB = 0x63, 128 RPN_LSB = 0x64, 129 RPN_MSB = 0x65, 130 ALL_SOUND_OFF = 0x78, 131 ALL_CTRL_OFF = 0x79, 132 LOCAL_CONTROL = 0x7A, 133 ALL_NOTES_OFF = 0x7B, 134 OMNI_OFF = 0x7C, 135 OMNI_ON = 0x7D, 136 POLY_OFF = 0x7E, 137 POLY_ON = 0x7F 138 }; 139 140 /** 141 * Generator (effect) numbers (SoundFont 2.01 specifications section 8.1.3) 142 */ 143 enum fluid_gen_type { 144 GEN_STARTADDROFS, /**< Sample start address offset (0-32767) */ 145 GEN_ENDADDROFS, /**< Sample end address offset (-32767-0) */ 146 GEN_STARTLOOPADDROFS, /**< Sample loop start address offset (-32767-32767) */ 147 GEN_ENDLOOPADDROFS, /**< Sample loop end address offset (-32767-32767) */ 148 GEN_STARTADDRCOARSEOFS, /**< Sample start address coarse offset (X 32768) */ 149 GEN_MODLFOTOPITCH, /**< Modulation LFO to pitch */ 150 GEN_VIBLFOTOPITCH, /**< Vibrato LFO to pitch */ 151 GEN_MODENVTOPITCH, /**< Modulation envelope to pitch */ 152 GEN_FILTERFC, /**< Filter cutoff */ 153 GEN_FILTERQ, /**< Filter Q */ 154 GEN_MODLFOTOFILTERFC, /**< Modulation LFO to filter cutoff */ 155 GEN_MODENVTOFILTERFC, /**< Modulation envelope to filter cutoff */ 156 GEN_ENDADDRCOARSEOFS, /**< Sample end address coarse offset (X 32768) */ 157 GEN_MODLFOTOVOL, /**< Modulation LFO to volume */ 158 GEN_UNUSED1, /**< Unused */ 159 GEN_CHORUSSEND, /**< Chorus send amount */ 160 GEN_REVERBSEND, /**< Reverb send amount */ 161 GEN_PAN, /**< Stereo panning */ 162 GEN_UNUSED2, /**< Unused */ 163 GEN_UNUSED3, /**< Unused */ 164 GEN_UNUSED4, /**< Unused */ 165 GEN_MODLFODELAY, /**< Modulation LFO delay */ 166 GEN_MODLFOFREQ, /**< Modulation LFO frequency */ 167 GEN_VIBLFODELAY, /**< Vibrato LFO delay */ 168 GEN_VIBLFOFREQ, /**< Vibrato LFO frequency */ 169 GEN_MODENVDELAY, /**< Modulation envelope delay */ 170 GEN_MODENVATTACK, /**< Modulation envelope attack */ 171 GEN_MODENVHOLD, /**< Modulation envelope hold */ 172 GEN_MODENVDECAY, /**< Modulation envelope decay */ 173 GEN_MODENVSUSTAIN, /**< Modulation envelope sustain */ 174 GEN_MODENVRELEASE, /**< Modulation envelope release */ 175 GEN_KEYTOMODENVHOLD, /**< Key to modulation envelope hold */ 176 GEN_KEYTOMODENVDECAY, /**< Key to modulation envelope decay */ 177 GEN_VOLENVDELAY, /**< Volume envelope delay */ 178 GEN_VOLENVATTACK, /**< Volume envelope attack */ 179 GEN_VOLENVHOLD, /**< Volume envelope hold */ 180 GEN_VOLENVDECAY, /**< Volume envelope decay */ 181 GEN_VOLENVSUSTAIN, /**< Volume envelope sustain */ 182 GEN_VOLENVRELEASE, /**< Volume envelope release */ 183 GEN_KEYTOVOLENVHOLD, /**< Key to volume envelope hold */ 184 GEN_KEYTOVOLENVDECAY, /**< Key to volume envelope decay */ 185 GEN_INSTRUMENT, /**< Instrument ID (shouldn't be set by user) */ 186 GEN_RESERVED1, /**< Reserved */ 187 GEN_KEYRANGE, /**< MIDI note range */ 188 GEN_VELRANGE, /**< MIDI velocity range */ 189 GEN_STARTLOOPADDRCOARSEOFS, /**< Sample start loop address coarse offset (X 32768) */ 190 GEN_KEYNUM, /**< Fixed MIDI note number */ 191 GEN_VELOCITY, /**< Fixed MIDI velocity value */ 192 GEN_ATTENUATION, /**< Initial volume attenuation */ 193 GEN_RESERVED2, /**< Reserved */ 194 GEN_ENDLOOPADDRCOARSEOFS, /**< Sample end loop address coarse offset (X 32768) */ 195 GEN_COARSETUNE, /**< Coarse tuning */ 196 GEN_FINETUNE, /**< Fine tuning */ 197 GEN_SAMPLEID, /**< Sample ID (shouldn't be set by user) */ 198 GEN_SAMPLEMODE, /**< Sample mode flags */ 199 GEN_RESERVED3, /**< Reserved */ 200 GEN_SCALETUNE, /**< Scale tuning */ 201 GEN_EXCLUSIVECLASS, /**< Exclusive class number */ 202 GEN_OVERRIDEROOTKEY, /**< Sample root note override */ 203 204 /* the initial pitch is not a "standard" generator. It is not 205 * mentioned in the list of generator in the SF2 specifications. It 206 * is used, however, as the destination for the default pitch wheel 207 * modulator. */ 208 GEN_PITCH, /**< Pitch (NOTE: Not a real SoundFont generator) */ 209 GEN_LAST /**< Value defines the count of generators (#fluid_gen_type) */ 210 }; 211 212 //--------------------------------------------------------- 213 // Channel 214 //--------------------------------------------------------- 215 216 class Channel { 217 Fluid* synth; 218 219 unsigned int sfontnum; 220 unsigned int banknum; 221 unsigned int prognum; 222 Preset* _preset; 223 224 public: 225 enum { INVALID_NOTE = -1}; 226 227 int channum; 228 short channel_pressure; 229 short pitch_bend; 230 short pitch_wheel_sensitivity; 231 232 short cc[128]; // controller values 233 short key_pressure[128]; // MIDI polyphonic key pressure from [0;127] 234 235 /* cached values of last MSB values of MSB/LSB controllers */ 236 unsigned char bank_msb; 237 int interp_method; 238 239 /* NRPN system */ 240 short nrpn_select; 241 242 /* The values of the generators, set by NRPN messages, or by 243 * fluid_synth_set_gen(), are cached in the channel so they can be 244 * applied to future notes. They are copied to a voice's generators 245 * in fluid_voice_init(), which calls fluid_gen_init(). */ 246 247 float gen[GEN_LAST]; 248 249 /* By default, the NRPN values are relative to the values of the 250 * generators set in the SoundFont. For example, if the NRPN 251 * specifies an attack of 100 msec then 100 msec will be added to the 252 * combined attack time of the sound font and the modulators. 253 * 254 * However, it is useful to be able to specify the generator value 255 * absolutely, completely ignoring the generators of the sound font 256 * and the values of modulators. The gen_abs field, is a boolean 257 * flag indicating whether the NRPN value is absolute or not. 258 */ 259 char gen_abs[GEN_LAST]; 260 261 public: 262 Channel(Fluid* synth, int num); 263 sustained()264 bool sustained() const { return cc[SUSTAIN_SWITCH] >= 64; } setGen(int n,float v,char a)265 void setGen(int n, float v, char a) { gen[n] = v; gen_abs[n] = a; } getGen(int n)266 float getGen(int n) const { return gen[n]; } getGenAbs(int n)267 char getGenAbs(int n) const { return gen_abs[n]; } 268 void init(); 269 void initCtrl(); setCC(int n,int val)270 void setCC(int n, int val) { cc[n] = val; } 271 void reset(); 272 void setPreset(Preset* p); preset()273 Preset* preset() const { return _preset; } getSfontnum()274 unsigned int getSfontnum() const { return sfontnum; } setSfontnum(unsigned int s)275 void setSfontnum(unsigned int s) { sfontnum = s; } getBanknum()276 unsigned int getBanknum() const { return banknum; } setBanknum(unsigned int b)277 void setBanknum(unsigned int b) { banknum = b; } setPrognum(int p)278 void setPrognum(int p) { prognum = p; } getPrognum()279 int getPrognum() const { return prognum; } 280 void setcc(int ctrl, int val); 281 void pitchBend(int val); getPitchBend()282 int getPitchBend() const { return pitch_bend; } 283 void pitchWheelSens(int val); 284 int getCC(int num); getNum()285 int getNum() const { return channum; } setInterpMethod(int m)286 void setInterpMethod(int m) { interp_method = m; } getInterpMethod()287 int getInterpMethod() const { return interp_method; } 288 void setChannelPressure(int val); channelPressure()289 int channelPressure() const { return channel_pressure; } 290 void setKeyPressure(int key, int val); keyPressure(int key)291 int keyPressure(int key) const { return key_pressure[key]; } 292 int getFromKeyPortamento(); 293 }; 294 295 // subsystems: 296 enum { 297 FLUID_GROUP = 0, 298 }; 299 300 //--------------------------------------------------------- 301 // Fluid 302 //--------------------------------------------------------- 303 304 class Fluid : public Synthesizer { 305 QList<SFont*> sfonts; // the loaded soundfonts 306 QList<MidiPatch*> patches; 307 308 QList<Voice*> freeVoices; // unused synthesis processes 309 QList<Voice*> activeVoices; // active synthesis processes 310 QString _error; // last error message 311 312 static bool initialized; 313 314 double sample_rate; // The sample rate 315 float _masterTuning; // usually 440.0 316 double _tuning[128]; // the pitch of every key, in cents 317 318 int _loadProgress = 0; 319 bool _loadWasCanceled = false; 320 321 QMutex mutex; 322 void updatePatchList(); 323 324 //the variable is used to stop loading samples from the sf files 325 bool _globalTerminate = false; 326 327 int fromkey_portamento = Channel::INVALID_NOTE; 328 int lastNote = Channel::INVALID_NOTE; 329 330 protected: 331 int _state; // the synthesizer state 332 333 unsigned int sfont_id; 334 335 QList<Channel*> channel; // the channels 336 337 unsigned int noteid; // the id is incremented for every new note. it's used for noteoff's 338 339 SFont* get_sfont_by_name(const QString& name); 340 SFont* get_sfont_by_id(int id); get_sfont(int idx)341 SFont* get_sfont(int idx) const { return sfonts[idx]; } 342 bool sfunload(int id); 343 int sfload(const QString& filename); 344 345 public: 346 Fluid(); 347 ~Fluid(); 348 virtual void init(float sampleRate); 349 name()350 virtual const char* name() const { return "Fluid"; } 351 352 virtual void play(const PlayEvent&); getPatchInfo()353 virtual const QList<MidiPatch*>& getPatchInfo() const { return patches; } 354 355 // get/set synthesizer state (parameter set) 356 virtual SynthesizerGroup state() const; 357 virtual bool setState(const SynthesizerGroup&); 358 359 virtual void allSoundsOff(int); 360 virtual void allNotesOff(int); 361 loadProgress()362 int loadProgress() { return _loadProgress; } setLoadProgress(int val)363 void setLoadProgress(int val) { _loadProgress = val; } loadWasCanceled()364 bool loadWasCanceled() { return _loadWasCanceled; } setLoadWasCanceled(bool status)365 void setLoadWasCanceled(bool status) { _loadWasCanceled = status; } 366 367 Preset* get_preset(unsigned int sfontnum, unsigned int banknum, unsigned int prognum); 368 Preset* find_preset(unsigned int banknum, unsigned int prognum); 369 void modulate_voices(int chan, bool is_cc, int ctrl); 370 void modulate_voices_all(int chan); 371 void damp_voices(int chan); 372 int kill_voice(Voice * voice); 373 void print_voice(); 374 375 /** This function assures that every MIDI channels has a valid preset 376 * (NULL is okay). This function is called after a SoundFont is 377 * unloaded or reloaded. */ 378 void update_presets(); 379 get_cc(int chan,int num)380 int get_cc(int chan, int num) const { return channel[chan]->cc[num]; } resetPortamento(int chan)381 void resetPortamento(int chan) const { channel[chan]->cc[PORTAMENTO_CTRL] = Channel::INVALID_NOTE; } portamentoTime(int chan)382 int portamentoTime(int chan) const { 383 return get_cc(chan, PORTAMENTO_TIME_MSB) * 128 + get_cc(chan, PORTAMENTO_TIME_LSB); 384 } 385 386 void system_reset(); 387 void program_change(int chan, int prognum); 388 389 void set_gen2(int chan, int param, float value, int absolute, int normalized); 390 float get_gen(int chan, int param); 391 void set_gen(int chan, int param, float value); 392 void set_interp_method(int chan, int interp_method); 393 get_channel_preset(int chan)394 Preset* get_channel_preset(int chan) const { return channel[chan]->preset(); } 395 396 virtual bool loadSoundFonts(const QStringList& s); 397 virtual bool addSoundFont(const QString& s); 398 virtual bool removeSoundFont(const QString& s); 399 QStringList soundFonts() const; 400 std::vector<SoundFontInfo> soundFontsInfo() const override; 401 402 void start_voice(Voice* voice); 403 Voice* alloc_voice(unsigned id, Sample* sample, int chan, int key, int vel, double vt); 404 void free_voice_by_kill(); 405 406 virtual void process(unsigned len, float* out, float* effect1, float* effect2); 407 408 bool program_select(int chan, unsigned sfont_id, unsigned bank_num, unsigned preset_num); 409 void get_program(int chan, unsigned* sfont_id, unsigned* bank_num, unsigned* preset_num); 410 // void sfont_select(int chan, unsigned int sfont_id) { channel[chan]->setSfontnum(sfont_id); } 411 // void bank_select(int chan, unsigned int bank) { channel[chan]->setBanknum(bank); } 412 413 void get_pitch_wheel_sens(int chan, int* pval); 414 void pitch_wheel_sens(int chan, int val); 415 void get_pitch_bend(int chan, int* ppitch_bend); 416 417 void freeVoice(Voice* v); 418 getPitch(int k)419 double getPitch(int k) const { return _tuning[k]; } ct2hz_real(float cents)420 float ct2hz_real(float cents) { return powf(2.0f, (cents - 6900.0f) / 1200.0f) * _masterTuning; } 421 act2hz(float c)422 float act2hz(float c) { return 8.176 * pow(2.0, (double) c / 1200.0); } ct2hz(float cents)423 float ct2hz(float cents) { return act2hz(qBound(1500.0f, cents, 13500.0f)); } 424 masterTuning()425 virtual double masterTuning() const { return _masterTuning; } setMasterTuning(double f)426 virtual void setMasterTuning(double f) { _masterTuning = f; } 427 error()428 QString error() const { return _error; } 429 430 virtual SynthesizerGui* gui(); 431 432 static QFileInfoList sfFiles(); 433 globalTerminate()434 bool globalTerminate() { return _globalTerminate; } 435 void setGlobalTerminate(bool terminate = true) { _globalTerminate = terminate; } getFromKeyPortamento()436 int getFromKeyPortamento() { return fromkey_portamento; } 437 void setFromKeyPortamento(int chan, int defaultValue); setLastNote(int last_note)438 void setLastNote(int last_note) { this->lastNote = last_note; } 439 440 friend class Voice; 441 friend class Preset; 442 }; 443 444 /* 445 * 446 * Chorus 447 * 448 */ 449 450 enum fluid_chorus_mod { 451 FLUID_CHORUS_MOD_SINE = 0, 452 FLUID_CHORUS_MOD_TRIANGLE = 1 453 }; 454 455 /* Those are the default settings for the chorus. */ 456 #define FLUID_CHORUS_DEFAULT_N 3 457 #define FLUID_CHORUS_DEFAULT_LEVEL 2.0f 458 #define FLUID_CHORUS_DEFAULT_SPEED 0.3f 459 #define FLUID_CHORUS_DEFAULT_DEPTH 8.0f 460 #define FLUID_CHORUS_DEFAULT_TYPE FLUID_CHORUS_MOD_SINE 461 462 463 /* 464 * 465 * Synthesis parameters 466 * 467 */ 468 469 /* Flags to choose the interpolation method */ 470 enum fluid_interp { 471 /* no interpolation: Fastest, but questionable audio quality */ 472 FLUID_INTERP_NONE = 0, 473 /* Straight-line interpolation: A bit slower, reasonable audio quality */ 474 FLUID_INTERP_LINEAR = 1, 475 /* Fourth-order interpolation: Requires 50 % of the whole DSP processing time, good quality 476 * Default. */ 477 FLUID_INTERP_DEFAULT = 4, 478 FLUID_INTERP_4THORDER = 4, 479 FLUID_INTERP_7THORDER = 7, 480 FLUID_INTERP_HIGHEST = 7 481 }; 482 483 #define fluid_sample_refcount(_sample) ((_sample)->refcount) 484 485 486 /** Sample types */ 487 488 enum { 489 FLUID_SAMPLETYPE_MONO = 1, 490 FLUID_SAMPLETYPE_RIGHT = 2, 491 FLUID_SAMPLETYPE_LEFT = 4, 492 FLUID_SAMPLETYPE_LINKED = 8, 493 FLUID_SAMPLETYPE_OGG_VORBIS = 0x10, 494 FLUID_SAMPLETYPE_ROM = 0x8000 495 }; 496 497 /* Sets the sound data of the sample 498 * Warning : if copy_data is FALSE, data should have 8 unused frames at start 499 * and 8 unused frames at the end. 500 */ 501 int fluid_sample_set_sound_data(Sample* sample, short *data, 502 unsigned int nbframes, short copy_data, int rootkey); 503 504 /* 505 * 506 * Utility functions 507 */ 508 509 /* Maximum number of modulators in a voice */ 510 #define FLUID_NUM_MOD 64 511 512 /** 513 * SoundFont generator structure. 514 */ 515 class Generator { 516 public: 517 unsigned char flags; /**< Is the generator set or not (#fluid_gen_flags) */ 518 double val; /**< The nominal value */ 519 double mod; /**< Change by modulators */ 520 double nrpn; /**< Change by NRPN messages */ 521 set_mod(double _val)522 void set_mod(double _val) { mod = _val; } set_nrpn(double _val)523 void set_nrpn(double _val) { nrpn = _val; } 524 }; 525 526 /** 527 * Enum value for 'flags' field of #_Generator (not really flags). 528 */ 529 enum fluid_gen_flags { 530 GEN_UNUSED, /**< Generator value is not set */ 531 GEN_SET, /**< Generator value is set */ 532 GEN_ABS_NRPN /**< DOCME */ 533 }; 534 535 void fluid_gen_set_default_values(Generator* gen); 536 /* 537 * The interface to the synthesizer's voices 538 * Examples on using them can be found in fluid_defsfont.c 539 */ 540 541 /* for fluid_voice_add_mod */ 542 enum fluid_voice_add_mod { 543 FLUID_VOICE_OVERWRITE, 544 FLUID_VOICE_ADD, 545 FLUID_VOICE_DEFAULT 546 }; 547 548 /* Disable FPE exception check */ 549 #define fluid_check_fpe(expl) 550 551 unsigned int fluid_check_fpe_i386(char * explanation_in_case_of_fpe); 552 553 /* 554 * interpolation data 555 */ 556 struct fluid_interp_coeff_t { 557 float a0, a1, a2, a3; 558 }; 559 560 /* Flags telling the polarity of a modulator. Compare with SF2.01 561 section 8.2. Note: The numbers of the bits are different! (for 562 example: in the flags of a SF modulator, the polarity bit is bit 563 nr. 9) */ 564 565 enum fluid_mod_flags { 566 FLUID_MOD_POSITIVE = 0, 567 FLUID_MOD_NEGATIVE = 1, 568 FLUID_MOD_UNIPOLAR = 0, 569 FLUID_MOD_BIPOLAR = 2, 570 FLUID_MOD_LINEAR = 0, 571 FLUID_MOD_CONCAVE = 4, 572 FLUID_MOD_CONVEX = 8, 573 FLUID_MOD_SWITCH = 12, 574 FLUID_MOD_GC = 0, 575 FLUID_MOD_CC = 16 576 }; 577 578 //--------------------------------------------------------- 579 // Mod 580 //--------------------------------------------------------- 581 582 struct Mod 583 { 584 unsigned char dest; 585 unsigned char src1; 586 unsigned char flags1; 587 unsigned char src2; 588 unsigned char flags2; 589 double amount; 590 591 void clone(Mod* mod) const; 592 void dump() const; has_sourceMod593 int has_source(bool cc, int ctrl) { 594 return (((src1 == ctrl) && (flags1 & FLUID_MOD_CC) && cc) 595 || (((src1 == ctrl) && (!(flags1 & FLUID_MOD_CC)) && !cc))) 596 || (((src2 == ctrl) && (flags2 & FLUID_MOD_CC) && cc) 597 || (((src2 == ctrl) && (!(flags2 & FLUID_MOD_CC)) && !cc))); 598 } 599 void set_source1(int src, int flags); 600 void set_source2(int src, int flags); set_destMod601 void set_dest(int val) { dest = val; } set_amountMod602 void set_amount(double val) { amount = val; } get_source1Mod603 int get_source1() const { return src1; } get_flags1Mod604 int get_flags1() const { return flags1; } get_source2Mod605 int get_source2() const { return src2; } get_flags2Mod606 int get_flags2() const { return flags2; } get_destMod607 int get_dest() const { return dest; } get_amountMod608 double get_amount() const { return amount; } 609 float get_value(Channel* chan, Voice* voice); 610 }; 611 612 /* Flags telling the source of a modulator. This corresponds to 613 * SF2.01 section 8.2.1 */ 614 615 enum fluid_mod_src { 616 FLUID_MOD_NONE = 0, 617 FLUID_MOD_VELOCITY = 2, 618 FLUID_MOD_KEY = 3, 619 FLUID_MOD_KEYPRESSURE = 10, // polyphonic aftertouch 620 FLUID_MOD_CHANNELPRESSURE = 13, // channel aftertouch 621 FLUID_MOD_PITCHWHEEL = 14, 622 FLUID_MOD_PITCHWHEELSENS = 16 623 }; 624 625 /* Determines, if two modulators are 'identical' (all parameters 626 except the amount match) */ 627 bool test_identity(const Mod * mod1, const Mod * mod2); 628 629 void fluid_dump_modulator(Mod * mod); 630 631 #define fluid_mod_has_source(mod,cc,ctrl) \ 632 ( ((((mod)->src1 == ctrl) && (((mod)->flags1 & FLUID_MOD_CC) != 0) && (cc != 0)) \ 633 || ((((mod)->src1 == ctrl) && (((mod)->flags1 & FLUID_MOD_CC) == 0) && (cc == 0)))) \ 634 || ((((mod)->src2 == ctrl) && (((mod)->flags2 & FLUID_MOD_CC) != 0) && (cc != 0)) \ 635 || ((((mod)->src2 == ctrl) && (((mod)->flags2 & FLUID_MOD_CC) == 0) && (cc == 0))))) 636 637 #define fluid_mod_has_dest(mod,gen) ((mod)->dest == gen) 638 639 /* 640 * phase 641 */ 642 643 #define FLUID_INTERP_BITS 8 644 #define FLUID_INTERP_BITS_MASK 0xff000000 645 #define FLUID_INTERP_BITS_SHIFT 24 646 #define FLUID_INTERP_MAX 256 647 648 #define FLUID_FRACT_MAX ((double)4294967296.0) 649 650 //--------------------------------------------------------- 651 // Phase 652 /* Purpose: 653 * Playing pointer for voice playback 654 * 655 * When a sample is played back at a different pitch, the playing pointer in the 656 * source sample will not advance exactly one sample per output sample. 657 * This playing pointer is implemented using Phase. 658 * It is a 64 bit number. The higher 32 bits contain the 'index' (number of 659 * the current sample), the lower 32 bits the fractional part. 660 * Access is possible in two ways: 661 * -through the 64 bit part 'b64', if the architecture supports 64 bit integers 662 * -through 'index' and 'fract' 663 * Note: b64 and index / fract share the same memory location! 664 */ 665 666 struct Phase { 667 qint64 data; 668 669 void operator+=(const Phase& p) { data += p.data; } setIntPhase670 void setInt(qint32 b) { data = qint64(b) << 32; } setFloatPhase671 void setFloat(double b) { 672 data = (((qint64)(b)) << 32) | (quint32) (((double)(b) - (int)(b)) * (double)FLUID_FRACT_MAX); 673 } 674 675 void operator-=(const Phase& b) { data -= b.data; } 676 void operator-=(int b) { data -= (qint64(b) << 32); } indexPhase677 int index() const { return data >> 32; } fractPhase678 quint32 fract() const { return quint32(data & 0xffffffff); } index_roundPhase679 quint32 index_round() const { return quint32((data+0x80000000) >> 32); } 680 PhasePhase681 Phase() {} PhasePhase682 Phase(qint64 v) : data(v) {} 683 }; 684 685 /* Purpose: 686 * Takes the fractional part of the argument phase and 687 * calculates the corresponding position in the interpolation table. 688 * The fractional position of the playing pointer is calculated with a quite high 689 * resolution (32 bits). It would be unpractical to keep a set of interpolation 690 * coefficients for each possible fractional part... 691 */ 692 #define fluid_phase_fract_to_tablerow(_x) \ 693 ((int)(((_x).fract() & FLUID_INTERP_BITS_MASK) >> FLUID_INTERP_BITS_SHIFT)) 694 695 #define fluid_phase_double(_x) \ 696 ((double)((_x).index()) + ((double)((_x).fract()) / FLUID_FRACT_MAX)) 697 698 /* Purpose: 699 * The playing pointer is _phase. How many output samples are produced, until the point _p1 in the sample is reached, 700 * if _phase advances in steps of _incr? 701 */ 702 #define fluid_phase_steps(_phase,_idx,_incr) \ 703 (int)(((double)(_idx) - fluid_phase_double(_phase)) / (double)_incr) 704 705 /* Purpose: 706 * Creates the expression a.index++. 707 */ 708 #define fluid_phase_index_plusplus(a) (((a)._index)++) 709 710 } // namespace Fluid 711 712 #endif // __FLUID_S_H__ 713