2 TiMidity++ -- MIDI to WAVE converter and player
3 Copyright (C) 1999-2018 Masanao Izumo <iz@onicos.co.jp>
4 Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
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.
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.
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
22 Code to load and unload GUS-compatible instrument patches.
27 #endif /* HAVE_CONFIG_H */
31 #endif /* HAVE_STDLIB_H */
56 #define INSTRUMENT_HASH_SIZE 128
57 struct InstrumentCache
60 int panning, amp, note_to_use, strip_loop, strip_envelope, strip_tail;
62 struct InstrumentCache *next;
64 static struct InstrumentCache *instrument_cache[INSTRUMENT_HASH_SIZE];
66 /* Some functions get aggravated if not even the standard banks are
68 static ToneBank standard_tonebank, standard_drumset;
70 *tonebank[128 + MAP_BANK_COUNT] = {&standard_tonebank};
72 *drumset[128 + MAP_BANK_COUNT] = {&standard_drumset};
74 /* bank mapping (mapped bank) */
75 struct bank_map_elem {
79 static struct bank_map_elem map_bank[MAP_BANK_COUNT], map_drumset[MAP_BANK_COUNT];
80 static int map_bank_counter;
82 /* This is a special instrument, used for all melodic programs */
83 Instrument *default_instrument=0;
84 SpecialPatch *special_patch[NSPECIAL_PATCH];
88 int set, elem, mapped;
91 static struct inst_map_elem *inst_map_table[NUM_INST_MAP][128];
93 /* This is only used for tracks that don't specify a program */
94 int default_program[MAX_CHANNELS];
96 int special_program[MAX_CHANNELS];
99 char *default_instrument_name = NULL;
101 int antialiasing_allowed=0;
107 int opt_print_fontname = 0;
110 int32 modify_release;
112 /** below three functinos are imported from sndfont.c **/
114 /* convert from 8bit value to fractional offset (15.15) */
115 static int32 to_offset(int offset)
117 return (int32)offset << (7+15);
120 /* calculate ramp rate in fractional unit;
121 * diff = 8bit, time = msec
123 static int32 calc_rate(int diff, double msec)
132 rate = ((double)diff / play_mode->rate) * control_ratio * 1000.0 / msec;
137 /*End of Pseudo Reverb*/
139 void free_instrument(Instrument *ip)
143 extern void free_pcm_sample_file(Instrument*); /* from smplfile.c */
145 extern void free_int_synth_file(Instrument *ip);
148 extern void free_dls_file(Instrument *ip);
151 extern void free_sfz_file(Instrument *ip);
156 for (i=0; i<ip->samples; i++) {
158 if(sp->data_alloced) {
159 sp->data_alloced = 0;
164 safe_free(ip->sample);
169 free_pcm_sample_file(ip);
174 free_int_synth_file(ip);
191 void clear_magic_instruments(void)
196 for(j = 0; j < 128 + map_bank_counter; j++)
200 ToneBank *bank = tonebank[j];
201 for(i = 0; i < 128; i++)
202 for(elm = 0; elm < MAX_ELEMENT; elm++)
203 if(bank->tone[i][elm])
204 if(IS_MAGIC_INSTRUMENT(bank->tone[i][elm]->instrument))
205 bank->tone[i][elm]->instrument = NULL;
209 ToneBank *bank = drumset[j];
210 for(i = 0; i < 128; i++)
211 for(elm = 0; elm < MAX_ELEMENT; elm++)
212 if(bank->tone[i][elm])
213 if(IS_MAGIC_INSTRUMENT(bank->tone[i][elm]->instrument))
214 bank->tone[i][elm]->instrument = NULL;
219 #define GUS_ENVRATE_MAX (int32)(0x3FFFFFFF >> 9)
221 static int32 convert_envelope_rate(uint8 rate)
225 r=3-((rate>>6) & 0x3);
227 r = (int32)(rate & 0x3f) << r; /* 6.9 fixed point */
229 /* 15.15 fixed point. */
230 r = r * 44100 / play_mode->rate * control_ratio * (1 << fast_decay);
231 if(r > GUS_ENVRATE_MAX) {r = GUS_ENVRATE_MAX;}
235 static int32 convert_envelope_offset(uint8 offset)
237 /* This is not too good... Can anyone tell me what these values mean?
238 Are they GUS-style "exponential" volumes? And what does that mean? */
240 /* 15.15 fixed point */
241 return offset << (7+15);
244 static int32 convert_gus_lfo_sweep(uint8 sweep)
248 return 1000 * sweep / 38; // SWEEP_TUNING
251 static int32 convert_gus_lfo_rate(uint8 rate)
255 return 1000 * rate / 38; // TREMOLO_RATE_TUNING, VIBRATO_RATE_TUNING
258 static void reverse_data(int16 *sp, int32 ls, int32 le)
265 for(i = 0; i < le; i++)
273 static int name_hash(char *name)
275 unsigned int addr = 0;
279 return addr % INSTRUMENT_HASH_SIZE;
282 static Instrument *search_instrument_cache(char *name,
283 int panning, int amp, int note_to_use,
284 int strip_loop, int strip_envelope,
287 struct InstrumentCache *p;
289 for(p = instrument_cache[name_hash(name)]; p != NULL; p = p->next)
291 if(strcmp(p->name, name) != 0)
293 if(p->panning == panning &&
295 p->note_to_use == note_to_use &&
296 p->strip_loop == strip_loop &&
297 p->strip_envelope == strip_envelope &&
298 p->strip_tail == strip_tail)
304 static void store_instrument_cache(Instrument *ip,
306 int panning, int amp, int note_to_use,
307 int strip_loop, int strip_envelope,
310 struct InstrumentCache *p;
313 addr = name_hash(name);
314 p = (struct InstrumentCache *)safe_malloc(sizeof(struct InstrumentCache));
315 p->next = instrument_cache[addr];
316 instrument_cache[addr] = p;
318 p->panning = panning;
320 p->note_to_use = note_to_use;
321 p->strip_loop = strip_loop;
322 p->strip_envelope = strip_envelope;
323 p->strip_tail = strip_tail;
327 static int32 adjust_tune_freq(int32 val, float tune)
331 return val / pow(2.0, tune * DIV_12);
334 static int16 adjust_scale_tune(int16 val)
336 return 1024 * (double) val / 100 + 0.5;
339 static int16 adjust_fc(int16 val)
341 if (val < 0 || val > play_mode->rate / 2) {
348 static int16 adjust_reso(int16 val)
350 if (val < 0 || val > 960) {
357 static int32 to_rate(int rate)
359 return (rate) ? (int32) (0x200 * pow(2.0, rate / 17.0)
360 * 44100 / play_mode->rate * control_ratio) << fast_decay : 0;
364 static int32 to_control(int control)
366 return (int32) (0x2000 / pow(2.0, control / 31.0));
371 static void apply_bank_parameter(Instrument *ip, ToneBankElement *tone)
376 // timidity.c read_config_file() "soundfont" // init tonebank
\82Å reinit_tone_bank_element()
377 // soundfont
\83R
\83}
\83\93\83h
\82Ì
\82Æ
\82«tonebank
\8f\89\8aú
\8fó
\91Ô
\82ªall0
\82É
\82È
\82é
\82Ì
\82ð
\89ñ
\94ð
380 if (tone->amp != -1) {
381 for (i = 0; i < ip->samples; i++)
382 #if 1 // float (timidity.c set_gus_patchconf_opts()
383 ip->sample[i].cfg_amp = (FLOAT_T)tone->amp * DIV_12BIT;
385 ip->sample[i].cfg_amp = (FLOAT_T)tone->amp * DIV_100;
388 /* normalize volume */
389 if (tone->amp_normalize == 1) {
390 FLOAT_T volume_max = 0;
391 for (i = 0; i < ip->samples; i++)
392 if (volume_max < ip->sample[i].volume)
393 volume_max = ip->sample[i].volume;
394 if (volume_max != 0){
395 volume_max = 1.0 / volume_max; // div to mul
396 for (i = 0; i < ip->samples; i++)
397 ip->sample[i].volume *= volume_max;
401 if (tone->def_pan != -1) {
402 int pan = ((int) tone->def_pan & 0x7f);
403 for (i = 0; i < ip->samples; i++) {
404 ip->sample[i].def_pan = pan;
408 if ((tone->sample_pan != -1 || tone->sample_width != -1)) {
409 FLOAT_T span = (tone->sample_pan == -1) ?
410 0.0 : ((FLOAT_T)(tone->sample_pan - 200) * DIV_200); // def 0 , offset 200
411 FLOAT_T swid = (tone->sample_width == -1) ?
412 1.0 : ((FLOAT_T)(tone->sample_width - 800) * DIV_100); // def 100 , offset 800
413 for (i = 0; i < ip->samples; i++) {
414 FLOAT_T panning = ip->sample[i].sample_pan;
418 else if(panning < -0.5)
423 else if(panning < -0.5)
425 ip->sample[i].sample_pan = panning;
429 if (tone->note != -1)
430 for (i = 0; i < ip->samples; i++){
431 ip->sample[i].root_key = tone->note & 0x7f;
432 ip->sample[i].root_freq = freq_table[tone->note & 0x7f];
434 /* filter key-follow */
435 if (tone->key_to_fc != 0)
436 for (i = 0; i < ip->samples; i++)
437 ip->sample[i].key_to_fc = tone->key_to_fc;
438 /* filter velocity-follow */
439 if (tone->vel_to_fc != 0)
440 for (i = 0; i < ip->samples; i++)
441 ip->sample[i].vel_to_fc = tone->vel_to_fc;
442 /* resonance velocity-follow */
443 if (tone->vel_to_resonance != 0)
444 for (i = 0; i < ip->samples; i++){
446 ip->sample[i].vel_to_resonance = tone->vel_to_resonance;
449 if (tone->strip_tail == 1)
450 for (i = 0; i < ip->samples; i++){
451 ip->sample[i].data_length = ip->sample[i].loop_end;
454 if (tone->lpf_type != -1)
455 for (i = 0; i < ip->samples; i++){
456 ip->sample[i].lpf_type = tone->lpf_type;
459 if(tone->keep_voice != 0)
460 for (i = 0; i < ip->samples; i++){
461 ip->sample[i].keep_voice = tone->keep_voice;
465 /* sample_lokey/hikey/lovel/hivel */
466 if (tone->sample_lokeynum)
467 for (i = 0; i < ip->samples; i++) {
469 if (tone->sample_lokeynum == 1) {
470 if (tone->sample_lokey[0] >= 0)
471 sp->low_key = tone->sample_lokey[0] & 0x7f;
472 } else if (i < tone->sample_lokeynum) {
473 if (tone->sample_lokey[i] >= 0)
474 sp->low_key = tone->sample_lokey[i] & 0x7f;
477 if (tone->sample_hikeynum)
478 for (i = 0; i < ip->samples; i++) {
480 if (tone->sample_hikeynum == 1) {
481 if (tone->sample_hikey[0] >= 0)
482 sp->high_key = tone->sample_hikey[0] & 0x7f;
483 } else if (i < tone->sample_hikeynum) {
484 if (tone->sample_hikey[i] >= 0)
485 sp->high_key = tone->sample_hikey[i] & 0x7f;
488 if (tone->sample_lovelnum)
489 for (i = 0; i < ip->samples; i++) {
491 if (tone->sample_lovelnum == 1) {
492 if (tone->sample_lovel[0] >= 0)
493 sp->low_vel = tone->sample_lovel[0];
494 } else if (i < tone->sample_lovelnum) {
495 if (tone->sample_lovel[i] >= 0)
496 sp->low_vel = tone->sample_lovel[i];
499 if (tone->sample_hivelnum)
500 for (i = 0; i < ip->samples; i++) {
502 if (tone->sample_hivelnum == 1) {
503 if (tone->sample_hivel[0] >= 0)
504 sp->high_vel = tone->sample_hivel[0];
505 } else if (i < tone->sample_hivelnum) {
506 if (tone->sample_hivel[i] >= 0)
507 sp->high_vel = tone->sample_hivel[i];
510 /* after sample_lokey/hikey/lovel/hivel */
511 if (tone->lokey != -1){
512 int32 key = tone->lokey & 0x7f;
513 for (i = 0; i < ip->samples; i++){
515 if(sp->low_key < key)
519 if (tone->hikey != -1){
520 int32 key = tone->hikey & 0x7f;
521 for (i = 0; i < ip->samples; i++){
523 if(sp->high_key > key)
527 if (tone->lovel != -1)
528 for (i = 0; i < ip->samples; i++){
530 if(sp->low_vel < tone->lovel)
531 sp->low_vel = tone->lovel;
533 if (tone->hivel != -1)
534 for (i = 0; i < ip->samples; i++){
536 if(sp->high_vel > tone->hivel)
537 sp->high_vel = tone->hivel;
541 for (i = 0; i < ip->samples; i++) {
543 if (tone->tunenum == 1) {
544 // sp->root_freq = adjust_tune_freq(sp->root_freq, tone->tune[0]);
545 sp->tune *= pow(2.0, (double)tone->tune[0] * DIV_12);
546 } else if (i < tone->tunenum) {
547 // sp->root_freq = adjust_tune_freq(sp->root_freq, tone->tune[i]);
548 sp->tune *= pow(2.0, (double)tone->tune[i] * DIV_12);
551 if (tone->envratenum)
552 for (i = 0; i < ip->samples; i++) {
554 if (tone->envratenum == 1) {
555 for (j = 0; j < 6; j++)
556 if (tone->envrate[0][j] >= 0)
557 sp->envelope_rate[j] = to_rate(tone->envrate[0][j]);
558 } else if (i < tone->envratenum) {
559 for (j = 0; j < 6; j++)
560 if (tone->envrate[i][j] >= 0)
561 sp->envelope_rate[j] = to_rate(tone->envrate[i][j]);
565 for (i = 0; i < ip->samples; i++) {
567 if (tone->envofsnum == 1) {
568 for (j = 0; j < 6; j++)
569 if (tone->envofs[0][j] >= 0)
570 sp->envelope_offset[j] = to_offset(tone->envofs[0][j]);
571 } else if (i < tone->envofsnum) {
572 for (j = 0; j < 6; j++)
573 if (tone->envofs[i][j] >= 0)
574 sp->envelope_offset[j] = to_offset(tone->envofs[i][j]);
579 for (i = 0; i < ip->samples; i++) {
581 if (tone->tremnum == 1) {
582 if (IS_QUANTITY_DEFINED(tone->trem[0][0]))
583 sp->tremolo_sweep = quantity_to_int(&tone->trem[0][0], 0);
584 if (IS_QUANTITY_DEFINED(tone->trem[0][1]))
585 sp->tremolo_freq = quantity_to_int(&tone->trem[0][1], 0);
586 if (IS_QUANTITY_DEFINED(tone->trem[0][2]))
587 sp->tremolo_to_amp = quantity_to_int(&tone->trem[0][2], 0);
588 } else if (i < tone->tremnum) {
589 if (IS_QUANTITY_DEFINED(tone->trem[i][0]))
590 sp->tremolo_sweep = quantity_to_int(&tone->trem[i][0], 0);
591 if (IS_QUANTITY_DEFINED(tone->trem[i][1]))
592 sp->tremolo_freq = quantity_to_int(&tone->trem[i][1], 0);
593 if (IS_QUANTITY_DEFINED(tone->trem[i][2]))
594 sp->tremolo_to_amp = quantity_to_int(&tone->trem[i][2], 0);
597 if (tone->tremdelaynum)
598 for (i = 0; i < ip->samples; i++) {
600 if (tone->tremdelaynum == 1)
601 sp->tremolo_delay = tone->tremdelay[0];
602 else if (i < tone->tremdelaynum)
603 sp->tremolo_delay = tone->tremdelay[i];
605 if (tone->tremsweepnum)
606 for (i = 0; i < ip->samples; i++) {
608 if (tone->tremsweepnum == 1)
609 sp->tremolo_sweep = tone->tremsweep[0];
610 else if (i < tone->tremsweepnum)
611 sp->tremolo_sweep = tone->tremsweep[i];
613 if (tone->tremfreqnum)
614 for (i = 0; i < ip->samples; i++) {
616 if (tone->tremfreqnum == 1)
617 sp->tremolo_freq = tone->tremfreq[0];
618 else if (i < tone->tremfreqnum)
619 sp->tremolo_freq = tone->tremfreq[i];
621 if (tone->tremampnum)
622 for (i = 0; i < ip->samples; i++) {
624 if (tone->tremampnum == 1)
625 sp->tremolo_to_amp = tone->tremamp[0];
626 else if (i < tone->tremampnum)
627 sp->tremolo_to_amp = tone->tremamp[i];
629 if (tone->trempitchnum)
630 for (i = 0; i < ip->samples; i++) {
632 if (tone->trempitchnum == 1)
633 sp->tremolo_to_pitch = tone->trempitch[0];
634 else if (i < tone->trempitchnum)
635 sp->tremolo_to_pitch = tone->trempitch[i];
638 for (i = 0; i < ip->samples; i++) {
640 if (tone->tremfcnum == 1)
641 sp->tremolo_to_fc = tone->tremfc[0];
642 else if (i < tone->tremfcnum)
643 sp->tremolo_to_fc = tone->tremfc[i];
647 for (i = 0; i < ip->samples; i++) {
649 if (tone->vibnum == 1) {
650 if (IS_QUANTITY_DEFINED(tone->vib[0][0]))
651 sp->vibrato_sweep = quantity_to_int(&tone->vib[0][0], 0);
652 if (IS_QUANTITY_DEFINED(tone->vib[0][1]))
653 sp->vibrato_freq = quantity_to_int(&tone->vib[0][1], 0);
654 if (IS_QUANTITY_DEFINED(tone->vib[0][2]))
655 sp->vibrato_to_pitch = quantity_to_int(&tone->vib[0][2], 0);
656 } else if (i < tone->vibnum) {
657 if (IS_QUANTITY_DEFINED(tone->vib[i][0]))
658 sp->vibrato_sweep = quantity_to_int(&tone->vib[i][0], 0);
659 if (IS_QUANTITY_DEFINED(tone->vib[i][1]))
660 sp->vibrato_freq = quantity_to_int(&tone->vib[i][1], 0);
661 if (IS_QUANTITY_DEFINED(tone->vib[i][2]))
662 sp->vibrato_to_pitch = quantity_to_int(&tone->vib[i][2], 0);
665 if (tone->vibdelaynum)
666 for (i = 0; i < ip->samples; i++) {
668 if (tone->vibdelaynum == 1)
669 sp->vibrato_delay = tone->vibdelay[0];
670 else if (i < tone->vibdelaynum)
671 sp->vibrato_delay = tone->vibdelay[i];
673 if (tone->vibsweepnum)
674 for (i = 0; i < ip->samples; i++) {
676 if (tone->vibsweepnum == 1)
677 sp->vibrato_sweep = tone->vibsweep[0];
678 else if (i < tone->vibsweepnum)
679 sp->vibrato_sweep = tone->vibsweep[i];
681 if (tone->vibfreqnum)
682 for (i = 0; i < ip->samples; i++) {
684 if (tone->vibfreqnum == 1)
685 sp->vibrato_freq = tone->vibfreq[0];
686 else if (i < tone->vibfreqnum)
687 sp->vibrato_freq = tone->vibfreq[i];
690 for (i = 0; i < ip->samples; i++) {
692 if (tone->vibampnum == 1)
693 sp->vibrato_to_amp = tone->vibamp[0];
694 else if (i < tone->vibampnum)
695 sp->vibrato_to_amp = tone->vibamp[i];
697 if (tone->vibpitchnum)
698 for (i = 0; i < ip->samples; i++) {
700 if (tone->vibpitchnum == 1)
701 sp->vibrato_to_pitch = tone->vibpitch[0];
702 else if (i < tone->vibpitchnum)
703 sp->vibrato_to_pitch = tone->vibpitch[i];
706 for (i = 0; i < ip->samples; i++) {
708 if (tone->vibfcnum == 1)
709 sp->vibrato_to_fc = tone->vibfc[0];
710 else if (i < tone->vibfcnum)
711 sp->vibrato_to_fc = tone->vibfc[i];
713 if (tone->sclnotenum)
714 for (i = 0; i < ip->samples; i++) {
716 if (tone->sclnotenum == 1)
717 sp->scale_freq = tone->sclnote[0];
718 else if (i < tone->sclnotenum)
719 sp->scale_freq = tone->sclnote[i];
721 if (tone->scltunenum)
722 for (i = 0; i < ip->samples; i++) {
724 if (tone->scltunenum == 1)
725 sp->scale_factor = adjust_scale_tune(tone->scltune[0]);
726 else if (i < tone->scltunenum)
727 sp->scale_factor = adjust_scale_tune(tone->scltune[i]);
729 if (tone->modenvratenum)
730 for (i = 0; i < ip->samples; i++) {
732 if (tone->modenvratenum == 1) {
733 for (j = 0; j < 6; j++)
734 if (tone->modenvrate[0][j] >= 0)
735 sp->modenv_rate[j] = to_rate(tone->modenvrate[0][j]);
736 } else if (i < tone->modenvratenum) {
737 for (j = 0; j < 6; j++)
738 if (tone->modenvrate[i][j] >= 0)
739 sp->modenv_rate[j] = to_rate(tone->modenvrate[i][j]);
742 if (tone->modenvofsnum)
743 for (i = 0; i < ip->samples; i++) {
745 if (tone->modenvofsnum == 1) {
746 for (j = 0; j < 6; j++)
747 if (tone->modenvofs[0][j] >= 0)
748 sp->modenv_offset[j] =
749 to_offset(tone->modenvofs[0][j]);
750 } else if (i < tone->modenvofsnum) {
751 for (j = 0; j < 6; j++)
752 if (tone->modenvofs[i][j] >= 0)
753 sp->modenv_offset[j] =
754 to_offset(tone->modenvofs[i][j]);
757 if (tone->envkeyfnum)
758 for (i = 0; i < ip->samples; i++) {
760 if (tone->envkeyfnum == 1) {
761 for (j = 0; j < 6; j++)
762 if (tone->envkeyf[0][j] != -1)
763 sp->envelope_keyf[j] = tone->envkeyf[0][j];
764 } else if (i < tone->envkeyfnum) {
765 for (j = 0; j < 6; j++)
766 if (tone->envkeyf[i][j] != -1)
767 sp->envelope_keyf[j] = tone->envkeyf[i][j];
770 if (tone->envvelfnum)
771 for (i = 0; i < ip->samples; i++) {
773 if (tone->envvelfnum == 1) {
774 for (j = 0; j < 6; j++)
775 if (tone->envvelf[0][j] != -1)
776 sp->envelope_velf[j] = tone->envvelf[0][j];
777 } else if (i < tone->envvelfnum) {
778 for (j = 0; j < 6; j++)
779 if (tone->envvelf[i][j] != -1)
780 sp->envelope_velf[j] = tone->envvelf[i][j];
783 if (tone->modenvkeyfnum)
784 for (i = 0; i < ip->samples; i++) {
786 if (tone->modenvkeyfnum == 1) {
787 for (j = 0; j < 6; j++)
788 if (tone->modenvkeyf[0][j] != -1)
789 sp->modenv_keyf[j] = tone->modenvkeyf[0][j];
790 } else if (i < tone->modenvkeyfnum) {
791 for (j = 0; j < 6; j++)
792 if (tone->modenvkeyf[i][j] != -1)
793 sp->modenv_keyf[j] = tone->modenvkeyf[i][j];
796 if (tone->modenvvelfnum)
797 for (i = 0; i < ip->samples; i++) {
799 if (tone->modenvvelfnum == 1) {
800 for (j = 0; j < 6; j++)
801 if (tone->modenvvelf[0][j] != -1)
802 sp->modenv_velf[j] = tone->modenvvelf[0][j];
803 } else if (i < tone->modenvvelfnum) {
804 for (j = 0; j < 6; j++)
805 if (tone->modenvvelf[i][j] != -1)
806 sp->modenv_velf[j] = tone->modenvvelf[i][j];
809 if (tone->modpitchnum)
810 for (i = 0; i < ip->samples; i++) {
812 if (tone->modpitchnum == 1)
813 sp->modenv_to_pitch = tone->modpitch[0];
814 else if (i < tone->modpitchnum)
815 sp->modenv_to_pitch = tone->modpitch[i];
818 for (i = 0; i < ip->samples; i++) {
820 if (tone->modfcnum == 1)
821 sp->modenv_to_fc = tone->modfc[0];
822 else if (i < tone->modfcnum)
823 sp->modenv_to_fc = tone->modfc[i];
826 for (i = 0; i < ip->samples; i++) {
828 if (tone->fcnum == 1)
829 sp->cutoff_freq = adjust_fc(tone->fc[0]);
830 else if (i < tone->fcnum)
831 sp->cutoff_freq = adjust_fc(tone->fc[i]);
834 for (i = 0; i < ip->samples; i++) {
836 if (tone->resonum == 1) {
837 sp->resonance = adjust_reso(tone->reso[0]);
838 } else if (i < tone->resonum) {
839 sp->resonance = adjust_reso(tone->reso[i]);
844 for (i = 0; i < ip->samples; i++) {
846 if (tone->fclownum == 1)
847 sp->cutoff_low_limit = tone->fclow[0];
848 else if (i < tone->fclownum)
849 sp->cutoff_low_limit = tone->fclow[i];
851 if (tone->fclowkeyfnum)
852 for (i = 0; i < ip->samples; i++) {
854 if (tone->fclowkeyfnum == 1)
855 sp->cutoff_low_keyf = tone->fclowkeyf[0];
856 else if (i < tone->fclowkeyfnum)
857 sp->cutoff_low_keyf = tone->fclowkeyf[i];
859 if (tone->fcmulnum) // after fc
860 for (i = 0; i < ip->samples; i++) {
862 if (tone->fcmulnum == 1 && sp->cutoff_freq)
863 sp->cutoff_freq *= pow((FLOAT_T)2.0, (FLOAT_T)tone->fcmul[0] * DIV_1200);
864 else if (i < tone->fcmulnum && sp->cutoff_freq)
865 sp->cutoff_freq *= pow((FLOAT_T)2.0, (FLOAT_T)tone->fcmul[i] * DIV_1200);
867 if (tone->fcaddnum) // after fc
868 for (i = 0; i < ip->samples; i++) {
870 if (tone->fcaddnum == 1 && sp->cutoff_freq)
871 sp->cutoff_freq += tone->fcadd[0];
872 else if (i < tone->fcaddnum && sp->cutoff_freq)
873 sp->cutoff_freq += tone->fcadd[i];
876 for (i = 0; i < ip->samples; i++) {
878 if (tone->pitenvnum == 1) {
879 if (tone->pitenv[0][0] != 0)
880 sp->pitch_envelope[0] = tone->pitenv[0][0]; // cent init
881 if (tone->pitenv[0][1] != 0)
882 sp->pitch_envelope[1] = tone->pitenv[0][1]; // cent attack
883 if (tone->pitenv[0][2] >= 1)
884 sp->pitch_envelope[2] = tone->pitenv[0][2]; // ms attack
885 if (tone->pitenv[0][3] != 0)
886 sp->pitch_envelope[3] = tone->pitenv[0][3]; // cent decay1
887 if (tone->pitenv[0][4] >= 1)
888 sp->pitch_envelope[4] = tone->pitenv[0][4]; // ms decay1
889 if (tone->pitenv[0][5] != 0)
890 sp->pitch_envelope[5] = tone->pitenv[0][5]; // cent decay2
891 if (tone->pitenv[0][6] >= 1)
892 sp->pitch_envelope[6] = tone->pitenv[0][6]; // ms decay2
893 if (tone->pitenv[0][7] != 0)
894 sp->pitch_envelope[7] = tone->pitenv[0][7]; // cent release
895 if (tone->pitenv[0][8] >= 1)
896 sp->pitch_envelope[8] = tone->pitenv[0][8]; // ms release
897 } else if (i < tone->pitenvnum) {
898 if (tone->pitenv[i][0] != 0)
899 sp->pitch_envelope[0] = tone->pitenv[i][0]; // cent
900 if (tone->pitenv[i][1] != 0)
901 sp->pitch_envelope[1] = tone->pitenv[i][1]; // cent
902 if (tone->pitenv[i][2] >= 1)
903 sp->pitch_envelope[2] = tone->pitenv[i][2]; // ms
904 if (tone->pitenv[i][3] != 0)
905 sp->pitch_envelope[3] = tone->pitenv[i][3]; // cent
906 if (tone->pitenv[i][4] >= 1)
907 sp->pitch_envelope[4] = tone->pitenv[i][4]; // ms
908 if (tone->pitenv[i][5] != 0)
909 sp->pitch_envelope[5] = tone->pitenv[i][5]; // cent
910 if (tone->pitenv[i][6] >= 1)
911 sp->pitch_envelope[6] = tone->pitenv[i][6]; // ms
912 if (tone->pitenv[i][7] != 0)
913 sp->pitch_envelope[7] = tone->pitenv[i][7]; // cent
914 if (tone->pitenv[i][8] >= 1)
915 sp->pitch_envelope[8] = tone->pitenv[i][8]; // ms
919 for (i = 0; i < ip->samples; i++) {
921 if (tone->hpfnum == 1) {
922 for (j = 0; j < 3; j++)
923 sp->hpf[j] = tone->hpf[0][j];
924 } else if (i < tone->hpfnum) {
925 for (j = 0; j < 3; j++)
926 sp->hpf[j] = tone->hpf[i][j];
929 for (k = 0; k < VOICE_EFFECT_NUM; k++){
931 for (i = 0; i < ip->samples; i++) {
933 sp->vfxe_num = tone->vfxe_num;
934 if (tone->vfxnum[k] == 1) {
935 for (j = 0; j < VOICE_EFFECT_PARAM_NUM; j++)
936 sp->vfx[k][j] = tone->vfx[k][0][j];
937 } else if (i < tone->vfxnum[k]) {
938 for (j = 0; j < VOICE_EFFECT_PARAM_NUM; j++)
939 sp->vfx[k][j] = tone->vfx[k][i][j];
943 if (tone->seq_length > 0)
944 for (i = 0; i < ip->samples; i++) {
945 ip->sample[i].seq_length = tone->seq_length;
947 if (tone->seq_position > 0)
948 for (i = 0; i < ip->samples; i++) {
949 ip->sample[i].seq_position = tone->seq_position;
951 if (tone->lorand >= 0 || tone->hirand >= 0)
952 for (i = 0; i < ip->samples; i++) {
954 if (sp->enable_rand) {
955 if (tone->lorand >= 0 && sp->lorand < tone->lorand)
956 sp->lorand = tone->lorand;
957 if (tone->hirand >= 0 && tone->hirand < sp->hirand)
958 sp->hirand = tone->hirand;
961 sp->lorand = (tone->lorand >= 0 ? tone->lorand : 0);
962 sp->hirand = (tone->hirand >= 0 ? tone->hirand : 1);
967 #define READ_CHAR(thing) { \
970 if (tf_read(&tmpchar, 1, 1, tf) != 1) \
974 #define READ_SHORT(thing) { \
977 if (tf_read(&tmpshort, 2, 1, tf) != 1) \
979 thing = LE_SHORT(tmpshort); \
981 #define READ_LONG(thing) { \
984 if (tf_read(&tmplong, 4, 1, tf) != 1) \
986 thing = LE_LONG(tmplong); \
989 /* If panning or note_to_use != -1, it will be used for all samples,
990 * instead of the sample-specific values in the instrument file.
992 * For note_to_use, any value < 0 or > 127 will be forced to 0.
994 * For other parameters, 1 means yes, 0 means no, other values are
997 * TODO: do reverse loops right
999 static Instrument *load_gus_instrument(char *name,
1000 ToneBank *bank, int dr, int prog, char *infomsg, int elm)
1002 ToneBankElement *tone;
1003 int amp, panning, note_to_use; // c191-
1004 int strip_envelope, strip_loop, strip_tail;
1006 struct timidity_file *tf;
1007 uint8 tmp[FILEPATH_MAX], fractions;
1009 int i, j, noluck = 0;
1010 int32 low_freq, high_freq, root_freq;
1015 if (infomsg != NULL)
1016 ctl->cmsg(CMSG_INFO, VERB_NOISY, "%s: %s", infomsg, name);
1018 ctl->cmsg(CMSG_INFO, VERB_NOISY, "Loading instrument %s", name);
1021 // amp = note_to_use = panning = -1;
1022 strip_envelope = strip_loop = strip_tail = 0;
1023 }else if (bank->tone[prog][elm] == NULL) {
1025 // amp = note_to_use = panning = -1;
1026 strip_envelope = strip_loop = strip_tail = 0;
1028 tone = bank->tone[prog][elm];
1031 // note_to_use = (tone->note != -1) ? tone->note : ((dr) ? prog : -1);
1032 note_to_use = (dr) ? prog : -1; // c191
1033 // panning = tone->def_pan;
1034 amp = panning = -1; //
1035 strip_envelope = (tone->strip_envelope != -1)
1036 ? tone->strip_envelope : ((dr) ? 1 : -1);
1037 strip_loop = (tone->strip_loop != -1)
1038 ? tone->strip_loop : ((dr) ? 1 : -1);
1039 strip_tail = tone->strip_tail;
1041 amp = note_to_use = panning = -1;
1042 if (tone && tone->tunenum == 0
1043 && tone->envratenum == 0 && tone->envofsnum == 0
1044 && tone->tremnum == 0 && tone->vibnum == 0
1045 && tone->sclnotenum == 0 && tone->scltunenum == 0
1046 && tone->modenvratenum == 0 && tone->modenvofsnum == 0
1047 && tone->envkeyfnum == 0 && tone->envvelfnum == 0
1048 && tone->modenvkeyfnum == 0 && tone->modenvvelfnum == 0
1049 && tone->trempitchnum == 0 && tone->tremfcnum == 0
1050 && tone->modpitchnum == 0 && tone->modfcnum == 0
1051 && tone->fcnum == 0 && tone->resonum == 0)
1052 if ((ip = search_instrument_cache(name, panning, amp, note_to_use,
1053 strip_loop, strip_envelope, strip_tail)) != NULL) {
1054 ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * Cached");
1057 /* Open patch file */
1058 if (! (tf = open_file_r(name, 2, OF_NORMAL))) {
1059 #ifdef PATCH_EXT_LIST
1060 size_t name_len, ext_len;
1061 static char *patch_ext[] = PATCH_EXT_LIST;
1065 #ifdef PATCH_EXT_LIST
1066 name_len = strlen(name);
1067 /* Try with various extensions */
1068 for (i = 0; patch_ext[i]; i++) {
1069 ext_len = strlen(patch_ext[i]);
1070 if (name_len + ext_len < FILEPATH_MAX) {
1071 if (name_len >= ext_len && strcmp(name + name_len - ext_len,
1073 continue; /* duplicated ext. */
1074 strcpy((char *) tmp, name);
1075 strcat((char *) tmp, patch_ext[i]);
1076 if ((tf = open_file_r((char *) tmp, 1, OF_NORMAL))) {
1085 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1086 "Instrument `%s' can't be found.", name);
1089 /* Read some headers and do cursory sanity checks. There are loads
1090 * of magic offsets. This could be rewritten...
1092 tmp[0] = tf_getc(tf);
1093 if (tmp[0] == '\0') {
1094 /* for Mac binary */
1096 tmp[0] = tf_getc(tf);
1098 if ((tf_read(tmp + 1, 1, 238, tf) != 238)
1099 || (memcmp(tmp, "GF1PATCH110\0ID#000002", 22)
1100 && memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) {
1101 /* don't know what the differences are */
1102 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: not an instrument", name);
1106 /* instruments. To some patch makers, 0 means 1 */
1107 if (tmp[82] != 1 && tmp[82] != 0) {
1108 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1109 "Can't handle patches with %d instruments", tmp[82]);
1113 if (tmp[151] != 1 && tmp[151] != 0) { /* layers. What's a layer? */
1114 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1115 "Can't handle instruments with %d layers", tmp[151]);
1119 ip = (Instrument *) safe_malloc(sizeof(Instrument));
1120 ip->instname = NULL;
1121 ip->type = INST_GUS;
1122 ip->samples = tmp[198];
1123 ip->sample = (Sample *) safe_malloc(sizeof(Sample) * ip->samples);
1124 memset(ip->sample, 0, sizeof(Sample) * ip->samples);
1125 for (i = 0; i < ip->samples; i++) {
1126 skip(tf, 7); /* Skip the wave name */
1127 if (tf_read(&fractions, 1, 1, tf) != 1) {
1129 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Error reading sample %d", i);
1130 for (j = 0; j < i; j++) {
1131 safe_free(ip->sample[j].data);
1132 ip->sample[j].data = NULL;
1134 safe_free(ip->sample);
1139 sp = &(ip->sample[i]);
1143 sp->cutoff_freq = 20000;
1144 sp->cutoff_low_limit = -1;
1145 sp->cutoff_low_keyf = 0; // cent
1147 sp->tremolo_delay = sp->vibrato_delay = 0;
1148 sp->vibrato_to_amp = sp->vibrato_to_fc = 0;
1149 sp->tremolo_to_pitch = sp->tremolo_to_fc = 0;
1150 sp->modenv_to_pitch = sp->modenv_to_fc = 0;
1151 sp->vel_to_fc = sp->key_to_fc = sp->vel_to_resonance = 0;
1152 sp->envelope_velf_bpo = sp->modenv_velf_bpo = 64;
1153 sp->envelope_keyf_bpo = sp->modenv_keyf_bpo = 60;
1154 sp->vel_to_fc_threshold = 0;
1155 sp->key_to_fc_bpo = 60;
1156 sp->envelope_delay = sp->modenv_delay = 0;
1157 sp->inst_type = INST_GUS;
1158 sp->sample_type = SF_SAMPLETYPE_MONO;
1159 sp->sf_sample_link = -1;
1160 sp->sf_sample_index = 0;
1163 sp->hpf[0] = -1; // opt_hpf_def
1167 sp->sample_pan = 0.0;
1168 sp->pitch_envelope[0] = 0; // 0cent init
1169 sp->pitch_envelope[1] = 0; // 0cent atk
1170 sp->pitch_envelope[2] = 0; // 125ms atk
1171 sp->pitch_envelope[3] = 0; // 0cent dcy1
1172 sp->pitch_envelope[4] = 0; // 125ms dcy1
1173 sp->pitch_envelope[5] = 0; // 0cent dcy2
1174 sp->pitch_envelope[6] = 0; // 125ms dcy3
1175 sp->pitch_envelope[7] = 0; // 0cent rls
1176 sp->pitch_envelope[8] = 0; // 125ms rls
1178 memset(sp->envelope_velf, 0, sizeof(sp->envelope_velf));
1179 memset(sp->envelope_keyf, 0, sizeof(sp->envelope_keyf));
1180 memset(sp->modenv_velf, 0, sizeof(sp->modenv_velf));
1181 memset(sp->modenv_keyf, 0, sizeof(sp->modenv_keyf));
1182 memset(sp->modenv_rate, 0, sizeof(sp->modenv_rate));
1183 memset(sp->modenv_offset, 0, sizeof(sp->modenv_offset));
1185 sp->modenv_offset[0] = to_offset(250);
1186 sp->modenv_rate[0] = to_rate(255);
1187 sp->modenv_offset[1] = to_offset(254);
1188 sp->modenv_rate[1] = to_rate(1);
1189 sp->modenv_offset[2] = to_offset(253);
1190 sp->modenv_rate[2] = to_rate(1);
1191 sp->modenv_offset[3] = 0;
1192 sp->modenv_rate[3] = to_rate(64);
1193 sp->modenv_offset[4] = 0;
1194 sp->modenv_rate[4] = to_rate(64);
1195 sp->modenv_offset[5] = 0;
1196 sp->modenv_rate[5] = to_rate(64);
1198 READ_LONG(sp->data_length);
1199 READ_LONG(sp->loop_start);
1200 READ_LONG(sp->loop_end);
1201 READ_SHORT(sp->sample_rate);
1202 READ_LONG(low_freq);
1203 READ_LONG(high_freq);
1204 READ_LONG(root_freq);
1205 skip(tf, 2); /* Why have a "root frequency" and then "tuning"?? */
1207 /* convert freq to key */
1215 for(k = 0; k < 128; k++){
1218 freq2 = freq_table[k + 1];
1220 freq1 = freq_table[k];
1223 freq1 = freq_table[k];
1224 freq2 = freq_table[k + 1];
1226 if(low_freq >= freq1 && low_freq < freq2)
1228 if(high_freq >= freq1 && high_freq < freq2)
1230 if(root_freq >= freq1 && root_freq < freq2)
1233 #if 1 // c219
\83\8b\81[
\83g
\83L
\81[
\8eü
\94g
\90\94\82Ætune
\82ð
\95ª
\97£
1234 sp->root_freq = freq_table[sp->root_key];
1235 sp->tune = (FLOAT_T)sp->root_freq / (FLOAT_T)root_freq;
1236 #else // root_freq
\82Í
\83\8b\81[
\83g
\83L
\81[
\8eü
\94g
\90\94(freq_table[sp->root_key])
\82Æ
\82Ì
\94ä(tune)
\82ª
\8aÜ
\82Ü
\82ê
\82Ä
\82¢
\82é
1237 sp->root_freq = root_freq;
1240 ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Rate=%d LK=%d HK=%d RK=%d RF=%d Tune=%f",
1241 sp->sample_rate, sp->low_key, sp->high_key, sp->root_key, sp->root_freq, sp->tune);
1244 sp->def_pan = ((tmp[0] - ((tmp[0] < 8) ? 7 : 8)) * 63) / 7 + 64;
1247 /* 0x07 and 0x08 are both center panning */
1248 sp->def_pan = ((tmp[0] - ((tmp[0] < 8) ? 7 : 8)) * 63) / 7 + 64;
1250 sp->def_pan = (uint8) (panning & 0x7f);
1253 /* envelope, tremolo, and vibrato */
1254 if (tf_read(tmp, 1, 18, tf) != 18)
1256 if (! tmp[13] || ! tmp[14]) {
1257 sp->tremolo_sweep = sp->tremolo_freq = 0;
1258 sp->tremolo_to_amp = 0;
1259 ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no tremolo");
1261 sp->tremolo_sweep = convert_gus_lfo_sweep(tmp[12]);
1262 sp->tremolo_freq = convert_gus_lfo_rate(tmp[13]);
1263 sp->tremolo_to_amp = (int32)tmp[14] * 10000 / 255; // max255 to max10000
1264 ctl->cmsg(CMSG_INFO, VERB_DEBUG,
1265 " * tremolo: sweep %d(ms), freq %d(mHz), depth %d(0.01%)",
1266 sp->tremolo_sweep, sp->tremolo_freq, sp->tremolo_to_amp);
1268 if (! tmp[16] || ! tmp[17]) {
1269 sp->vibrato_sweep = sp->vibrato_freq = 0;
1270 sp->vibrato_to_pitch = 0;
1271 ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no vibrato");
1273 sp->vibrato_freq = convert_gus_lfo_rate(tmp[16]);
1274 sp->vibrato_sweep = convert_gus_lfo_sweep(tmp[15]);
1275 sp->vibrato_to_pitch = tmp[17];
1276 ctl->cmsg(CMSG_INFO, VERB_DEBUG,
1277 " * vibrato: sweep %d(ms), freq %d(mHz), depth %d(cent)",
1278 sp->vibrato_sweep, sp->vibrato_freq, sp->vibrato_to_pitch);
1280 READ_CHAR(sp->modes);
1281 ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * mode: 0x%02x", sp->modes);
1282 READ_SHORT(sp->scale_freq);
1283 READ_SHORT(sp->scale_factor);
1284 skip(tf, 36); /* skip reserved space */
1285 /* Mark this as a fixed-pitch instrument if such a deed is desired. */
1286 sp->note_to_use = (note_to_use != -1) ? (uint8) note_to_use : 0;
1287 /* seashore.pat in the Midia patch set has no Sustain. I don't
1288 * understand why, and fixing it by adding the Sustain flag to
1289 * all looped patches probably breaks something else. We do it
1292 if (sp->modes & MODES_LOOPING)
1293 sp->modes |= MODES_SUSTAIN;
1294 /* Strip any loops and envelopes we're permitted to */
1295 if ((strip_loop == 1) && (sp->modes & (MODES_SUSTAIN | MODES_LOOPING
1296 | MODES_PINGPONG | MODES_REVERSE))) {
1297 sp->modes &= ~(MODES_SUSTAIN | MODES_LOOPING
1298 | MODES_PINGPONG | MODES_REVERSE);
1299 ctl->cmsg(CMSG_INFO, VERB_DEBUG,
1300 " - Removing loop and/or sustain");
1302 if (strip_envelope == 1) {
1303 if (sp->modes & MODES_ENVELOPE)
1304 ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing envelope");
1305 sp->modes &= ~MODES_ENVELOPE;
1306 } else if (strip_envelope != 0) {
1307 /* Have to make a guess. */
1308 if (! (sp->modes & (MODES_LOOPING
1309 | MODES_PINGPONG | MODES_REVERSE))) {
1310 /* No loop? Then what's there to sustain?
1311 * No envelope needed either...
1313 sp->modes &= ~(MODES_SUSTAIN|MODES_ENVELOPE);
1314 ctl->cmsg(CMSG_INFO, VERB_DEBUG,
1315 " - No loop, removing sustain and envelope");
1316 } else if (! memcmp(tmp, "??????", 6) || tmp[11] >= 100) {
1317 /* Envelope rates all maxed out?
1318 * Envelope end at a high "offset"?
1319 * That's a weird envelope. Take it out.
1321 sp->modes &= ~MODES_ENVELOPE;
1322 ctl->cmsg(CMSG_INFO, VERB_DEBUG,
1323 " - Weirdness, removing envelope");
1324 } else if (! (sp->modes & MODES_SUSTAIN)) {
1325 /* No sustain? Then no envelope. I don't know if this is
1326 * justified, but patches without sustain usually don't need
1327 * the envelope either... at least the Gravis ones. They're
1328 * mostly drums. I think.
1330 sp->modes &= ~MODES_ENVELOPE;
1331 ctl->cmsg(CMSG_INFO, VERB_DEBUG,
1332 " - No sustain, removing envelope");
1337 double limit = (double)control_ratio * (double)0x3FFFFFFF / ((double)play_mode->rate * 0.02);
1338 int32 tmp_rate, tmp_rate2, limit_rate = limit < 1.0 ? 1 : (int32)limit;
1339 sp->envelope_rate[0] = convert_envelope_rate(tmp[0]);
1340 sp->envelope_offset[0] = to_offset(255);
1341 tmp_rate = convert_envelope_rate(tmp[1]);
1342 sp->envelope_rate[1] = (tmp_rate > limit_rate) ? limit_rate : tmp_rate;
1343 sp->envelope_offset[1] = convert_envelope_offset(tmp[1 + 6]);
1344 tmp_rate = convert_envelope_rate(tmp[2]);
1345 sp->envelope_rate[2] = (tmp_rate > limit_rate) ? limit_rate : tmp_rate;
1346 sp->envelope_offset[2] = convert_envelope_offset(tmp[2 + 6]);
1347 if(sp->envelope_offset[2] > sp->envelope_offset[1])
1348 sp->envelope_offset[2] = sp->envelope_offset[1];
1349 tmp_rate = convert_envelope_rate(tmp[3]);
1350 sp->envelope_rate[3] = (tmp_rate > limit_rate) ? limit_rate : tmp_rate;
1351 sp->envelope_offset[3] = to_offset(0);
1352 sp->envelope_rate[5] = sp->envelope_rate[4] = sp->envelope_rate[3];
1353 sp->envelope_offset[5] = sp->envelope_offset[4] = sp->envelope_offset[3];
1355 double limit = (double)control_ratio * (double)0x3FFFFFFF / ((double)play_mode->rate * 0.02);
1356 int32 tmp_rate, tmp_rate2, limit_rate = limit < 1.0 ? 1 : (int32)limit;
1357 sp->envelope_rate[0] = convert_envelope_rate(tmp[0]);
1358 sp->envelope_offset[0] = convert_envelope_offset(tmp[0 + 6]);
1359 tmp_rate = convert_envelope_rate(tmp[1]);
1360 sp->envelope_rate[1] = (tmp_rate > limit_rate) ? limit_rate : tmp_rate;
1361 sp->envelope_offset[1] = convert_envelope_offset(tmp[1 + 6]);
1362 tmp_rate = convert_envelope_rate(tmp[2]);
1363 sp->envelope_rate[2] = (tmp_rate > limit_rate) ? limit_rate : tmp_rate;
1364 sp->envelope_offset[2] = convert_envelope_offset(tmp[2 + 6]);
1365 tmp_rate = convert_envelope_rate(tmp[3]);
1366 sp->envelope_rate[3] = (tmp_rate > limit_rate) ? limit_rate : tmp_rate;
1367 sp->envelope_offset[3] = convert_envelope_offset(tmp[3 + 6]);
1368 tmp_rate = convert_envelope_rate(tmp[4]);
1369 sp->envelope_rate[4] = (tmp_rate > limit_rate) ? limit_rate : tmp_rate;
1370 sp->envelope_offset[4] = convert_envelope_offset(tmp[4 + 6]);
1371 tmp_rate = convert_envelope_rate(tmp[5]);
1372 sp->envelope_rate[5] = (tmp_rate > limit_rate) ? limit_rate : tmp_rate;
1373 sp->envelope_offset[5] = convert_envelope_offset(tmp[5 + 6]);
1376 /* this envelope seems to give reverb like effects to most patches
1377 * use the same method as soundfont
1379 if (modify_release) {
1380 sp->envelope_rate[3] = calc_rate(255, modify_release);
1381 sp->envelope_offset[3] = 0;
1382 sp->envelope_rate[5] = sp->envelope_rate[4] = sp->envelope_rate[3];
1383 sp->envelope_offset[5] = sp->envelope_offset[4] = sp->envelope_offset[3];
1385 /* Then read the sample data */
1386 sp->data = (sample_t *) safe_large_malloc(sp->data_length + sizeof(sample_t) * 128);
1387 memset(sp->data, 0, sp->data_length + sizeof(sample_t) * 128);
1388 sp->data_alloced = 1;
1390 sp->data_type = SAMPLE_TYPE_INT16;
1391 if ((j = tf_read(sp->data, 1, sp->data_length, tf))
1392 != sp->data_length) {
1393 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1394 "Too small this patch length: %d < %d",
1395 j, sp->data_length);
1398 if (! (sp->modes & MODES_16BIT)) { /* convert to 16-bit data */
1401 uint8 *cp = (uint8 *) sp->data;
1403 tmp = (uint16 *) safe_large_malloc(sp->data_length * 2 + sizeof(sample_t) * 128);
1404 // memset(tmp, 0, sp->data_length * 2 + sizeof(sample_t) * 128);
1405 for (i = 0; i < sp->data_length; i++)
1406 tmp[i] = (uint16) cp[i] << 8;
1407 sp->data = (sample_t *) tmp;
1409 sp->data_length *= 2;
1410 sp->loop_start *= 2;
1413 #ifndef LITTLE_ENDIAN
1414 else { /* convert to machine byte order */
1416 int16 *tmp = (int16 *) sp->data, s;
1418 for (i = 0; i < sp->data_length / 2; i++)
1419 s = LE_SHORT(tmp[i]), tmp[i] = s;
1422 if (sp->modes & MODES_UNSIGNED) { /* convert to signed data */
1423 int32 i = sp->data_length / 2;
1424 int16 *tmp = (int16 *) sp->data;
1429 /* Reverse loops and pass them off as normal loops */
1430 if (sp->modes & MODES_REVERSE) {
1431 /* The GUS apparently plays reverse loops by reversing the
1432 * whole sample. We do the same because the GUS does not SUCK.
1436 reverse_data((int16 *) sp->data, 0, sp->data_length / 2);
1438 sp->loop_start = sp->data_length - sp->loop_end;
1439 sp->loop_end = sp->data_length - t;
1440 sp->modes &= ~MODES_REVERSE;
1441 sp->modes |= MODES_LOOPING; /* just in case */
1442 ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Reverse loop in %s", name);
1444 /* If necessary do some anti-aliasing filtering */
1445 if (antialiasing_allowed)
1446 antialiasing((int16 *) sp->data, sp->data_length / 2, sp->sample_rate, play_mode->rate);
1448 #ifdef ADJUST_SAMPLE_VOLUMES
1451 // sp->volume = (double) amp / 100; // -c190
1452 // sp->volume = 1.0;
1455 /* Try to determine a volume scaling factor for the sample.
1456 * This is a very crude adjustment, but things sound more
1457 * balanced with it. Still, this should be a runtime option.
1459 int32 i, a, maxamp = 0;
1460 int16 *tmp = (int16 *) sp->data;
1462 for (i = 0; i < sp->data_length / 2; i++)
1463 if ((a = abs(tmp[i])) > maxamp)
1465 sp->volume = (double)32768.0 / (double) maxamp;
1466 ctl->cmsg(CMSG_INFO, VERB_DEBUG,
1467 " * volume comp: %f", sp->volume);
1471 // sp->volume = (amp != -1) ? (double) amp / 100 : 1.0; // -c190
1472 sp->volume = 1.0; // c191-
1475 /* These are in bytes. Convert into samples. */
1476 sp->data_length /= 2;
1477 sp->loop_start /= 2;
1479 /* The sample must be padded out by 2 extra sample, so that
1480 * round off errors in the offsets used in interpolation will not
1481 * cause a "pop" by reading random data beyond data_length
1484 // sp->data[sp->data_length] = sp->data[sp->data_length + 1] = 0;
1485 memset(&sp->data[sp->data_length], 0, sizeof(sample_t) * 128);
1486 /* Remove abnormal loops which cause pop noise
1487 * in long sustain stage
1489 if (! (sp->modes & MODES_LOOPING)) {
1490 sp->loop_start = sp->data_length - 1;
1491 sp->loop_end = sp->data_length;
1492 sp->data[sp->data_length - 1] = 0;
1494 /* Then fractional samples */
1495 sp->data_length <<= FRACTION_BITS;
1496 sp->loop_start <<= FRACTION_BITS;
1497 sp->loop_end <<= FRACTION_BITS;
1498 /* Adjust for fractional loop points. This is a guess. Does anyone
1499 * know what "fractions" really stands for?
1501 sp->loop_start |= (fractions & 0x0f) << (FRACTION_BITS - 4);
1502 sp->loop_end |= ((fractions >> 4) & 0x0f) << (FRACTION_BITS - 4);
1503 /* If this instrument will always be played on the same note,
1504 * and it's not looped, we can resample it now.
1507 if (opt_pre_resamplation && sp->note_to_use && ! (sp->modes & MODES_LOOPING))
1511 squash_sample_16to8(sp);
1513 if (strip_tail == 1) {
1514 /* Let's not really, just say we did. */
1515 sp->data_length = sp->loop_end;
1516 ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Stripping tail");
1520 store_instrument_cache(ip, name, panning, amp, note_to_use,
1521 strip_loop, strip_envelope, strip_tail);
1526 /*! Squash the 16-bit data into 8 bits. */
1527 void squash_sample_16to8(Sample *sp)
1531 int l = sp->data_length >> FRACTION_BITS;
1533 gulp = ulp = (uint8 *)safe_malloc(l + 1);
1535 switch(sp->data_type){
1536 case SAMPLE_TYPE_INT16:
1539 swp = (int16 *)sp->data;
1541 *ulp++ = (*swp++ >> 8) & 0xff;
1544 case SAMPLE_TYPE_INT32:
1547 swp = (int32 *)sp->data;
1549 *ulp++ = (*swp++ >> 24) & 0xff;
1552 case SAMPLE_TYPE_FLOAT:
1554 float *swp = (float *)sp->data;
1556 *ulp++ = *swp++ * 127.0;
1559 case SAMPLE_TYPE_DOUBLE:
1561 double *swp = (double *)sp->data;
1563 *ulp++ = *swp++ * 127.0;
1567 ctl->cmsg(CMSG_INFO, VERB_NORMAL, "invalid squash data_type %d", data_type);
1570 safe_free(sp->data);
1571 sp->data = (sample_t *)gulp;
1576 int opt_load_all_instrument = 0;
1578 void load_all_instrument(void)
1580 int i, j, k, l, elm;
1581 ToneBankElement *tone;
1582 Instrument *ip = NULL;
1584 if(!opt_load_all_instrument)
1586 free_instruments_afterwards = 0;
1587 init_load_soundfont(); // load_instrument()
\82æ
\82è
\91O
1589 for(i= 0; i < 128 + MAP_BANK_COUNT; i++){
1590 if(!tonebank[i] || !tonebank[i]->tone)
1591 continue; // standard bank
1592 for(j= 0; j < 128; j++){
1593 if(!tonebank[i]->tone[j])
1595 if(tonebank[i]->tone[j][0] == NULL)
1597 for(elm= 0; elm <= tonebank[i]->tone[j][0]->element_num; elm++){
1598 if(tonebank[i]->tone[j][elm] == NULL)
1600 if( !((ip = tonebank[i]->tone[j][elm]->instrument) == NULL))
1602 ip = load_instrument(0, i, j, elm);
1603 if (ip == NULL || IS_MAGIC_INSTRUMENT(ip)){
1604 tonebank[i]->tone[j][elm]->instrument = MAGIC_ERROR_INSTRUMENT;
1607 tonebank[i]->tone[j][elm]->instrument = ip;
1612 for(i= 0; i < 128 + MAP_BANK_COUNT; i++){
1613 if(!drumset[i] || !drumset[i]->tone)
1614 continue; // standard bank
1615 for(j= 0; j < 128; j++){
1616 if(!drumset[i]->tone[j])
1618 if(drumset[i]->tone[j][0] == NULL)
1620 for(elm= 0; elm < drumset[i]->tone[j][0]->element_num; elm++){
1621 if(drumset[i]->tone[j][elm] == NULL)
1623 if(drumset[i]->tone[j][elm]->instrument != NULL)
1625 ip = load_instrument(1, i, j, elm);
1628 drumset[i]->tone[j][elm]->instrument;
1636 \83T
\83\93\83v
\83\8b\83\8d\81[
\83h
\82Ì
\8co
\98H
1638 \8bN
\93®
\8e\9e\82É
\91S
\89¹
\90F
\93Ç
\8d\9e opt_load_all_instrument
1639 load_all_instrument()
1640 SMF
\83\8d\81[
\83h
\8e\9e (c209
\96³
\8cø c211
\8dí
\8f\9c
1641 readmidi.c SMF
\83\8d\81[
\83h
\8e\9eread_midi_file() groom_list() NOTE_ON
\82Å
\83}
\81[
\83N(MAGIC_LOAD_INSTRUMENT)
\82µ
\82½
\82à
\82Ì
\82ðinstrum.c fill_bank()
\82Å
\83\8d\81[
\83h
1642 \8dÄ
\90¶
\91O
\83v
\83\8a\83X
\83L
\83\83\83\93(
\83C
\83x
\83\93\83g
\82¾
\82¯
\89¼
\8dÄ
\90¶)
\82ÌNOTE_ON
\82Å
\83\8d\81[
\83h (player
\82Ì
\8fê
\8d\87 \82±
\82±
\82Å
\91S
\95\94\83\8d\81[
\83h
\82µ
\82½
\82¢
\81E
\81E
1643 playmidi.c play_midi_prescan()
\82ÌNOTE_ON
\82Åfind_samples()
\82Å
\83\8d\81[
\83h
1644 \8dÄ
\90¶
\92\86\82ÌNOTE_ON
\82Å
\83\8d\81[
\83h
1645 playmidi.c play_midi()
1646 \88ê
\95\94 \83v
\83\8d\83O
\83\89\83\80\83`
\83F
\83\93\83W
\83h
\83\89\83\80\83p
\81[
\83g (
\8eå
\82ÉSynth
\82Ì
\8fê
\8d\87
1647 playmidi.c midi_program_change()
1649 Instrument *load_instrument(int dr, int b, int prog, int elm)
1651 ToneBank *bank = ((dr) ? drumset[b] : tonebank[b]);
1653 int i, j, font_bank, font_preset, font_keynote;
1655 extern Instrument *extract_scc_file(char *, int);
1656 extern Instrument *extract_mms_file(char *, int);
1658 extern Instrument *extract_sample_file(char *);
1664 if (play_system_mode == GS_SYSTEM_MODE && (b == 64 || b == 65)) {
1665 if (! dr) /* User Instrument */
1666 recompute_userinst(b, prog, elm);
1667 else { /* User Drumset */
1668 ip = recompute_userdrum(b, prog, elm);
1675 if(bank->tone[prog][elm] == NULL) // error
1677 switch(bank->tone[prog][elm]->instype){
1678 case 0: /* load GUS/patch file */
1680 sprintf(infomsg, "Tonebank %d %d", b, prog + progbase);
1682 sprintf(infomsg, "Drumset %d %d(%s)",
1683 b + progbase, prog, note_name[prog % 12]);
1684 infomsg[256 - 1] = '\0';
1685 ip = load_gus_instrument(bank->tone[prog][elm]->name,
1686 bank, dr, prog, infomsg, elm);
1688 case 1: /* Font extention */
1689 font_bank = bank->tone[prog][elm]->font_bank;
1690 font_preset = bank->tone[prog][elm]->font_preset;
1691 font_keynote = bank->tone[prog][elm]->font_keynote;
1692 ip = extract_soundfont(bank->tone[prog][elm]->name, font_bank, font_preset, font_keynote);
1694 case 2: /* Sample extension */
1695 ip = extract_sample_file(bank->tone[prog][elm]->name);
1698 case 3: /* mms extention */
1699 ip = extract_mms_file(bank->tone[prog][elm]->name, bank->tone[prog][elm]->is_preset);
1701 case 4: /* scc extention */
1702 ip = extract_scc_file(bank->tone[prog][elm]->name, bank->tone[prog][elm]->is_preset);
1706 case 5: /* sfz extension */
1707 ip = extract_sfz_file(bank->tone[prog][elm]->name);
1711 case 6: /* dls extension */
1712 font_bank = bank->tone[prog][elm]->font_bank;
1713 font_preset = bank->tone[prog][elm]->font_preset;
1714 font_keynote = bank->tone[prog][elm]->font_keynote;
1715 ip = extract_dls_file(bank->tone[prog][elm]->name, font_bank, font_preset, font_keynote);
1719 goto TONEBANK_INSTRUMENT_NULL;
1724 goto TONEBANK_INSTRUMENT_NULL;
1726 i = (dr) ? 0 : prog;
1727 if(bank->tone[i][elm] == NULL) // error
1729 if (!bank->tone[i][elm]->name)
1730 bank->tone[i][elm]->name = safe_strdup(DYNAMIC_INSTRUMENT_NAME);
1731 #if 1 // c210 CFG comm
1733 if (!bank->tone[i][elm]->comment) {
1734 bank->tone[i][elm]->comment = (char *)safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
1735 bank->tone[i][elm]->comment[0] = '\0';
1737 if(!(dr && bank->tone[0][0] && bank->tone[0][0]->comment && bank->tone[0][0]->comment[0]
1738 || bank->tone[i][elm]->comment[0]) ){
1739 if(ip->instname != NULL){
1740 strncpy(bank->tone[i][elm]->comment, ip->instname, MAX_TONEBANK_COMM_LEN - 1);
1741 if (opt_print_fontname && !dr && bank->tone[i][elm]->name && bank->tone[i][elm]->instype != 2) {
1743 const char *name = pathsep_strrchr(bank->tone[i][elm]->name);
1747 name = bank->tone[i][elm]->name;
1748 strlcat(bank->tone[i][elm]->comment, " (", MAX_TONEBANK_COMM_LEN);
1749 strlcat(bank->tone[i][elm]->comment, name, MAX_TONEBANK_COMM_LEN);
1750 strlcat(bank->tone[i][elm]->comment, ")", MAX_TONEBANK_COMM_LEN);
1752 }else if(bank->tone[i][elm]->name != NULL){
1753 strncpy(bank->tone[i][elm]->comment, bank->tone[i][elm]->name, MAX_TONEBANK_COMM_LEN - 1);
1755 bank->tone[i][elm]->comment[MAX_TONEBANK_COMM_LEN - 1] = '\0';
1759 if (!bank->tone[i][elm]->comment) {
1760 bank->tone[i][elm]->comment = (char *)safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
1761 bank->tone[i][elm]->comment[0] = '\0';
1763 if(bank->tone[i][elm]->instype > 0){
1764 if(ip->instname != NULL){
1766 if(!bank->tone[i][elm]->comment[0] || strcmp(bank->tone[i][elm]->comment, bank->tone[i][elm]->name) == 0) {
1767 strncpy(bank->tone[i][elm]->comment, ip->instname, MAX_TONEBANK_COMM_LEN - 1);
1771 strncpy(bank->tone[i][elm]->comment, ip->instname, MAX_TONEBANK_COMM_LEN - 1);
1772 bank->tone[i][elm]->comment[MAX_TONEBANK_COMM_LEN - 1] = '\0';
1774 if (opt_print_fontname && !dr && bank->tone[i][elm]->name != NULL && bank->tone[i][elm]->instype != 2) {
1776 const char *name = pathsep_strrchr(bank->tone[i][elm]->name);
1780 name = bank->tone[i][elm]->name;
1781 strlcat(bank->tone[i][elm]->comment, " (", MAX_TONEBANK_COMM_LEN);
1782 strlcat(bank->tone[i][elm]->comment, name, MAX_TONEBANK_COMM_LEN);
1783 strlcat(bank->tone[i][elm]->comment, ")", MAX_TONEBANK_COMM_LEN);
1787 apply_bank_parameter(ip, bank->tone[prog][elm]);
1790 TONEBANK_INSTRUMENT_NULL:
1798 font_keynote = prog;
1800 /* preload soundfont */
1801 ip = load_soundfont_inst(0, font_bank, font_preset, font_keynote);
1803 if (bank->tone[prog][elm]->name == NULL) /* this should not be NULL to play the instrument */
1804 bank->tone[prog][elm]->name = safe_strdup(DYNAMIC_INSTRUMENT_NAME);
1805 #if 1 // c210 CFG comm
1807 if (!bank->tone[prog][elm]->comment){
1808 bank->tone[prog][elm]->comment = (char *)safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
1809 bank->tone[prog][elm]->comment[0] = '\0';
1811 if(!(dr && bank->tone[0][0] && bank->tone[0][0]->comment && bank->tone[0][0]->comment[0]
1812 || bank->tone[prog][elm]->comment[0]) ){
1813 if(ip->instname != NULL){
1814 strncpy(bank->tone[prog][elm]->comment, ip->instname, MAX_TONEBANK_COMM_LEN - 1);
1815 if (opt_print_fontname && !dr && bank->tone[prog][elm]->name && bank->tone[prog][elm]->instype != 2) {
1816 const char *name = pathsep_strrchr(bank->tone[prog][elm]->name);
1820 name = bank->tone[prog][elm]->name;
1821 strlcat(bank->tone[prog][elm]->comment, " (", MAX_TONEBANK_COMM_LEN);
1822 strlcat(bank->tone[prog][elm]->comment, name, MAX_TONEBANK_COMM_LEN);
1823 strlcat(bank->tone[prog][elm]->comment, ")", MAX_TONEBANK_COMM_LEN);
1825 }else if(bank->tone[prog][elm]->name != NULL){
1826 strncpy(bank->tone[prog][elm]->comment, bank->tone[prog][elm]->name, MAX_TONEBANK_COMM_LEN - 1);
1828 bank->tone[prog][elm]->comment[MAX_TONEBANK_COMM_LEN - 1] = '\0';
1832 if (!bank->tone[prog][elm]->comment)
1833 bank->tone[prog][elm]->comment = (char *)safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
1834 strncpy(bank->tone[prog][elm]->comment, ip->instname, MAX_TONEBANK_COMM_LEN - 1);
1835 bank->tone[prog][elm]->comment[MAX_TONEBANK_COMM_LEN - 1] = '\0';
1836 if (opt_print_fontname && !dr && bank->tone[prog][elm]->instype == 1 && !bank->tone[prog][elm]->name[0]) {
1837 const char *name = pathsep_strrchr(bank->tone[prog][elm]->name);
1841 name = bank->tone[prog][elm]->name;
1842 strlcat(bank->tone[prog][elm]->comment, " (", MAX_TONEBANK_COMM_LEN);
1843 strlcat(bank->tone[prog][elm]->comment, name, MAX_TONEBANK_COMM_LEN);
1844 strlcat(bank->tone[prog][elm]->comment, ")", MAX_TONEBANK_COMM_LEN);
1847 } else { // ip == NULL
1848 /* no patch; search soundfont again */
1849 ip = load_soundfont_inst(1, font_bank, font_preset, font_keynote);
1851 #if 1 // c210 CFG comm
1853 if(bank->tone[0][elm]){
1854 if (!bank->tone[0][elm]->comment){
1855 bank->tone[0][elm]->comment = (char *)safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
1856 bank->tone[0][elm]->comment[0] = '\0';
1858 if(!bank->tone[0][elm]->comment[0]){
1860 strncpy(bank->tone[0][elm]->comment, ip->instname, MAX_TONEBANK_COMM_LEN - 1);
1861 if (opt_print_fontname && !dr && bank->tone[0][elm]->name && bank->tone[0][elm]->instype != 2) {
1862 const char *name = pathsep_strrchr(bank->tone[0][elm]->name);
1866 name = bank->tone[0][elm]->name;
1867 strlcat(bank->tone[0][elm]->comment, " (", MAX_TONEBANK_COMM_LEN);
1868 strlcat(bank->tone[0][elm]->comment, name, MAX_TONEBANK_COMM_LEN);
1869 strlcat(bank->tone[0][elm]->comment, ")", MAX_TONEBANK_COMM_LEN);
1871 }else if(bank->tone[0][elm]->name != NULL){
1872 strncpy(bank->tone[0][elm]->comment, bank->tone[0][elm]->name, MAX_TONEBANK_COMM_LEN - 1);
1874 strncpy(bank->tone[0][elm]->comment, ip->instname, MAX_TONEBANK_COMM_LEN - 1);
1879 if(bank->tone[0][elm]){
1880 if (!bank->tone[0][elm]->comment)
1881 bank->tone[0][elm]->comment = (char *)safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
1882 strncpy(bank->tone[0][elm]->comment, ip->instname, MAX_TONEBANK_COMM_LEN - 1);
1883 bank->tone[0][elm]->comment[MAX_TONEBANK_COMM_LEN - 1] = '\0';
1889 apply_bank_parameter(ip, bank->tone[prog][elm]);
1893 static void *safe_memdup(void *s, size_t size)
1895 return memcpy(safe_malloc(size), s, size);
1898 /*! Copy ToneBankElement src to elm. The original elm is released. */
1899 void copy_tone_bank_element(ToneBankElement *elm, const ToneBankElement *src)
1903 free_tone_bank_element(elm);
1904 memcpy(elm, src, sizeof(ToneBankElement));
1906 elm->name = safe_strdup(elm->name);
1908 if (elm->sample_lokeynum)
1909 elm->sample_lokey = (int16 *) safe_memdup(elm->sample_lokey,
1910 elm->sample_lokeynum * sizeof(int16));
1911 if (elm->sample_hikeynum)
1912 elm->sample_hikey = (int16 *) safe_memdup(elm->sample_hikey,
1913 elm->sample_hikeynum * sizeof(int16));
1914 if (elm->sample_lovelnum)
1915 elm->sample_lovel = (int16 *) safe_memdup(elm->sample_lovel,
1916 elm->sample_lovelnum * sizeof(int16));
1917 if (elm->sample_hivelnum)
1918 elm->sample_hivel = (int16 *) safe_memdup(elm->sample_hivel,
1919 elm->sample_hivelnum * sizeof(int16));
1921 elm->tune = (float *) safe_memdup(elm->tune,
1922 elm->tunenum * sizeof(float));
1923 if (elm->envratenum) {
1924 elm->envrate = (int **) safe_memdup(elm->envrate,
1925 elm->envratenum * sizeof(int *));
1926 for (i = 0; i < elm->envratenum; i++)
1927 elm->envrate[i] = (int *) safe_memdup(elm->envrate[i],
1930 if (elm->envofsnum) {
1931 elm->envofs = (int **) safe_memdup(elm->envofs,
1932 elm->envofsnum * sizeof(int *));
1933 for (i = 0; i < elm->envofsnum; i++)
1934 elm->envofs[i] = (int *) safe_memdup(elm->envofs[i],
1938 elm->trem = (Quantity **) safe_memdup(elm->trem,
1939 elm->tremnum * sizeof(Quantity *));
1940 for (i = 0; i < elm->tremnum; i++)
1941 elm->trem[i] = (Quantity *) safe_memdup(elm->trem[i],
1942 3 * sizeof(Quantity));
1944 if (elm->tremdelaynum)
1945 elm->tremdelay = (int16 *) safe_memdup(elm->tremdelay,
1946 elm->tremdelaynum * sizeof(int16));
1947 if (elm->tremsweepnum)
1948 elm->tremsweep = (int16 *) safe_memdup(elm->tremsweep,
1949 elm->tremsweepnum * sizeof(int16));
1950 if (elm->tremfreqnum)
1951 elm->tremfreq = (int16 *) safe_memdup(elm->tremfreq,
1952 elm->tremfreqnum * sizeof(int16));
1953 if (elm->tremampnum)
1954 elm->tremamp = (int16 *) safe_memdup(elm->tremamp,
1955 elm->tremampnum * sizeof(int16));
1956 if (elm->trempitchnum)
1957 elm->trempitch = (int16 *) safe_memdup(elm->trempitch,
1958 elm->trempitchnum * sizeof(int16));
1960 elm->tremfc = (int16 *) safe_memdup(elm->tremfc,
1961 elm->tremfcnum * sizeof(int16));
1963 elm->vib = (Quantity **) safe_memdup(elm->vib,
1964 elm->vibnum * sizeof(Quantity *));
1965 for (i = 0; i < elm->vibnum; i++)
1966 elm->vib[i] = (Quantity *) safe_memdup(elm->vib[i],
1967 3 * sizeof(Quantity));
1969 if (elm->vibdelaynum)
1970 elm->vibdelay = (int16 *) safe_memdup(elm->vibdelay,
1971 elm->vibdelaynum * sizeof(int16));
1972 if (elm->vibsweepnum)
1973 elm->vibsweep = (int16 *) safe_memdup(elm->vibsweep,
1974 elm->vibsweepnum * sizeof(int16));
1975 if (elm->vibfreqnum)
1976 elm->vibfreq = (int16 *) safe_memdup(elm->vibfreq,
1977 elm->vibfreqnum * sizeof(int16));
1979 elm->vibamp = (int16 *) safe_memdup(elm->vibamp,
1980 elm->vibampnum * sizeof(int16));
1981 if (elm->vibpitchnum)
1982 elm->vibpitch = (int16 *) safe_memdup(elm->vibpitch,
1983 elm->vibpitchnum * sizeof(int16));
1985 elm->vibfc = (int16 *) safe_memdup(elm->vibfc,
1986 elm->vibfcnum * sizeof(int16));
1987 if (elm->sclnotenum)
1988 elm->sclnote = (int16 *) safe_memdup(elm->sclnote,
1989 elm->sclnotenum * sizeof(int16));
1990 if (elm->scltunenum)
1991 elm->scltune = (int16 *) safe_memdup(elm->scltune,
1992 elm->scltunenum * sizeof(int16));
1994 elm->comment = (char *) safe_memdup(elm->comment,
1995 MAX_TONEBANK_COMM_LEN * sizeof(char));
1996 if (elm->modenvratenum) {
1997 elm->modenvrate = (int **) safe_memdup(elm->modenvrate,
1998 elm->modenvratenum * sizeof(int *));
1999 for (i = 0; i < elm->modenvratenum; i++)
2000 elm->modenvrate[i] = (int *) safe_memdup(elm->modenvrate[i],
2003 if (elm->modenvofsnum) {
2004 elm->modenvofs = (int **) safe_memdup(elm->modenvofs,
2005 elm->modenvofsnum * sizeof(int *));
2006 for (i = 0; i < elm->modenvofsnum; i++)
2007 elm->modenvofs[i] = (int *) safe_memdup(elm->modenvofs[i],
2010 if (elm->envkeyfnum) {
2011 elm->envkeyf = (int **) safe_memdup(elm->envkeyf,
2012 elm->envkeyfnum * sizeof(int *));
2013 for (i = 0; i < elm->envkeyfnum; i++)
2014 elm->envkeyf[i] = (int *) safe_memdup(elm->envkeyf[i],
2017 if (elm->envvelfnum) {
2018 elm->envvelf = (int **) safe_memdup(elm->envvelf,
2019 elm->envvelfnum * sizeof(int *));
2020 for (i = 0; i < elm->envvelfnum; i++)
2021 elm->envvelf[i] = (int *) safe_memdup(elm->envvelf[i],
2024 if (elm->modenvkeyfnum) {
2025 elm->modenvkeyf = (int **) safe_memdup(elm->modenvkeyf,
2026 elm->modenvkeyfnum * sizeof(int *));
2027 for (i = 0; i < elm->modenvkeyfnum; i++)
2028 elm->modenvkeyf[i] = (int *) safe_memdup(elm->modenvkeyf[i],
2031 if (elm->modenvvelfnum) {
2032 elm->modenvvelf = (int **) safe_memdup(elm->modenvvelf,
2033 elm->modenvvelfnum * sizeof(int *));
2034 for (i = 0; i < elm->modenvvelfnum; i++)
2035 elm->modenvvelf[i] = (int *) safe_memdup(elm->modenvvelf[i],
2038 if (elm->modpitchnum)
2039 elm->modpitch = (int16 *) safe_memdup(elm->modpitch,
2040 elm->modpitchnum * sizeof(int16));
2042 elm->modfc = (int16 *) safe_memdup(elm->modfc,
2043 elm->modfcnum * sizeof(int16));
2045 elm->fc = (int16 *) safe_memdup(elm->fc,
2046 elm->fcnum * sizeof(int16));
2048 elm->reso = (int16 *) safe_memdup(elm->reso,
2049 elm->resonum * sizeof(int16));
2052 elm->fclow = (int16 *) safe_memdup(elm->fclow,
2053 elm->fclownum * sizeof(int16));
2054 if (elm->fclowkeyfnum)
2055 elm->fclowkeyf = (int16 *) safe_memdup(elm->fclowkeyf,
2056 elm->fclowkeyfnum * sizeof(int16));
2058 elm->fcmul = (int16 *) safe_memdup(elm->fcmul,
2059 elm->fcmulnum * sizeof(int16));
2061 elm->fcadd = (int16 *) safe_memdup(elm->fcadd,
2062 elm->fcaddnum * sizeof(int16));
2063 if (elm->pitenvnum) {
2064 elm->pitenv = (int **) safe_memdup(elm->pitenv,
2065 elm->pitenvnum * sizeof(int *));
2066 for (i = 0; i < elm->pitenvnum; i++)
2067 elm->pitenv[i] = (int *) safe_memdup(elm->pitenv[i],
2071 elm->hpf = (int **) safe_memdup(elm->hpf, elm->hpfnum * sizeof(int *));
2072 for (i = 0; i < elm->hpfnum; i++)
2073 elm->hpf[i] = (int *) safe_memdup(elm->hpf[i], 3 * sizeof(int));
2075 for(i = 0; i < VOICE_EFFECT_NUM; i++){
2076 if (elm->vfxnum[i]) {
2077 elm->vfx[i] = (int **) safe_memdup(elm->vfx[i], elm->vfxnum[i] * sizeof(int *));
2078 for (j = 0; j < elm->vfxnum[i]; j++)
2079 elm->vfx[i][j] = (int *) safe_memdup(elm->vfx[i][j], VOICE_EFFECT_PARAM_NUM * sizeof(int));
2088 /*! Release ToneBank[128 + MAP_BANK_COUNT] */
2089 static void free_tone_bank_list(ToneBank *tb[])
2095 //for (i = 0; i < 128 + map_bank_counter; i++)//del by Kobarin
2096 for(i = 0; i < 128 + MAP_BANK_COUNT; i++)//add by Kobarin(
\83\81\83\82\83\8a\83\8a\81[
\83N
\8fC
\90³)
2101 for (j = 0; j < 128; j++)
2102 for(elm = 0; elm < MAX_ELEMENT; elm++){
2103 if(bank->tone[j][elm]){
2104 free_tone_bank_element(bank->tone[j][elm]);
2105 safe_free(bank->tone[j][elm]);
2106 bank->tone[j][elm] = NULL;
2110 {//added by Kobarin(
\83\81\83\82\83\8a\83\8a\81[
\83N
\8fC
\90³)
2111 struct _AlternateAssign *del=bank->alt;
2112 struct _AlternateAssign *next;
2128 /*! Release tonebank and drumset */
2129 void free_tone_bank(void)
2131 free_tone_bank_list(tonebank);
2132 free_tone_bank_list(drumset);
2135 /*! Release ToneBankElement. */
2136 void free_tone_bank_element(ToneBankElement *elm)
2143 safe_free(elm->name);
2146 safe_free(elm->sample_lokey);
2147 elm->sample_lokey = NULL, elm->sample_lokeynum = 0;
2148 safe_free(elm->sample_hikey);
2149 elm->sample_hikey = NULL, elm->sample_hikeynum = 0;
2150 safe_free(elm->sample_lovel);
2151 elm->sample_lovel = NULL, elm->sample_lovelnum = 0;
2152 safe_free(elm->sample_hivel);
2153 elm->sample_hivel = NULL, elm->sample_hivelnum = 0;
2154 safe_free(elm->tune);
2155 elm->tune = NULL, elm->tunenum = 0;
2156 if (elm->envratenum)
2157 free_ptr_list(elm->envrate, elm->envratenum);
2158 elm->envrate = NULL, elm->envratenum = 0;
2160 free_ptr_list(elm->envofs, elm->envofsnum);
2161 elm->envofs = NULL, elm->envofsnum = 0;
2164 free_ptr_list(elm->trem, elm->tremnum);
2165 elm->trem = NULL, elm->tremnum = 0;
2166 safe_free(elm->tremdelay);
2167 elm->tremdelay = NULL, elm->tremdelaynum = 0;
2168 safe_free(elm->tremsweep);
2169 elm->tremsweep = NULL, elm->tremsweepnum = 0;
2170 safe_free(elm->tremfreq);
2171 elm->tremfreq = NULL, elm->tremfreqnum = 0;
2172 safe_free(elm->tremamp);
2173 elm->tremamp = NULL, elm->tremampnum = 0;
2174 safe_free(elm->trempitch);
2175 elm->trempitch = NULL, elm->trempitchnum = 0;
2176 safe_free(elm->tremfc);
2177 elm->tremfc = NULL, elm->tremfcnum = 0;
2179 free_ptr_list(elm->vib, elm->vibnum);
2180 elm->vib = NULL, elm->vibnum = 0;
2181 safe_free(elm->vibdelay);
2182 elm->vibdelay = NULL, elm->vibdelaynum = 0;
2183 safe_free(elm->vibsweep);
2184 elm->vibsweep = NULL, elm->vibsweepnum = 0;
2185 safe_free(elm->vibfreq);
2186 elm->vibfreq = NULL, elm->vibfreqnum = 0;
2187 safe_free(elm->vibamp);
2188 elm->vibamp = NULL, elm->vibampnum = 0;
2189 safe_free(elm->vibpitch);
2190 elm->vibpitch = NULL, elm->vibpitchnum = 0;
2191 safe_free(elm->vibfc);
2192 elm->vibfc = NULL, elm->vibfcnum = 0;
2193 safe_free(elm->sclnote);
2194 elm->sclnote = NULL, elm->sclnotenum = 0;
2195 safe_free(elm->scltune);
2196 elm->scltune = NULL, elm->scltunenum = 0;
2197 safe_free(elm->comment);
2198 elm->comment = NULL;
2199 if (elm->modenvratenum)
2200 free_ptr_list(elm->modenvrate, elm->modenvratenum);
2201 elm->modenvrate = NULL, elm->modenvratenum = 0;
2202 if (elm->modenvofsnum)
2203 free_ptr_list(elm->modenvofs, elm->modenvofsnum);
2204 elm->modenvofs = NULL, elm->modenvofsnum = 0;
2205 if (elm->envkeyfnum)
2206 free_ptr_list(elm->envkeyf, elm->envkeyfnum);
2207 elm->envkeyf = NULL, elm->envkeyfnum = 0;
2208 if (elm->envvelfnum)
2209 free_ptr_list(elm->envvelf, elm->envvelfnum);
2210 elm->envvelf = NULL, elm->envvelfnum = 0;
2211 if (elm->modenvkeyfnum)
2212 free_ptr_list(elm->modenvkeyf, elm->modenvkeyfnum);
2213 elm->modenvkeyf = NULL, elm->modenvkeyfnum = 0;
2214 if (elm->modenvvelfnum)
2215 free_ptr_list(elm->modenvvelf, elm->modenvvelfnum);
2216 elm->modenvvelf = NULL, elm->modenvvelfnum = 0;
2217 safe_free(elm->modpitch);
2218 elm->modpitch = NULL, elm->modpitchnum = 0;
2219 safe_free(elm->modfc);
2220 elm->modfc = NULL, elm->modfcnum = 0;
2222 elm->fc = NULL, elm->fcnum = 0;
2223 safe_free(elm->reso);
2224 elm->reso = NULL, elm->resonum = 0;
2226 safe_free(elm->fclow);
2227 elm->fclow = NULL, elm->fclownum = 0;
2228 safe_free(elm->fclowkeyf);
2229 elm->fclowkeyf = NULL, elm->fclowkeyfnum = 0;
2230 safe_free(elm->fcmul);
2231 elm->fcmul = NULL, elm->fcmulnum = 0;
2232 safe_free(elm->fcadd);
2233 elm->fcadd = NULL, elm->fcaddnum = 0;
2235 free_ptr_list(elm->pitenv, elm->pitenvnum);
2236 elm->pitenv = NULL, elm->pitenvnum = 0;
2238 free_ptr_list(elm->hpf, elm->hpfnum);
2239 elm->hpf = NULL, elm->hpfnum = 0;
2240 for(i = 0; i < VOICE_EFFECT_NUM; i++){
2241 if (elm->vfxnum[i]){
2242 free_ptr_list(elm->vfx[i], elm->vfxnum[i]);
2244 elm->vfx[i] = NULL, elm->vfxnum[i] = 0;
2249 void free_instruments(int reload_default_inst)
2251 int i = 128 + map_bank_counter, j;
2252 struct InstrumentCache *p;
2255 struct InstrumentCache *default_entry;
2256 int default_entry_addr;
2259 clear_magic_instruments();
2261 /* Free soundfont/SCC/MMS instruments */
2264 /* Note that bank[*]->tone[j].instrument may pointer to
2265 bank[0]->tone[j].instrument. See play_midi_load_instrument()
2266 at playmidi.c for the implementation */
2268 if((bank = tonebank[i]) != NULL)
2269 for(j = 127; j >= 0; j--)
2270 for(elm = 0; elm < MAX_ELEMENT; elm++)
2272 if(bank->tone[j][elm] == NULL)
2274 ip = bank->tone[j][elm]->instrument;
2275 if(ip && (ip->type == INST_SF2 || ip->type == INST_PCM || ip->type == INST_MMS || ip->type == INST_SCC || ip->type == INST_SFZ || ip->type == INST_DLS) &&
2276 (i == 0 || !tonebank[0]->tone[j][elm] || ip != tonebank[0]->tone[j][elm]->instrument) )
2277 free_instrument(ip);
2278 bank->tone[j][elm]->instrument = NULL;
2280 if((bank = drumset[i]) != NULL)
2281 for(j = 127; j >= 0; j--)
2282 for(elm = 0; elm < MAX_ELEMENT; elm++)
2284 if(bank->tone[j][elm] == NULL)
2286 ip = bank->tone[j][elm]->instrument;
2287 if(ip && (ip->type == INST_SF2 || ip->type == INST_PCM || ip->type == INST_MMS || ip->type == INST_SCC || ip->type == INST_SFZ || ip->type == INST_DLS) &&
2288 (i == 0 || !drumset[0]->tone[j][elm] || ip != drumset[0]->tone[j][elm]->instrument) )
2289 free_instrument(ip);
2290 bank->tone[j][elm]->instrument = NULL;
2293 if ((drumset[i] != NULL) && (drumset[i]->alt != NULL)) {
2294 safe_free(drumset[i]->alt);
2295 drumset[i]->alt = NULL;
2300 /* Free GUS/patch instruments */
2301 default_entry = NULL;
2302 default_entry_addr = 0;
2303 for(i = 0; i < INSTRUMENT_HASH_SIZE; i++)
2305 p = instrument_cache[i];
2308 if(!reload_default_inst && p->ip == default_instrument)
2311 default_entry_addr = i;
2316 struct InstrumentCache *tmp;
2320 free_instrument(tmp->ip);
2324 instrument_cache[i] = NULL;
2327 if(reload_default_inst)
2328 set_default_instrument(NULL);
2329 else if(default_entry)
2331 default_entry->next = NULL;
2332 instrument_cache[default_entry_addr] = default_entry;
2335 free_int_synth_preset();
2339 void free_special_patch(int id)
2341 int i, j, start, end;
2348 end = NSPECIAL_PATCH - 1;
2351 for(i = start; i <= end; i++)
2352 if(special_patch[i] != NULL)
2357 if(special_patch[i]->name != NULL)
2358 safe_free(special_patch[i]->name);
2359 special_patch[i]->name = NULL;
2360 n = special_patch[i]->samples;
2361 sp = special_patch[i]->sample;
2364 for(j = 0; j < n; j++)
2365 if(sp[j].data_alloced && sp[j].data)
2366 safe_free(sp[j].data);
2369 safe_free(special_patch[i]);
2370 special_patch[i] = NULL;
2374 int set_default_instrument(char *name)
2378 static char *last_name;
2388 if(!(ip = load_gus_instrument(name, NULL, 0, 0, NULL, elm)))
2390 if(default_instrument)
2391 free_instrument(default_instrument);
2392 default_instrument = ip;
2393 for(i = 0; i < MAX_CHANNELS; i++)
2394 default_program[i] = SPECIAL_PROGRAM;
2400 /*! search mapped bank.
2401 returns negative value indicating free bank if not found,
2402 0 if no free bank was available */
2403 int find_instrument_map_bank(int dr, int map, int bk)
2405 struct bank_map_elem *bm;
2408 if (map == INST_NO_MAP)
2410 bm = dr ? map_drumset : map_bank;
2411 for(i = 0; i < MAP_BANK_COUNT; i++)
2415 else if (bm[i].mapid == map && bm[i].bankno == bk)
2421 /*! allocate mapped bank if needed. returns -1 if allocation failed. */
2422 int alloc_instrument_map_bank(int dr, int map, int bk)
2424 struct bank_map_elem *bm;
2427 if (map == INST_NO_MAP)
2429 alloc_instrument_bank(dr, bk);
2432 i = find_instrument_map_bank(dr, map, bk);
2438 bm = dr ? map_drumset : map_bank;
2442 if (map_bank_counter < i + 1)
2443 map_bank_counter = i + 1;
2445 alloc_instrument_bank(dr, i);
2452 static void init_tone_bank_element(ToneBankElement *tone)
2455 tone->strip_loop = -1;
2456 tone->strip_envelope = -1;
2457 tone->strip_tail = -1;
2459 tone->amp_normalize = 0;
2465 tone->loop_timeout = 0;
2467 tone->damper_mode = 0;
2468 tone->key_to_fc = 0;
2469 tone->vel_to_fc = 0;
2470 tone->reverb_send = DEFALT_REVERB_SEND; // def -1
2471 tone->chorus_send = DEFALT_CHORUS_SEND; // def -1
2472 tone->delay_send = DEFALT_DELAY_SEND; // def -1
2473 tone->lpf_type = -1;
2474 tone->rx_note_off = 1;
2475 tone->keep_voice = 0;
2476 tone->tva_level = -1;
2477 tone->play_note = -1;
2478 tone->element_num = 0;
2480 tone->sample_pan = -1;
2481 tone->sample_width = -1;
2483 tone->seq_length = 0;
2484 tone->seq_position = 0;
2490 void reinit_tone_bank_element(ToneBankElement *tone)
2492 free_tone_bank_element(tone);
2493 init_tone_bank_element(tone);
2497 int alloc_tone_bank_element(ToneBankElement **tone_ptr)
2499 *tone_ptr = (ToneBankElement *)safe_malloc(sizeof(ToneBankElement));
2500 if(*tone_ptr == NULL){
2501 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2502 "alloc_tone_bank_element: ToneBankElement malloc error.");
2505 memset(*tone_ptr, 0, sizeof(ToneBankElement));
2506 init_tone_bank_element(*tone_ptr);
2511 void alloc_instrument_bank(int dr, int bk)
2517 if((b = drumset[bk]) == NULL)
2519 b = drumset[bk] = (ToneBank *)safe_malloc(sizeof(ToneBank));
2520 if(drumset[bk] == NULL){
2521 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2522 "alloc_instrument_bank: ToneBank malloc error. drumset");
2525 memset(b, 0, sizeof(ToneBank));
2526 if(b->tone[0][0] == NULL)
2528 if(alloc_tone_bank_element(&b->tone[0][0])){
2529 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2530 "alloc_instrument_bank: ToneBankElement malloc error. drumset");
2538 if((b = tonebank[bk]) == NULL)
2540 b = tonebank[bk] = (ToneBank *)safe_malloc(sizeof(ToneBank));
2541 if(tonebank[bk] == NULL){
2542 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2543 "alloc_instrument_bank: ToneBank malloc error. tonebank");
2546 memset(b, 0, sizeof(ToneBank));
2547 if(b->tone[0][0] == NULL)
2549 if(alloc_tone_bank_element(&b->tone[0][0])){
2550 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2551 "alloc_instrument_bank: ToneBankElement malloc error. tonebank");
2560 /* Instrument alias map - Written by Masanao Izumo */
2562 int instrument_map_no_mapped(int mapID, int *set, int *elem)
2565 struct inst_map_elem *p;
2567 if(mapID == INST_NO_MAP)
2568 return 0; /* No map */
2572 p = inst_map_table[mapID][s];
2582 p = inst_map_table[mapID][0];
2593 int instrument_map(int mapID, int *set, int *elem)
2596 struct inst_map_elem *p;
2598 if(mapID == INST_NO_MAP)
2599 return 0; /* No map */
2603 p = inst_map_table[mapID][s];
2604 if(p != NULL && p[e].mapped)
2613 p = inst_map_table[mapID][0];
2614 if(p != NULL && p[e].mapped)
2624 void set_instrument_map(int mapID,
2625 int set_from, int elem_from,
2626 int set_to, int elem_to)
2628 struct inst_map_elem *p;
2630 p = inst_map_table[mapID][set_from];
2633 p = (struct inst_map_elem *)
2634 safe_malloc(128 * sizeof(struct inst_map_elem));
2635 memset(p, 0, 128 * sizeof(struct inst_map_elem));
2636 inst_map_table[mapID][set_from] = p;
2638 p[elem_from].set = set_to;
2639 p[elem_from].elem = elem_to;
2640 p[elem_from].mapped = 1;
2643 void free_instrument_map(void)
2647 for(i = 0; i < map_bank_counter; i++)
2648 map_bank[i].used = map_drumset[i].used = 0;
2649 /* map_bank_counter = 0; never shrinks rather than assuming tonebank was already freed */
2650 for (i = 0; i < NUM_INST_MAP; i++) {
2651 for (j = 0; j < 128; j++) {
2652 struct inst_map_elem *map;
2653 map = inst_map_table[i][j];
2656 inst_map_table[i][j] = NULL;
2662 /* Alternate assign - Written by Masanao Izumo */
2664 AlternateAssign *add_altassign_string(AlternateAssign *old,
2665 char **params, int n)
2670 AlternateAssign *alt;
2674 if(!strcmp(*params, "clear")) {
2676 AlternateAssign *next;
2687 alt = (AlternateAssign *)safe_malloc(sizeof(AlternateAssign));
2688 memset(alt, 0, sizeof(AlternateAssign));
2689 for(i = 0; i < n; i++) {
2696 if((p = strchr(p, '-')) != NULL) {
2713 for(j = beg; j <= end; j++)
2714 alt->bits[(j >> 5) & 0x3] |= 1 << (j & 0x1F);
2720 AlternateAssign *find_altassign(AlternateAssign *altassign, int note)
2726 mask = 1 << (note & 0x1F);
2727 idx = (note >> 5) & 0x3;
2728 for(p = altassign; p != NULL; p = p->next)
2729 if(p->bits[idx] & mask)