summaryrefslogtreecommitdiffstats
path: root/arts/modules/synth/synth_play_pat_impl.cpp
blob: 7c2699f7f7b5a09928dd421edb5acb2f88c25955 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#    /*

    Copyright (C) 2001 Stefan Westerfeld
                       stefan@space.twc.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#include "artsmodulessynth.h"
#include <sys/stat.h>
#include <stdsynthmodule.h>
#include <unistd.h>
#include <math.h>
#include <debug.h>
#include <cache.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

namespace Arts {

namespace PatchLoader {
	typedef unsigned char byte;
	typedef unsigned short int word;
	typedef unsigned int dword;
	typedef char sbyte;
	typedef short int sword;
	typedef int sdword;

	static int pos = 0;
	static int apos = 0;

	inline void xRead(FILE *file, int len, void *data)
	{
		//	printf("(0x%2x) - 0x%02x  ... reading %d bytes\n",apos,pos,len);
		pos += len;
		apos += len;
		if(fread(data, len, 1, file) != 1)
			fprintf(stdout, "short read\n");
	}

	inline void skip(FILE *file, int len)
	{
		//	printf("(0x%2x) - 0x%02x  ... skipping %d bytes\n",apos,pos,len);
		pos += len;
		apos += len;
		while(len > 0)
		{
			char junk;
			if(fread(&junk, 1, 1, file) != 1)
				fprintf(stdout, "short read\n");
			len--;
		}
	}


	inline void readBytes(FILE *file, unsigned char *bytes, int len)
	{
		xRead(file, len, bytes);
	}

	inline void readString(FILE *file, char *str, int len)
	{
		xRead(file, len, str);
	}

	/* readXXX with sizeof(xxx) == 1 */
	inline void readByte(FILE *file, byte& b)
	{
		xRead(file, 1, &b);
	}

	/* readXXX with sizeof(xxx) == 2 */
	inline void readWord(FILE *file, word& w)
	{
		byte h, l;
		xRead(file, 1, &l);
		xRead(file, 1, &h);
		w = (h << 8) + l;
	}

	inline void readSWord(FILE *file, sword& sw)
	{
		word w;
		readWord(file, w);
		sw = (sword)w;
	}

	/* readXXX with sizeof(xxx) == 4 */
	inline void readDWord(FILE *file, dword& dw)
	{
		byte h, l, hh, hl;
		xRead(file, 1, &l);
		xRead(file, 1, &h);
		xRead(file, 1, &hl);
		xRead(file, 1, &hh);
		dw = (hh << 24) + (hl << 16) + (h << 8) + l;
	}

	struct PatHeader {
		char id[12];					/* ID='GF1PATCH110' */
		char manufacturer_id[10];		/* Manufacturer ID */
		char description[60];			/* Description of the contained Instruments
										   or copyright of manufacturer. */
		byte instruments;				/* Number of instruments in this patch */
		byte voices;					/* Number of voices for sample */
		byte channels;					/* Number of output channels
										   (1=mono,2=stereo) */
		word waveforms;					/* Number of waveforms */
		word mastervolume;				/* Master volume for all samples */
		dword size;						/* Size of the following data */
		char reserved[36];				/* reserved */

		PatHeader(FILE *file)
		{
			readString(file, id, 12);
			readString(file, manufacturer_id, 10);
			readString(file, description, 60);
			/*		skip(file, 2);*/

			readByte(file, instruments);
			readByte(file, voices);
			readByte(file, channels);

			readWord(file, waveforms);
			readWord(file, mastervolume);
			readDWord(file, size);

			readString(file, reserved, 36);
		}
	};

	struct PatInstrument {
		word	number;
		char	name[16];
		dword 	size;					/* Size of the whole instrument in bytes. */
		byte	layers;
		char	reserved[40];

		/* layer? */
		word	layerUnknown;
		dword	layerSize;
		byte	sampleCount;			/* number of samples in this layer (?) */
		char	layerReserved[40];

		PatInstrument(FILE *file)
		{
			readWord(file, number);
			readString(file, name, 16);
			readDWord(file, size);
			readByte(file, layers);
			readString(file, reserved, 40);

			/* layer: (?) */
			readWord(file, layerUnknown);
			readDWord(file, layerSize);
			readByte(file, sampleCount);
			readString(file, reserved, 40);
		}
	};

