summaryrefslogtreecommitdiff
path: root/jeu-test/Lemmini/0.84/src/micromod/Micromod.java
blob: 45e52d6547d7c40bcfceca606c3cbc36bd759572 (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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package micromod;

/**
 * micromod/e fast module player (rev b)
 */
public class Micromod {
	private final int
	FP_SHIFT = 13,
	FP_ONE   = 1 << FP_SHIFT,
	IN_STRUCT_LEN   = 0x05,
	IN_SAMPLE_INDEX = 0x00,
	IN_LOOP_START   = 0x01,
	IN_LOOP_END     = 0x02,
	IN_VOLUME       = 0x03,
	IN_FINETUNE     = 0x04,
	CH_STRUCT_LEN   = 0x16,
	CH_SPOS         = 0x00,
	CH_STEP         = 0x01,
	CH_AMPL         = 0x02,
	CH_INSTRUMENT   = 0x03,
	CH_ASSIGNED     = 0x04,
	CH_VOLUME       = 0x05,
	CH_FINETUNE     = 0x06,
	CH_PERIOD       = 0x07,
	CH_PORTA_PERIOD = 0x08,
	CH_PORTA_PARAM  = 0x09,
	CH_PANNING      = 0x0A,
	CH_ARPEGGIO     = 0x0B,
	CH_VIBR_PERIOD  = 0x0C,
	CH_VIBR_PARAM   = 0x0D,
	CH_VIBR_COUNT   = 0x0E,
	CH_TREM_VOLUME  = 0x0F,
	CH_TREM_PARAM   = 0x10,
	CH_PAT_LOOP_ROW = 0x11,
	CH_NOTE_PERIOD  = 0x12,
	CH_NOTE_INSTRU  = 0x13,
	CH_NOTE_EFFECT  = 0x14,
	CH_NOTE_EPARAM  = 0x15,
	FX_ARPEGGIO     = 0x00,
	FX_PORTA_UP     = 0x01,
	FX_PORTA_DOWN   = 0x02,
	FX_TONE_PORTA   = 0x03,
	FX_VIBRATO      = 0x04,
	FX_TPORTA_VOL   = 0x05,
	FX_VIBRATO_VOL  = 0x06,
	FX_TREMOLO      = 0x07,
	FX_SET_PANNING  = 0x08,
	FX_SET_SPOS     = 0x09,
	FX_VOLUME_SLIDE = 0x0A,
	FX_PAT_JUMP     = 0x0B,
	FX_SET_VOLUME   = 0x0C,
	FX_PAT_BREAK    = 0x0D,
	FX_EXTENDED     = 0x0E,
	FX_SET_SPEED    = 0x0F,
	EX_FINE_PORT_UP = 0x10,
	EX_FINE_PORT_DN = 0x20,
	EX_SET_GLISS    = 0x30,
	EX_SET_VIBR_WAV = 0x40,
	EX_SET_FINETUNE = 0x50,
	EX_PAT_LOOP     = 0x60,
	EX_SET_TREM_WAV = 0x70,
	EX_SET_PANNING  = 0x80,
	EX_RETRIG       = 0x90,
	EX_FINE_VOL_UP  = 0xA0,
	EX_FINE_VOL_DN  = 0xB0,
	EX_NOTE_CUT     = 0xC0,
	EX_NOTE_DELAY   = 0xD0,
	EX_PAT_DELAY    = 0xE0,
	EX_INVERT_LOOP  = 0xF0;

	private final int[] arptable = new int[] {
			8192,  8679,  9195,  9742, 10321, 10935, 11585, 12274,
			13004, 13777, 14596, 15464, 16384, 17358, 18390, 19484
	};

	private final int[] fttable = new int[] {
			15464, 15576, 15689, 15803, 15918, 16033, 16149, 16266,
			16384, 16503, 16622, 16743, 16864, 16986, 17109, 17233
	};

	private final int[] sintable = new int[] {
			0, 24 ,  49,  74,  97, 120, 141, 161, 180, 197, 212, 224, 235, 244, 250, 253,
			255, 253, 250, 244, 235, 224, 212, 197, 180, 161, 141, 120,  97,  74,  49,  24
	};

	private byte[] mod;
	private boolean amiga;
	private int numchan, songlen, restart;
	private int pat, npat, row, nrow, tick, tempo, bpm;
	private int fcount, loopcount, loopchan;
	private int[] instruments = new int[ IN_STRUCT_LEN * 32 ];
	private int[] channels    = new int[ CH_STRUCT_LEN * 32 ];
	private int samplerate, tickremain;

	/* constructor ( mod - module data ) */
	/**
	 * Constructor
	 * @param mod byte buffer containing MOD data
	 * @param samplerate sample rate in Hertz
	 */
	public Micromod( final byte[] mod, final int samplerate ) {
		this.mod = mod;
		this.samplerate = samplerate;
		//System.out.println( "micromod/e (c)2005 mumart@gmail.com" );
		songlen = mod[ 950 ] & 0x7F;
		restart = mod[ 951 ] & 0x7F;
		if( restart >= songlen ) restart = 0;
		int numpatterns = 0;
		for( int n = 0; n < 128; n++ ) {
			int pat = mod[ 952 + n ] & 0x7F;
			if( pat >= numpatterns ) numpatterns = pat + 1;
		}
		switch( ( mod[ 1082 ] << 8 ) | mod[ 1083 ] ) {
			case 0x4b2e: // M.K.
			case 0x4b21: // M!K!
			case 0x5434: // FLT4
				numchan = 4;
				amiga = true;
				break;
			case 0x484e: // xCHN
				numchan = mod[ 1080 ] - 48;
				amiga = false;
				break;
			case 0x4348: // xxCH
				numchan = ( ( mod[ 1080 ] - 48 ) * 10 ) + ( mod[ 1081 ] - 48 );
				amiga = false;
				break;
			default:
				throw new IllegalArgumentException( "MOD Format not recognised!" );
		}
		int sampleidx = 1084 + 4 * numchan * 64 * numpatterns;
		for( int inst = 0; inst < 31; inst++ ) {
			int slen = ushortbe( mod, inst * 30 + 42 ) << 1;
			int fine = mod[ inst * 30 + 44 ] & 0xF;
			if( fine > 7 ) fine -= 16;
			int vol  = mod[ inst * 30 + 45 ] & 0x7F;
			if( vol > 64 ) vol = 64;
			int lsta = ushortbe( mod, inst * 30 + 46 ) << 1;
			int llen = ushortbe( mod, inst * 30 + 48 ) << 1;
			if( sampleidx + slen - 1 >= mod.length ) {
				System.out.println( "Module is truncated!" );
				slen = mod.length - sampleidx;
				if( slen < 0 ) slen = 0;
			}
			if( llen < 4 || lsta >= slen ) {
				lsta = slen - 1;
				llen = 1;
			}
			int lend = lsta + llen - 1;
			if( lend >= slen ) lend = slen - 1;
			int ioffset = ( inst + 1 ) * IN_STRUCT_LEN;
			instruments[ ioffset + IN_SAMPLE_INDEX ] = sampleidx;
			instruments[ ioffset + IN_LOOP_START ] = lsta;
			instruments[ ioffset + IN_LOOP_END ] = lend;
			instruments[ ioffset + IN_FINETUNE ] = fine;
			instruments[ ioffset + IN_VOLUME ] = vol;
			sampleidx += slen;
		}
		reset();
	}

	/**
	 * return the song length in samples
	 * @return song length in samples
	 */
	public int getlen() {
		reset();
		int len = getticklen();
		while( !tick() ) len += getticklen();
		reset();
		return len;
	}

	/**
	 * Mix 16 bit stereo audio into the buffers.
	 * @param l left
	 * @param r right
	 * @param ofs offset
	 * @param length length
	 */
	public void mix( final int[] l, final int[] r, final int ofs, final int length ) {
		int len = length;
		int offset = ofs;
		while( len > 0 ) {
			int count = tickremain;
			if( count > len ) count = len;
			for( int chan = 0; chan < numchan ; chan++ ) {
				int coffset = chan * CH_STRUCT_LEN;
				int ampl = channels[ coffset + CH_AMPL ];
				int pann = channels[ coffset + CH_PANNING ] << FP_SHIFT - 8;
				int lamp = ampl * ( FP_ONE - pann ) >> FP_SHIFT;
				int ramp = ampl * pann >> FP_SHIFT;
			int inst = channels[ coffset + CH_INSTRUMENT ];
			int ioffset = inst * IN_STRUCT_LEN;
			int sidx = instruments[ ioffset + IN_SAMPLE_INDEX ];
			int lsta = instruments[ ioffset + IN_LOOP_START ] << FP_SHIFT;
			int lep1 = instruments[ ioffset + IN_LOOP_END ] + 1 << FP_SHIFT;
			int spos = channels[ coffset + CH_SPOS ];
			int step = channels[ coffset + CH_STEP ];
			int llen = lep1 - lsta;
			boolean dontmix = llen <= FP_ONE && spos >= lsta;
			if( !dontmix ) for( int x = 0; x < count; x++ ) {
				while( spos >= lep1 ) spos -= llen;
				int sample = mod[ sidx + ( spos >> FP_SHIFT ) ] << 8;
				l[ offset + x ] += sample * lamp >> FP_SHIFT;
				r[ offset + x ] += sample * ramp >> FP_SHIFT;
			spos += step;
			}
			channels[ coffset + CH_SPOS ] = spos;
			}
			tickremain -= count;
			if( tickremain == 0 ) {
				tick();
				tickremain = getticklen();
			}
			offset += count;
			len -= count;
		}
	}

	private void reset()  {
		pat = npat = 0;
		row = nrow = 0;
		tick = tempo = 6;
		bpm = 125;
		loopcount = loopchan = 0;
		for( int n = 0; n < channels.length; n++ ) channels[ n ] = 0;
		for( int chan = 0; chan < numchan; chan++ ) {
			int p = 128;
			switch( chan & 0x3 ) {
				case 0: p = 64; break;
				case 1: p = 192; break;
				case 2: p = 192; break;
				case 3: p = 64; break;
			}
			channels[ chan * CH_STRUCT_LEN + CH_PANNING ] = p;
		}
		row();
		tickremain = getticklen();
	}

	private boolean tick() {
		tick--;
		if( tick <= 0 ) {
			tick = tempo;
			return row();
		}
		// Update channel fx
		for( int chan = 0; chan < numchan; chan++ ) {
			int coffset = chan * CH_STRUCT_LEN;
			int effect = channels[ coffset + CH_NOTE_EFFECT ];
			int eparam = channels[ coffset + CH_NOTE_EPARAM ];
			switch( effect ) {
				case FX_ARPEGGIO:
					switch( fcount % 3 ) {
						case 0: channels[ coffset + CH_ARPEGGIO ] = 0; break;
						case 1: channels[ coffset + CH_ARPEGGIO ] = ( eparam & 0xF0 ) >> 4; break;
		case 2: channels[ coffset + CH_ARPEGGIO ] = eparam & 0x0F; break;
					}
					break;
				case FX_PORTA_UP:
					channels[ coffset + CH_PERIOD ] -= eparam;
					break;
				case FX_PORTA_DOWN:
					channels[ coffset + CH_PERIOD ] += eparam;
					break;
				case FX_TONE_PORTA:
					toneporta( coffset );
					break;
				case FX_VIBRATO:
					vibrato( coffset );
					break;
				case FX_TPORTA_VOL:
					volslide( coffset, eparam );
					toneporta( coffset );
					break;
				case FX_VIBRATO_VOL:
					volslide( coffset, eparam );
					vibrato( coffset );
					break;
				case FX_TREMOLO:
					tremolo( coffset );
					break;
				case FX_VOLUME_SLIDE:
					volslide( coffset, eparam );
					break;
				case FX_EXTENDED:
					switch( eparam & 0xF0 ) {
						case EX_RETRIG:
							int rtparam = eparam & 0x0F;
							if( rtparam == 0 ) rtparam = 1;
							if( fcount % rtparam == 0 ) channels[ coffset + CH_SPOS ] = 0;
							break;
						case EX_NOTE_CUT:
							if( ( eparam & 0x0F ) == fcount ) channels[ coffset + CH_VOLUME ] = 0;
							break;
						case EX_NOTE_DELAY:
							if( ( eparam & 0x0F ) == fcount ) trigger( coffset );
							break;
					}
					break;
			}
			channels[ coffset + CH_VIBR_COUNT ]++;
		}
		mixupdate();
		fcount++;
		return false;
	}

	private boolean row() {
		// Decide whether to restart.
		boolean songend = false;
		if( npat < pat ) songend = true;
		if( npat == pat && nrow <= row && loopcount <= 0 ) songend = true;
		// Jump to next row
		pat = npat;
		row = nrow;
		// Decide next row.
		nrow = row + 1;
		if( nrow == 64 ) {
			npat = pat + 1;
			nrow = 0;
		}
		// Load channels and process fx
		fcount = 0;
		int poffset = mod[ 952 + pat ] & 0x7F;
		int roffset = 1084 + ( poffset * 64 * numchan * 4 ) + ( row * numchan * 4 );
		for( int chan = 0; chan < numchan; chan++ ) {
			int coffset = chan * CH_STRUCT_LEN;
			int noffset = roffset + ( chan * 4 );
			channels[ coffset + CH_NOTE_PERIOD ] = ( mod[ noffset + 1 ] & 0xFF ) | ( ( mod[ noffset ] & 0x0F ) << 8 );
			channels[ coffset + CH_NOTE_INSTRU ] = ( ( mod[ noffset + 2 ] & 0xF0 ) >> 4 ) | ( mod[ noffset ] & 0x10 );
			channels[ coffset + CH_NOTE_EFFECT ] = mod[ noffset + 2 ] & 0x0F;
			channels[ coffset + CH_NOTE_EPARAM ] = mod[ noffset + 3 ] & 0xFF;
			int effect = channels[ coffset + CH_NOTE_EFFECT ];
			int eparam = channels[ coffset + CH_NOTE_EPARAM ];
			if( !( effect == FX_EXTENDED && ( ( eparam & 0xF0 ) == EX_NOTE_DELAY ) ) ) trigger( coffset );
			channels[ coffset + CH_ARPEGGIO ] = 0;
			channels[ coffset + CH_VIBR_PERIOD ] = 0;
			channels[ coffset + CH_TREM_VOLUME ] = 0;
			switch( effect ) {
				case FX_ARPEGGIO:
					break;
				case FX_PORTA_UP:
					break;
				case FX_PORTA_DOWN:
					break;
				case FX_TONE_PORTA:
					if( eparam != 0 ) channels[ coffset + CH_PORTA_PARAM ] = eparam;
					break;
				case FX_VIBRATO:
					if( eparam != 0 ) channels[ coffset + CH_VIBR_PARAM ] = eparam;
					vibrato( coffset );
					break;
				case FX_TPORTA_VOL:
					break;
				case FX_VIBRATO_VOL:
					vibrato( coffset );
					break;
				case FX_TREMOLO:
					if( eparam != 0 ) channels[ coffset + CH_TREM_PARAM ] = eparam;
					tremolo( coffset );
					break;
				case FX_SET_PANNING:
					if( !amiga ) channels[ coffset + CH_PANNING ] = eparam;
					break;
				case FX_SET_SPOS:
					channels[ coffset + CH_SPOS ] = eparam << FP_SHIFT + 8;
					break;
				case FX_VOLUME_SLIDE:
					break;
				case FX_PAT_JUMP:
					if( loopcount <= 0 ) {
						npat = eparam;
						nrow = 0;
					}
					break;
				case FX_SET_VOLUME:
					channels[ coffset + CH_VOLUME ] = ( eparam > 64 ) ? 64 : eparam;
					break;
				case FX_PAT_BREAK:
					if( loopcount <= 0 ) {
						npat = pat + 1;
						nrow = ( ( eparam & 0xF0 ) >> 4 ) * 10 + ( eparam & 0x0F );
					}
					break;
				case FX_EXTENDED:
					switch( eparam & 0xF0 ) {
						case EX_FINE_PORT_UP:
							channels[ coffset + CH_PERIOD ] -= ( eparam & 0x0F );
							break;
						case EX_FINE_PORT_DN:
							channels[ coffset + CH_PERIOD ] += ( eparam & 0x0F );
							break;
						case EX_SET_GLISS:
							break;
						case EX_SET_VIBR_WAV:
							break;
						case EX_SET_FINETUNE:
							int ftval = eparam & 0x0F;
							if( ftval > 7 ) ftval -= 16;
							channels[ coffset + CH_FINETUNE ] = ftval;
							break;
						case EX_PAT_LOOP:
							int plparam = eparam & 0x0F;
							if( plparam == 0 ) channels[ coffset + CH_PAT_LOOP_ROW ] = row;
							if( plparam > 0 && channels[ coffset + CH_PAT_LOOP_ROW ] < row ) {
								if( loopcount <= 0 ) {
									loopcount = plparam;
									loopchan = chan;
									nrow = channels[ coffset + CH_PAT_LOOP_ROW ];
									npat = pat;
								} else if( loopchan == chan ) {
									if( loopcount == 1 ) {
										channels[ coffset + CH_PAT_LOOP_ROW ] = row + 1;
									} else {
										nrow = channels[ coffset + CH_PAT_LOOP_ROW ];
										npat = pat;
									}
									loopcount--;
								}
							}
							break;
						case EX_SET_TREM_WAV:
							break;
						case EX_SET_PANNING:
							break;
						case EX_RETRIG:
							break;
						case EX_FINE_VOL_UP:
							int fvolup = channels[ coffset + CH_VOLUME ] + ( eparam & 0x0F );
							if( fvolup > 64 ) fvolup = 64;
							channels[ coffset + CH_VOLUME ] = fvolup;
							break;
						case EX_FINE_VOL_DN:
							int fvoldn = channels[ coffset + CH_VOLUME ] - ( eparam & 0x0F );
							if( fvoldn > 64 ) fvoldn = 0;
							channels[ coffset + CH_VOLUME ] = fvoldn;
							break;
						case EX_NOTE_CUT:
							if( ( eparam & 0x0F ) == fcount ) channels[ coffset + CH_VOLUME ] = 0;
							break;
						case EX_NOTE_DELAY:
							if( ( eparam & 0x0F ) == fcount ) trigger( coffset );
							break;
						case EX_PAT_DELAY:
							tick = tempo + tempo * ( eparam & 0x0F );
							break;
						case EX_INVERT_LOOP:
							break;
					}
					break;
				case FX_SET_SPEED:
					if( eparam < 32 ) tick = tempo = eparam;
					else bpm = eparam;
					break;
			}
		}
		mixupdate();
		fcount++;
		if( npat >= songlen ) npat = restart;
		if( nrow >= 64 ) nrow = 0;
		return songend;
	}

	private void trigger( final int coffset ) {
		int period = channels[ coffset + CH_NOTE_PERIOD ];
		int instru = channels[ coffset + CH_NOTE_INSTRU ];
		int effect = channels[ coffset + CH_NOTE_EFFECT ];
		if( instru != 0 ) {
			channels[ coffset + CH_ASSIGNED ] = instru;
			int ioffset = instru * IN_STRUCT_LEN;
			channels[ coffset + CH_VOLUME ] = instruments[ ioffset + IN_VOLUME ];
			channels[ coffset + CH_FINETUNE ] = instruments[ ioffset + IN_FINETUNE ];
			if( amiga ) {
				int atlsta = instruments[ ioffset + IN_LOOP_START ];
				int atlend = instruments[ ioffset + IN_LOOP_END ];
				if( atlend > atlsta ) channels[ coffset + CH_INSTRUMENT ] = instru;
			}
		}
		if( period != 0 ) {
			channels[ coffset + CH_INSTRUMENT ] = channels[ coffset + CH_ASSIGNED ];
			channels[ coffset + CH_PORTA_PERIOD ] = period;
			if( effect != FX_TONE_PORTA && effect != FX_TPORTA_VOL ) {
				channels[ coffset + CH_PERIOD ] = period;
				channels[ coffset + CH_SPOS ] = 0;
			}
			channels[ coffset + CH_VIBR_COUNT ] = 0;
		}
	}

	private void volslide( final int coffset, final int eparam ) {
		int vol = channels[ coffset + CH_VOLUME ];
		vol += ( eparam & 0xF0 ) >> 4;
		vol -=   eparam & 0x0F;
		if( vol > 64 ) vol = 64;
		if( vol < 0  ) vol = 0;
		channels[ coffset + CH_VOLUME ] = vol;
	}

	private void toneporta( final int coffset ) {
		int sp = channels[ coffset + CH_PERIOD ];
		int dp = channels[ coffset + CH_PORTA_PERIOD ];
		if( sp < dp ) {
			sp += channels[ coffset + CH_PORTA_PARAM ];
			if( sp > dp ) sp = dp;
		}
		if( sp > dp ) {
			sp -= channels[ coffset + CH_PORTA_PARAM ];
			if( sp < dp ) sp = dp;
		}
		channels[ coffset + CH_PERIOD ] = sp;
	}

	private void vibrato( final int coffset ) {
		int vparam = channels[ coffset + CH_VIBR_PARAM ];
		int vspeed = ( vparam & 0xF0 ) >> 4;
			int vdepth =   vparam & 0x0F;
			int vibpos = vspeed * channels[ coffset + CH_VIBR_COUNT ];
			int tval = sintable[ vibpos & 0x1F ];
			if( ( vibpos & 0x20 ) > 0 ) tval = -tval;
			channels[ coffset + CH_VIBR_PERIOD ] = tval * vdepth >> 7;
	}

	private void tremolo( final int coffset ) {
		int tparam = channels[ coffset + CH_TREM_PARAM ];
		int tspeed = ( tparam & 0xF0 ) >> 4;
		int tdepth =   tparam & 0x0F;
		int trempos = tspeed * channels[ coffset + CH_VIBR_COUNT ];
		int tval = sintable[ trempos & 0x1F ];
		if( ( trempos & 0x20 ) > 0 ) tval = -tval;
		channels[ coffset + CH_TREM_VOLUME ] = tval * tdepth >> 7;
	}

	private void mixupdate() {
		for( int chan = 0; chan < numchan; chan++ ) {
			int coffset = chan * CH_STRUCT_LEN;
			int a = channels[ coffset + CH_VOLUME ] + channels[ coffset + CH_TREM_VOLUME ];
			if( a < 0 ) a = 0;
			if( a > 64 ) a = 64;
			channels[ coffset + CH_AMPL ] = a << FP_SHIFT - 8;
			int p = channels[ coffset + CH_PERIOD ] + channels[ coffset + CH_VIBR_PERIOD ];
			if( p < 27 ) p = 27;
			int clk = amiga ? 3546894 : 3579364;
			int s = ( clk / p << FP_SHIFT ) / samplerate;
			s = s * fttable[ channels[ coffset + CH_FINETUNE ] + 8 ] >> 14;
			s = s * arptable[ channels[ coffset + CH_ARPEGGIO ] ] >> 13;
			channels[ coffset + CH_STEP ] = s;
		}
	}

	private int getticklen() {
		return ( ( samplerate << 1 ) + ( samplerate >> 1 ) ) / bpm;
	}

	private int ushortbe( final byte[] buf, final int offset ) {
		return ( ( buf[ offset ] & 0xFF ) << 8 ) | ( buf[ offset + 1 ] & 0xFF );
	}
}