if (opt_pure_intonation) { if (current_keysig < 8) f = freq_table_pureint[current_freq_table][note]; else f = freq_table_pureint[current_freq_table + 12][note]; } else if (opt_temper_control) switch (tt) { case 0: f = freq_table_tuning[tp][note]; break; case 1: if (current_temper_keysig < 8) f = freq_table_pytha[current_freq_table][note];
if (ISDRUMCHANNEL(ch)) { f = fs = freq_table[note]; } else { if (opt_pure_intonation) { if (current_keysig < 8) f = freq_table_pureint[current_freq_table][note];
static int select_play_sample(Sample *splist,		int nsp, int note, int *vlist, MidiEvent *e){	int ch = e->channel, kn = e->a & 0x7f, vel = e->b;	int32 f, fs, ft, fst, fc, fr, cdiff, diff, sample_link;	int8 tt = channel[ch].temper_type;	uint8 tp = channel[ch].rpnmap[RPN_ADDR_0003];	Sample *sp, *spc, *spr;	int16 st;	double ratio;	int i, j, k, nv, nvc;		if (opt_pure_intonation) {		if (current_keysig < 8)			f = freq_table_pureint[current_freq_table][note];		else			f = freq_table_pureint[current_freq_table + 12][note];	} else if (opt_temper_control)		switch (tt) {		case 0:			f = freq_table_tuning[tp][note];			break;		case 1:			if (current_temper_keysig < 8)				f = freq_table_pytha[current_freq_table][note];			else				f = freq_table_pytha[current_freq_table + 12][note];			break;		case 2:			if (current_temper_keysig < 8)				f = freq_table_meantone[current_freq_table						+ ((temper_adj) ? 36 : 0)][note];			else				f = freq_table_meantone[current_freq_table						+ ((temper_adj) ? 24 : 12)][note];			break;		case 3:			if (current_temper_keysig < 8)				f = freq_table_pureint[current_freq_table						+ ((temper_adj) ? 36 : 0)][note];			else				f = freq_table_pureint[current_freq_table						+ ((temper_adj) ? 24 : 12)][note];			break;		default:	/* user-defined temperament */			if ((tt -= 0x40) >= 0 && tt < 4) {				if (current_temper_keysig < 8)					f = freq_table_user[tt][current_freq_table							+ ((temper_adj) ? 36 : 0)][note];				else					f = freq_table_user[tt][current_freq_table							+ ((temper_adj) ? 24 : 12)][note];			} else				f = freq_table[note];			break;		}	else		f = freq_table[note];	if (opt_temper_control)		fs = (tt) ? freq_table[note] : freq_table_tuning[tp][note];	else		fs = freq_table[note];	nv = 0;	for (i = 0, sp = splist; i < nsp; i++, sp++) {		/* SF2 - Scale Tuning */		if ((st = sp->scale_tuning) != 100) {			ratio = pow((double) f / sp->root_freq, st / 100.0);			ft = sp->root_freq * ratio + 0.5;			ratio = pow((double) fs / sp->root_freq, st / 100.0);			fst = sp->root_freq * ratio + 0.5;		} else			ft = f, fst = fs;		if ((ratio = get_play_note_ratio(ch, kn)) != 1.0)			ft = ft * ratio + 0.5, fst = fst * ratio + 0.5;		if (sp->low_freq <= fst && sp->high_freq >= fst				&& sp->low_vel <= vel && sp->high_vel >= vel				&& ! (sp->inst_type == INST_SF2				&& sp->sample_type == SF_SAMPLETYPE_RIGHT)) {			j = vlist[nv] = find_voice(e);			voice[j].orig_frequency = ft;			MYCHECK(voice[j].orig_frequency);			voice[j].sample = sp;			voice[j].status = VOICE_ON;			nv++;		}	}	if (nv == 0) {	/* we must select at least one sample. */		cdiff = 0x7fffffff;		fr = fc = 0;		spc = spr = NULL;		for (i = 0, sp = splist; i < nsp; i++, sp++) {			/* SF2 - Scale Tuning */			if ((st = sp->scale_tuning) != 100) {				ratio = pow((double) f / sp->root_freq, st / 100.0);				ft = sp->root_freq * ratio + 0.5;				ratio = pow((double) fs / sp->root_freq, st / 100.0);				fst = sp->root_freq * ratio + 0.5;			} else				ft = f, fst = fs;			if ((ratio = get_play_note_ratio(ch, kn)) != 1.0)				ft = ft * ratio + 0.5, fst = fst * ratio + 0.5;			diff = abs(sp->root_freq - fst);			if (cdiff > diff) {				if (sp->inst_type == INST_SF2						&& sp->sample_type == SF_SAMPLETYPE_RIGHT) {					fr = ft;	/* reserve */					spr = sp;	/* reserve */				} else {					cdiff = diff;					fc = ft;					spc = sp;				}			}		}		if (spc != NULL) {	/* a makeshift sample is found. */			j = vlist[nv] = find_voice(e);			voice[j].orig_frequency = fc;			MYCHECK(voice[j].orig_frequency);			voice[j].sample = spc;			voice[j].status = VOICE_ON;			nv++;		} else {			/* it's a lonely right sample, but better than nothing. */			j = vlist[nv] = find_voice(e);			voice[j].orig_frequency = fr;			MYCHECK(voice[j].orig_frequency);			voice[j].sample = spr;			voice[j].status = VOICE_ON;			nv++;		}	}	nvc = nv;	for (i = 0; i < nvc; i++) {		spc = voice[vlist[i]].sample;		if (spc->inst_type == INST_SF2				&& spc->sample_type == SF_SAMPLETYPE_LEFT) {			/* If it's left sample, there must be right sample. */			sample_link = spc->sf_sample_link;			for (j = 0, sp = splist; j < nsp; j++, sp++)				if (sp->inst_type == INST_SF2						&& sp->sample_type == SF_SAMPLETYPE_RIGHT						&& sp->sf_sample_index == sample_link) {					/* right sample is found. */					/* SF2 - Scale Tuning */					if ((st = sp->scale_tuning) != 100) {						ratio = pow((double) f / sp->root_freq, st / 100.0);						ft = sp->root_freq * ratio + 0.5;					} else						ft = f;					if ((ratio = get_play_note_ratio(ch, kn)) != 1.0)						ft = ft * ratio + 0.5;					k = vlist[nv] = find_voice(e);					voice[k].orig_frequency = ft;					MYCHECK(voice[k].orig_frequency);					voice[k].sample = sp;					voice[k].status = VOICE_ON;					nv++;					break;				}		}	}	return nv;}