9static const int rateValues[32] = {0, 2048, 1536, 1280, 1024, 768, 640, 512,
10 384, 320, 256, 192, 160, 128, 96, 80,
11 64, 48, 40, 32, 24, 20, 16, 12,
12 10, 8, 6, 5, 4, 3, 2, 1};
14static const int rateOffsets[32] = {0, 0, 1040, 536, 0, 1040, 536, 0, 1040,
15 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
16 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
17 536, 0, 1040, 536, 0};
19static const int gaussValues[512] = {
20 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
21 0x000, 0x000, 0x000, 0x000, 0x000, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
22 0x001, 0x001, 0x001, 0x001, 0x001, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002,
23 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x004, 0x004, 0x004, 0x004,
24 0x005, 0x005, 0x005, 0x005, 0x006, 0x006, 0x006, 0x006, 0x007, 0x007, 0x007,
25 0x008, 0x008, 0x008, 0x009, 0x009, 0x009, 0x00a, 0x00a, 0x00a, 0x00b, 0x00b,
26 0x00b, 0x00c, 0x00c, 0x00d, 0x00d, 0x00e, 0x00e, 0x00f, 0x00f, 0x00f, 0x010,
27 0x010, 0x011, 0x011, 0x012, 0x013, 0x013, 0x014, 0x014, 0x015, 0x015, 0x016,
28 0x017, 0x017, 0x018, 0x018, 0x019, 0x01a, 0x01b, 0x01b, 0x01c, 0x01d, 0x01d,
29 0x01e, 0x01f, 0x020, 0x020, 0x021, 0x022, 0x023, 0x024, 0x024, 0x025, 0x026,
30 0x027, 0x028, 0x029, 0x02a, 0x02b, 0x02c, 0x02d, 0x02e, 0x02f, 0x030, 0x031,
31 0x032, 0x033, 0x034, 0x035, 0x036, 0x037, 0x038, 0x03a, 0x03b, 0x03c, 0x03d,
32 0x03e, 0x040, 0x041, 0x042, 0x043, 0x045, 0x046, 0x047, 0x049, 0x04a, 0x04c,
33 0x04d, 0x04e, 0x050, 0x051, 0x053, 0x054, 0x056, 0x057, 0x059, 0x05a, 0x05c,
34 0x05e, 0x05f, 0x061, 0x063, 0x064, 0x066, 0x068, 0x06a, 0x06b, 0x06d, 0x06f,
35 0x071, 0x073, 0x075, 0x076, 0x078, 0x07a, 0x07c, 0x07e, 0x080, 0x082, 0x084,
36 0x086, 0x089, 0x08b, 0x08d, 0x08f, 0x091, 0x093, 0x096, 0x098, 0x09a, 0x09c,
37 0x09f, 0x0a1, 0x0a3, 0x0a6, 0x0a8, 0x0ab, 0x0ad, 0x0af, 0x0b2, 0x0b4, 0x0b7,
38 0x0ba, 0x0bc, 0x0bf, 0x0c1, 0x0c4, 0x0c7, 0x0c9, 0x0cc, 0x0cf, 0x0d2, 0x0d4,
39 0x0d7, 0x0da, 0x0dd, 0x0e0, 0x0e3, 0x0e6, 0x0e9, 0x0ec, 0x0ef, 0x0f2, 0x0f5,
40 0x0f8, 0x0fb, 0x0fe, 0x101, 0x104, 0x107, 0x10b, 0x10e, 0x111, 0x114, 0x118,
41 0x11b, 0x11e, 0x122, 0x125, 0x129, 0x12c, 0x130, 0x133, 0x137, 0x13a, 0x13e,
42 0x141, 0x145, 0x148, 0x14c, 0x150, 0x153, 0x157, 0x15b, 0x15f, 0x162, 0x166,
43 0x16a, 0x16e, 0x172, 0x176, 0x17a, 0x17d, 0x181, 0x185, 0x189, 0x18d, 0x191,
44 0x195, 0x19a, 0x19e, 0x1a2, 0x1a6, 0x1aa, 0x1ae, 0x1b2, 0x1b7, 0x1bb, 0x1bf,
45 0x1c3, 0x1c8, 0x1cc, 0x1d0, 0x1d5, 0x1d9, 0x1dd, 0x1e2, 0x1e6, 0x1eb, 0x1ef,
46 0x1f3, 0x1f8, 0x1fc, 0x201, 0x205, 0x20a, 0x20f, 0x213, 0x218, 0x21c, 0x221,
47 0x226, 0x22a, 0x22f, 0x233, 0x238, 0x23d, 0x241, 0x246, 0x24b, 0x250, 0x254,
48 0x259, 0x25e, 0x263, 0x267, 0x26c, 0x271, 0x276, 0x27b, 0x280, 0x284, 0x289,
49 0x28e, 0x293, 0x298, 0x29d, 0x2a2, 0x2a6, 0x2ab, 0x2b0, 0x2b5, 0x2ba, 0x2bf,
50 0x2c4, 0x2c9, 0x2ce, 0x2d3, 0x2d8, 0x2dc, 0x2e1, 0x2e6, 0x2eb, 0x2f0, 0x2f5,
51 0x2fa, 0x2ff, 0x304, 0x309, 0x30e, 0x313, 0x318, 0x31d, 0x322, 0x326, 0x32b,
52 0x330, 0x335, 0x33a, 0x33f, 0x344, 0x349, 0x34e, 0x353, 0x357, 0x35c, 0x361,
53 0x366, 0x36b, 0x370, 0x374, 0x379, 0x37e, 0x383, 0x388, 0x38c, 0x391, 0x396,
54 0x39b, 0x39f, 0x3a4, 0x3a9, 0x3ad, 0x3b2, 0x3b7, 0x3bb, 0x3c0, 0x3c5, 0x3c9,
55 0x3ce, 0x3d2, 0x3d7, 0x3dc, 0x3e0, 0x3e5, 0x3e9, 0x3ed, 0x3f2, 0x3f6, 0x3fb,
56 0x3ff, 0x403, 0x408, 0x40c, 0x410, 0x415, 0x419, 0x41d, 0x421, 0x425, 0x42a,
57 0x42e, 0x432, 0x436, 0x43a, 0x43e, 0x442, 0x446, 0x44a, 0x44e, 0x452, 0x455,
58 0x459, 0x45d, 0x461, 0x465, 0x468, 0x46c, 0x470, 0x473, 0x477, 0x47a, 0x47e,
59 0x481, 0x485, 0x488, 0x48c, 0x48f, 0x492, 0x496, 0x499, 0x49c, 0x49f, 0x4a2,
60 0x4a6, 0x4a9, 0x4ac, 0x4af, 0x4b2, 0x4b5, 0x4b7, 0x4ba, 0x4bd, 0x4c0, 0x4c3,
61 0x4c5, 0x4c8, 0x4cb, 0x4cd, 0x4d0, 0x4d2, 0x4d5, 0x4d7, 0x4d9, 0x4dc, 0x4de,
62 0x4e0, 0x4e3, 0x4e5, 0x4e7, 0x4e9, 0x4eb, 0x4ed, 0x4ef, 0x4f1, 0x4f3, 0x4f5,
63 0x4f6, 0x4f8, 0x4fa, 0x4fb, 0x4fd, 0x4ff, 0x500, 0x502, 0x503, 0x504, 0x506,
64 0x507, 0x508, 0x50a, 0x50b, 0x50c, 0x50d, 0x50e, 0x50f, 0x510, 0x511, 0x511,
65 0x512, 0x513, 0x514, 0x514, 0x515, 0x516, 0x516, 0x517, 0x517, 0x517, 0x518,
66 0x518, 0x518, 0x518, 0x518, 0x519, 0x519};
69 memset(
ram, 0,
sizeof(
ram));
71 for (
int i = 0; i < 8; i++) {
75 memset(
channel[i].decodeBuffer, 0,
sizeof(
channel[i].decodeBuffer));
139 for (
int i = 0; i < 8; i++) {
157static int clamp16(
int val) {
158 return val < -0x8000 ? -0x8000 : (val > 0x7fff ? 0x7fff : val);
161static int clip16(
int val) {
return (int16_t)(val & 0xffff); }
164 if (rate == 0)
return false;
165 return ((
counter + rateOffsets[rate]) % rateValues[rate]) == 0;
174 int16_t ramSample =
aram_[adr] | (
aram_[(adr + 1) & 0xffff] << 8);
176 ramSample =
aram_[(adr + 2) & 0xffff] | (
aram_[(adr + 3) & 0xffff] << 8);
179 int sumL = 0, sumR = 0;
180 for (
int i = 0; i < 8; i++) {
189 sumL = clamp16(sumL) & ~1;
190 sumR = clamp16(sumR) & ~1;
201 aram_[adr] = echoL & 0xff;
202 aram_[(adr + 1) & 0xffff] = echoL >> 8;
203 aram_[(adr + 2) & 0xffff] = echoR & 0xff;
204 aram_[(adr + 3) & 0xffff] = echoR >> 8;
219 if (ch > 0 &&
channel[ch].pitchModulation) {
225 if (
channel[ch].startDelay == 0) samplePointer += 2;
227 aram_[samplePointer] | (
aram_[(samplePointer + 1) & 0xffff] << 8);
229 if (
channel[ch].startDelay > 0) {
230 if (
channel[ch].startDelay == 5) {
236 ram[0x7c] &= ~(1 << ch);
271 if (
channel[ch].startDelay == 0) {
275 if (
channel[ch].pitchCounter >= 0x4000) {
277 if (
channel[ch].blockOffset >= 7) {
278 if (
channel[ch].brrHeader & 0x1) {
280 ram[0x7c] |= 1 << ch;
295 ram[(ch << 4) | 9] = sample >> 8;
309 if (
channel[ch].adsrState == 3) {
315 switch (
channel[ch].adsrState) {
317 newGain += rate == 31 ? 1024 : 32;
320 newGain -= ((newGain - 1) >> 8) + 1;
323 newGain -= ((newGain - 1) >> 8) + 1;
329 switch (
channel[ch].gainMode) {
334 newGain -= ((newGain - 1) >> 8) + 1;
352 if (
channel[ch].adsrState == 1 && (newGain >> 8) == sustainLevel) {
358 if (newGain < 0 || newGain > 0x7ff) {
359 newGain = newGain < 0 ? 0 : 0x7ff;
360 if (
channel[ch].adsrState == 0) {
375 int out = (gaussValues[0xff - offset] * oldests) >> 11;
376 out += (gaussValues[0x1ff - offset] * olders) >> 11;
377 out += (gaussValues[0x100 + offset] * olds) >> 11;
378 out = clip16(out) + ((gaussValues[offset] * news) >> 11);
379 return clamp16(out) & ~1;
389 for (
int i = 0; i < 4; i++) {
401 s = (s << shift) >> 1;
407 s += old + (-old >> 4);
410 s += 2 * old + ((3 * -old) >> 5) - older + (older >> 4);
413 s += 2 * old + ((13 * -old) >> 6) - older + ((3 * older) >> 4);
548 for (
int i = 0; i < 8; i++) {
554 for (
int i = 0; i < 8; i++) {
575 for (
int i = 0; i < 8; i++) {
581 for (
int i = 0; i < 8; i++) {
587 for (
int i = 0; i < 8; i++) {
628 double c1 = 0.5 * (p2 - p0);
629 double c2 = (p0 - 2.5 * p1 + 2.0 * p2 - 0.5 * p3);
630 double c3 = 0.5 * (-p0 + 3.0 * p1 - 3.0 * p2 + p3);
632 double result = c0 + c1 * t + c2 * t2 + c3 * t3;
635 return result > 32767.0
637 : (result < -32768.0 ? -32768 :
static_cast<int16_t
>(result));
642 const double mu2 = (1.0 - cos(mu * 3.14159265358979323846)) / 2.0;
643 return static_cast<int16_t
>(s0 * (1.0 - mu2) + s1 * mu2);
648 return static_cast<int16_t
>(s0 + frac * (s1 - s0));
654 const double c0 = p1;
655 const double c1 = (p2 - p0) * 0.5;
656 const double c2 = p0 - 2.5 * p1 + 2.0 * p2 - 0.5 * p3;
657 const double c3 = (p3 - p0) * 0.5 + 1.5 * (p1 - p2);
659 const double result = c0 + c1 * t + c2 * t * t + c3 * t * t * t;
662 return result > 32767.0 ? 32767
663 : (result < -32768.0 ? -32768
664 :
static_cast<int16_t
>(result));
670 const double native_per_frame = pal_timing ? 641.0 : 534.0;
671 const double step = native_per_frame /
static_cast<double>(samples_per_frame);
675 location -= native_per_frame;
678 while (location < 0) location += 0x400;
680 for (
int i = 0; i < samples_per_frame; i++) {
681 const int idx =
static_cast<int>(location) & 0x3ff;
682 const double frac = location -
static_cast<int>(location);
686 const int next_idx = (idx + 1) & 0x3ff;
691 sample_data[(i * 2) + 0] =
static_cast<int16_t
>(
692 s0_l + frac * (s1_l - s0_l));
697 sample_data[(i * 2) + 1] =
static_cast<int16_t
>(
698 s0_r + frac * (s1_r - s0_r));
702 const int idx0 = (idx - 1 + 0x400) & 0x3ff;
703 const int idx1 = idx & 0x3ff;
704 const int idx2 = (idx + 1) & 0x3ff;
705 const int idx3 = (idx + 2) & 0x3ff;
721 const int next_idx = (idx + 1) & 0x3ff;
731 const int idx0 = (idx - 1 + 0x400) & 0x3ff;
732 const int idx1 = idx & 0x3ff;
733 const int idx2 = (idx + 1) & 0x3ff;
734 const int idx3 = (idx + 2) & 0x3ff;
740 sample_data[(i * 2) + 0] =
747 sample_data[(i * 2) + 1] =
uint32_t lastFrameBoundary
void CycleChannel(int ch)
void GetSamples(int16_t *sample_data, int samples_per_frame, bool pal_timing)
int16_t sampleBuffer[0x400 *2]
uint8_t Read(uint8_t adr)
std::vector< uint8_t > & aram_
bool CheckCounter(int rate)
void Write(uint8_t adr, uint8_t val)
InterpolationType interpolation_type
int16_t GetSample(int ch)
int16_t InterpolateCubic(int16_t p0, int16_t p1, int16_t p2, int16_t p3, double t)
int16_t InterpolateLinear(int16_t s0, int16_t s1, double frac)
int16_t InterpolateHermite(int16_t p0, int16_t p1, int16_t p2, int16_t p3, double t)
int16_t InterpolateCosine(int16_t s0, int16_t s1, double mu)
Main namespace for the application.