	struct PatPatch {
		char	filename[7];			/* Wave file name */
		byte	fractions;				/* Fractions */
		dword	wavesize;				/* Wave size.
										   Size of the wave digital data */
		dword	loopStart;
		dword	loopEnd;
		word	sampleRate;
		dword	minFreq;
		dword	maxFreq;
		dword	origFreq;
		sword	fineTune;
		byte	balance;
		byte	filterRate[6];
		byte	filterOffset[6];
		byte	tremoloSweep;
		byte	tremoloRate;
		byte	tremoloDepth;
		byte	vibratoSweep;
		byte	vibratoRate;
		byte	vibratoDepth;
		byte	waveFormat;
		sword	freqScale;
		word	freqScaleFactor;
		char	reserved[36];

		PatPatch(FILE *file)
		{
			readString(file, filename, 7);
			readByte(file, fractions);
			readDWord(file, wavesize);
			readDWord(file, loopStart);
			readDWord(file, loopEnd);
			readWord(file, sampleRate);
			readDWord(file, minFreq);
			readDWord(file, maxFreq);
			readDWord(file, origFreq);
			readSWord(file, fineTune);
			readByte(file, balance);
			readBytes(file, filterRate, 6);
			readBytes(file, filterOffset, 6);
			readByte(file, tremoloSweep);
			readByte(file, tremoloRate);
			readByte(file, tremoloDepth);
			readByte(file, vibratoSweep);
			readByte(file, vibratoRate);
			readByte(file, vibratoDepth);
			readByte(file, waveFormat);
			readSWord(file, freqScale);
			readWord(file, freqScaleFactor);
			readString(file, reserved, 36);
		}
	};
}

class CachedPat : public CachedObject
{
protected:
	struct stat oldstat;
	string filename;
	bool initOk;
	long dataSize;

	CachedPat(Cache *cache, const string& filename);
	~CachedPat();

public:

	struct Data {
		PatchLoader::PatPatch patch;
		mcopbyte *rawdata;
		Data(FILE *file)
			: patch(file)
		{
			rawdata = new mcopbyte[patch.wavesize];
			fread(rawdata, 1, patch.wavesize, file);

			// sign conversion only for 16bit!
			if(patch.waveFormat & (1 << 1))
			{
				for(unsigned int i = 1; i < patch.wavesize; i+=2)
					rawdata[i] ^= 0x80;
			}

			// unfold ping-pong loops
			if(patch.waveFormat & (1 << 3))
			{
				int looplen = patch.loopEnd - patch.loopStart;
				arts_assert(looplen > 0);

				mcopbyte *newdata = new mcopbyte[patch.wavesize + looplen];

				// copy head
				memcpy(&newdata[0], &rawdata[0], patch.loopStart + looplen);

				// 16 bit unfolding only:
				for(int i=0; i<looplen; i+=2)
				{
					newdata[patch.loopStart+looplen+i] =
						newdata[patch.loopStart+looplen-i-2];
					newdata[patch.loopStart+looplen+i+1] =
						newdata[patch.loopStart+looplen-i-1];
				}

				// copy tail:
				memcpy(&newdata[patch.loopStart+2*looplen],
					   &rawdata[patch.loopStart+looplen],
					   patch.wavesize - patch.loopEnd);

				delete[] rawdata;
				rawdata = newdata;

				patch.wavesize += looplen;
				patch.loopEnd += looplen;
				patch.waveFormat &= ~(1 << 3);
			}
		}
		~Data()
		{
			delete[] rawdata;
		}
	};

	list<Data*> dList;

