📄 instrum.c
字号:
sp->scale_factor = adjust_scale_tune(tone->scltune[i]); } if (tone->modenvratenum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->modenvratenum == 1) { for (j = 0; j < 6; j++) if (tone->modenvrate[0][j] >= 0) sp->modenv_rate[j] = to_rate(tone->modenvrate[0][j]); } else if (i < tone->modenvratenum) { for (j = 0; j < 6; j++) if (tone->modenvrate[i][j] >= 0) sp->modenv_rate[j] = to_rate(tone->modenvrate[i][j]); } } if (tone->modenvofsnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->modenvofsnum == 1) { for (j = 0; j < 6; j++) if (tone->modenvofs[0][j] >= 0) sp->modenv_offset[j] = to_offset(tone->modenvofs[0][j]); } else if (i < tone->modenvofsnum) { for (j = 0; j < 6; j++) if (tone->modenvofs[i][j] >= 0) sp->modenv_offset[j] = to_offset(tone->modenvofs[i][j]); } } if (tone->envkeyfnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->envkeyfnum == 1) { for (j = 0; j < 6; j++) if (tone->envkeyf[0][j] != -1) sp->envelope_keyf[j] = tone->envkeyf[0][j]; } else if (i < tone->envkeyfnum) { for (j = 0; j < 6; j++) if (tone->envkeyf[i][j] != -1) sp->envelope_keyf[j] = tone->envkeyf[i][j]; } } if (tone->envvelfnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->envvelfnum == 1) { for (j = 0; j < 6; j++) if (tone->envvelf[0][j] != -1) sp->envelope_velf[j] = tone->envvelf[0][j]; } else if (i < tone->envvelfnum) { for (j = 0; j < 6; j++) if (tone->envvelf[i][j] != -1) sp->envelope_velf[j] = tone->envvelf[i][j]; } } if (tone->modenvkeyfnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->modenvkeyfnum == 1) { for (j = 0; j < 6; j++) if (tone->modenvkeyf[0][j] != -1) sp->modenv_keyf[j] = tone->modenvkeyf[0][j]; } else if (i < tone->modenvkeyfnum) { for (j = 0; j < 6; j++) if (tone->modenvkeyf[i][j] != -1) sp->modenv_keyf[j] = tone->modenvkeyf[i][j]; } } if (tone->modenvvelfnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->modenvvelfnum == 1) { for (j = 0; j < 6; j++) if (tone->modenvvelf[0][j] != -1) sp->modenv_velf[j] = tone->modenvvelf[0][j]; } else if (i < tone->modenvvelfnum) { for (j = 0; j < 6; j++) if (tone->modenvvelf[i][j] != -1) sp->modenv_velf[j] = tone->modenvvelf[i][j]; } } if (tone->trempitchnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->trempitchnum == 1) sp->tremolo_to_pitch = tone->trempitch[0]; else if (i < tone->trempitchnum) sp->tremolo_to_pitch = tone->trempitch[i]; } if (tone->tremfcnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->tremfcnum == 1) sp->tremolo_to_fc = tone->tremfc[0]; else if (i < tone->tremfcnum) sp->tremolo_to_fc = tone->tremfc[i]; } if (tone->modpitchnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->modpitchnum == 1) sp->modenv_to_pitch = tone->modpitch[0]; else if (i < tone->modpitchnum) sp->modenv_to_pitch = tone->modpitch[i]; } if (tone->modfcnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->modfcnum == 1) sp->modenv_to_fc = tone->modfc[0]; else if (i < tone->modfcnum) sp->modenv_to_fc = tone->modfc[i]; } if (tone->fcnum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->fcnum == 1) sp->cutoff_freq = adjust_fc(tone->fc[0]); else if (i < tone->fcnum) sp->cutoff_freq = adjust_fc(tone->fc[i]); } if (tone->resonum) for (i = 0; i < ip->samples; i++) { sp = &ip->sample[i]; if (tone->resonum == 1) sp->resonance = adjust_reso(tone->reso[0]); else if (i < tone->resonum) sp->resonance = adjust_reso(tone->reso[i]); }}#define READ_CHAR(thing) { \ uint8 tmpchar; \ \ if (tf_read(&tmpchar, 1, 1, tf) != 1) \ goto fail; \ thing = tmpchar; \}#define READ_SHORT(thing) { \ uint16 tmpshort; \ \ if (tf_read(&tmpshort, 2, 1, tf) != 1) \ goto fail; \ thing = LE_SHORT(tmpshort); \}#define READ_LONG(thing) { \ int32 tmplong; \ \ if (tf_read(&tmplong, 4, 1, tf) != 1) \ goto fail; \ thing = LE_LONG(tmplong); \}/* If panning or note_to_use != -1, it will be used for all samples, * instead of the sample-specific values in the instrument file. * * For note_to_use, any value < 0 or > 127 will be forced to 0. * * For other parameters, 1 means yes, 0 means no, other values are * undefined. * * TODO: do reverse loops right */static Instrument *load_gus_instrument(char *name, ToneBank *bank, int dr, int prog, char *infomsg){ ToneBankElement *tone; int amp, note_to_use, panning, strip_envelope, strip_loop, strip_tail; Instrument *ip; struct timidity_file *tf; uint8 tmp[1024], fractions; Sample *sp; int i, j, noluck = 0; if (! name) return 0; if (infomsg != NULL) ctl->cmsg(CMSG_INFO, VERB_NOISY, "%s: %s", infomsg, name); else ctl->cmsg(CMSG_INFO, VERB_NOISY, "Loading instrument %s", name); if (bank) { tone = &bank->tone[prog]; amp = tone->amp; note_to_use = (tone->note != -1) ? tone->note : ((dr) ? prog : -1); panning = tone->pan; strip_envelope = (tone->strip_envelope != -1) ? tone->strip_envelope : ((dr) ? 1 : -1); strip_loop = (tone->strip_loop != -1) ? tone->strip_loop : ((dr) ? 1 : -1); strip_tail = tone->strip_tail; } else { tone = NULL; amp = note_to_use = panning = -1; strip_envelope = strip_loop = strip_tail = 0; } if (tone && tone->tunenum == 0 && tone->envratenum == 0 && tone->envofsnum == 0 && tone->tremnum == 0 && tone->vibnum == 0 && tone->sclnotenum == 0 && tone->scltunenum == 0 && tone->modenvratenum == 0 && tone->modenvofsnum == 0 && tone->envkeyfnum == 0 && tone->envvelfnum == 0 && tone->modenvkeyfnum == 0 && tone->modenvvelfnum == 0 && tone->trempitchnum == 0 && tone->tremfcnum == 0 && tone->modpitchnum == 0 && tone->modfcnum == 0 && tone->fcnum == 0 && tone->resonum == 0) if ((ip = search_instrument_cache(name, panning, amp, note_to_use, strip_loop, strip_envelope, strip_tail)) != NULL) { ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * Cached"); return ip; } /* Open patch file */ if (! (tf = open_file(name, 2, OF_NORMAL))) {#ifdef PATCH_EXT_LIST int name_len, ext_len; static char *patch_ext[] = PATCH_EXT_LIST;#endif noluck = 1;#ifdef PATCH_EXT_LIST name_len = strlen(name); /* Try with various extensions */ for (i = 0; patch_ext[i]; i++) { ext_len = strlen(patch_ext[i]); if (name_len + ext_len < 1024) { if (name_len >= ext_len && strcmp(name + name_len - ext_len, patch_ext[i]) == 0) continue; /* duplicated ext. */ strcpy((char *) tmp, name); strcat((char *) tmp, patch_ext[i]); if ((tf = open_file((char *) tmp, 1, OF_NORMAL))) { noluck = 0; break; } } }#endif } if (noluck) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Instrument `%s' can't be found.", name); return 0; } /* Read some headers and do cursory sanity checks. There are loads * of magic offsets. This could be rewritten... */ tmp[0] = tf_getc(tf); if (tmp[0] == '\0') { /* for Mac binary */ skip(tf, 127); tmp[0] = tf_getc(tf); } if ((tf_read(tmp + 1, 1, 238, tf) != 238) || (memcmp(tmp, "GF1PATCH110\0ID#000002", 22) && memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) { /* don't know what the differences are */ ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: not an instrument", name); close_file(tf); return 0; } /* instruments. To some patch makers, 0 means 1 */ if (tmp[82] != 1 && tmp[82] != 0) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't handle patches with %d instruments", tmp[82]); close_file(tf); return 0; } if (tmp[151] != 1 && tmp[151] != 0) { /* layers. What's a layer? */ ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't handle instruments with %d layers", tmp[151]); close_file(tf); return 0; } ip = (Instrument *) safe_malloc(sizeof(Instrument)); ip->type = INST_GUS; ip->samples = tmp[198]; ip->sample = (Sample *) safe_malloc(sizeof(Sample) * ip->samples); memset(ip->sample, 0, sizeof(Sample) * ip->samples); for (i = 0; i < ip->samples; i++) { skip(tf, 7); /* Skip the wave name */ if (tf_read(&fractions, 1, 1, tf) != 1) {fail: ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Error reading sample %d", i); for (j = 0; j < i; j++) free(ip->sample[j].data); free(ip->sample); free(ip); close_file(tf); return 0; } sp = &(ip->sample[i]); sp->low_vel = 0; sp->high_vel = 127; sp->cutoff_freq = sp->resonance = 0; sp->tremolo_to_pitch = sp->tremolo_to_fc = 0; sp->modenv_to_pitch = sp->modenv_to_fc = 0; sp->vel_to_fc = sp->key_to_fc = sp->vel_to_resonance = 0; sp->envelope_velf_bpo = sp->modenv_velf_bpo = 64; sp->vel_to_fc_threshold = 64; sp->key_to_fc_bpo = 60; sp->envelope_delay = sp->modenv_delay = 0; sp->tremolo_delay = sp->vibrato_delay = 0; sp->inst_type = INST_GUS; sp->sample_type = SF_SAMPLETYPE_MONO; sp->sf_sample_link = -1; sp->sf_sample_index = 0; memset(sp->envelope_velf, 0, sizeof(sp->envelope_velf)); memset(sp->envelope_keyf, 0, sizeof(sp->envelope_keyf)); memset(sp->modenv_velf, 0, sizeof(sp->modenv_velf)); memset(sp->modenv_keyf, 0, sizeof(sp->modenv_keyf)); memset(sp->modenv_rate, 0, sizeof(sp->modenv_rate)); memset(sp->modenv_offset, 0, sizeof(sp->modenv_offset)); READ_LONG(sp->data_length); READ_LONG(sp->loop_start); READ_LONG(sp->loop_end); READ_SHORT(sp->sample_rate); READ_LONG(sp->low_freq); READ_LONG(sp->high_freq); READ_LONG(sp->root_freq); skip(tf, 2); /* Why have a "root frequency" and then "tuning"?? */ READ_CHAR(tmp[0]); ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Rate/Low/Hi/Root = %d/%d/%d/%d", sp->sample_rate, sp->low_freq, sp->high_freq, sp->root_freq); if (panning == -1) /* 0x07 and 0x08 are both center panning */ sp->panning = ((tmp[0] - ((tmp[0] < 8) ? 7 : 8)) * 63) / 7 + 64; else sp->panning = (uint8) (panning & 0x7f); /* envelope, tremolo, and vibrato */ if (tf_read(tmp, 1, 18, tf) != 18) goto fail; if (! tmp[13] || ! tmp[14]) { sp->tremolo_sweep_increment = sp->tremolo_phase_increment = 0; sp->tremolo_depth = 0; ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no tremolo"); } else { sp->tremolo_sweep_increment = convert_tremolo_sweep(tmp[12]); sp->tremolo_phase_increment = convert_tremolo_rate(tmp[13]); sp->tremolo_depth = tmp[14]; ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * tremolo: sweep %d, phase %d, depth %d", sp->tremolo_sweep_increment, sp->tremolo_phase_increment, sp->tremolo_depth); } if (! tmp[16] || ! tmp[17]) { sp->vibrato_sweep_increment = sp->vibrato_control_ratio = 0; sp->vibrato_depth = 0; ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no vibrato"); } else { sp->vibrato_control_ratio = convert_vibrato_rate(tmp[16]); sp->vibrato_sweep_increment = convert_vibrato_sweep(tmp[15], sp->vibrato_control_ratio); sp->vibrato_depth = tmp[17]; ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * vibrato: sweep %d, ctl %d, depth %d", sp->vibrato_sweep_increment, sp->vibrato_control_ratio, sp->vibrato_depth); } READ_CHAR(sp->modes); ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * mode: 0x%02x", sp->modes); READ_SHORT(sp->scale_freq); READ_SHORT(sp->scale_factor); skip(tf, 36); /* skip reserved space */ /* Mark this as a fixed-pitch instrument if such a deed is desired. */ sp->note_to_use = (note_to_use != -1) ? (uint8) note_to_use : 0; /* seashore.pat in the Midia patch set has no Sustain. I don't * understand why, and fixing it by adding the Sustain flag to * all looped patches probably breaks something else. We do it * anyway. */ if (sp->modes & MODES_LOOPING) sp->modes |= MODES_SUSTAIN; /* Strip any loops and envelopes we're permitted to */ if ((strip_loop == 1) && (sp->modes & (MODES_SUSTAIN | MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE))) { sp->modes &= ~(MODES_SUSTAIN | MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE); ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing loop and/or sustain"); } if (strip_envelope == 1) { if (sp->modes & MODES_ENVELOPE) ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing envelope"); sp->modes &= ~MODES_ENVELOPE; } else if (strip_envelope != 0) { /* Have to make a guess. */ if (! (sp->modes & (MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE))) { /* No loop? Then what's there to sustain? * No envelope needed either... */ sp->modes &= ~(MODES_SUSTAIN|MODES_ENVELOPE); ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - No loop, removing sustain and envelope"); } else if (! memcmp(tmp, "??????", 6) || tmp[11] >= 100) { /* Envelope rates all maxed out? * Envelope end at a high "offset"? * That's a weird envelope. Take it out. */ sp->modes &= ~MODES_ENVELOPE; ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Weirdness, removing envelope"); } else if (! (sp->modes & MODES_SUSTAIN)) { /* No sustain? Then no envelope. I don't know if this is * justified, but patches without sustain usually don't need * the envelope either... at least the Gravis ones. They're * mostly drums. I think. */ sp->modes &= ~MODES_ENVELOPE; ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - No sustain, removing envelope"); } } for (j = 0; j < 6; j++) { sp->envelope_rate[j]= convert_envelope_rate(tmp[j]); sp->envelope_offset[j] = convert_envelope_offset(tmp[j + 6]); } /* this envelope seems to give reverb like effects to most patches * use the same method as soundfont */ if (modify_release) { sp->envelope_offset[3] = to_offset(5); sp->envelope_rate[3] = calc_rate(255, modify_release); sp->envelope_offset[4] = to_offset(4); sp->envelope_rate[4] = to_offset(200); sp->envelope_offset[5] = to_offset(4); sp->envelope_rate[5] = to_offset(200); } /* Then read the sample data */ sp->data = (sample_t *) safe_malloc(sp->data_length + 4); sp->data_alloced = 1; if ((j = tf_read(sp->data, 1, sp->data_length, tf)) != sp->data_length) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Too small this patch length: %d < %d", j, sp->data_length); goto fail; } if (! (sp->modes & MODES_16BIT)) { /* convert to 16-bit data */ int32 i; uint16 *tmp; uint8 *cp = (uint8 *) sp->data; tmp = (uint16 *) safe_malloc(sp->data_length * 2 + 4); for (i = 0; i < sp->data_length; i++) tmp[i] = (uint16) cp[i] << 8; sp->data = (sample_t *) tmp; free(cp); sp->data_length *= 2; sp->loop_start *= 2; sp->loop_end *= 2; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -