Beispiel #1
0
static bool load_drumtable(const char *pxt_path)		// pxt_path = the path where drum pxt files can be found
{
char fname[80];
int d;
FILE *fp;
static const char *drum_cache = "drum.pcm";

	#ifndef DRUM_PXT
		for(d=0;d<NUM_DRUMS;d++)
		{
			sprintf(fname, "./drums/%s.wav", drum_names[d]);
			if (load_drum(fname, d)) return 1;
		}
	#else
		
		// try and load the drums from cache instead of synthing them
		fp = fileopen(drum_cache, "rb");
		if (fp)
		{
			for(d=0;d<NUM_DRUMS;d++)
			{
				drumtable[d].nsamples = fgetl(fp);
				drumtable[d].samples = (signed short *)malloc(drumtable[d].nsamples * 2);
				fread(drumtable[d].samples, drumtable[d].nsamples*2, 1, fp);
			}
			fclose(fp);
			stat("-- Drums loaded from cache");
			return 0;
		}
		
		stat("load_drumtable: cache gone; rebuilding drums...");
		
		pxt_initsynth();
		
		for(d=0;d<NUM_DRUMS;d++)
		{
			if (drum_pxt[d])
			{
				sprintf(fname, "%sfx%02x.pxt", pxt_path, drum_pxt[d]);
				if (load_drum_pxt(fname, d)) return 1;
			}
		}
		
		// cache the drums for next time
		fp = fileopen(drum_cache, "wb");
		if (fp)
		{
			for(d=0;d<NUM_DRUMS;d++)
			{
				fputl(drumtable[d].nsamples, fp);
				fwrite(drumtable[d].samples, drumtable[d].nsamples*2, 1, fp);
			}
			fclose(fp);
		}
		
		load_drumtable(pxt_path);
	#endif
	
	//for(d=0;d<256;d++) { lprintf("%d ", drumtable[0].samples[d]); if (d%32==0) lprintf("\n"); }
	//lprintf("\n");
	
	return 0;
}
Beispiel #2
0
static bool load_drumtable(const char *pxt_path) {		// pxt_path = the path where drum pxt files can be found
	char fname[80];
	int d;
	FILE *fp;
	static const char *drum_cache = "drum.pcm";
#define DRUM_VERSION	0x0001
	uint16_t version;
	unsigned long error;

	#ifndef DRUM_PXT
		for(d=0;d<NUM_DRUMS;d++)
		{
			sprintf(fname, "./drums/%s.wav", drum_names[d]);
			if (load_drum(fname, d)) return 1;
		}
	#else
		
		// try and load the drums from cache instead of synthing them
		fp = fileopen(drum_cache, "rb");
		if (fp)
		{
			// this also checks for correct endianness
			error = fread(&version, sizeof(version), 1, fp);
			if (error != 1 ) {
				printf("org.cpp: expected to read 1 version, but read %lu\n",error);
			}
			if (version != DRUM_VERSION)
			{
				printf("%s: version incorrect\n", drum_cache);
			}
			else
			{
				for(d=0;d<NUM_DRUMS;d++)
				{
					drumtable[d].nsamples = fgetl(fp);
					drumtable[d].samples = (signed short *)malloc(drumtable[d].nsamples * 2);
					error = fread(drumtable[d].samples, drumtable[d].nsamples*2, 1, fp);
					if(error != 1) {
						printf("org.cpp: expected to read 1 drumtable sample, but read %lu\n",error);
					}
				}
				fclose(fp);
				stat("-- Drums loaded from cache");
				return 0;
			}
		}
		
		stat("load_drumtable: cache gone; rebuilding drums...");
		
		pxt_initsynth();
		
		for(d=0;d<NUM_DRUMS;d++)
		{
			if (drum_pxt[d])
			{
				sprintf(fname, "%sfx%02x.pxt", pxt_path, drum_pxt[d]);
				if (load_drum_pxt(fname, d)) return 1;
			}
		}
		
		// cache the drums for next time
		fp = fileopen(drum_cache, "wb");
		if (fp)
		{
			version = DRUM_VERSION;
			fwrite(&version, sizeof(version), 1, fp);
			for(d=0;d<NUM_DRUMS;d++)
			{
				fputl(drumtable[d].nsamples, fp);
				fwrite(drumtable[d].samples, drumtable[d].nsamples*2, 1, fp);
			}
			fclose(fp);
		}
		
		load_drumtable(pxt_path);
	#endif
	
	//for(d=0;d<256;d++) { lprintf("%d ", drumtable[0].samples[d]); if (d%32==0) lprintf("\n"); }
	//lprintf("\n");
	
	return 0;
}