	static CachedPat *load(Cache *cache, const string& filename);
	/**
	 * validity test for the cache - returns false if the object is having
	 * reflecting the correct contents anymore (e.g. if the file on the
	 * disk has changed), and there is no point in keeping it in the cache any
	 * longer
	 */
	bool isValid();
	/**
	 * memory usage for the cache
	 */
	int memoryUsage();
};

CachedPat *CachedPat::load(Cache *cache, const string& filename)
{
	CachedPat *pat;

	pat = (CachedPat *)cache->get(string("CachedPat:")+filename);
	if(!pat) {
		pat = new CachedPat(cache, filename);

		if(!pat->initOk)		// loading failed
		{
			pat->decRef();
			return 0;
		}
	}

	return pat;
}

bool CachedPat::isValid()
{
	if(!initOk)
		return false;

	struct stat newstat;

	lstat(filename.c_str(),&newstat);
	return(newstat.st_mtime == oldstat.st_mtime);
}

int CachedPat::memoryUsage()
{
	return dataSize;
}

CachedPat::CachedPat(Cache *cache, const string& filename)
	: CachedObject(cache), filename(filename), initOk(false), dataSize(0)
{
	setKey(string("CachedPat:")+filename);

	if(lstat(filename.c_str(),&oldstat) == -1)
	{
		arts_info("CachedPat: Can't stat file '%s'", filename.c_str());
		return;
	}

	FILE *patfile = fopen(filename.c_str(), "r");
	if(patfile)
	{
		//PatchLoader::PatHeader header(patfile);
		PatchLoader::PatInstrument ins(patfile);

		for(int i=0;i<ins.sampleCount;i++)
		{
			Data *data = new Data(patfile);
			dList.push_back(data);
			dataSize += data->patch.wavesize;
		}
		fclose(patfile);

		arts_debug("loaded pat %s",filename.c_str());
		arts_debug("  %d patches, datasize total is %d bytes",
											ins.sampleCount, dataSize);

		initOk = true;
	}
}

CachedPat::~CachedPat()
{
	while(!dList.empty())
	{
		delete dList.front();
		dList.pop_front();
	}
}

class Synth_PLAY_PAT_impl : virtual public Synth_PLAY_PAT_skel,
							virtual public StdSynthModule
{
protected:
	string _filename;
	CachedPat *pat;
	CachedPat::Data *selected;
	float fpos;

public:
	Synth_PLAY_PAT_impl()
	{
		pat = 0;
		selected = 0;
	}

	void unload()
	{
		if(pat)
		{
			pat->decRef();
			pat = 0;
		}
	}

	~Synth_PLAY_PAT_impl()
	{
		unload();
	}

	void streamStart()
	{
		fpos = 0.0;
		selected = 0;
	}
	
	void calculateBlock(unsigned long samples)
	{
		/* the normal offset is 60
		   60 = 12*5
		   so we scale with 2^5 = 32
		 */
		float freq = frequency[0]; /* * 32.0 / 2.0; * why /2.0? */
		int ifreq = (int)(freq * 1024.0);
		if(!selected && pat)
		{
			int bestdiff = 20000 * 1024;

			list<CachedPat::Data*>::iterator i;
			for(i = pat->dList.begin(); i != pat->dList.end(); i++)
			{
				int diff = ::abs(double(ifreq - (*i)->patch.origFreq));
				if(diff < bestdiff)
				{
					selected = *i;
					bestdiff = diff;
				}
			}

			/* drums */
			if(selected && selected->patch.freqScaleFactor == 0)
				ifreq = selected->patch.origFreq;
		}
		if(selected)
		{
			const PatchLoader::PatPatch& patch = selected->patch;

			/*
			 * thats just a *guess*, I have no idea how tuning actually
			 * should work
			 */
#if 0
			float tonetune = float(pat.fineTune)/float(pat.freqScaleFactor);
			float tuning = pow(2.0, tonetune/12.0);
			freq *= tuning;
#endif
			//printf("tonetune: %f\n",tonetune);
			//printf("tuning: %f\n",tuning);

			float step = double(patch.sampleRate)/samplingRateFloat *
				  		 float(ifreq) / float(patch.origFreq);
			for(unsigned int i = 0; i < samples; i++)
			{
				// ASSUME 16bit signed, native byte order
				int ifpos = int(fpos)*2;

				//                     looped?      bidi?     backw?
				if((patch.waveFormat & ((1 << 2) + (1 << 3) + (1 << 4)))
							== (1 << 2))
				{
					while(ifpos >= signed(patch.loopEnd))
					{
						ifpos -= (patch.loopEnd - patch.loopStart);
						fpos -= (patch.loopEnd - patch.loopStart)/2;
					}
				}
				
				short int *x = (short int *)&selected->rawdata[ifpos];
				float sample = (ifpos >= 0 && ifpos < signed(patch.wavesize))?
										x[0]/32768.0:0.0;
				float sample1 = ((ifpos+2) >= 0 && (ifpos+2) < signed(patch.wavesize))?
										x[1]/32768.0:0.0;
				float error = fpos - (int)fpos;
				outvalue[i] = sample * (1.0-error) + sample1 * error;

				fpos += step;
			}
		}
		else
		{
			for(unsigned int i = 0; i < samples; i++)
				outvalue[i] = 0.0;
		}
	}

	void clearDList()
	{
		selected = 0;

	}

	string filename() { return _filename; }
	void filename(const string& newFile)
	{
		if(newFile == _filename) return;
	
		unload();
		pat = CachedPat::load(Cache::the(), newFile);

		_filename = newFile;
		filename_changed(newFile);
	}
};

REGISTER_IMPLEMENTATION(Synth_PLAY_PAT_impl);

}