File: AudioChannel.cc

package info (click to toggle)
exult 1.12.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,856 kB
  • sloc: cpp: 170,016; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 550; objc: 416
file content (814 lines) | stat: -rw-r--r-- 20,687 bytes parent folder | download
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/*
Copyright (C) 2005 The Pentagram team
Copyright (C) 2010-2022 The Exult Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "pent_include.h"

#include "AudioChannel.h"

#include "AudioSample.h"

#include <cstring>

namespace Pentagram {

	sint16 ReadSample(uint8* src) {
		sint16 val;
		std::memcpy(&val, src, sizeof(sint16));
		return val;
	}

// We divide the data by 2, to prevent overshots. Imagine this sample pattern:
// 0, 65535, 65535, 0. Now you want to compute a value between the two 65535.
// Obviously, it will be *bigger* than 65535 (it can get to about 80,000).
// It is possibly to clamp it, but that leads to a distored wave form. Compare
// this to turning up the volume of your stereo to much, it will start to sound
// bad at a certain level (depending on the power of your stereo, your speakers
// etc, this can be quite loud, though ;-). Hence we reduce the original range.
// A factor of roughly 1/1.2 = 0.8333 is sufficient. Since we want to avoid
// floating point, we approximate that by 27/32
#define RANGE_REDUX(x) (((x) * 27) >> 5)

	AudioChannel::AudioChannel(uint32 sample_rate_, bool stereo_)
			: sample_rate(sample_rate_), stereo(stereo_) {}

	AudioChannel::~AudioChannel() {
		if (sample && playdata) {
			sample->freeDecompressor(decomp);
		}
		if (sample) {
			sample->Release();
		}
		playdata.reset();
		sample = nullptr;
	}

	void AudioChannel::playSample(
			AudioSample* sample_, int loop_, int priority_, bool paused_,
			uint32 pitch_shift_, int lvol_, int rvol_, sint32 instance_id_) {
		stop();
		sample = sample_;

		loop             = loop_;
		priority         = priority_;
		lvol             = lvol_;
		rvol             = rvol_;
		paused           = paused_;
		pitch_shift      = pitch_shift_;
		instance_id      = instance_id_;
		distance         = 0;
		balance          = 0;
		overall_position = 0;

		if (!sample) {
			return;
		}
		sample->IncRef();

		// Lambda to round size up to next multiple of maximum alignment size,
		// if needed.
		auto round_up = [](size_t val) {
			constexpr const size_t maxalign = alignof(std::max_align_t);
			return ((val + maxalign - 1) / maxalign) * maxalign;
		};
		// Persistent data for the decompressor
		const size_t decompressor_size
				= round_up(sample->getDecompressorDataSize());
		const size_t frame_size         = round_up(sample->getFrameSize());
		const size_t decompressor_align = sample->getDecompressorAlignment();
		const size_t new_size
				= decompressor_size + frame_size * 2 + decompressor_align;

		// Setup buffers
		if (new_size > playdata_size) {
			playdata_size = new_size;
			playdata      = std::make_unique<uint8[]>(playdata_size);
		}

		size_t avail_space = playdata_size;
		decomp             = playdata.get();
		// Align decomp to required alignment.
		std::align(decompressor_align, decompressor_size, decomp, avail_space);
		uint8* frameptr = static_cast<uint8*>(decomp) + decompressor_size;
		frames[0]       = frameptr;
		// Second frame should be aligned for free.
		frames[1] = frameptr + frame_size;

		// Init the sample decompressor
		sample->initDecompressor(decomp);

		// Reset counter and stuff
		frame_evenodd = 0;
		position      = 0;
		fp_pos        = 0;
		fp_speed      = (pitch_shift * sample->getRate()) / sample_rate;

		// Decompress frame 0
		frame0_size = sample->decompressFrame(decomp, frames[0]);

		// Decompress frame 1
		DecompressNextFrame();

		// Setup resampler
		if (sample->getBits() == 8 && !sample->isStereo()) {
			uint8* src = frames[0];
			int    a   = *(src + 0);
			a          = (a | (a << 8)) - 32768;
			int b      = *(src + 1);
			b          = (a | (b << 8)) - 32768;
			int c      = *(src + 2);
			c          = (a | (c << 8)) - 32768;

			interp_l.init(RANGE_REDUX(a), RANGE_REDUX(b), RANGE_REDUX(c));
		}
	}

	void AudioChannel::resampleAndMix(sint16* stream, uint32 bytes) {
		if (!sample || paused) {
			return;
		}

		// Update fp_speed
		fp_speed = (pitch_shift * sample->getRate()) / sample_rate;
		// Get and Mix data
		do {
			int startpos = position;

			// 8 bit resampling
			if (sample->getBits() == 8) {
				if (!sample->isStereo() && stereo) {
					resampleFrameM8toS(
							stream, bytes);    // Mono Sample to Stereo Output
				} else if (!sample->isStereo() && !stereo) {
					resampleFrameM8toM(
							stream, bytes);    // Mono Sample to Stereo Output
				} else if (sample->isStereo() && !stereo) {
					resampleFrameS8toM(
							stream, bytes);    // Stereo Sample to Mono Output
				} else {
					resampleFrameS8toS(
							stream, bytes);    // Stereo Sample to Stereo Output
				}
			} else if (sample->getBits() == 16) {
				if (!sample->isStereo() && stereo) {
					resampleFrameM16toS(
							stream, bytes);    // Mono Sample to Stereo Output
				} else if (!sample->isStereo() && !stereo) {
					resampleFrameM16toM(
							stream, bytes);    // Mono Sample to Mono Output
				} else if (sample->isStereo() && !stereo) {
					resampleFrameS16toM(
							stream, bytes);    // Stereo Sample to Mono Output
				} else {
					resampleFrameS16toS(
							stream, bytes);    // Stereo Sample to Stereo Output
				}
			}
			overall_position
					+= (position - startpos)
					   / (sample->getBits() / (sample->isStereo() ? 4 : 8));
			// We ran out of data
			if (bytes || (position == frame0_size)) {
				// No more data
				if (!frame1_size) {
					if (sample) {
						sample->Release();
					}
					sample = nullptr;
					return;
				}

				// Invert evenodd
				frame_evenodd = 1 - frame_evenodd;

				// Set frame1 to be frame0
				frame0_size = frame1_size;
				position    = 0;

				DecompressNextFrame();
			}

		} while (bytes != 0);
	}

	uint32 AudioChannel::getPlaybackLength() const {
		if (!sample) {
			return UINT32_MAX;
		}
		return ((sample->getPlaybackLength()) * UINT64_C(1000)) / sample->getRate();
	}

	uint32 AudioChannel::getPlaybackPosition() const {
		if (!sample) {
			return UINT32_MAX;
		}
		return (overall_position * UINT64_C(1000)) / sample->getRate();
	}

	// Decompress a frame
	void AudioChannel::DecompressNextFrame() {
		// Get next frame of data
		uint8* src2 = frames[1 - frame_evenodd];
		frame1_size = sample->decompressFrame(decomp, src2);

		// No stream, go back to beginning and get first frame
		if (!frame1_size && loop) {
			if (loop != -1) {
				loop--;
			}
			overall_position = 0;
			sample->rewind(decomp);
			frame1_size = sample->decompressFrame(decomp, src2);
		} else if (!frame1_size) {
			sample->freeDecompressor(decomp);
		}
	}

	//
	// 8 Bit
	//

	// Resample a frame of mono 8bit unsigned to Stereo 16bit
	void AudioChannel::resampleFrameM8toS(sint16*& stream, uint32& bytes) {
		uint8* src  = frames[frame_evenodd];
		uint8* src2 = frames[1 - frame_evenodd];

		uint8* src_end  = src + frame0_size;
		uint8* src2_end = src2 + frame1_size;

		src += position;

		int lvol = this->lvol;
		int rvol = this->rvol;

		calculate2DVolume(lvol, rvol);

		do {
			// Add a new src sample (if required)
			if (fp_pos >= 0x10000) {
				if (src + 2 < src_end) {
					int c = *(src + 2);
					c     = (c | (c << 8)) - 32768;
					interp_l.feedData(c);
				} else if (src2 < src2_end) {
					int c = *(src2);
					c     = (c | (c << 8)) - 32768;
					interp_l.feedData(c);
					src2++;
				} else {
					interp_l.feedData();
				}
				src++;
				fp_pos -= 0x10000;
			}

			if (fp_pos < 0x10000) {
				do {
					// Do the interpolation
					const int result = interp_l.interpolate(fp_pos);

					int lresult = *(stream + 0) + (result * lvol) / 256;
					int rresult = *(stream + 1) + (result * rvol) / 256;

					// Enforce range in case of an "overshot". Shouldn't happen
					// since we scale down already, but safe is safe.
					if (lresult < -32768) {
						lresult = -32768;
					} else if (lresult > 32767) {
						lresult = 32767;
					}

					if (rresult < -32768) {
						rresult = -32768;
					} else if (rresult > 32767) {
						rresult = 32767;
					}

					*stream++ = lresult;
					*stream++ = rresult;
					bytes -= 4;
					fp_pos += fp_speed;

				} while (fp_pos < 0x10000 && bytes != 0);
			}

		} while (bytes != 0 && src != src_end);

		position = frame0_size - (src_end - src);
	}

	// Resample a frame of mono 8bit unsigned to Mono 16bit
	void AudioChannel::resampleFrameM8toM(sint16*& stream, uint32& bytes) {
		uint8* src  = frames[frame_evenodd];
		uint8* src2 = frames[1 - frame_evenodd];

		uint8* src_end  = src + frame0_size;
		uint8* src2_end = src2 + frame1_size;

		src += position;

		int lvol = this->lvol;
		int rvol = this->rvol;

		calculate2DVolume(lvol, rvol);

		const int volume = (rvol + lvol) / 2;

		do {
			// Add a new src sample (if required)
			if (fp_pos >= 0x10000) {
				if (src + 2 < src_end) {
					int c = *(src + 2);
					c     = (c | (c << 8)) - 32768;
					interp_l.feedData(c);
				} else if (src2 < src2_end) {
					int c = *(src2);
					c     = (c | (c << 8)) - 32768;
					interp_l.feedData(c);
					src2++;
				} else {
					interp_l.feedData();
				}
				src++;
				fp_pos -= 0x10000;
			}

			if (fp_pos < 0x10000) {
				do {
					// Do the interpolation
					int result = (interp_l.interpolate(fp_pos) * volume) / 256;

					result += *stream;

					// Enforce range in case of an "overshot". Shouldn't happen
					// since we scale down already, but safe is safe.
					if (result < -32768) {
						result = -32768;
					} else if (result > 32767) {
						result = 32767;
					}

					*stream++ = result;
					bytes -= 2;
					fp_pos += fp_speed;

				} while (fp_pos < 0x10000 && bytes != 0);
			}

		} while (bytes != 0 && src != src_end);

		position = frame0_size - (src_end - src);
	}

	// Resample a frame of stereo 8bit unsigned to Mono 16bit
	void AudioChannel::resampleFrameS8toM(sint16*& stream, uint32& bytes) {
		uint8* src  = frames[frame_evenodd];
		uint8* src2 = frames[1 - frame_evenodd];

		uint8* src_end  = src + frame0_size;
		uint8* src2_end = src2 + frame1_size;

		src += position;

		int lvol = this->lvol;
		int rvol = this->rvol;

		calculate2DVolume(lvol, rvol);

		do {
			// Add a new src sample (if required)
			if (fp_pos >= 0x10000) {
				if (src + 4 < src_end) {
					const int c  = *(src + 4);
					const int c2 = *(src + 5);
					interp_l.feedData((c | (c << 8)) - 32768);
					interp_r.feedData((c2 | (c2 << 8)) - 32768);
				} else if (src2 < src2_end) {
					const int c  = *(src2);
					const int c2 = *(src2 + 1);
					interp_l.feedData((c | (c << 8)) - 32768);
					interp_r.feedData((c2 | (c2 << 8)) - 32768);
					src2 += 2;
				} else {
					interp_l.feedData();
					interp_r.feedData();
				}
				src += 2;
				fp_pos -= 0x10000;
			}

			if (fp_pos < 0x10000) {
				do {
					// Do the interpolation
					int result = ((interp_l.interpolate(fp_pos) * lvol
								   + interp_r.interpolate(fp_pos) * rvol))
								 / 512;

					result += *stream;

					// Enforce range in case of an "overshot". Shouldn't happen
					// since we scale down already, but safe is safe.
					if (result < -32768) {
						result = -32768;
					} else if (result > 32767) {
						result = 32767;
					}

					*stream++ = result;
					bytes -= 2;
					fp_pos += fp_speed;

				} while (fp_pos < 0x10000 && bytes != 0);
			}

		} while (bytes != 0 && src != src_end);

		position = frame0_size - (src_end - src);
	}

	// Resample a frame of stereo 8bit unsigned to Stereo 16bit
	void AudioChannel::resampleFrameS8toS(sint16*& stream, uint32& bytes) {
		uint8* src  = frames[frame_evenodd];
		uint8* src2 = frames[1 - frame_evenodd];

		uint8* src_end  = src + frame0_size;
		uint8* src2_end = src2 + frame1_size;

		src += position;

		int lvol = this->lvol;
		int rvol = this->rvol;

		calculate2DVolume(lvol, rvol);

		do {
			// Add a new src sample (if required)
			if (fp_pos >= 0x10000) {
				if (src + 4 < src_end) {
					const int c  = *(src + 4);
					const int c2 = *(src + 5);
					interp_l.feedData((c | (c << 8)) - 32768);
					interp_r.feedData((c2 | (c2 << 8)) - 32768);
				} else if (src2 < src2_end) {
					const int c  = *(src2);
					const int c2 = *(src2 + 1);
					interp_l.feedData((c | (c << 8)) - 32768);
					interp_r.feedData((c2 | (c2 << 8)) - 32768);
					src2 += 2;
				} else {
					interp_l.feedData();
					interp_r.feedData();
				}
				src += 2;
				fp_pos -= 0x10000;
			}

			if (fp_pos < 0x10000) {
				do {
					// Do the interpolation
					int lresult = *(stream + 0)
								  + (interp_l.interpolate(fp_pos) * lvol) / 256;
					int rresult = *(stream + 1)
								  + (interp_r.interpolate(fp_pos) * rvol) / 256;

					// Enforce range in case of an "overshot". Shouldn't happen
					// since we scale down already, but safe is safe.
					if (lresult < -32768) {
						lresult = -32768;
					} else if (lresult > 32767) {
						lresult = 32767;
					}

					if (rresult < -32768) {
						rresult = -32768;
					} else if (rresult > 32767) {
						rresult = 32767;
					}

					*stream++ = lresult;
					*stream++ = rresult;
					bytes -= 4;
					fp_pos += fp_speed;

				} while (fp_pos < 0x10000 && bytes != 0);
			}

		} while (bytes != 0 && src != src_end);

		position = frame0_size - (src_end - src);
	}

	//
	// 16 Bit
	//

	// Resample a frame of mono 16bit unsigned to Stereo 16bit
	void AudioChannel::resampleFrameM16toS(sint16*& stream, uint32& bytes) {
		uint8* src  = frames[frame_evenodd];
		uint8* src2 = frames[1 - frame_evenodd];

		uint8* src_end  = src + frame0_size;
		uint8* src2_end = src2 + frame1_size;

		src += position;

		int lvol = this->lvol;
		int rvol = this->rvol;

		calculate2DVolume(lvol, rvol);

		do {
			// Add a new src sample (if required)
			if (fp_pos >= 0x10000) {
				if (src + 4 < src_end) {
					const int c = ReadSample(src + 4);
					interp_l.feedData(c);
				} else if (src2 < src2_end) {
					const int c = ReadSample(src2);
					interp_l.feedData(c);
					src2 += 2;
				} else {
					interp_l.feedData();
				}
				src += 2;
				fp_pos -= 0x10000;
			}

			if (fp_pos < 0x10000) {
				do {
					// Do the interpolation
					const int result = interp_l.interpolate(fp_pos);

					int lresult = *(stream + 0) + (result * lvol) / 256;
					int rresult = *(stream + 1) + (result * rvol) / 256;

					// Enforce range in case of an "overshot". Shouldn't happen
					// since we scale down already, but safe is safe.
					if (lresult < -32768) {
						lresult = -32768;
					} else if (lresult > 32767) {
						lresult = 32767;
					}

					if (rresult < -32768) {
						rresult = -32768;
					} else if (rresult > 32767) {
						rresult = 32767;
					}

					*stream++ = lresult;
					*stream++ = rresult;
					bytes -= 4;
					fp_pos += fp_speed;

				} while (fp_pos < 0x10000 && bytes != 0);
			}

		} while (bytes != 0 && src != src_end);

		position = frame0_size - (src_end - src);
	}

	// Resample a frame of mono 16bit unsigned to Mono 16bit
	void AudioChannel::resampleFrameM16toM(sint16*& stream, uint32& bytes) {
		uint8* src  = frames[frame_evenodd];
		uint8* src2 = frames[1 - frame_evenodd];

		uint8* src_end  = src + frame0_size;
		uint8* src2_end = src2 + frame1_size;

		src += position;

		int lvol = this->lvol;
		int rvol = this->rvol;

		calculate2DVolume(lvol, rvol);

		const int volume = (rvol + lvol) / 2;

		do {
			// Add a new src sample (if required)
			if (fp_pos >= 0x10000) {
				if (src + 4 < src_end) {
					const int c = ReadSample(src + 4);
					interp_l.feedData(c);
				} else if (src2 < src2_end) {
					const int c = ReadSample(src2);
					interp_l.feedData(c);
					src2 += 2;
				} else {
					interp_l.feedData();
				}
				src += 2;
				fp_pos -= 0x10000;
			}

			if (fp_pos < 0x10000) {
				do {
					// Do the interpolation
					int result = (interp_l.interpolate(fp_pos) * volume) / 256;

					result += *stream;

					// Enforce range in case of an "overshot". Shouldn't happen
					// since we scale down already, but safe is safe.
					if (result < -32768) {
						result = -32768;
					} else if (result > 32767) {
						result = 32767;
					}

					*stream++ = result;
					bytes -= 2;
					fp_pos += fp_speed;

				} while (fp_pos < 0x10000 && bytes != 0);
			}

		} while (bytes != 0 && src != src_end);

		position = frame0_size - (src_end - src);
	}

	// Resample a frame of stereo 16bit unsigned to Mono 16bit
	void AudioChannel::resampleFrameS16toM(sint16*& stream, uint32& bytes) {
		uint8* src  = frames[frame_evenodd];
		uint8* src2 = frames[1 - frame_evenodd];

		uint8* src_end  = src + frame0_size;
		uint8* src2_end = src2 + frame1_size;

		src += position;

		int lvol = this->lvol;
		int rvol = this->rvol;

		calculate2DVolume(lvol, rvol);

		do {
			// Add a new src sample (if required)
			if (fp_pos >= 0x10000) {
				if (src + 8 < src_end) {
					const int c  = ReadSample(src + 8);
					const int c2 = ReadSample(src + 10);
					interp_l.feedData(c);
					interp_r.feedData(c2);
				} else if (src2 < src2_end) {
					const int c  = ReadSample(src2);
					const int c2 = ReadSample(src2 + 2);
					interp_l.feedData(c);
					interp_r.feedData(c2);
					src2 += 4;
				} else {
					interp_l.feedData();
					interp_r.feedData();
				}
				src += 4;
				fp_pos -= 0x10000;
			}

			if (fp_pos < 0x10000) {
				do {
					// Do the interpolation
					int result = ((interp_l.interpolate(fp_pos) * lvol
								   + interp_r.interpolate(fp_pos) * rvol))
								 / 512;

					result += *stream;

					// Enforce range in case of an "overshot". Shouldn't happen
					// since we scale down already, but safe is safe.
					if (result < -32768) {
						result = -32768;
					} else if (result > 32767) {
						result = 32767;
					}

					*stream++ = result;
					bytes -= 2;
					fp_pos += fp_speed;

				} while (fp_pos < 0x10000 && bytes != 0);
			}

		} while (bytes != 0 && src != src_end);

		position = frame0_size - (src_end - src);
	}

	// Resample a frame of stereo 16bit unsigned to Stereo 16bit
	void AudioChannel::resampleFrameS16toS(sint16*& stream, uint32& bytes) {
		uint8* src  = frames[frame_evenodd];
		uint8* src2 = frames[1 - frame_evenodd];

		uint8* src_end  = src + frame0_size;
		uint8* src2_end = src2 + frame1_size;

		src += position;

		int lvol = this->lvol;
		int rvol = this->rvol;

		calculate2DVolume(lvol, rvol);

		do {
			// Add a new src sample (if required)
			if (fp_pos >= 0x10000) {
				if (src + 8 < src_end) {
					const int c  = ReadSample(src + 8);
					const int c2 = ReadSample(src + 10);
					interp_l.feedData(c);
					interp_r.feedData(c2);
				} else if (src2 < src2_end) {
					const int c  = ReadSample(src2);
					const int c2 = ReadSample(src2 + 2);
					interp_l.feedData(c);
					interp_r.feedData(c2);
					src2 += 4;
				} else {
					interp_l.feedData();
					interp_r.feedData();
				}
				src += 4;
				fp_pos -= 0x10000;
			}

			if (fp_pos < 0x10000) {
				do {
					// Do the interpolation
					int lresult = *(stream + 0)
								  + (interp_l.interpolate(fp_pos) * lvol) / 256;
					int rresult = *(stream + 1)
								  + (interp_r.interpolate(fp_pos) * rvol) / 256;

					// Enforce range in case of an "overshot". Shouldn't happen
					// since we scale down already, but safe is safe.
					if (lresult < -32768) {
						lresult = -32768;
					} else if (lresult > 32767) {
						lresult = 32767;
					}

					if (rresult < -32768) {
						rresult = -32768;
					} else if (rresult > 32767) {
						rresult = 32767;
					}

					*stream++ = lresult;
					*stream++ = rresult;
					bytes -= 4;
					fp_pos += fp_speed;

				} while (fp_pos < 0x10000 && bytes != 0);
			}

		} while (bytes != 0 && src != src_end);

		position = frame0_size - (src_end - src);
	}

	void AudioChannel::calculate2DVolume(int& lvol, int& rvol) {
		if (distance > 255) {
			lvol = 0;
			rvol = 0;
			return;
		}

		int lbal = 256;
		int rbal = 256;

		if (balance < 0) {
			if (balance < -256) {
				rbal = 0;
			} else {
				rbal = balance + 256;
			}
		} else if (balance > 0) {
			if (balance > 256) {
				lbal = 0;
			} else {
				lbal = 256 - balance;
			}
		}

		lvol = lvol * (256 - distance) * lbal / 65536;
		rvol = rvol * (256 - distance) * rbal / 65536;
	}

}    // namespace Pentagram