type_size = 8 + (chunk_size & 1); 
static int import_wave_load(char *sample_file, Instrument *inst){	struct timidity_file	*tf;	char			buf[12];	int				state;		/* initial > fmt_read > data_read */	int				i, chunk_size, type_index, samples;	int32			chunk_flags;	Sample			*sample;	WAVFormatChunk	format;	WAVSamplerChunk	samplerc;	GeneralInstrumentInfo	instc;		if ((tf = open_file(sample_file, 1, OF_NORMAL)) == NULL)		return 1;	if (tf_read(buf, 12, 1, tf) != 1			|| memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0)	{		close_file(tf);		return 1;	}	ctl->cmsg(CMSG_INFO, VERB_NOISY, "Loading WAV: %s", sample_file);	state = chunk_flags = 0;	type_index = 4;	for(;;) {		if (tf_read(&buf[type_index], 8, 1, tf) != 1)			break;		chunk_size = LE_LONG(*(int32 *)&buf[4 + 4]);		if (memcmp(&buf[4 + 0], "fmt ", 4) == 0)		{			if (state != 0					/* only one format chunk is required */					|| chunk_size < 0x10)	/* too small */				break;			if (!read_WAVFormatChunk(tf, &format, chunk_size))				break;			if (format.wChannels < 1				/* invalid range */					|| format.wChannels > MAX_SAMPLE_CHANNELS					|| format.wFormatTag != 1		/* compressed */					|| format.wBitsPerSample & 0x7	/* padding not supported */					|| format.wBitsPerSample > 16)	/* more than 16-bit is not supported */				break;			state++;		}		else if (memcmp(&buf[4 + 0], "data", 4) == 0)		{			int				frames;			sample_t		*sdata[MAX_SAMPLE_CHANNELS];						if (state != 1)				break;			frames = chunk_size / format.wBlockAlign;			inst->samples = samples = format.wChannels;			inst->sample = (Sample *)safe_malloc(sizeof(Sample) * samples);			ctl->cmsg(CMSG_INFO, VERB_NOISY, "Format: %d-bits %dHz %dch, %d frames",					format.wBitsPerSample, format.dwSamplesPerSec, samples, frames);			initialize_sample(inst, frames, format.wBitsPerSample, format.dwSamplesPerSec);			/* load waveform data */			#ifdef LOOKUP_HACK				#error "not implemented"			#endif			for(i = 0; i < samples; i++)			{				inst->sample[i].data = sdata[i] = (sample_t *)safe_malloc(sizeof(sample_t) * frames);				inst->sample[i].data_alloced = 1;			}			if (!read_sample_data(SAMPLE_8BIT_UNSIGNED, tf, format.wBitsPerSample, samples, frames, sdata))				break;			state++;		}		else if (!(chunk_flags & WAVE_CHUNKFLAG_SAMPLER) && memcmp(&buf[4 + 0], "smpl", 4) == 0)		{			if (!read_WAVSamplerChunk(tf, &samplerc, chunk_size))				break;			chunk_flags |= WAVE_CHUNKFLAG_SAMPLER;		}		else if (!(chunk_flags & WAVE_CHUNKFLAG_INSTRUMENT) && memcmp(&buf[4 + 0], "inst", 4) == 0)		{			if (!read_WAVInstrumentChunk(tf, &instc, chunk_size))				break;			chunk_flags |= WAVE_CHUNKFLAG_INSTRUMENT;		}		else if (tf_seek(tf, chunk_size, SEEK_CUR) == -1)			break;		type_index = 4 - (chunk_size & 1);	}	close_file(tf);	if (chunk_flags & WAVE_CHUNKFLAG_SAMPLER)	{		uint8		modes;		int32		sample_rate, root_freq;		uint32		loopStart, loopEnd;				sample_rate = 1000000000 / samplerc.dwSamplePeriod;		root_freq = freq_table[samplerc.dwMIDIUnityNote];		if (samplerc.dwMIDIPitchFraction != 0				&& samplerc.dwMIDIUnityNote != 127)	/* no table data */		{			int32		diff;						diff = freq_table[samplerc.dwMIDIUnityNote + 1] - root_freq;			root_freq += (float)samplerc.dwMIDIPitchFraction * diff / 0xFFFFFFFF;		}		if (samplerc.hasLoop)		{			const uint8		loopModes[] = {MODES_LOOPING, MODES_LOOPING | MODES_PINGPONG, MODES_LOOPING | MODES_REVERSE};						modes = loopModes[samplerc.loopType];			loopStart = samplerc.loop_dwStart << FRACTION_BITS;			loopEnd = samplerc.loop_dwEnd << FRACTION_BITS;		}		else			modes = 0;		for(i = 0; i < samples; i++)		{			sample = &inst->sample[i];			sample->sample_rate = sample_rate;			sample->root_freq = root_freq;			if (modes != 0)			{				sample->loop_start = loopStart;				sample->loop_end = loopEnd;			}			sample->modes |= modes;		}	}	if (chunk_flags & WAVE_CHUNKFLAG_INSTRUMENT)		apply_GeneralInstrumentInfo(samples, inst->sample, &instc);	return (state != 2);}
4357	/local/tlutelli/issta_data/temp/c/2005_temp/2005/4357/f07b17d6cb813000cb5ad3c2502ea8726afeac01/smplfile.c/buggy/timidity/smplfile.c