ToneBank *bank=((dr) ? drumset[b] : tonebank[b]); Instrument *ip; char infomsg[256]; int font_bank, font_preset, font_keynote;
ToneBank *bank = ((dr) ? drumset[b] : tonebank[b]); Instrument *ip; int i, font_bank, font_preset, font_keynote; extern Instrument *extract_sample_file(char *); FLOAT_T volume_max; int pan, panning; char infomsg[256];
Instrument *load_instrument(int dr, int b, int prog){    ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);    Instrument *ip;    char infomsg[256];    int font_bank, font_preset, font_keynote;#ifndef CFG_FOR_SF	if(play_system_mode == GS_SYSTEM_MODE) {		if(b == 64 || b == 65) {			if(dr) {	/* User Drumset */				recompute_userdrum(b, prog);			} else {	/* User Instrument */				recompute_userinst(b, prog);			}		}	}#endif    if(bank->tone[prog].instype == 1	|| bank->tone[prog].instype == 2)    {	if (bank->tone[prog].instype == 1)	{	    /* Font extention */	    font_bank = bank->tone[prog].font_bank;	    font_preset = bank->tone[prog].font_preset;	    font_keynote = bank->tone[prog].font_keynote;	    ip = extract_soundfont(bank->tone[prog].name,			       font_bank, font_preset, font_keynote);	}	else	{	    /* Sample extension */		extern Instrument *extract_sample_file(char *);			    ip = extract_sample_file(bank->tone[prog].name);	}	if(ip != NULL && bank->tone[prog].amp != -1)	/* amp tuning */	{	    int i;		FLOAT_T volume_max = 0;		for(i = 0; i < ip->samples; i++) {			if(ip->sample[i].volume > volume_max) {volume_max = ip->sample[i].volume;}		}		if(volume_max != 0) {			for(i = 0; i < ip->samples; i++) {				/*ip->sample[i].volume = bank->tone[prog].amp / 100.0;*/				ip->sample[i].volume = (ip->sample[i].volume * (FLOAT_T)bank->tone[prog].amp) * 0.01 / volume_max;			}		}	}	if(ip != NULL && bank->tone[prog].pan != -1)	/* panning */	{	    int i, pan, panning;		pan = ((int)bank->tone[prog].pan & 0x7F) - 64;		for(i = 0; i < ip->samples; i++) {			panning = (int)ip->sample[i].panning + pan;			if(panning < 0) {panning = 0;}			else if(panning > 127) {panning = 127;}			ip->sample[i].panning = panning;		}	}	if(ip != NULL && bank->tone[prog].note != -1)	/* note to use */	{	    int i;		for(i = 0; i < ip->samples; i++) {			ip->sample[i].root_freq = freq_table[bank->tone[prog].note & 0x7F];		}	}	if(ip != NULL && bank->tone[prog].key_to_fc != 0)	/* filter key-follow */	{	    int i;		for(i = 0; i < ip->samples; i++) {			ip->sample[i].key_to_fc = bank->tone[prog].key_to_fc;		}	}	if(ip != NULL && bank->tone[prog].vel_to_fc != 0)	/* filter velocity-follow */	{	    int i;		for(i = 0; i < ip->samples; i++) {			ip->sample[i].key_to_fc = bank->tone[prog].vel_to_fc;		}	}	if(ip != NULL && bank->tone[prog].vel_to_resonance != 0)	/* resonance velocity-follow */	{	    int i;		for(i = 0; i < ip->samples; i++) {			ip->sample[i].vel_to_resonance = bank->tone[prog].vel_to_resonance;		}	}	if(ip != NULL && bank->tone[prog].strip_tail == 1)	/* strip tail */	{	    int i;		for(i = 0; i < ip->samples; i++) {			ip->sample[i].data_length = ip->sample[i].loop_end;		}	}	if (ip != NULL) {	  int i;	  i = (dr ? 0 : prog);	  if (bank->tone[i].comment)	    free(bank->tone[i].comment);	  bank->tone[i].comment = safe_strdup(ip->instname);		apply_bank_parameter(ip, &bank->tone[prog]);	}	return ip;    }    if(dr)    {	font_bank = 128;	font_preset = b;	font_keynote = prog;    }    else    {	font_bank = b;	font_preset = prog;	font_keynote = -1;    }    /* preload soundfont */    ip = load_soundfont_inst(0, font_bank, font_preset, font_keynote);    if (ip != NULL) {      if (bank->tone[prog].comment)	free(bank->tone[prog].comment);      bank->tone[prog].comment = safe_strdup(ip->instname);    }    if(ip == NULL)    {	/* load GUS/patch file */	if(dr)	    sprintf(infomsg, "Drumset %d %d(%s)", b + progbase, prog,		    note_name[prog % 12]);	else	    sprintf(infomsg, "Tonebank %d %d", b, prog + progbase);	ip = load_gus_instrument(bank->tone[prog].name, bank, dr, prog,				 infomsg);	if(ip == NULL) { /* no patch; search soundfont again */	    ip = load_soundfont_inst(1, font_bank, font_preset, font_keynote);            if (ip != NULL) {	      if (bank->tone[0].comment)		free(bank->tone[0].comment);	      bank->tone[0].comment = safe_strdup(ip->instname);	    }	}    }	if (ip != NULL)		apply_bank_parameter(ip, &bank->tone[prog]);    return ip;}