11static const int rateValues[32] = {0, 2048, 1536, 1280, 1024, 768, 640, 512,
12 384, 320, 256, 192, 160, 128, 96, 80,
13 64, 48, 40, 32, 24, 20, 16, 12,
14 10, 8, 6, 5, 4, 3, 2, 1};
16static const int rateOffsets[32] = {0, 0, 1040, 536, 0, 1040, 536, 0, 1040,
17 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
18 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
19 536, 0, 1040, 536, 0};
21static const int gaussValues[512] = {
22 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
23 0x000, 0x000, 0x000, 0x000, 0x000, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
24 0x001, 0x001, 0x001, 0x001, 0x001, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002,
25 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x004, 0x004, 0x004, 0x004,
26 0x005, 0x005, 0x005, 0x005, 0x006, 0x006, 0x006, 0x006, 0x007, 0x007, 0x007,
27 0x008, 0x008, 0x008, 0x009, 0x009, 0x009, 0x00a, 0x00a, 0x00a, 0x00b, 0x00b,
28 0x00b, 0x00c, 0x00c, 0x00d, 0x00d, 0x00e, 0x00e, 0x00f, 0x00f, 0x00f, 0x010,
29 0x010, 0x011, 0x011, 0x012, 0x013, 0x013, 0x014, 0x014, 0x015, 0x015, 0x016,
30 0x017, 0x017, 0x018, 0x018, 0x019, 0x01a, 0x01b, 0x01b, 0x01c, 0x01d, 0x01d,
31 0x01e, 0x01f, 0x020, 0x020, 0x021, 0x022, 0x023, 0x024, 0x024, 0x025, 0x026,
32 0x027, 0x028, 0x029, 0x02a, 0x02b, 0x02c, 0x02d, 0x02e, 0x02f, 0x030, 0x031,
33 0x032, 0x033, 0x034, 0x035, 0x036, 0x037, 0x038, 0x03a, 0x03b, 0x03c, 0x03d,
34 0x03e, 0x040, 0x041, 0x042, 0x043, 0x045, 0x046, 0x047, 0x049, 0x04a, 0x04c,
35 0x04d, 0x04e, 0x050, 0x051, 0x053, 0x054, 0x056, 0x057, 0x059, 0x05a, 0x05c,
36 0x05e, 0x05f, 0x061, 0x063, 0x064, 0x066, 0x068, 0x06a, 0x06b, 0x06d, 0x06f,
37 0x071, 0x073, 0x075, 0x076, 0x078, 0x07a, 0x07c, 0x07e, 0x080, 0x082, 0x084,
38 0x086, 0x089, 0x08b, 0x08d, 0x08f, 0x091, 0x093, 0x096, 0x098, 0x09a, 0x09c,
39 0x09f, 0x0a1, 0x0a3, 0x0a6, 0x0a8, 0x0ab, 0x0ad, 0x0af, 0x0b2, 0x0b4, 0x0b7,
40 0x0ba, 0x0bc, 0x0bf, 0x0c1, 0x0c4, 0x0c7, 0x0c9, 0x0cc, 0x0cf, 0x0d2, 0x0d4,
41 0x0d7, 0x0da, 0x0dd, 0x0e0, 0x0e3, 0x0e6, 0x0e9, 0x0ec, 0x0ef, 0x0f2, 0x0f5,
42 0x0f8, 0x0fb, 0x0fe, 0x101, 0x104, 0x107, 0x10b, 0x10e, 0x111, 0x114, 0x118,
43 0x11b, 0x11e, 0x122, 0x125, 0x129, 0x12c, 0x130, 0x133, 0x137, 0x13a, 0x13e,
44 0x141, 0x145, 0x148, 0x14c, 0x150, 0x153, 0x157, 0x15b, 0x15f, 0x162, 0x166,
45 0x16a, 0x16e, 0x172, 0x176, 0x17a, 0x17d, 0x181, 0x185, 0x189, 0x18d, 0x191,
46 0x195, 0x19a, 0x19e, 0x1a2, 0x1a6, 0x1aa, 0x1ae, 0x1b2, 0x1b7, 0x1bb, 0x1bf,
47 0x1c3, 0x1c8, 0x1cc, 0x1d0, 0x1d5, 0x1d9, 0x1dd, 0x1e2, 0x1e6, 0x1eb, 0x1ef,
48 0x1f3, 0x1f8, 0x1fc, 0x201, 0x205, 0x20a, 0x20f, 0x213, 0x218, 0x21c, 0x221,
49 0x226, 0x22a, 0x22f, 0x233, 0x238, 0x23d, 0x241, 0x246, 0x24b, 0x250, 0x254,
50 0x259, 0x25e, 0x263, 0x267, 0x26c, 0x271, 0x276, 0x27b, 0x280, 0x284, 0x289,
51 0x28e, 0x293, 0x298, 0x29d, 0x2a2, 0x2a6, 0x2ab, 0x2b0, 0x2b5, 0x2ba, 0x2bf,
52 0x2c4, 0x2c9, 0x2ce, 0x2d3, 0x2d8, 0x2dc, 0x2e1, 0x2e6, 0x2eb, 0x2f0, 0x2f5,
53 0x2fa, 0x2ff, 0x304, 0x309, 0x30e, 0x313, 0x318, 0x31d, 0x322, 0x326, 0x32b,
54 0x330, 0x335, 0x33a, 0x33f, 0x344, 0x349, 0x34e, 0x353, 0x357, 0x35c, 0x361,
55 0x366, 0x36b, 0x370, 0x374, 0x379, 0x37e, 0x383, 0x388, 0x38c, 0x391, 0x396,
56 0x39b, 0x39f, 0x3a4, 0x3a9, 0x3ad, 0x3b2, 0x3b7, 0x3bb, 0x3c0, 0x3c5, 0x3c9,
57 0x3ce, 0x3d2, 0x3d7, 0x3dc, 0x3e0, 0x3e5, 0x3e9, 0x3ed, 0x3f2, 0x3f6, 0x3fb,
58 0x3ff, 0x403, 0x408, 0x40c, 0x410, 0x415, 0x419, 0x41d, 0x421, 0x425, 0x42a,
59 0x42e, 0x432, 0x436, 0x43a, 0x43e, 0x442, 0x446, 0x44a, 0x44e, 0x452, 0x455,
60 0x459, 0x45d, 0x461, 0x465, 0x468, 0x46c, 0x470, 0x473, 0x477, 0x47a, 0x47e,
61 0x481, 0x485, 0x488, 0x48c, 0x48f, 0x492, 0x496, 0x499, 0x49c, 0x49f, 0x4a2,
62 0x4a6, 0x4a9, 0x4ac, 0x4af, 0x4b2, 0x4b5, 0x4b7, 0x4ba, 0x4bd, 0x4c0, 0x4c3,
63 0x4c5, 0x4c8, 0x4cb, 0x4cd, 0x4d0, 0x4d2, 0x4d5, 0x4d7, 0x4d9, 0x4dc, 0x4de,
64 0x4e0, 0x4e3, 0x4e5, 0x4e7, 0x4e9, 0x4eb, 0x4ed, 0x4ef, 0x4f1, 0x4f3, 0x4f5,
65 0x4f6, 0x4f8, 0x4fa, 0x4fb, 0x4fd, 0x4ff, 0x500, 0x502, 0x503, 0x504, 0x506,
66 0x507, 0x508, 0x50a, 0x50b, 0x50c, 0x50d, 0x50e, 0x50f, 0x510, 0x511, 0x511,
67 0x512, 0x513, 0x514, 0x514, 0x515, 0x516, 0x516, 0x517, 0x517, 0x517, 0x518,
68 0x518, 0x518, 0x518, 0x518, 0x519, 0x519};
71 memset(
ram, 0,
sizeof(
ram));
73 for (
int i = 0; i < 8; i++) {
77 memset(
channel[i].decodeBuffer, 0,
sizeof(
channel[i].decodeBuffer));
158 for (
int i = 0; i < 8; i++) {
186static int clamp16(
int val) {
187 return val < -0x8000 ? -0x8000 : (val > 0x7fff ? 0x7fff : val);
190static int clip16(
int val) {
191 return (int16_t)(val & 0xffff);
197 return ((
counter + rateOffsets[rate]) % rateValues[rate]) == 0;
206 int16_t ramSample =
aram_[adr] | (
aram_[(adr + 1) & 0xffff] << 8);
208 ramSample =
aram_[(adr + 2) & 0xffff] | (
aram_[(adr + 3) & 0xffff] << 8);
213 int sumL = 0, sumR = 0;
214 for (
int i = 0; i < 8; i++) {
223 sumL = clamp16(sumL) & ~1;
224 sumR = clamp16(sumR) & ~1;
239 aram_[adr] = echoL & 0xff;
240 aram_[(adr + 1) & 0xffff] = echoL >> 8;
241 aram_[(adr + 2) & 0xffff] = echoR & 0xff;
242 aram_[(adr + 3) & 0xffff] = echoR >> 8;
257 if (ch > 0 &&
channel[ch].pitchModulation) {
263 if (
channel[ch].startDelay == 0)
266 aram_[samplePointer] | (
aram_[(samplePointer + 1) & 0xffff] << 8);
268 if (
channel[ch].startDelay > 0) {
269 if (
channel[ch].startDelay == 5) {
275 ram[0x7c] &= ~(1 << ch);
314 if (
channel[ch].startDelay == 0) {
318 if (
channel[ch].pitchCounter >= 0x4000) {
320 if (
channel[ch].blockOffset >= 7) {
321 if (
channel[ch].brrHeader & 0x1) {
323 ram[0x7c] |= 1 << ch;
335 if (
channel[ch].pitchCounter > 0x7fff)
340 ram[(ch << 4) | 9] = sample >> 8;
359 if (
channel[ch].adsrState == 3) {
365 switch (
channel[ch].adsrState) {
367 newGain += rate == 31 ? 1024 : 32;
370 newGain -= ((newGain - 1) >> 8) + 1;
373 newGain -= ((newGain - 1) >> 8) + 1;
379 switch (
channel[ch].gainMode) {
384 newGain -= ((newGain - 1) >> 8) + 1;
402 if (
channel[ch].adsrState == 1 && (newGain >> 8) == sustainLevel) {
408 if (newGain < 0 || newGain > 0x7ff) {
409 newGain = newGain < 0 ? 0 : 0x7ff;
410 if (
channel[ch].adsrState == 0) {
427 int out = (gaussValues[0xff - offset] * oldests) >> 11;
428 out += (gaussValues[0x1ff - offset] * olders) >> 11;
429 out += (gaussValues[0x100 + offset] * olds) >> 11;
430 out = clip16(out) + ((gaussValues[offset] * news) >> 11);
431 return clamp16(out) & ~1;
441 for (
int i = 0; i < 4; i++) {
454 s = (s << shift) >> 1;
460 s += old + (-old >> 4);
463 s += 2 * old + ((3 * -old) >> 5) - older + (older >> 4);
466 s += 2 * old + ((13 * -old) >> 6) - older + ((3 * older) >> 4);
474 if (
channel[ch].bufferOffset >= 12)
604 for (
int i = 0; i < 8; i++) {
610 for (
int i = 0; i < 8; i++) {
631 for (
int i = 0; i < 8; i++) {
637 for (
int i = 0; i < 8; i++) {
643 for (
int i = 0; i < 8; i++) {
684 double c1 = 0.5 * (p2 - p0);
685 double c2 = (p0 - 2.5 * p1 + 2.0 * p2 - 0.5 * p3);
686 double c3 = 0.5 * (-p0 + 3.0 * p1 - 3.0 * p2 + p3);
688 double result = c0 + c1 * t + c2 * t2 + c3 * t3;
691 return result > 32767.0
693 : (result < -32768.0 ? -32768 :
static_cast<int16_t
>(result));
698 const double mu2 = (1.0 - cos(mu * 3.14159265358979323846)) / 2.0;
699 return static_cast<int16_t
>(s0 * (1.0 - mu2) + s1 * mu2);
704 return static_cast<int16_t
>(s0 + frac * (s1 - s0));
710 int16_t p3,
double t) {
711 const double c0 = p1;
712 const double c1 = (p2 - p0) * 0.5;
713 const double c2 = p0 - 2.5 * p1 + 2.0 * p2 - 0.5 * p3;
714 const double c3 = (p3 - p0) * 0.5 + 1.5 * (p1 - p2);
716 const double result = c0 + c1 * t + c2 * t * t + c3 * t * t * t;
719 return result > 32767.0
721 : (result < -32768.0 ? -32768 :
static_cast<int16_t
>(result));
729 const double native_per_frame =
730 pal_timing ? (32040.0 / 50.007) : (32040.0 / 60.0988);
731 const double step = native_per_frame /
static_cast<double>(samples_per_frame);
735 location -= native_per_frame;
741 for (
int i = 0; i < samples_per_frame; i++) {
742 const int idx =
static_cast<int>(location) & 0x7ff;
743 const double frac = location -
static_cast<int>(location);
747 const int next_idx = (idx + 1) & 0x7ff;
752 sample_data[(i * 2) + 0] =
753 static_cast<int16_t
>(s0_l + frac * (s1_l - s0_l));
758 sample_data[(i * 2) + 1] =
759 static_cast<int16_t
>(s0_r + frac * (s1_r - s0_r));
763 const int idx0 = (idx - 1 + 0x800) & 0x7ff;
764 const int idx1 = idx & 0x7ff;
765 const int idx2 = (idx + 1) & 0x7ff;
766 const int idx3 = (idx + 2) & 0x7ff;
772 sample_data[(i * 2) + 0] =
779 sample_data[(i * 2) + 1] =
784 const int offset =
static_cast<int>(frac * 256.0) & 0xff;
785 const int idx0 = (idx - 1 + 0x800) & 0x7ff;
786 const int idx1 = idx & 0x7ff;
787 const int idx2 = (idx + 1) & 0x7ff;
788 const int idx3 = (idx + 2) & 0x7ff;
796 int out_l = (gaussValues[0xff - offset] * p0_l) >> 11;
797 out_l += (gaussValues[0x1ff - offset] * p1_l) >> 11;
798 out_l += (gaussValues[0x100 + offset] * p2_l) >> 11;
799 out_l = clip16(out_l) + ((gaussValues[offset] * p3_l) >> 11);
800 sample_data[(i * 2) + 0] = clamp16(out_l) & ~1;
808 int out_r = (gaussValues[0xff - offset] * p0_r) >> 11;
809 out_r += (gaussValues[0x1ff - offset] * p1_r) >> 11;
810 out_r += (gaussValues[0x100 + offset] * p2_r) >> 11;
811 out_r = clip16(out_r) + ((gaussValues[offset] * p3_r) >> 11);
812 sample_data[(i * 2) + 1] = clamp16(out_r) & ~1;
816 const int next_idx = (idx + 1) & 0x7ff;
827 (idx - 1 + 0x800) & 0x7ff;
828 const int idx1 = idx & 0x7ff;
829 const int idx2 = (idx + 1) & 0x7ff;
830 const int idx3 = (idx + 2) & 0x7ff;
836 sample_data[(i * 2) + 0] =
843 sample_data[(i * 2) + 1] =
853 if (sample_data ==
nullptr) {
857 const int native_per_frame = pal_timing ? 641 : 534;
858 const int total_samples = native_per_frame * 2;
863 for (
int i = 0; i < native_per_frame; ++i) {
864 const int idx = (start_index + i) & 0x7ff;
869 return total_samples / 2;
873 stream.write(
reinterpret_cast<const char*
>(
ram),
sizeof(
ram));
874 auto write_bool = [&](
bool value) {
875 uint8_t encoded = value ? 1 : 0;
876 stream.write(
reinterpret_cast<const char*
>(&encoded),
sizeof(encoded));
878 auto write_channel = [&](
const DspChannel& ch) {
879 stream.write(
reinterpret_cast<const char*
>(&ch.pitch),
sizeof(ch.pitch));
880 stream.write(
reinterpret_cast<const char*
>(&ch.pitchCounter),
881 sizeof(ch.pitchCounter));
882 write_bool(ch.pitchModulation);
883 stream.write(
reinterpret_cast<const char*
>(ch.decodeBuffer),
884 sizeof(ch.decodeBuffer));
885 stream.write(
reinterpret_cast<const char*
>(&ch.bufferOffset),
886 sizeof(ch.bufferOffset));
887 stream.write(
reinterpret_cast<const char*
>(&ch.srcn),
sizeof(ch.srcn));
888 stream.write(
reinterpret_cast<const char*
>(&ch.decodeOffset),
889 sizeof(ch.decodeOffset));
890 stream.write(
reinterpret_cast<const char*
>(&ch.blockOffset),
891 sizeof(ch.blockOffset));
892 stream.write(
reinterpret_cast<const char*
>(&ch.brrHeader),
893 sizeof(ch.brrHeader));
894 write_bool(ch.useNoise);
895 stream.write(
reinterpret_cast<const char*
>(&ch.startDelay),
896 sizeof(ch.startDelay));
897 stream.write(
reinterpret_cast<const char*
>(ch.adsrRates),
898 sizeof(ch.adsrRates));
899 stream.write(
reinterpret_cast<const char*
>(&ch.adsrState),
900 sizeof(ch.adsrState));
901 stream.write(
reinterpret_cast<const char*
>(&ch.sustainLevel),
902 sizeof(ch.sustainLevel));
903 stream.write(
reinterpret_cast<const char*
>(&ch.gainSustainLevel),
904 sizeof(ch.gainSustainLevel));
905 write_bool(ch.useGain);
906 stream.write(
reinterpret_cast<const char*
>(&ch.gainMode),
907 sizeof(ch.gainMode));
908 write_bool(ch.directGain);
909 stream.write(
reinterpret_cast<const char*
>(&ch.gainValue),
910 sizeof(ch.gainValue));
911 stream.write(
reinterpret_cast<const char*
>(&ch.preclampGain),
912 sizeof(ch.preclampGain));
913 stream.write(
reinterpret_cast<const char*
>(&ch.gain),
sizeof(ch.gain));
914 write_bool(ch.keyOn);
915 write_bool(ch.keyOff);
916 stream.write(
reinterpret_cast<const char*
>(&ch.sampleOut),
917 sizeof(ch.sampleOut));
918 stream.write(
reinterpret_cast<const char*
>(&ch.volumeL),
920 stream.write(
reinterpret_cast<const char*
>(&ch.volumeR),
922 write_bool(ch.echoEnable);
924 for (
const auto& ch :
channel) {
928 stream.write(
reinterpret_cast<const char*
>(&
counter),
sizeof(
counter));
929 stream.write(
reinterpret_cast<const char*
>(&
dirPage),
sizeof(
dirPage));
931 stream.write(
reinterpret_cast<const char*
>(&
mute),
sizeof(
mute));
932 stream.write(
reinterpret_cast<const char*
>(&
reset),
sizeof(
reset));
943 stream.write(
reinterpret_cast<const char*
>(&
noiseSample),
948 stream.write(
reinterpret_cast<const char*
>(&
echoVolumeL),
950 stream.write(
reinterpret_cast<const char*
>(&
echoVolumeR),
972 stream.read(
reinterpret_cast<char*
>(
ram),
sizeof(
ram));
973 auto read_bool = [&](
bool* value) {
975 stream.read(
reinterpret_cast<char*
>(&encoded),
sizeof(encoded));
976 *value = encoded != 0;
979 stream.read(
reinterpret_cast<char*
>(&ch.pitch),
sizeof(ch.pitch));
980 stream.read(
reinterpret_cast<char*
>(&ch.pitchCounter),
981 sizeof(ch.pitchCounter));
982 read_bool(&ch.pitchModulation);
983 stream.read(
reinterpret_cast<char*
>(ch.decodeBuffer),
984 sizeof(ch.decodeBuffer));
985 stream.read(
reinterpret_cast<char*
>(&ch.bufferOffset),
986 sizeof(ch.bufferOffset));
987 stream.read(
reinterpret_cast<char*
>(&ch.srcn),
sizeof(ch.srcn));
988 stream.read(
reinterpret_cast<char*
>(&ch.decodeOffset),
989 sizeof(ch.decodeOffset));
990 stream.read(
reinterpret_cast<char*
>(&ch.blockOffset),
991 sizeof(ch.blockOffset));
992 stream.read(
reinterpret_cast<char*
>(&ch.brrHeader),
sizeof(ch.brrHeader));
993 read_bool(&ch.useNoise);
994 stream.read(
reinterpret_cast<char*
>(&ch.startDelay),
sizeof(ch.startDelay));
995 stream.read(
reinterpret_cast<char*
>(ch.adsrRates),
sizeof(ch.adsrRates));
996 stream.read(
reinterpret_cast<char*
>(&ch.adsrState),
sizeof(ch.adsrState));
997 stream.read(
reinterpret_cast<char*
>(&ch.sustainLevel),
998 sizeof(ch.sustainLevel));
999 stream.read(
reinterpret_cast<char*
>(&ch.gainSustainLevel),
1000 sizeof(ch.gainSustainLevel));
1001 read_bool(&ch.useGain);
1002 stream.read(
reinterpret_cast<char*
>(&ch.gainMode),
sizeof(ch.gainMode));
1003 read_bool(&ch.directGain);
1004 stream.read(
reinterpret_cast<char*
>(&ch.gainValue),
sizeof(ch.gainValue));
1005 stream.read(
reinterpret_cast<char*
>(&ch.preclampGain),
1006 sizeof(ch.preclampGain));
1007 stream.read(
reinterpret_cast<char*
>(&ch.gain),
sizeof(ch.gain));
1008 read_bool(&ch.keyOn);
1009 read_bool(&ch.keyOff);
1010 stream.read(
reinterpret_cast<char*
>(&ch.sampleOut),
sizeof(ch.sampleOut));
1011 stream.read(
reinterpret_cast<char*
>(&ch.volumeL),
sizeof(ch.volumeL));
1012 stream.read(
reinterpret_cast<char*
>(&ch.volumeR),
sizeof(ch.volumeR));
1013 read_bool(&ch.echoEnable);
1022 stream.read(
reinterpret_cast<char*
>(&
mute),
sizeof(
mute));
1023 stream.read(
reinterpret_cast<char*
>(&
reset),
sizeof(
reset));
uint32_t lastFrameBoundary
void CycleChannel(int ch)
void GetSamples(int16_t *sample_data, int samples_per_frame, bool pal_timing)
uint8_t Read(uint8_t adr)
std::vector< uint8_t > & aram_
bool CheckCounter(int rate)
int16_t sampleBuffer[0x800 *2]
int CopyNativeFrame(int16_t *sample_data, bool pal_timing)
bool debug_mute_channels_[8]
void Write(uint8_t adr, uint8_t val)
InterpolationType interpolation_type
void LoadState(std::istream &stream)
int16_t GetSample(int ch)
void SaveState(std::ostream &stream)
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)