1 /* 2 TiMidity++ -- MIDI to WAVE converter and player 3 Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp> 4 Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi> 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 20 instrum.h 21 22 */ 23 24 #ifndef ___INSTRUM_H_ 25 #define ___INSTRUM_H_ 26 27 #include <string> 28 #include "common.h" 29 #include "sysdep.h" 30 #include "sffile.h" 31 #include "sflayer.h" 32 #include "sfitem.h" 33 #include "../../../source/zmusic/fileio.h" 34 35 36 namespace TimidityPlus 37 { 38 using timidity_file = MusicIO::FileInterface; 39 40 enum 41 { 42 READ_CONFIG_SUCCESS = 0, 43 READ_CONFIG_ERROR = 1, 44 READ_CONFIG_RECURSION = 2, /* Too much recursion */ 45 READ_CONFIG_FILE_NOT_FOUND = 3, /* Returned only w. allow_missing_file */ 46 }; 47 48 49 struct Sample 50 { 51 splen_t 52 loop_start, loop_end, data_length; 53 int32_t 54 sample_rate, low_freq, high_freq, root_freq; 55 int8_t panning, note_to_use; 56 int32_t 57 envelope_rate[6], envelope_offset[6], 58 modenv_rate[6], modenv_offset[6]; 59 double 60 volume; 61 sample_t 62 *data; 63 int32_t 64 tremolo_sweep_increment, tremolo_phase_increment, 65 vibrato_sweep_increment, vibrato_control_ratio; 66 int16_t 67 tremolo_depth; 68 int16_t vibrato_depth; 69 uint8_t 70 modes, data_alloced, 71 low_vel, high_vel; 72 int32_t cutoff_freq; /* in Hz, [1, 20000] */ 73 int16_t resonance; /* in centibels, [0, 960] */ 74 /* in cents, [-12000, 12000] */ 75 int16_t tremolo_to_pitch, tremolo_to_fc, modenv_to_pitch, modenv_to_fc, 76 envelope_keyf[6], envelope_velf[6], modenv_keyf[6], modenv_velf[6], 77 vel_to_fc, key_to_fc; 78 int16_t vel_to_resonance; /* in centibels, [-960, 960] */ 79 int8_t envelope_velf_bpo, modenv_velf_bpo, 80 key_to_fc_bpo, vel_to_fc_threshold; /* in notes */ 81 int32_t vibrato_delay, tremolo_delay, envelope_delay, modenv_delay; /* in samples */ 82 int16_t scale_freq; /* in notes */ 83 int16_t scale_factor; /* in 1024divs/key */ 84 int8_t inst_type; 85 int32_t sf_sample_index, sf_sample_link; /* for stereo SoundFont */ 86 uint16_t sample_type; /* 1 = Mono, 2 = Right, 4 = Left, 8 = Linked, $8000 = ROM */ 87 double root_freq_detected; /* root freq from pitch detection */ 88 int transpose_detected; /* note offset from detected root */ 89 int chord; /* type of chord for detected pitch */ 90 }; 91 92 /* Bits in modes: */ 93 enum 94 { 95 MODES_16BIT = (1 << 0), 96 MODES_UNSIGNED = (1 << 1), 97 MODES_LOOPING = (1 << 2), 98 MODES_PINGPONG = (1 << 3), 99 MODES_REVERSE = (1 << 4), 100 MODES_SUSTAIN = (1 << 5), 101 MODES_ENVELOPE = (1 << 6), 102 MODES_CLAMPED = (1 << 7), /* ?? (for last envelope??) */ 103 104 INST_GUS = 0, 105 INST_SF2 = 1, 106 INST_MOD = 2, 107 INST_PCM = 3, /* %sample */ 108 109 /* sfSampleType */ 110 SF_SAMPLETYPE_MONO = 1, 111 SF_SAMPLETYPE_RIGHT = 2, 112 SF_SAMPLETYPE_LEFT = 4, 113 SF_SAMPLETYPE_LINKED = 8, 114 SF_SAMPLETYPE_ROM = 0x8000, 115 }; 116 117 struct Instrument 118 { 119 int type; 120 int samples; 121 Sample *sample; 122 char *instname; 123 }; 124 125 struct ToneBankElement 126 { 127 char *name; 128 char *comment; 129 Instrument *instrument; 130 int8_t note, pan, strip_loop, strip_envelope, strip_tail, loop_timeout, 131 font_preset, font_keynote, legato, tva_level, play_note, damper_mode; 132 uint8_t font_bank; 133 uint8_t instype; /* 0: Normal 134 1: %font 135 2: %sample 136 3-255: reserved 137 */ 138 int16_t amp; 139 int16_t rnddelay; 140 int tunenum; 141 float *tune; 142 int sclnotenum; 143 int16_t *sclnote; 144 int scltunenum; 145 int16_t *scltune; 146 int fcnum; 147 int16_t *fc; 148 int resonum; 149 int16_t *reso; 150 int trempitchnum, tremfcnum, modpitchnum, modfcnum; 151 int16_t *trempitch, *tremfc, *modpitch, *modfc; 152 int envratenum, envofsnum; 153 int **envrate, **envofs; 154 int modenvratenum, modenvofsnum; 155 int **modenvrate, **modenvofs; 156 int envvelfnum, envkeyfnum; 157 int **envvelf, **envkeyf; 158 int modenvvelfnum, modenvkeyfnum; 159 int **modenvvelf, **modenvkeyf; 160 int tremnum, vibnum; 161 struct Quantity_ **trem, **vib; 162 int16_t vel_to_fc, key_to_fc, vel_to_resonance; 163 int8_t reverb_send, chorus_send, delay_send; 164 }; 165 166 /* A hack to delay instrument loading until after reading the 167 entire MIDI file. */ 168 #define MAGIC_LOAD_INSTRUMENT ((Instrument *)(-1)) 169 #define MAGIC_ERROR_INSTRUMENT ((Instrument *)(-2)) 170 #define IS_MAGIC_INSTRUMENT(ip) ((ip) == MAGIC_LOAD_INSTRUMENT || (ip) == MAGIC_ERROR_INSTRUMENT) 171 172 #define DYNAMIC_INSTRUMENT_NAME "" 173 174 struct AlternateAssign 175 { 176 /* 128 bit vector: 177 * bits[(note >> 5) & 0x3] & (1 << (note & 0x1F)) 178 */ 179 uint32_t bits[4]; 180 AlternateAssign* next; 181 }; 182 183 struct ToneBank 184 { 185 ToneBankElement tone[128]; 186 AlternateAssign *alt; 187 }; 188 189 struct SpecialPatch /* To be used MIDI Module play mode */ 190 { 191 int type; 192 int samples; 193 Sample *sample; 194 char *name; 195 int32_t sample_offset; 196 }; 197 198 enum instrument_mapID 199 { 200 INST_NO_MAP = 0, 201 SC_55_TONE_MAP, 202 SC_55_DRUM_MAP, 203 SC_88_TONE_MAP, 204 SC_88_DRUM_MAP, 205 SC_88PRO_TONE_MAP, 206 SC_88PRO_DRUM_MAP, 207 SC_8850_TONE_MAP, 208 SC_8850_DRUM_MAP, 209 XG_NORMAL_MAP, 210 XG_SFX64_MAP, 211 XG_SFX126_MAP, 212 XG_DRUM_MAP, 213 GM2_TONE_MAP, 214 GM2_DRUM_MAP, 215 NUM_INST_MAP 216 }; 217 218 enum 219 { 220 MAP_BANK_COUNT = 256, 221 NSPECIAL_PATCH = 256, 222 SPECIAL_PROGRAM = -1, 223 MAX_MREL = 5000, 224 DEFAULT_MREL = 800, 225 }; 226 227 struct SFInsts; 228 struct InstList; 229 struct SampleList; 230 struct AIFFCommonChunk; 231 struct AIFFSoundDataChunk; 232 struct SampleImporter; 233 234 class Instruments 235 { 236 std::string configFileName; 237 MusicIO::SoundFontReaderInterface *sfreader; 238 239 ToneBank standard_tonebank, standard_drumset; 240 241 enum 242 { 243 INSTRUMENT_HASH_SIZE = 128, 244 }; 245 246 struct InstrumentCache 247 { 248 char *name; 249 int panning, amp, note_to_use, strip_loop, strip_envelope, strip_tail; 250 Instrument *ip; 251 InstrumentCache *next; 252 }; 253 254 InstrumentCache *instrument_cache[INSTRUMENT_HASH_SIZE] = { nullptr }; 255 256 /* bank mapping (mapped bank) */ 257 struct bank_map_elem 258 { 259 int16_t used = 0, mapid = 0; 260 int bankno = 0; 261 }; 262 bank_map_elem map_bank[MAP_BANK_COUNT], map_drumset[MAP_BANK_COUNT]; 263 int map_bank_counter = 0; 264 265 struct inst_map_elem 266 { 267 int set, elem, mapped; 268 }; 269 270 inst_map_elem *inst_map_table[NUM_INST_MAP][128] = { { nullptr} }; 271 272 struct UserInstrument 273 { 274 int8_t bank; 275 int8_t prog; 276 int8_t source_map; 277 int8_t source_bank; 278 int8_t source_prog; 279 int8_t vibrato_rate; 280 int8_t vibrato_depth; 281 int8_t cutoff_freq; 282 int8_t resonance; 283 int8_t env_attack; 284 int8_t env_decay; 285 int8_t env_release; 286 int8_t vibrato_delay; 287 UserInstrument *next; 288 }; 289 290 UserInstrument *userinst_first = (UserInstrument *)NULL; 291 UserInstrument *userinst_last = (UserInstrument *)NULL; 292 293 struct UserDrumset { 294 int8_t bank; 295 int8_t prog; 296 int8_t play_note; 297 int8_t level; 298 int8_t assign_group; 299 int8_t pan; 300 int8_t reverb_send_level; 301 int8_t chorus_send_level; 302 int8_t rx_note_off; 303 int8_t rx_note_on; 304 int8_t delay_send_level; 305 int8_t source_map; 306 int8_t source_prog; 307 int8_t source_note; 308 UserDrumset *next; 309 }; 310 311 struct SFBags 312 { 313 int nbags; 314 uint16_t *bag; 315 int ngens; 316 SFGenRec *gen; 317 }; 318 319 SFBags prbags, inbags; 320 321 UserDrumset *userdrum_first = (UserDrumset *)NULL; 322 UserDrumset *userdrum_last = (UserDrumset *)NULL; 323 324 AlternateAssign alt[2]; 325 326 /* Some functions get aggravated if not even the standard banks are available. */ 327 ToneBank 328 *tonebank[128 + MAP_BANK_COUNT] = { &standard_tonebank }, 329 *drumset[128 + MAP_BANK_COUNT] = { &standard_drumset }; 330 331 Instrument *default_instrument = 0; 332 SpecialPatch *special_patch[NSPECIAL_PATCH] = { nullptr }; 333 int default_program[MAX_CHANNELS] = { 0 }; /* This is only used for tracks that don't specify a program */ 334 335 char *default_instrument_name = nullptr; 336 int progbase = 0; 337 int32_t modify_release = 0; 338 bool opt_sf_close_each_file = true; 339 char def_instr_name[256] = { '\0' }; 340 SFInsts *sfrecs = nullptr; 341 SFInsts *current_sfrec = nullptr; 342 343 int last_sample_type = 0; 344 int last_sample_instrument = 0; 345 int last_sample_keyrange = 0; 346 SampleList *last_sample_list = nullptr; 347 348 LayerItem layer_items[SF_EOF]; 349 350 /* convert from 8bit value to fractional offset (15.15) */ to_offset_22(int offset)351 int32_t to_offset_22(int offset) 352 { 353 return (int32_t)offset << (7 + 15); 354 } 355 356 int32_t calc_rate_i(int diff, double msec); 357 int32_t convert_envelope_rate(uint8_t rate); 358 int32_t convert_envelope_offset(uint8_t offset); 359 int32_t convert_tremolo_sweep(uint8_t sweep); 360 int32_t convert_vibrato_sweep(uint8_t sweep, int32_t vib_control_ratio); 361 int32_t convert_tremolo_rate(uint8_t rate); 362 int32_t convert_vibrato_rate(uint8_t rate); 363 void reverse_data(int16_t *sp, int32_t ls, int32_t le); 364 int name_hash(char *name); 365 Instrument *search_instrument_cache(char *name, int panning, int amp, int note_to_use, int strip_loop, int strip_envelope, int strip_tail); 366 void store_instrument_cache(Instrument *ip, char *name, int panning, int amp, int note_to_use, int strip_loop, int strip_envelope, int strip_tail); 367 int32_t to_rate(int rate); 368 void apply_bank_parameter(Instrument *ip, ToneBankElement *tone); 369 Instrument *load_gus_instrument(char *name, ToneBank *bank, int dr, int prog); 370 int fill_bank(int dr, int b, int *rc); 371 void free_tone_bank_list(ToneBank *tb[]); 372 void free_tone_bank(void); 373 void free_instrument_map(void); 374 int set_default_instrument(char *name); 375 void *safe_memdup(void *s, size_t size); 376 void MarkInstrument(int banknum, int percussion, int instr); 377 378 //smplfile.c 379 Instrument *extract_sample_file(char *); 380 int32_t convert_envelope_rate_s(uint8_t rate); 381 void initialize_sample(Instrument *inst, int frames, int sample_bits, int sample_rate); 382 int get_importers(const char *sample_file, int limit, SampleImporter **importers); 383 int get_next_importer(char *sample_file, int start, int count, SampleImporter **importers); 384 385 int import_wave_discriminant(char *sample_file); 386 int import_wave_load(char *sample_file, Instrument *inst); 387 int import_aiff_discriminant(char *sample_file); 388 int import_aiff_load(char *sample_file, Instrument *inst); 389 int read_AIFFCommonChunk(timidity_file *tf, AIFFCommonChunk *comm, int csize, int compressed); 390 int read_AIFFSoundData(timidity_file *tf, Instrument *inst, AIFFCommonChunk *common); 391 int read_AIFFSoundDataChunk(timidity_file *tf, AIFFSoundDataChunk *sound, int csize, int mode); 392 393 // sndfont.cpp 394 395 SFInsts *find_soundfont(char *sf_file); 396 SFInsts *new_soundfont(char *sf_file); 397 void init_sf(SFInsts *rec); 398 void end_soundfont(SFInsts *rec); 399 Instrument *try_load_soundfont(SFInsts *rec, int order, int bank, int preset, int keynote); 400 Instrument *load_from_file(SFInsts *rec, InstList *ip); 401 int is_excluded(SFInsts *rec, int bank, int preset, int keynote); 402 int is_ordered(SFInsts *rec, int bank, int preset, int keynote); 403 int load_font(SFInfo *sf, int pridx); 404 int parse_layer(SFInfo *sf, int pridx, LayerTable *tbl, int level); 405 int is_global(SFGenLayer *layer); 406 void clear_table(LayerTable *tbl); 407 void set_to_table(SFInfo *sf, LayerTable *tbl, SFGenLayer *lay, int level); 408 void add_item_to_table(LayerTable *tbl, int oper, int amount, int level); 409 void merge_table(SFInfo *sf, LayerTable *dst, LayerTable *src); 410 void init_and_merge_table(SFInfo *sf, LayerTable *dst, LayerTable *src); 411 int sanity_range(LayerTable *tbl); 412 int make_patch(SFInfo *sf, int pridx, LayerTable *tbl); 413 void make_info(SFInfo *sf, SampleList *vp, LayerTable *tbl); 414 double calc_volume(LayerTable *tbl); 415 void set_sample_info(SFInfo *sf, SampleList *vp, LayerTable *tbl); 416 void set_init_info(SFInfo *sf, SampleList *vp, LayerTable *tbl); 417 void reset_last_sample_info(void); 418 int abscent_to_Hz(int abscents); 419 void set_rootkey(SFInfo *sf, SampleList *vp, LayerTable *tbl); 420 void set_rootfreq(SampleList *vp); 421 int32_t to_offset(int32_t offset); 422 int32_t to_rate(int32_t diff, int timecent); 423 int32_t calc_rate(int32_t diff, double msec); 424 double to_msec(int timecent); 425 int32_t calc_sustain(int sust_cB); 426 void convert_volume_envelope(SampleList *vp, LayerTable *tbl); 427 void convert_tremolo(SampleList *vp, LayerTable *tbl); 428 void convert_vibrato(SampleList *vp, LayerTable *tbl); 429 void set_envelope_parameters(SampleList *vp); 430 431 // configfile 432 433 int set_patchconf(const char *name, int line, ToneBank *bank, char *w[], int dr, int mapid, int bankmapfrom, int bankno); 434 int strip_trailing_comment(char *string, int next_token_index); 435 char *expand_variables(char *string, MBlockList *varbuf, const char *basedir); 436 int set_gus_patchconf(const char *name, int line, ToneBankElement *tone, char *pat, char **opts); 437 void reinit_tone_bank_element(ToneBankElement *tone); 438 int set_gus_patchconf_opts(const char *name, int line, char *opts, ToneBankElement *tone); 439 int copymap(int mapto, int mapfrom, int isdrum); 440 void copybank(ToneBank *to, ToneBank *from, int mapid, int bankmapfrom, int bankno); 441 442 // sffile.cpp 443 444 int chunkid(char *id); 445 int process_list(int size, SFInfo *sf, timidity_file *fd); 446 int process_info(int size, SFInfo *sf, timidity_file *fd); 447 int process_sdta(int size, SFInfo *sf, timidity_file *fd); 448 int process_pdta(int size, SFInfo *sf, timidity_file *fd); 449 void load_sample_names(int size, SFInfo *sf, timidity_file *fd); 450 void load_preset_header(int size, SFInfo *sf, timidity_file *fd); 451 void load_inst_header(int size, SFInfo *sf, timidity_file *fd); 452 void load_bag(int size, SFBags *bagp, timidity_file *fd); 453 void load_gen(int size, SFBags *bagp, timidity_file *fd); 454 void load_sample_info(int size, SFInfo *sf, timidity_file *fd); 455 void convert_layers(SFInfo *sf); 456 void generate_layers(SFHeader *hdr, SFHeader *next, SFBags *bags); 457 void free_layer(SFHeader *hdr); 458 int load_soundfont(SFInfo *sf, timidity_file *fd); 459 void free_soundfont(SFInfo *sf); 460 void correct_samples(SFInfo *sf); 461 462 463 public: 464 465 Instruments(); 466 bool load(MusicIO::SoundFontReaderInterface *); 467 ~Instruments(); 468 toneBank(int i)469 const ToneBank *toneBank(int i) const 470 { 471 return tonebank[i]; 472 } 473 defaultProgram(int i)474 int defaultProgram(int i) const 475 { 476 return default_program[i]; 477 } 478 drumSet(int i)479 const ToneBank *drumSet(int i) const 480 { 481 return drumset[i]; 482 } 483 specialPatch(int i)484 const SpecialPatch *specialPatch(int i) const 485 { 486 return special_patch[i]; 487 } 488 setSpecialPatchOffset(int i,int32_t ofs)489 void setSpecialPatchOffset(int i, int32_t ofs) 490 { 491 special_patch[i]->sample_offset = ofs; 492 } defaultInstrument()493 Instrument *defaultInstrument() const 494 { 495 return default_instrument; 496 } 497 498 /* instrum.c */ 499 int load_missing_instruments(int *rc); 500 void free_instruments(int reload_default_inst); 501 void free_special_patch(int id); 502 void clear_magic_instruments(void); 503 Instrument *load_instrument(int dr, int b, int prog); 504 int find_instrument_map_bank(int dr, int map, int bk); 505 int alloc_instrument_map_bank(int dr, int map, int bk); 506 void alloc_instrument_bank(int dr, int bankset); 507 int instrument_map(int mapID, int *set_in_out, int *elem_in_out) const; 508 void set_instrument_map(int mapID, int set_from, int elem_from, int set_to, int elem_to); 509 AlternateAssign *add_altassign_string(AlternateAssign *old, char **params, int n); 510 AlternateAssign *find_altassign(AlternateAssign *altassign, int note); 511 void copy_tone_bank_element(ToneBankElement *elm, const ToneBankElement *src); 512 void free_tone_bank_element(ToneBankElement *elm); 513 void free_instrument(Instrument *ip); 514 void squash_sample_16to8(Sample *sp); 515 Instrument *play_midi_load_instrument(int dr, int bk, int prog, bool *pLoad_success); 516 void recompute_userinst(int bank, int prog); 517 Instrument *recompute_userdrum(int bank, int prog); 518 UserInstrument *get_userinst(int bank, int prog); 519 UserDrumset *get_userdrum(int bank, int prog); 520 void recompute_userdrum_altassign(int bank, int group); 521 /*! initialize GS user drumset. */ 522 void init_userdrum(); 523 void free_userdrum(); init_userinst()524 void init_userinst() { free_userinst(); } 525 void free_userinst(); 526 mark_instrument(int newbank,int newprog)527 void mark_instrument(int newbank, int newprog) 528 { 529 if (!(tonebank[newbank]->tone[newprog].instrument)) 530 tonebank[newbank]->tone[newprog].instrument = 531 MAGIC_LOAD_INSTRUMENT; 532 } 533 mark_drumset(int newbank,int newprog)534 void mark_drumset(int newbank, int newprog) 535 { 536 if (!(drumset[newbank]->tone[newprog].instrument)) 537 drumset[newbank]->tone[newprog].instrument = 538 MAGIC_LOAD_INSTRUMENT; 539 } 540 541 /* sndfont.c */ 542 void add_soundfont(char *sf_file, int sf_order, int cutoff_allowed, int resonance_allowed, int amp); 543 void remove_soundfont(char *sf_file); 544 void init_load_soundfont(void); 545 Instrument *load_soundfont_inst(int order, int bank, int preset, int keynote); 546 Instrument *extract_soundfont(char *sf_file, int bank, int preset, int keynote); 547 int exclude_soundfont(int bank, int preset, int keynote); 548 int order_soundfont(int bank, int preset, int keynote, int order); 549 char *soundfont_preset_name(int bank, int preset, int keynote, char **sndfile); 550 void free_soundfonts(void); 551 void PrecacheInstruments(const uint16_t *instruments, int count); 552 553 554 int read_config_file(const char *name, int self, int allow_missing_file); 555 set_default_instrument()556 void set_default_instrument() 557 { 558 set_default_instrument(def_instr_name); 559 } 560 }; 561 562 563 } 564 #endif /* ___INSTRUM_H_ */ 565