| 1 |
/** |
| 2 |
* emu2413 v1.5.9 |
| 3 |
* https://github.com/digital-sound-antiques/emu2413 |
| 4 |
* Copyright (C) 2020 Mitsutaka Okazaki |
| 5 |
* |
| 6 |
* This source refers to the following documents. The author would like to thank all the authors who have |
| 7 |
* contributed to the writing of them. |
| 8 |
* - [YM2413 notes](http://www.smspower.org/Development/YM2413) by andete |
| 9 |
* - ymf262.c by Jarek Burczynski |
| 10 |
* - [VRC7 presets](https://siliconpr0n.org/archive/doku.php?id=vendor:yamaha:opl2#opll_vrc7_patch_format) by Nuke.YKT |
| 11 |
* - YMF281B presets by Chabin |
| 12 |
*/ |
| 13 |
#include "emu2413.h" |
| 14 |
#include <math.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#ifndef INLINE |
| 20 |
#if defined(_MSC_VER) |
| 21 |
#define INLINE __inline |
| 22 |
#elif defined(__GNUC__) |
| 23 |
#define INLINE __inline__ |
| 24 |
#else |
| 25 |
#define INLINE inline |
| 26 |
#endif |
| 27 |
#endif |
| 28 |
|
| 29 |
#define _PI_ 3.14159265358979323846264338327950288 |
| 30 |
|
| 31 |
#define OPLL_TONE_NUM 4 |
| 32 |
/* clang-format off */ |
| 33 |
static uint8_t default_inst[OPLL_TONE_NUM][(16 + 3) * 8] = {{ |
| 34 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 0: User |
| 35 |
0x71,0x61,0x1e,0x17,0xd0,0x78,0x00,0x17, // 1: Violin |
| 36 |
0x13,0x41,0x1a,0x0d,0xd8,0xf7,0x23,0x13, // 2: Guitar |
| 37 |
0x13,0x01,0x99,0x00,0xf2,0xc4,0x21,0x23, // 3: Piano |
| 38 |
0x11,0x61,0x0e,0x07,0x8d,0x64,0x70,0x27, // 4: Flute |
| 39 |
0x32,0x21,0x1e,0x06,0xe1,0x76,0x01,0x28, // 5: Clarinet |
| 40 |
0x31,0x22,0x16,0x05,0xe0,0x71,0x00,0x18, // 6: Oboe |
| 41 |
0x21,0x61,0x1d,0x07,0x82,0x81,0x11,0x07, // 7: Trumpet |
| 42 |
0x33,0x21,0x2d,0x13,0xb0,0x70,0x00,0x07, // 8: Organ |
| 43 |
0x61,0x61,0x1b,0x06,0x64,0x65,0x10,0x17, // 9: Horn |
| 44 |
0x41,0x61,0x0b,0x18,0x85,0xf0,0x81,0x07, // A: Synthesizer |
| 45 |
0x33,0x01,0x83,0x11,0xea,0xef,0x10,0x04, // B: Harpsichord |
| 46 |
0x17,0xc1,0x24,0x07,0xf8,0xf8,0x22,0x12, // C: Vibraphone |
| 47 |
0x61,0x50,0x0c,0x05,0xd2,0xf5,0x40,0x42, // D: Synthsizer Bass |
| 48 |
0x01,0x01,0x55,0x03,0xe9,0x90,0x03,0x02, // E: Acoustic Bass |
| 49 |
0x41,0x41,0x89,0x03,0xf1,0xe4,0xc0,0x13, // F: Electric Guitar |
| 50 |
0x01,0x01,0x18,0x0f,0xdf,0xf8,0x6a,0x6d, // R: Bass Drum (from VRC7) |
| 51 |
0x01,0x01,0x00,0x00,0xc8,0xd8,0xa7,0x68, // R: High-Hat(M) / Snare Drum(C) (from VRC7) |
| 52 |
0x05,0x01,0x00,0x00,0xf8,0xaa,0x59,0x55, // R: Tom-tom(M) / Top Cymbal(C) (from VRC7) |
| 53 |
},{ |
| 54 |
/* VRC7 presets from Nuke.YKT */ |
| 55 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 56 |
0x03,0x21,0x05,0x06,0xe8,0x81,0x42,0x27, |
| 57 |
0x13,0x41,0x14,0x0d,0xd8,0xf6,0x23,0x12, |
| 58 |
0x11,0x11,0x08,0x08,0xfa,0xb2,0x20,0x12, |
| 59 |
0x31,0x61,0x0c,0x07,0xa8,0x64,0x61,0x27, |
| 60 |
0x32,0x21,0x1e,0x06,0xe1,0x76,0x01,0x28, |
| 61 |
0x02,0x01,0x06,0x00,0xa3,0xe2,0xf4,0xf4, |
| 62 |
0x21,0x61,0x1d,0x07,0x82,0x81,0x11,0x07, |
| 63 |
0x23,0x21,0x22,0x17,0xa2,0x72,0x01,0x17, |
| 64 |
0x35,0x11,0x25,0x00,0x40,0x73,0x72,0x01, |
| 65 |
0xb5,0x01,0x0f,0x0F,0xa8,0xa5,0x51,0x02, |
| 66 |
0x17,0xc1,0x24,0x07,0xf8,0xf8,0x22,0x12, |
| 67 |
0x71,0x23,0x11,0x06,0x65,0x74,0x18,0x16, |
| 68 |
0x01,0x02,0xd3,0x05,0xc9,0x95,0x03,0x02, |
| 69 |
0x61,0x63,0x0c,0x00,0x94,0xC0,0x33,0xf6, |
| 70 |
0x21,0x72,0x0d,0x00,0xc1,0xd5,0x56,0x06, |
| 71 |
0x01,0x01,0x18,0x0f,0xdf,0xf8,0x6a,0x6d, |
| 72 |
0x01,0x01,0x00,0x00,0xc8,0xd8,0xa7,0x68, |
| 73 |
0x05,0x01,0x00,0x00,0xf8,0xaa,0x59,0x55, |
| 74 |
},{ |
| 75 |
/* YMF281B presets */ |
| 76 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 0: User |
| 77 |
0x62,0x21,0x1a,0x07,0xf0,0x6f,0x00,0x16, // 1: Electric Strings (form Chabin's patch) |
| 78 |
0x40,0x10,0x45,0x00,0xf6,0x83,0x73,0x63, // 2: Bow Wow (based on plgDavid's patch, KSL fixed) |
| 79 |
0x13,0x01,0x99,0x00,0xf2,0xc3,0x21,0x23, // 3: Electric Guitar (similar to YM2413 but different DR(C)) |
| 80 |
0x01,0x61,0x0b,0x0f,0xf9,0x64,0x70,0x17, // 4: Organ (based on Chabin, TL/DR fixed) |
| 81 |
0x32,0x21,0x1e,0x06,0xe1,0x76,0x01,0x28, // 5: Clarinet (identical to YM2413) |
| 82 |
0x60,0x01,0x82,0x0e,0xf9,0x61,0x20,0x27, // 6: Saxophone (based on plgDavid, PM/EG fixed) |
| 83 |
0x21,0x61,0x1c,0x07,0x84,0x81,0x11,0x07, // 7: Trumpet (similar to YM2413 but different TL/DR(M)) |
| 84 |
0x37,0x32,0xc9,0x01,0x66,0x64,0x40,0x28, // 8: Street Organ (from Chabin) |
| 85 |
0x01,0x21,0x07,0x03,0xa5,0x71,0x51,0x07, // 9: Synth Brass (based on Chabin, TL fixed) |
| 86 |
0x06,0x01,0x5e,0x07,0xf3,0xf3,0xf6,0x13, // A: Electric Piano (based on Chabin, DR/RR/KR fixed) |
| 87 |
0x00,0x00,0x18,0x06,0xf5,0xf3,0x20,0x23, // B: Bass (based on Chabin, EG fixed) |
| 88 |
0x17,0xc1,0x24,0x07,0xf8,0xf8,0x22,0x12, // C: Vibraphone (identical to YM2413) |
| 89 |
0x35,0x64,0x00,0x00,0xff,0xf3,0x77,0xf5, // D: Chimes (from plgDavid) |
| 90 |
0x11,0x31,0x00,0x07,0xdd,0xf3,0xff,0xfb, // E: Tom Tom II (from plgDavid) |
| 91 |
0x3a,0x21,0x00,0x07,0x80,0x84,0x0f,0xf5, // F: Noise (based on plgDavid, AR fixed) |
| 92 |
0x01,0x01,0x18,0x0f,0xdf,0xf8,0x6a,0x6d, // R: Bass Drum (identical to YM2413) |
| 93 |
0x01,0x01,0x00,0x00,0xc8,0xd8,0xa7,0x68, // R: High-Hat(M) / Snare Drum(C) (identical to YM2413) |
| 94 |
0x05,0x01,0x00,0x00,0xf8,0xaa,0x59,0x55, // R: Tom-tom(M) / Top Cymbal(C) (identical to YM2413) |
| 95 |
},{ |
| 96 |
/* YM2423, taken from ymfm */ |
| 97 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 98 |
0x61,0x61,0x1B,0x07,0x94,0x5F,0x10,0x06, |
| 99 |
0x93,0xB1,0x51,0x04,0xF3,0xF2,0x70,0xFB, |
| 100 |
0x41,0x21,0x11,0x85,0xF2,0xF2,0x70,0x75, |
| 101 |
0x93,0xB2,0x28,0x07,0xF3,0xF2,0x70,0xB4, |
| 102 |
0x72,0x31,0x97,0x05,0x51,0x6F,0x60,0x09, |
| 103 |
0x13,0x30,0x18,0x06,0xF7,0xF4,0x50,0x85, |
| 104 |
0x51,0x31,0x1C,0x07,0x51,0x71,0x20,0x26, |
| 105 |
0x41,0xF4,0x1B,0x07,0x74,0x34,0x00,0x06, |
| 106 |
0x50,0x30,0x4D,0x03,0x42,0x65,0x20,0x06, |
| 107 |
0x40,0x20,0x10,0x85,0xF3,0xF5,0x20,0x04, |
| 108 |
0x61,0x61,0x1B,0x07,0xC5,0x96,0xF3,0xF6, |
| 109 |
0xF9,0xF1,0xDC,0x00,0xF5,0xF3,0x77,0xF2, |
| 110 |
0x60,0xA2,0x91,0x03,0x94,0xC1,0xF7,0xF7, |
| 111 |
0x30,0x30,0x17,0x06,0xF3,0xF1,0xB7,0xFC, |
| 112 |
0x31,0x36,0x0D,0x05,0xF2,0xF4,0x27,0x9C, |
| 113 |
0x01,0x01,0x18,0x0F,0xDF,0xF8,0x6A,0x6D, |
| 114 |
0x01,0x01,0x00,0x00,0xC8,0xD8,0xA7,0x48, |
| 115 |
0x05,0x01,0x00,0x00,0xF8,0xAA,0x59,0x55, |
| 116 |
}}; |
| 117 |
/* clang-format on */ |
| 118 |
|
| 119 |
/* phase increment counter */ |
| 120 |
#define DP_BITS 19 |
| 121 |
#define DP_WIDTH (1 << DP_BITS) |
| 122 |
#define DP_BASE_BITS (DP_BITS - PG_BITS) |
| 123 |
|
| 124 |
/* dynamic range of envelope output */ |
| 125 |
#define EG_STEP 0.375 |
| 126 |
#define EG_BITS 7 |
| 127 |
#define EG_MUTE ((1 << EG_BITS) - 1) |
| 128 |
#define EG_MAX (EG_MUTE - 4) |
| 129 |
|
| 130 |
/* dynamic range of total level */ |
| 131 |
#define TL_STEP 0.75 |
| 132 |
#define TL_BITS 6 |
| 133 |
|
| 134 |
/* dynamic range of sustine level */ |
| 135 |
#define SL_STEP 3.0 |
| 136 |
#define SL_BITS 4 |
| 137 |
|
| 138 |
/* damper speed before key-on. key-scale affects. */ |
| 139 |
#define DAMPER_RATE 12 |
| 140 |
|
| 141 |
#define TL2EG(d) ((d) << 1) |
| 142 |
|
| 143 |
/* sine table */ |
| 144 |
#define PG_BITS 10 /* 2^10 = 1024 length sine table */ |
| 145 |
#define PG_WIDTH (1 << PG_BITS) |
| 146 |
|
| 147 |
/* clang-format off */ |
| 148 |
/* exp_table[x] = round((exp2((double)x / 256.0) - 1) * 1024) */ |
| 149 |
static uint16_t exp_table[256] = { |
| 150 |
0, 3, 6, 8, 11, 14, 17, 20, 22, 25, 28, 31, 34, 37, 40, 42, |
| 151 |
45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, |
| 152 |
93, 96, 99, 102, 105, 108, 111, 114, 117, 120, 123, 126, 130, 133, 136, 139, |
| 153 |
142, 145, 148, 152, 155, 158, 161, 164, 168, 171, 174, 177, 181, 184, 187, 190, |
| 154 |
194, 197, 200, 204, 207, 210, 214, 217, 220, 224, 227, 231, 234, 237, 241, 244, |
| 155 |
248, 251, 255, 258, 262, 265, 268, 272, 276, 279, 283, 286, 290, 293, 297, 300, |
| 156 |
304, 308, 311, 315, 318, 322, 326, 329, 333, 337, 340, 344, 348, 352, 355, 359, |
| 157 |
363, 367, 370, 374, 378, 382, 385, 389, 393, 397, 401, 405, 409, 412, 416, 420, |
| 158 |
424, 428, 432, 436, 440, 444, 448, 452, 456, 460, 464, 468, 472, 476, 480, 484, |
| 159 |
488, 492, 496, 501, 505, 509, 513, 517, 521, 526, 530, 534, 538, 542, 547, 551, |
| 160 |
555, 560, 564, 568, 572, 577, 581, 585, 590, 594, 599, 603, 607, 612, 616, 621, |
| 161 |
625, 630, 634, 639, 643, 648, 652, 657, 661, 666, 670, 675, 680, 684, 689, 693, |
| 162 |
698, 703, 708, 712, 717, 722, 726, 731, 736, 741, 745, 750, 755, 760, 765, 770, |
| 163 |
774, 779, 784, 789, 794, 799, 804, 809, 814, 819, 824, 829, 834, 839, 844, 849, |
| 164 |
854, 859, 864, 869, 874, 880, 885, 890, 895, 900, 906, 911, 916, 921, 927, 932, |
| 165 |
937, 942, 948, 953, 959, 964, 969, 975, 980, 986, 991, 996, 1002, 1007, 1013, 1018 |
| 166 |
}; |
| 167 |
/* fullsin_table[x] = round(-log2(sin((x + 0.5) * PI / (PG_WIDTH / 4) / 2)) * 256) */ |
| 168 |
static uint16_t fullsin_table[PG_WIDTH] = { |
| 169 |
2137, 1731, 1543, 1419, 1326, 1252, 1190, 1137, 1091, 1050, 1013, 979, 949, 920, 894, 869, |
| 170 |
846, 825, 804, 785, 767, 749, 732, 717, 701, 687, 672, 659, 646, 633, 621, 609, |
| 171 |
598, 587, 576, 566, 556, 546, 536, 527, 518, 509, 501, 492, 484, 476, 468, 461, |
| 172 |
453, 446, 439, 432, 425, 418, 411, 405, 399, 392, 386, 380, 375, 369, 363, 358, |
| 173 |
352, 347, 341, 336, 331, 326, 321, 316, 311, 307, 302, 297, 293, 289, 284, 280, |
| 174 |
276, 271, 267, 263, 259, 255, 251, 248, 244, 240, 236, 233, 229, 226, 222, 219, |
| 175 |
215, 212, 209, 205, 202, 199, 196, 193, 190, 187, 184, 181, 178, 175, 172, 169, |
| 176 |
167, 164, 161, 159, 156, 153, 151, 148, 146, 143, 141, 138, 136, 134, 131, 129, |
| 177 |
127, 125, 122, 120, 118, 116, 114, 112, 110, 108, 106, 104, 102, 100, 98, 96, |
| 178 |
94, 92, 91, 89, 87, 85, 83, 82, 80, 78, 77, 75, 74, 72, 70, 69, |
| 179 |
67, 66, 64, 63, 62, 60, 59, 57, 56, 55, 53, 52, 51, 49, 48, 47, |
| 180 |
46, 45, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, |
| 181 |
29, 28, 27, 26, 25, 24, 23, 23, 22, 21, 20, 20, 19, 18, 17, 17, |
| 182 |
16, 15, 15, 14, 13, 13, 12, 12, 11, 10, 10, 9, 9, 8, 8, 7, |
| 183 |
7, 7, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, |
| 184 |
2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 185 |
}; |
| 186 |
/* clang-format on */ |
| 187 |
|
| 188 |
static uint16_t halfsin_table[PG_WIDTH]; |
| 189 |
static uint16_t *wave_table_map[2] = {fullsin_table, halfsin_table}; |
| 190 |
|
| 191 |
/* pitch modulator */ |
| 192 |
/* offset to fnum, rough approximation of 14 cents depth. */ |
| 193 |
static int8_t pm_table[8][8] = { |
| 194 |
{0, 0, 0, 0, 0, 0, 0, 0}, // fnum = 000xxxxxx |
| 195 |
{0, 0, 1, 0, 0, 0, -1, 0}, // fnum = 001xxxxxx |
| 196 |
{0, 1, 2, 1, 0, -1, -2, -1}, // fnum = 010xxxxxx |
| 197 |
{0, 1, 3, 1, 0, -1, -3, -1}, // fnum = 011xxxxxx |
| 198 |
{0, 2, 4, 2, 0, -2, -4, -2}, // fnum = 100xxxxxx |
| 199 |
{0, 2, 5, 2, 0, -2, -5, -2}, // fnum = 101xxxxxx |
| 200 |
{0, 3, 6, 3, 0, -3, -6, -3}, // fnum = 110xxxxxx |
| 201 |
{0, 3, 7, 3, 0, -3, -7, -3}, // fnum = 111xxxxxx |
| 202 |
}; |
| 203 |
|
| 204 |
/* amplitude lfo table */ |
| 205 |
/* The following envelop pattern is verified on real YM2413. */ |
| 206 |
/* each element repeates 64 cycles */ |
| 207 |
static uint8_t am_table[210] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, // |
| 208 |
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // |
| 209 |
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, // |
| 210 |
6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, // |
| 211 |
8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, // |
| 212 |
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, // |
| 213 |
12, 12, 12, 12, 12, 12, 12, 12, // |
| 214 |
13, 13, 13, // |
| 215 |
12, 12, 12, 12, 12, 12, 12, 12, // |
| 216 |
11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, // |
| 217 |
9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, // |
| 218 |
7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, // |
| 219 |
5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, // |
| 220 |
3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, // |
| 221 |
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}; |
| 222 |
|
| 223 |
/* envelope decay increment step table */ |
| 224 |
/* based on andete's research */ |
| 225 |
static uint8_t eg_step_tables[4][8] = { |
| 226 |
{0, 1, 0, 1, 0, 1, 0, 1}, |
| 227 |
{0, 1, 0, 1, 1, 1, 0, 1}, |
| 228 |
{0, 1, 1, 1, 0, 1, 1, 1}, |
| 229 |
{0, 1, 1, 1, 1, 1, 1, 1}, |
| 230 |
}; |
| 231 |
|
| 232 |
enum __OPLL_EG_STATE { ATTACK, DECAY, SUSTAIN, RELEASE, DAMP, UNKNOWN }; |
| 233 |
|
| 234 |
static uint32_t ml_table[16] = {1, 1 * 2, 2 * 2, 3 * 2, 4 * 2, 5 * 2, 6 * 2, 7 * 2, |
| 235 |
8 * 2, 9 * 2, 10 * 2, 10 * 2, 12 * 2, 12 * 2, 15 * 2, 15 * 2}; |
| 236 |
|
| 237 |
#define dB2(x) ((x)*2) |
| 238 |
static double kl_table[16] = {dB2(0.000), dB2(9.000), dB2(12.000), dB2(13.875), dB2(15.000), dB2(16.125), |
| 239 |
dB2(16.875), dB2(17.625), dB2(18.000), dB2(18.750), dB2(19.125), dB2(19.500), |
| 240 |
dB2(19.875), dB2(20.250), dB2(20.625), dB2(21.000)}; |
| 241 |
|
| 242 |
static uint32_t tll_table[8 * 16][1 << TL_BITS][4]; |
| 243 |
static int32_t rks_table[8 * 2][2]; |
| 244 |
|
| 245 |
static OPLL_PATCH null_patch = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 246 |
static OPLL_PATCH default_patch[OPLL_TONE_NUM][(16 + 3) * 2]; |
| 247 |
|
| 248 |
/* don't forget min/max is defined as a macro in stdlib.h of Visual C. */ |
| 249 |
#ifndef min |
| 250 |
static INLINE int min(int i, int j) { return (i < j) ? i : j; } |
| 251 |
#endif |
| 252 |
#ifndef max |
| 253 |
static INLINE int max(int i, int j) { return (i > j) ? i : j; } |
| 254 |
#endif |
| 255 |
|
| 256 |
/*************************************************** |
| 257 |
|
| 258 |
Internal Sample Rate Converter |
| 259 |
|
| 260 |
****************************************************/ |
| 261 |
/* Note: to disable internal rate converter, set clock/72 to output sampling rate. */ |
| 262 |
|
| 263 |
/* |
| 264 |
* LW is truncate length of sinc(x) calculation. |
| 265 |
* Lower LW is faster, higher LW results better quality. |
| 266 |
* LW must be a non-zero positive even number, no upper limit. |
| 267 |
* LW=16 or greater is recommended when upsampling. |
| 268 |
* LW=8 is practically okay for downsampling. |
| 269 |
*/ |
| 270 |
#define LW 16 |
| 271 |
|
| 272 |
/* resolution of sinc(x) table. sinc(x) where 0.0<=x<1.0 corresponds to sinc_table[0...SINC_RESO-1] */ |
| 273 |
#define SINC_RESO 256 |
| 274 |
#define SINC_AMP_BITS 12 |
| 275 |
|
| 276 |
// double hamming(double x) { return 0.54 - 0.46 * cos(2 * PI * x); } |
| 277 |
static double blackman(double x) { return 0.42 - 0.5 * cos(2 * _PI_ * x) + 0.08 * cos(4 * _PI_ * x); } |
| 278 |
static double sinc(double x) { return (x == 0.0 ? 1.0 : sin(_PI_ * x) / (_PI_ * x)); } |
| 279 |
static double windowed_sinc(double x) { return blackman(0.5 + 0.5 * x / (LW / 2)) * sinc(x); } |
| 280 |
|
| 281 |
/* f_inp: input frequency. f_out: output frequencey, ch: number of channels */ |
| 282 |
OPLL_RateConv *OPLL_RateConv_new(double f_inp, double f_out, int ch) { |
| 283 |
OPLL_RateConv *conv = malloc(sizeof(OPLL_RateConv)); |
| 284 |
int i; |
| 285 |
|
| 286 |
conv->ch = ch; |
| 287 |
conv->f_ratio = f_inp / f_out; |
| 288 |
conv->buf = malloc(sizeof(void *) * ch); |
| 289 |
for (i = 0; i < ch; i++) { |
| 290 |
conv->buf[i] = malloc(sizeof(conv->buf[0][0]) * LW); |
| 291 |
} |
| 292 |
|
| 293 |
/* create sinc_table for positive 0 <= x < LW/2 */ |
| 294 |
conv->sinc_table = malloc(sizeof(conv->sinc_table[0]) * SINC_RESO * LW / 2); |
| 295 |
for (i = 0; i < SINC_RESO * LW / 2; i++) { |
| 296 |
const double x = (double)i / SINC_RESO; |
| 297 |
if (f_out < f_inp) { |
| 298 |
/* for downsampling */ |
| 299 |
conv->sinc_table[i] = (int16_t)((1 << SINC_AMP_BITS) * windowed_sinc(x / conv->f_ratio) / conv->f_ratio); |
| 300 |
} else { |
| 301 |
/* for upsampling */ |
| 302 |
conv->sinc_table[i] = (int16_t)((1 << SINC_AMP_BITS) * windowed_sinc(x)); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
return conv; |
| 307 |
} |
| 308 |
|
| 309 |
static INLINE int16_t lookup_sinc_table(int16_t *table, double x) { |
| 310 |
int16_t index = (int16_t)(x * SINC_RESO); |
| 311 |
if (index < 0) |
| 312 |
index = -index; |
| 313 |
return table[min(SINC_RESO * LW / 2 - 1, index)]; |
| 314 |
} |
| 315 |
|
| 316 |
void OPLL_RateConv_reset(OPLL_RateConv *conv) { |
| 317 |
int i; |
| 318 |
conv->timer = 0; |
| 319 |
for (i = 0; i < conv->ch; i++) { |
| 320 |
memset(conv->buf[i], 0, sizeof(conv->buf[i][0]) * LW); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
/* put original data to this converter at f_inp. */ |
| 325 |
void OPLL_RateConv_putData(OPLL_RateConv *conv, int ch, int16_t data) { |
| 326 |
int16_t *buf = conv->buf[ch]; |
| 327 |
int i; |
| 328 |
for (i = 0; i < LW - 1; i++) { |
| 329 |
buf[i] = buf[i + 1]; |
| 330 |
} |
| 331 |
buf[LW - 1] = data; |
| 332 |
} |
| 333 |
|
| 334 |
/* get resampled data from this converter at f_out. */ |
| 335 |
/* this function must be called f_out / f_inp times per one putData call. */ |
| 336 |
int16_t OPLL_RateConv_getData(OPLL_RateConv *conv, int ch) { |
| 337 |
int16_t *buf = conv->buf[ch]; |
| 338 |
int32_t sum = 0; |
| 339 |
int k; |
| 340 |
double dn; |
| 341 |
conv->timer += conv->f_ratio; |
| 342 |
dn = conv->timer - floor(conv->timer); |
| 343 |
conv->timer = dn; |
| 344 |
|
| 345 |
for (k = 0; k < LW; k++) { |
| 346 |
double x = ((double)k - (LW / 2 - 1)) - dn; |
| 347 |
sum += buf[k] * lookup_sinc_table(conv->sinc_table, x); |
| 348 |
} |
| 349 |
return sum >> SINC_AMP_BITS; |
| 350 |
} |
| 351 |
|
| 352 |
void OPLL_RateConv_delete(OPLL_RateConv *conv) { |
| 353 |
int i; |
| 354 |
for (i = 0; i < conv->ch; i++) { |
| 355 |
free(conv->buf[i]); |
| 356 |
} |
| 357 |
free(conv->buf); |
| 358 |
free(conv->sinc_table); |
| 359 |
free(conv); |
| 360 |
} |
| 361 |
|
| 362 |
/*************************************************** |
| 363 |
|
| 364 |
Create tables |
| 365 |
|
| 366 |
****************************************************/ |
| 367 |
|
| 368 |
static void makeSinTable(void) { |
| 369 |
int x; |
| 370 |
|
| 371 |
for (x = 0; x < PG_WIDTH / 4; x++) { |
| 372 |
fullsin_table[PG_WIDTH / 4 + x] = fullsin_table[PG_WIDTH / 4 - x - 1]; |
| 373 |
} |
| 374 |
|
| 375 |
for (x = 0; x < PG_WIDTH / 2; x++) { |
| 376 |
fullsin_table[PG_WIDTH / 2 + x] = 0x8000 | fullsin_table[x]; |
| 377 |
} |
| 378 |
|
| 379 |
for (x = 0; x < PG_WIDTH / 2; x++) |
| 380 |
halfsin_table[x] = fullsin_table[x]; |
| 381 |
|
| 382 |
for (x = PG_WIDTH / 2; x < PG_WIDTH; x++) |
| 383 |
halfsin_table[x] = 0xfff; |
| 384 |
} |
| 385 |
|
| 386 |
static void makeTllTable(void) { |
| 387 |
|
| 388 |
int32_t tmp; |
| 389 |
int32_t fnum, block, TL, KL; |
| 390 |
|
| 391 |
for (fnum = 0; fnum < 16; fnum++) { |
| 392 |
for (block = 0; block < 8; block++) { |
| 393 |
for (TL = 0; TL < 64; TL++) { |
| 394 |
for (KL = 0; KL < 4; KL++) { |
| 395 |
if (KL == 0) { |
| 396 |
tll_table[(block << 4) | fnum][TL][KL] = TL2EG(TL); |
| 397 |
} else { |
| 398 |
tmp = (int32_t)(kl_table[fnum] - dB2(3.000) * (7 - block)); |
| 399 |
if (tmp <= 0) |
| 400 |
tll_table[(block << 4) | fnum][TL][KL] = TL2EG(TL); |
| 401 |
else |
| 402 |
tll_table[(block << 4) | fnum][TL][KL] = (uint32_t)((tmp >> (3 - KL)) / EG_STEP) + TL2EG(TL); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
static void makeRksTable(void) { |
| 411 |
int fnum8, block; |
| 412 |
for (fnum8 = 0; fnum8 < 2; fnum8++) |
| 413 |
for (block = 0; block < 8; block++) { |
| 414 |
rks_table[(block << 1) | fnum8][1] = (block << 1) + fnum8; |
| 415 |
rks_table[(block << 1) | fnum8][0] = block >> 1; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
static void makeDefaultPatch(void) { |
| 420 |
int i, j; |
| 421 |
for (i = 0; i < OPLL_TONE_NUM; i++) |
| 422 |
for (j = 0; j < 19; j++) |
| 423 |
OPLL_getDefaultPatch(i, j, &default_patch[i][j * 2]); |
| 424 |
} |
| 425 |
|
| 426 |
static uint8_t table_initialized = 0; |
| 427 |
|
| 428 |
static void initializeTables(void) { |
| 429 |
makeTllTable(); |
| 430 |
makeRksTable(); |
| 431 |
makeSinTable(); |
| 432 |
makeDefaultPatch(); |
| 433 |
table_initialized = 1; |
| 434 |
} |
| 435 |
|
| 436 |
/********************************************************* |
| 437 |
|
| 438 |
Synthesizing |
| 439 |
|
| 440 |
*********************************************************/ |
| 441 |
#define SLOT_BD1 12 |
| 442 |
#define SLOT_BD2 13 |
| 443 |
#define SLOT_HH 14 |
| 444 |
#define SLOT_SD 15 |
| 445 |
#define SLOT_TOM 16 |
| 446 |
#define SLOT_CYM 17 |
| 447 |
|
| 448 |
/* utility macros */ |
| 449 |
#define MOD(o, x) (&(o)->slot[(x) << 1]) |
| 450 |
#define CAR(o, x) (&(o)->slot[((x) << 1) | 1]) |
| 451 |
#define BIT(s, b) (((s) >> (b)) & 1) |
| 452 |
|
| 453 |
#if OPLL_DEBUG |
| 454 |
static void _debug_print_patch(OPLL_SLOT *slot) { |
| 455 |
OPLL_PATCH *p = slot->patch; |
| 456 |
printf("[slot#%d am:%d pm:%d eg:%d kr:%d ml:%d kl:%d tl:%d ws:%d fb:%d A:%d D:%d S:%d R:%d]\n", slot->number, // |
| 457 |
p->AM, p->PM, p->EG, p->KR, p->ML, // |
| 458 |
p->KL, p->TL, p->WS, p->FB, // |
| 459 |
p->AR, p->DR, p->SL, p->RR); |
| 460 |
} |
| 461 |
|
| 462 |
static char *_debug_eg_state_name(OPLL_SLOT *slot) { |
| 463 |
switch (slot->eg_state) { |
| 464 |
case ATTACK: |
| 465 |
return "attack"; |
| 466 |
case DECAY: |
| 467 |
return "decay"; |
| 468 |
case SUSTAIN: |
| 469 |
return "sustain"; |
| 470 |
case RELEASE: |
| 471 |
return "release"; |
| 472 |
case DAMP: |
| 473 |
return "damp"; |
| 474 |
default: |
| 475 |
return "unknown"; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
static INLINE void _debug_print_slot_info(OPLL_SLOT *slot) { |
| 480 |
char *name = _debug_eg_state_name(slot); |
| 481 |
printf("[slot#%d state:%s fnum:%03x rate:%d-%d]\n", slot->number, name, slot->blk_fnum, slot->eg_rate_h, |
| 482 |
slot->eg_rate_l); |
| 483 |
_debug_print_patch(slot); |
| 484 |
fflush(stdout); |
| 485 |
} |
| 486 |
#endif |
| 487 |
|
| 488 |
static INLINE int get_parameter_rate(OPLL_SLOT *slot) { |
| 489 |
|
| 490 |
if ((slot->type & 1) == 0 && slot->key_flag == 0) { |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
switch (slot->eg_state) { |
| 495 |
case ATTACK: |
| 496 |
return slot->patch->AR; |
| 497 |
case DECAY: |
| 498 |
return slot->patch->DR; |
| 499 |
case SUSTAIN: |
| 500 |
return slot->patch->EG ? 0 : slot->patch->RR; |
| 501 |
case RELEASE: |
| 502 |
if (slot->sus_flag) { |
| 503 |
return 5; |
| 504 |
} else if (slot->patch->EG) { |
| 505 |
return slot->patch->RR; |
| 506 |
} else { |
| 507 |
return 7; |
| 508 |
} |
| 509 |
case DAMP: |
| 510 |
return DAMPER_RATE; |
| 511 |
default: |
| 512 |
return 0; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
enum SLOT_UPDATE_FLAG { |
| 517 |
UPDATE_WS = 1, |
| 518 |
UPDATE_TLL = 2, |
| 519 |
UPDATE_RKS = 4, |
| 520 |
UPDATE_EG = 8, |
| 521 |
UPDATE_ALL = 255, |
| 522 |
}; |
| 523 |
|
| 524 |
static INLINE void request_update(OPLL_SLOT *slot, int flag) { slot->update_requests |= flag; } |
| 525 |
|
| 526 |
static void commit_slot_update(OPLL_SLOT *slot) { |
| 527 |
|
| 528 |
#if OPLL_DEBUG |
| 529 |
if (slot->last_eg_state != slot->eg_state) { |
| 530 |
_debug_print_slot_info(slot); |
| 531 |
slot->last_eg_state = slot->eg_state; |
| 532 |
} |
| 533 |
#endif |
| 534 |
|
| 535 |
if (slot->update_requests & UPDATE_WS) { |
| 536 |
slot->wave_table = wave_table_map[slot->patch->WS]; |
| 537 |
} |
| 538 |
|
| 539 |
if (slot->update_requests & UPDATE_TLL) { |
| 540 |
if ((slot->type & 1) == 0) { |
| 541 |
slot->tll = tll_table[slot->blk_fnum >> 5][slot->patch->TL][slot->patch->KL]; |
| 542 |
} else { |
| 543 |
slot->tll = tll_table[slot->blk_fnum >> 5][slot->volume][slot->patch->KL]; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
if (slot->update_requests & UPDATE_RKS) { |
| 548 |
slot->rks = rks_table[slot->blk_fnum >> 8][slot->patch->KR]; |
| 549 |
} |
| 550 |
|
| 551 |
if (slot->update_requests & (UPDATE_RKS | UPDATE_EG)) { |
| 552 |
int p_rate = get_parameter_rate(slot); |
| 553 |
|
| 554 |
if (p_rate == 0) { |
| 555 |
slot->eg_shift = 0; |
| 556 |
slot->eg_rate_h = 0; |
| 557 |
slot->eg_rate_l = 0; |
| 558 |
return; |
| 559 |
} |
| 560 |
|
| 561 |
slot->eg_rate_h = min(15, p_rate + (slot->rks >> 2)); |
| 562 |
slot->eg_rate_l = slot->rks & 3; |
| 563 |
if (slot->eg_state == ATTACK) { |
| 564 |
slot->eg_shift = (0 < slot->eg_rate_h && slot->eg_rate_h < 12) ? (13 - slot->eg_rate_h) : 0; |
| 565 |
} else { |
| 566 |
slot->eg_shift = (slot->eg_rate_h < 13) ? (13 - slot->eg_rate_h) : 0; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
slot->update_requests = 0; |
| 571 |
} |
| 572 |
|
| 573 |
static void reset_slot(OPLL_SLOT *slot, int number) { |
| 574 |
slot->number = number; |
| 575 |
slot->type = number % 2; |
| 576 |
slot->pg_keep = 0; |
| 577 |
slot->wave_table = wave_table_map[0]; |
| 578 |
slot->pg_phase = 0; |
| 579 |
slot->output[0] = 0; |
| 580 |
slot->output[1] = 0; |
| 581 |
slot->eg_state = RELEASE; |
| 582 |
slot->eg_shift = 0; |
| 583 |
slot->rks = 0; |
| 584 |
slot->tll = 0; |
| 585 |
slot->key_flag = 0; |
| 586 |
slot->sus_flag = 0; |
| 587 |
slot->blk_fnum = 0; |
| 588 |
slot->blk = 0; |
| 589 |
slot->fnum = 0; |
| 590 |
slot->volume = 0; |
| 591 |
slot->pg_out = 0; |
| 592 |
slot->eg_out = EG_MUTE; |
| 593 |
slot->patch = &null_patch; |
| 594 |
} |
| 595 |
|
| 596 |
static INLINE void slotOn(OPLL *opll, int i) { |
| 597 |
OPLL_SLOT *slot = &opll->slot[i]; |
| 598 |
slot->key_flag = 1; |
| 599 |
slot->eg_state = DAMP; |
| 600 |
request_update(slot, UPDATE_EG); |
| 601 |
} |
| 602 |
|
| 603 |
static INLINE void slotOff(OPLL *opll, int i) { |
| 604 |
OPLL_SLOT *slot = &opll->slot[i]; |
| 605 |
slot->key_flag = 0; |
| 606 |
if (slot->type & 1) { |
| 607 |
slot->eg_state = RELEASE; |
| 608 |
request_update(slot, UPDATE_EG); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
static INLINE void update_key_status(OPLL *opll) { |
| 613 |
const uint8_t r14 = opll->reg[0x0e]; |
| 614 |
const uint8_t rhythm_mode = BIT(r14, 5); |
| 615 |
uint32_t new_slot_key_status = 0; |
| 616 |
uint32_t updated_status; |
| 617 |
int ch; |
| 618 |
|
| 619 |
for (ch = 0; ch < 9; ch++) |
| 620 |
if (opll->reg[0x20 + ch] & 0x10) |
| 621 |
new_slot_key_status |= 3 << (ch * 2); |
| 622 |
|
| 623 |
if (rhythm_mode) { |
| 624 |
if (r14 & 0x10) |
| 625 |
new_slot_key_status |= 3 << SLOT_BD1; |
| 626 |
|
| 627 |
if (r14 & 0x01) |
| 628 |
new_slot_key_status |= 1 << SLOT_HH; |
| 629 |
|
| 630 |
if (r14 & 0x08) |
| 631 |
new_slot_key_status |= 1 << SLOT_SD; |
| 632 |
|
| 633 |
if (r14 & 0x04) |
| 634 |
new_slot_key_status |= 1 << SLOT_TOM; |
| 635 |
|
| 636 |
if (r14 & 0x02) |
| 637 |
new_slot_key_status |= 1 << SLOT_CYM; |
| 638 |
} |
| 639 |
|
| 640 |
updated_status = opll->slot_key_status ^ new_slot_key_status; |
| 641 |
|
| 642 |
if (updated_status) { |
| 643 |
int i; |
| 644 |
for (i = 0; i < 18; i++) |
| 645 |
if (BIT(updated_status, i)) { |
| 646 |
if (BIT(new_slot_key_status, i)) { |
| 647 |
slotOn(opll, i); |
| 648 |
} else { |
| 649 |
slotOff(opll, i); |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
opll->slot_key_status = new_slot_key_status; |
| 655 |
} |
| 656 |
|
| 657 |
static INLINE void set_patch(OPLL *opll, int32_t ch, int32_t num) { |
| 658 |
opll->patch_number[ch] = num; |
| 659 |
MOD(opll, ch)->patch = &opll->patch[num * 2 + 0]; |
| 660 |
CAR(opll, ch)->patch = &opll->patch[num * 2 + 1]; |
| 661 |
request_update(MOD(opll, ch), UPDATE_ALL); |
| 662 |
request_update(CAR(opll, ch), UPDATE_ALL); |
| 663 |
} |
| 664 |
|
| 665 |
static INLINE void set_sus_flag(OPLL *opll, int ch, int flag) { |
| 666 |
CAR(opll, ch)->sus_flag = flag; |
| 667 |
request_update(CAR(opll, ch), UPDATE_EG); |
| 668 |
if (MOD(opll, ch)->type & 1) { |
| 669 |
MOD(opll, ch)->sus_flag = flag; |
| 670 |
request_update(MOD(opll, ch), UPDATE_EG); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
/* set volume ( volume : 6bit, register value << 2 ) */ |
| 675 |
static INLINE void set_volume(OPLL *opll, int ch, int volume) { |
| 676 |
CAR(opll, ch)->volume = volume; |
| 677 |
request_update(CAR(opll, ch), UPDATE_TLL); |
| 678 |
} |
| 679 |
|
| 680 |
static INLINE void set_slot_volume(OPLL_SLOT *slot, int volume) { |
| 681 |
slot->volume = volume; |
| 682 |
request_update(slot, UPDATE_TLL); |
| 683 |
} |
| 684 |
|
| 685 |
/* set f-Nnmber ( fnum : 9bit ) */ |
| 686 |
static INLINE void set_fnumber(OPLL *opll, int ch, int fnum) { |
| 687 |
OPLL_SLOT *car = CAR(opll, ch); |
| 688 |
OPLL_SLOT *mod = MOD(opll, ch); |
| 689 |
car->fnum = fnum; |
| 690 |
car->blk_fnum = (car->blk_fnum & 0xe00) | (fnum & 0x1ff); |
| 691 |
mod->fnum = fnum; |
| 692 |
mod->blk_fnum = (mod->blk_fnum & 0xe00) | (fnum & 0x1ff); |
| 693 |
request_update(car, UPDATE_EG | UPDATE_RKS | UPDATE_TLL); |
| 694 |
request_update(mod, UPDATE_EG | UPDATE_RKS | UPDATE_TLL); |
| 695 |
} |
| 696 |
|
| 697 |
/* set block data (blk : 3bit ) */ |
| 698 |
static INLINE void set_block(OPLL *opll, int ch, int blk) { |
| 699 |
OPLL_SLOT *car = CAR(opll, ch); |
| 700 |
OPLL_SLOT *mod = MOD(opll, ch); |
| 701 |
car->blk = blk; |
| 702 |
car->blk_fnum = ((blk & 7) << 9) | (car->blk_fnum & 0x1ff); |
| 703 |
mod->blk = blk; |
| 704 |
mod->blk_fnum = ((blk & 7) << 9) | (mod->blk_fnum & 0x1ff); |
| 705 |
request_update(car, UPDATE_EG | UPDATE_RKS | UPDATE_TLL); |
| 706 |
request_update(mod, UPDATE_EG | UPDATE_RKS | UPDATE_TLL); |
| 707 |
} |
| 708 |
|
| 709 |
static INLINE void update_rhythm_mode(OPLL *opll) { |
| 710 |
const uint8_t new_rhythm_mode = (opll->reg[0x0e] >> 5) & 1; |
| 711 |
|
| 712 |
if (opll->rhythm_mode != new_rhythm_mode) { |
| 713 |
|
| 714 |
if (new_rhythm_mode) { |
| 715 |
opll->slot[SLOT_HH].type = 3; |
| 716 |
opll->slot[SLOT_HH].pg_keep = 1; |
| 717 |
opll->slot[SLOT_SD].type = 3; |
| 718 |
opll->slot[SLOT_TOM].type = 3; |
| 719 |
opll->slot[SLOT_CYM].type = 3; |
| 720 |
opll->slot[SLOT_CYM].pg_keep = 1; |
| 721 |
set_patch(opll, 6, 16); |
| 722 |
set_patch(opll, 7, 17); |
| 723 |
set_patch(opll, 8, 18); |
| 724 |
set_slot_volume(&opll->slot[SLOT_HH], ((opll->reg[0x37] >> 4) & 15) << 2); |
| 725 |
set_slot_volume(&opll->slot[SLOT_TOM], ((opll->reg[0x38] >> 4) & 15) << 2); |
| 726 |
} else { |
| 727 |
opll->slot[SLOT_HH].type = 0; |
| 728 |
opll->slot[SLOT_HH].pg_keep = 0; |
| 729 |
opll->slot[SLOT_SD].type = 1; |
| 730 |
opll->slot[SLOT_TOM].type = 0; |
| 731 |
opll->slot[SLOT_CYM].type = 1; |
| 732 |
opll->slot[SLOT_CYM].pg_keep = 0; |
| 733 |
set_patch(opll, 6, opll->reg[0x36] >> 4); |
| 734 |
set_patch(opll, 7, opll->reg[0x37] >> 4); |
| 735 |
set_patch(opll, 8, opll->reg[0x38] >> 4); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
opll->rhythm_mode = new_rhythm_mode; |
| 740 |
} |
| 741 |
|
| 742 |
static void update_ampm(OPLL *opll) { |
| 743 |
if (opll->test_flag & 2) { |
| 744 |
opll->pm_phase = 0; |
| 745 |
opll->am_phase = 0; |
| 746 |
} else { |
| 747 |
opll->pm_phase += (opll->test_flag & 8) ? 1024 : 1; |
| 748 |
opll->am_phase += (opll->test_flag & 8) ? 64 : 1; |
| 749 |
} |
| 750 |
opll->lfo_am = am_table[(opll->am_phase >> 6) % sizeof(am_table)]; |
| 751 |
} |
| 752 |
|
| 753 |
static void update_noise(OPLL *opll, int cycle) { |
| 754 |
int i; |
| 755 |
for (i = 0; i < cycle; i++) { |
| 756 |
if (opll->noise & 1) { |
| 757 |
opll->noise ^= 0x800200; |
| 758 |
} |
| 759 |
opll->noise >>= 1; |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
static void update_short_noise(OPLL *opll) { |
| 764 |
const uint32_t pg_hh = opll->slot[SLOT_HH].pg_out; |
| 765 |
const uint32_t pg_cym = opll->slot[SLOT_CYM].pg_out; |
| 766 |
|
| 767 |
const uint8_t h_bit2 = BIT(pg_hh, PG_BITS - 8); |
| 768 |
const uint8_t h_bit7 = BIT(pg_hh, PG_BITS - 3); |
| 769 |
const uint8_t h_bit3 = BIT(pg_hh, PG_BITS - 7); |
| 770 |
|
| 771 |
const uint8_t c_bit3 = BIT(pg_cym, PG_BITS - 7); |
| 772 |
const uint8_t c_bit5 = BIT(pg_cym, PG_BITS - 5); |
| 773 |
|
| 774 |
opll->short_noise = (h_bit2 ^ h_bit7) | (h_bit3 ^ c_bit5) | (c_bit3 ^ c_bit5); |
| 775 |
} |
| 776 |
|
| 777 |
static INLINE void calc_phase(OPLL_SLOT *slot, int32_t pm_phase, uint8_t reset) { |
| 778 |
const int8_t pm = slot->patch->PM ? pm_table[(slot->fnum >> 6) & 7][(pm_phase >> 10) & 7] : 0; |
| 779 |
if (reset) { |
| 780 |
slot->pg_phase = 0; |
| 781 |
} |
| 782 |
slot->pg_phase += (((slot->fnum & 0x1ff) * 2 + pm) * ml_table[slot->patch->ML]) << slot->blk >> 2; |
| 783 |
slot->pg_phase &= (DP_WIDTH - 1); |
| 784 |
slot->pg_out = slot->pg_phase >> DP_BASE_BITS; |
| 785 |
} |
| 786 |
|
| 787 |
static INLINE uint8_t lookup_attack_step(OPLL_SLOT *slot, uint32_t counter) { |
| 788 |
int index; |
| 789 |
|
| 790 |
switch (slot->eg_rate_h) { |
| 791 |
case 12: |
| 792 |
index = (counter & 0xc) >> 1; |
| 793 |
return 4 - eg_step_tables[slot->eg_rate_l][index]; |
| 794 |
case 13: |
| 795 |
index = (counter & 0xc) >> 1; |
| 796 |
return 3 - eg_step_tables[slot->eg_rate_l][index]; |
| 797 |
case 14: |
| 798 |
index = (counter & 0xc) >> 1; |
| 799 |
return 2 - eg_step_tables[slot->eg_rate_l][index]; |
| 800 |
case 0: |
| 801 |
case 15: |
| 802 |
return 0; |
| 803 |
default: |
| 804 |
index = counter >> slot->eg_shift; |
| 805 |
return eg_step_tables[slot->eg_rate_l][index & 7] ? 4 : 0; |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
static INLINE uint8_t lookup_decay_step(OPLL_SLOT *slot, uint32_t counter) { |
| 810 |
int index; |
| 811 |
|
| 812 |
switch (slot->eg_rate_h) { |
| 813 |
case 0: |
| 814 |
return 0; |
| 815 |
case 13: |
| 816 |
index = ((counter & 0xc) >> 1) | (counter & 1); |
| 817 |
return eg_step_tables[slot->eg_rate_l][index]; |
| 818 |
case 14: |
| 819 |
index = ((counter & 0xc) >> 1); |
| 820 |
return eg_step_tables[slot->eg_rate_l][index] + 1; |
| 821 |
case 15: |
| 822 |
return 2; |
| 823 |
default: |
| 824 |
index = counter >> slot->eg_shift; |
| 825 |
return eg_step_tables[slot->eg_rate_l][index & 7]; |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
static INLINE void start_envelope(OPLL_SLOT *slot) { |
| 830 |
if (min(15, slot->patch->AR + (slot->rks >> 2)) == 15) { |
| 831 |
slot->eg_state = DECAY; |
| 832 |
slot->eg_out = 0; |
| 833 |
} else { |
| 834 |
slot->eg_state = ATTACK; |
| 835 |
} |
| 836 |
request_update(slot, UPDATE_EG); |
| 837 |
} |
| 838 |
|
| 839 |
static INLINE void calc_envelope(OPLL_SLOT *slot, OPLL_SLOT *buddy, uint16_t eg_counter, uint8_t test) { |
| 840 |
|
| 841 |
uint32_t mask = (1 << slot->eg_shift) - 1; |
| 842 |
uint8_t s; |
| 843 |
|
| 844 |
if (slot->eg_state == ATTACK) { |
| 845 |
if (0 < slot->eg_out && 0 < slot->eg_rate_h && (eg_counter & mask & ~3) == 0) { |
| 846 |
s = lookup_attack_step(slot, eg_counter); |
| 847 |
if (0 < s) { |
| 848 |
slot->eg_out = max(0, ((int)slot->eg_out - (slot->eg_out >> s) - 1)); |
| 849 |
} |
| 850 |
} |
| 851 |
} else { |
| 852 |
if (slot->eg_rate_h > 0 && (eg_counter & mask) == 0) { |
| 853 |
slot->eg_out = min(EG_MUTE, slot->eg_out + lookup_decay_step(slot, eg_counter)); |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
switch (slot->eg_state) { |
| 858 |
case DAMP: |
| 859 |
// DAMP to ATTACK transition is occured when the envelope reaches EG_MAX (max attenuation but it's not mute). |
| 860 |
// Do not forget to check (eg_counter & mask) == 0 to synchronize it with the progress of the envelope. |
| 861 |
if (slot->eg_out >= EG_MAX && (eg_counter & mask) == 0) { |
| 862 |
start_envelope(slot); |
| 863 |
if (slot->type & 1) { |
| 864 |
if (!slot->pg_keep) { |
| 865 |
slot->pg_phase = 0; |
| 866 |
} |
| 867 |
if (buddy && !buddy->pg_keep) { |
| 868 |
buddy->pg_phase = 0; |
| 869 |
} |
| 870 |
} |
| 871 |
} |
| 872 |
break; |
| 873 |
|
| 874 |
case ATTACK: |
| 875 |
if (slot->eg_out == 0) { |
| 876 |
slot->eg_state = DECAY; |
| 877 |
request_update(slot, UPDATE_EG); |
| 878 |
} |
| 879 |
break; |
| 880 |
|
| 881 |
case DECAY: |
| 882 |
// DECAY to SUSTAIN transition must be checked at every cycle regardless of the conditions of the envelope rate and |
| 883 |
// counter. i.e. the transition is not synchronized with the progress of the envelope. |
| 884 |
if ((slot->eg_out >> 3) == slot->patch->SL) { |
| 885 |
slot->eg_state = SUSTAIN; |
| 886 |
request_update(slot, UPDATE_EG); |
| 887 |
} |
| 888 |
break; |
| 889 |
|
| 890 |
case SUSTAIN: |
| 891 |
case RELEASE: |
| 892 |
default: |
| 893 |
break; |
| 894 |
} |
| 895 |
|
| 896 |
if (test) { |
| 897 |
slot->eg_out = 0; |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
static void update_slots(OPLL *opll) { |
| 902 |
int i; |
| 903 |
opll->eg_counter++; |
| 904 |
|
| 905 |
for (i = 0; i < 18; i++) { |
| 906 |
OPLL_SLOT *slot = &opll->slot[i]; |
| 907 |
OPLL_SLOT *buddy = NULL; |
| 908 |
if (slot->type == 0) { |
| 909 |
buddy = &opll->slot[i + 1]; |
| 910 |
} |
| 911 |
if (slot->type == 1) { |
| 912 |
buddy = &opll->slot[i - 1]; |
| 913 |
} |
| 914 |
if (slot->update_requests) { |
| 915 |
commit_slot_update(slot); |
| 916 |
} |
| 917 |
calc_envelope(slot, buddy, opll->eg_counter, opll->test_flag & 1); |
| 918 |
calc_phase(slot, opll->pm_phase, opll->test_flag & 4); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
/* output: -4095...4095 */ |
| 923 |
static INLINE int16_t lookup_exp_table(uint16_t i) { |
| 924 |
/* from andete's expression */ |
| 925 |
int16_t t = (exp_table[(i & 0xff) ^ 0xff] + 1024); |
| 926 |
int16_t res = t >> ((i & 0x7f00) >> 8); |
| 927 |
return ((i & 0x8000) ? ~res : res) << 1; |
| 928 |
} |
| 929 |
|
| 930 |
static INLINE int16_t to_linear(uint16_t h, OPLL_SLOT *slot, int16_t am) { |
| 931 |
uint16_t att; |
| 932 |
if (slot->eg_out > EG_MAX) |
| 933 |
return 0; |
| 934 |
|
| 935 |
att = min(EG_MUTE, (slot->eg_out + slot->tll + am)) << 4; |
| 936 |
return lookup_exp_table(h + att); |
| 937 |
} |
| 938 |
|
| 939 |
static INLINE int16_t calc_slot_car(OPLL *opll, int ch, int16_t fm) { |
| 940 |
OPLL_SLOT *slot = CAR(opll, ch); |
| 941 |
|
| 942 |
uint8_t am = slot->patch->AM ? opll->lfo_am : 0; |
| 943 |
|
| 944 |
slot->output[1] = slot->output[0]; |
| 945 |
slot->output[0] = to_linear(slot->wave_table[(slot->pg_out + 2 * (fm >> 1)) & (PG_WIDTH - 1)], slot, am); |
| 946 |
|
| 947 |
return slot->output[0]; |
| 948 |
} |
| 949 |
|
| 950 |
static INLINE int16_t calc_slot_mod(OPLL *opll, int ch) { |
| 951 |
OPLL_SLOT *slot = MOD(opll, ch); |
| 952 |
|
| 953 |
int16_t fm = slot->patch->FB > 0 ? (slot->output[1] + slot->output[0]) >> (9 - slot->patch->FB) : 0; |
| 954 |
uint8_t am = slot->patch->AM ? opll->lfo_am : 0; |
| 955 |
|
| 956 |
slot->output[1] = slot->output[0]; |
| 957 |
slot->output[0] = to_linear(slot->wave_table[(slot->pg_out + fm) & (PG_WIDTH - 1)], slot, am); |
| 958 |
|
| 959 |
return slot->output[0]; |
| 960 |
} |
| 961 |
|
| 962 |
static INLINE int16_t calc_slot_tom(OPLL *opll) { |
| 963 |
OPLL_SLOT *slot = MOD(opll, 8); |
| 964 |
|
| 965 |
return to_linear(slot->wave_table[slot->pg_out], slot, 0); |
| 966 |
} |
| 967 |
|
| 968 |
/* Specify phase offset directly based on 10-bit (1024-length) sine table */ |
| 969 |
#define _PD(phase) ((PG_BITS < 10) ? (phase >> (10 - PG_BITS)) : (phase << (PG_BITS - 10))) |
| 970 |
|
| 971 |
static INLINE int16_t calc_slot_snare(OPLL *opll) { |
| 972 |
OPLL_SLOT *slot = CAR(opll, 7); |
| 973 |
|
| 974 |
uint32_t phase; |
| 975 |
|
| 976 |
if (BIT(slot->pg_out, PG_BITS - 2)) |
| 977 |
phase = (opll->noise & 1) ? _PD(0x300) : _PD(0x200); |
| 978 |
else |
| 979 |
phase = (opll->noise & 1) ? _PD(0x0) : _PD(0x100); |
| 980 |
|
| 981 |
return to_linear(slot->wave_table[phase], slot, 0); |
| 982 |
} |
| 983 |
|
| 984 |
static INLINE int16_t calc_slot_cym(OPLL *opll) { |
| 985 |
OPLL_SLOT *slot = CAR(opll, 8); |
| 986 |
|
| 987 |
uint32_t phase = opll->short_noise ? _PD(0x300) : _PD(0x100); |
| 988 |
|
| 989 |
return to_linear(slot->wave_table[phase], slot, 0); |
| 990 |
} |
| 991 |
|
| 992 |
static INLINE int16_t calc_slot_hat(OPLL *opll) { |
| 993 |
OPLL_SLOT *slot = MOD(opll, 7); |
| 994 |
|
| 995 |
uint32_t phase; |
| 996 |
|
| 997 |
if (opll->short_noise) |
| 998 |
phase = (opll->noise & 1) ? _PD(0x2d0) : _PD(0x234); |
| 999 |
else |
| 1000 |
phase = (opll->noise & 1) ? _PD(0x34) : _PD(0xd0); |
| 1001 |
|
| 1002 |
return to_linear(slot->wave_table[phase], slot, 0); |
| 1003 |
} |
| 1004 |
|
| 1005 |
#define _MO(x) (-(x) >> 1) |
| 1006 |
#define _RO(x) (x) |
| 1007 |
|
| 1008 |
static void update_output(OPLL *opll) { |
| 1009 |
int16_t *out; |
| 1010 |
int i; |
| 1011 |
|
| 1012 |
update_ampm(opll); |
| 1013 |
update_short_noise(opll); |
| 1014 |
update_slots(opll); |
| 1015 |
|
| 1016 |
out = opll->ch_out; |
| 1017 |
|
| 1018 |
/* CH1-6 */ |
| 1019 |
for (i = 0; i < 6; i++) { |
| 1020 |
if (!(opll->mask & OPLL_MASK_CH(i))) { |
| 1021 |
out[i] = _MO(calc_slot_car(opll, i, calc_slot_mod(opll, i))); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* CH7 */ |
| 1026 |
if (!opll->rhythm_mode) { |
| 1027 |
if (!(opll->mask & OPLL_MASK_CH(6))) { |
| 1028 |
out[6] = _MO(calc_slot_car(opll, 6, calc_slot_mod(opll, 6))); |
| 1029 |
} |
| 1030 |
} else { |
| 1031 |
if (!(opll->mask & OPLL_MASK_BD)) { |
| 1032 |
out[9] = _RO(calc_slot_car(opll, 6, calc_slot_mod(opll, 6))); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
update_noise(opll, 14); |
| 1036 |
|
| 1037 |
/* CH8 */ |
| 1038 |
if (!opll->rhythm_mode) { |
| 1039 |
if (!(opll->mask & OPLL_MASK_CH(7))) { |
| 1040 |
out[7] = _MO(calc_slot_car(opll, 7, calc_slot_mod(opll, 7))); |
| 1041 |
} |
| 1042 |
} else { |
| 1043 |
if (!(opll->mask & OPLL_MASK_HH)) { |
| 1044 |
out[10] = _RO(calc_slot_hat(opll)); |
| 1045 |
} |
| 1046 |
if (!(opll->mask & OPLL_MASK_SD)) { |
| 1047 |
out[11] = _RO(calc_slot_snare(opll)); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
update_noise(opll, 2); |
| 1051 |
|
| 1052 |
/* CH9 */ |
| 1053 |
if (!opll->rhythm_mode) { |
| 1054 |
if (!(opll->mask & OPLL_MASK_CH(8))) { |
| 1055 |
out[8] = _MO(calc_slot_car(opll, 8, calc_slot_mod(opll, 8))); |
| 1056 |
} |
| 1057 |
} else { |
| 1058 |
if (!(opll->mask & OPLL_MASK_TOM)) { |
| 1059 |
out[12] = _RO(calc_slot_tom(opll)); |
| 1060 |
} |
| 1061 |
if (!(opll->mask & OPLL_MASK_CYM)) { |
| 1062 |
out[13] = _RO(calc_slot_cym(opll)); |
| 1063 |
} |
| 1064 |
} |
| 1065 |
update_noise(opll, 2); |
| 1066 |
} |
| 1067 |
|
| 1068 |
INLINE static void mix_output(OPLL *opll) { |
| 1069 |
int16_t out = 0; |
| 1070 |
int i; |
| 1071 |
for (i = 0; i < 14; i++) { |
| 1072 |
out += opll->ch_out[i]; |
| 1073 |
} |
| 1074 |
if (opll->conv) { |
| 1075 |
OPLL_RateConv_putData(opll->conv, 0, out); |
| 1076 |
} else { |
| 1077 |
opll->mix_out[0] = out; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
INLINE static void mix_output_stereo(OPLL *opll) { |
| 1082 |
int16_t *out = opll->mix_out; |
| 1083 |
int i; |
| 1084 |
out[0] = out[1] = 0; |
| 1085 |
for (i = 0; i < 14; i++) { |
| 1086 |
if (opll->pan[i] & 2) |
| 1087 |
out[0] += (int16_t)(opll->ch_out[i] * opll->pan_fine[i][0]); |
| 1088 |
if (opll->pan[i] & 1) |
| 1089 |
out[1] += (int16_t)(opll->ch_out[i] * opll->pan_fine[i][1]); |
| 1090 |
} |
| 1091 |
if (opll->conv) { |
| 1092 |
OPLL_RateConv_putData(opll->conv, 0, out[0]); |
| 1093 |
OPLL_RateConv_putData(opll->conv, 1, out[1]); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
/*********************************************************** |
| 1098 |
|
| 1099 |
External Interfaces |
| 1100 |
|
| 1101 |
***********************************************************/ |
| 1102 |
|
| 1103 |
OPLL *OPLL_new(uint32_t clk, uint32_t rate) { |
| 1104 |
OPLL *opll; |
| 1105 |
int i; |
| 1106 |
|
| 1107 |
if (!table_initialized) { |
| 1108 |
initializeTables(); |
| 1109 |
} |
| 1110 |
|
| 1111 |
opll = (OPLL *)calloc(sizeof(OPLL), 1); |
| 1112 |
if (opll == NULL) |
| 1113 |
return NULL; |
| 1114 |
|
| 1115 |
for (i = 0; i < 19 * 2; i++) |
| 1116 |
memcpy(&opll->patch[i], &null_patch, sizeof(OPLL_PATCH)); |
| 1117 |
|
| 1118 |
opll->clk = clk; |
| 1119 |
opll->rate = rate; |
| 1120 |
opll->mask = 0; |
| 1121 |
opll->conv = NULL; |
| 1122 |
opll->mix_out[0] = 0; |
| 1123 |
opll->mix_out[1] = 0; |
| 1124 |
|
| 1125 |
OPLL_reset(opll); |
| 1126 |
OPLL_setChipType(opll, 0); |
| 1127 |
OPLL_resetPatch(opll, 0); |
| 1128 |
return opll; |
| 1129 |
} |
| 1130 |
|
| 1131 |
void OPLL_delete(OPLL *opll) { |
| 1132 |
if (opll->conv) { |
| 1133 |
OPLL_RateConv_delete(opll->conv); |
| 1134 |
opll->conv = NULL; |
| 1135 |
} |
| 1136 |
free(opll); |
| 1137 |
} |
| 1138 |
|
| 1139 |
static void reset_rate_conversion_params(OPLL *opll) { |
| 1140 |
const double f_out = opll->rate; |
| 1141 |
const double f_inp = opll->clk / 72.0; |
| 1142 |
|
| 1143 |
opll->out_time = 0; |
| 1144 |
opll->out_step = f_inp; |
| 1145 |
opll->inp_step = f_out; |
| 1146 |
|
| 1147 |
if (opll->conv) { |
| 1148 |
OPLL_RateConv_delete(opll->conv); |
| 1149 |
opll->conv = NULL; |
| 1150 |
} |
| 1151 |
|
| 1152 |
if (floor(f_inp) != f_out && floor(f_inp + 0.5) != f_out) { |
| 1153 |
opll->conv = OPLL_RateConv_new(f_inp, f_out, 2); |
| 1154 |
} |
| 1155 |
|
| 1156 |
if (opll->conv) { |
| 1157 |
OPLL_RateConv_reset(opll->conv); |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
void OPLL_reset(OPLL *opll) { |
| 1162 |
int i; |
| 1163 |
|
| 1164 |
if (!opll) |
| 1165 |
return; |
| 1166 |
|
| 1167 |
opll->adr = 0; |
| 1168 |
|
| 1169 |
opll->pm_phase = 0; |
| 1170 |
opll->am_phase = 0; |
| 1171 |
|
| 1172 |
opll->noise = 0x1; |
| 1173 |
opll->mask = 0; |
| 1174 |
|
| 1175 |
opll->rhythm_mode = 0; |
| 1176 |
opll->slot_key_status = 0; |
| 1177 |
opll->eg_counter = 0; |
| 1178 |
|
| 1179 |
reset_rate_conversion_params(opll); |
| 1180 |
|
| 1181 |
for (i = 0; i < 18; i++) |
| 1182 |
reset_slot(&opll->slot[i], i); |
| 1183 |
|
| 1184 |
for (i = 0; i < 9; i++) { |
| 1185 |
set_patch(opll, i, 0); |
| 1186 |
} |
| 1187 |
|
| 1188 |
for (i = 0; i < 0x40; i++) |
| 1189 |
OPLL_writeReg(opll, i, 0); |
| 1190 |
|
| 1191 |
for (i = 0; i < 15; i++) { |
| 1192 |
opll->pan[i] = 3; |
| 1193 |
opll->pan_fine[i][1] = opll->pan_fine[i][0] = 1.0f; |
| 1194 |
} |
| 1195 |
|
| 1196 |
for (i = 0; i < 14; i++) { |
| 1197 |
opll->ch_out[i] = 0; |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
void OPLL_forceRefresh(OPLL *opll) { |
| 1202 |
int i; |
| 1203 |
|
| 1204 |
if (opll == NULL) |
| 1205 |
return; |
| 1206 |
|
| 1207 |
for (i = 0; i < 9; i++) { |
| 1208 |
set_patch(opll, i, opll->patch_number[i]); |
| 1209 |
} |
| 1210 |
|
| 1211 |
for (i = 0; i < 18; i++) { |
| 1212 |
request_update(&opll->slot[i], UPDATE_ALL); |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
void OPLL_setRate(OPLL *opll, uint32_t rate) { |
| 1217 |
opll->rate = rate; |
| 1218 |
reset_rate_conversion_params(opll); |
| 1219 |
} |
| 1220 |
|
| 1221 |
void OPLL_setQuality(OPLL *opll, uint8_t q) {} |
| 1222 |
|
| 1223 |
void OPLL_setChipType(OPLL *opll, uint8_t type) { opll->chip_type = type; } |
| 1224 |
|
| 1225 |
void OPLL_writeReg(OPLL *opll, uint32_t reg, uint8_t data) { |
| 1226 |
int ch, i; |
| 1227 |
|
| 1228 |
if (reg >= 0x40) |
| 1229 |
return; |
| 1230 |
|
| 1231 |
/* mirror registers */ |
| 1232 |
if ((0x19 <= reg && reg <= 0x1f) || (0x29 <= reg && reg <= 0x2f) || (0x39 <= reg && reg <= 0x3f)) { |
| 1233 |
reg -= 9; |
| 1234 |
} |
| 1235 |
|
| 1236 |
// is the compiler stupid or what? |
| 1237 |
// -Wstringop-overflow seems to be the buggiest thing known to humanity... |
| 1238 |
if (reg<0x40) { |
| 1239 |
opll->reg[reg] = (uint8_t)data; |
| 1240 |
} |
| 1241 |
|
| 1242 |
switch (reg) { |
| 1243 |
case 0x00: |
| 1244 |
opll->patch[0].AM = (data >> 7) & 1; |
| 1245 |
opll->patch[0].PM = (data >> 6) & 1; |
| 1246 |
opll->patch[0].EG = (data >> 5) & 1; |
| 1247 |
opll->patch[0].KR = (data >> 4) & 1; |
| 1248 |
opll->patch[0].ML = (data)&15; |
| 1249 |
for (i = 0; i < 9; i++) { |
| 1250 |
if (opll->patch_number[i] == 0) { |
| 1251 |
request_update(MOD(opll, i), UPDATE_RKS | UPDATE_EG); |
| 1252 |
} |
| 1253 |
} |
| 1254 |
break; |
| 1255 |
|
| 1256 |
case 0x01: |
| 1257 |
opll->patch[1].AM = (data >> 7) & 1; |
| 1258 |
opll->patch[1].PM = (data >> 6) & 1; |
| 1259 |
opll->patch[1].EG = (data >> 5) & 1; |
| 1260 |
opll->patch[1].KR = (data >> 4) & 1; |
| 1261 |
opll->patch[1].ML = (data)&15; |
| 1262 |
for (i = 0; i < 9; i++) { |
| 1263 |
if (opll->patch_number[i] == 0) { |
| 1264 |
request_update(CAR(opll, i), UPDATE_RKS | UPDATE_EG); |
| 1265 |
} |
| 1266 |
} |
| 1267 |
break; |
| 1268 |
|
| 1269 |
case 0x02: |
| 1270 |
opll->patch[0].KL = (data >> 6) & 3; |
| 1271 |
opll->patch[0].TL = (data)&63; |
| 1272 |
for (i = 0; i < 9; i++) { |
| 1273 |
if (opll->patch_number[i] == 0) { |
| 1274 |
request_update(MOD(opll, i), UPDATE_TLL); |
| 1275 |
} |
| 1276 |
} |
| 1277 |
break; |
| 1278 |
|
| 1279 |
case 0x03: |
| 1280 |
opll->patch[1].KL = (data >> 6) & 3; |
| 1281 |
opll->patch[1].WS = (data >> 4) & 1; |
| 1282 |
opll->patch[0].WS = (data >> 3) & 1; |
| 1283 |
opll->patch[0].FB = (data)&7; |
| 1284 |
for (i = 0; i < 9; i++) { |
| 1285 |
if (opll->patch_number[i] == 0) { |
| 1286 |
request_update(MOD(opll, i), UPDATE_WS); |
| 1287 |
request_update(CAR(opll, i), UPDATE_WS | UPDATE_TLL); |
| 1288 |
} |
| 1289 |
} |
| 1290 |
break; |
| 1291 |
|
| 1292 |
case 0x04: |
| 1293 |
opll->patch[0].AR = (data >> 4) & 15; |
| 1294 |
opll->patch[0].DR = (data)&15; |
| 1295 |
for (i = 0; i < 9; i++) { |
| 1296 |
if (opll->patch_number[i] == 0) { |
| 1297 |
request_update(MOD(opll, i), UPDATE_EG); |
| 1298 |
} |
| 1299 |
} |
| 1300 |
break; |
| 1301 |
|
| 1302 |
case 0x05: |
| 1303 |
opll->patch[1].AR = (data >> 4) & 15; |
| 1304 |
opll->patch[1].DR = (data)&15; |
| 1305 |
for (i = 0; i < 9; i++) { |
| 1306 |
if (opll->patch_number[i] == 0) { |
| 1307 |
request_update(CAR(opll, i), UPDATE_EG); |
| 1308 |
} |
| 1309 |
} |
| 1310 |
break; |
| 1311 |
|
| 1312 |
case 0x06: |
| 1313 |
opll->patch[0].SL = (data >> 4) & 15; |
| 1314 |
opll->patch[0].RR = (data)&15; |
| 1315 |
for (i = 0; i < 9; i++) { |
| 1316 |
if (opll->patch_number[i] == 0) { |
| 1317 |
request_update(MOD(opll, i), UPDATE_EG); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
break; |
| 1321 |
|
| 1322 |
case 0x07: |
| 1323 |
opll->patch[1].SL = (data >> 4) & 15; |
| 1324 |
opll->patch[1].RR = (data)&15; |
| 1325 |
for (i = 0; i < 9; i++) { |
| 1326 |
if (opll->patch_number[i] == 0) { |
| 1327 |
request_update(CAR(opll, i), UPDATE_EG); |
| 1328 |
} |
| 1329 |
} |
| 1330 |
break; |
| 1331 |
|
| 1332 |
case 0x0e: |
| 1333 |
if (opll->chip_type == 1) |
| 1334 |
break; |
| 1335 |
update_rhythm_mode(opll); |
| 1336 |
update_key_status(opll); |
| 1337 |
break; |
| 1338 |
|
| 1339 |
case 0x0f: |
| 1340 |
opll->test_flag = data; |
| 1341 |
break; |
| 1342 |
|
| 1343 |
case 0x10: |
| 1344 |
case 0x11: |
| 1345 |
case 0x12: |
| 1346 |
case 0x13: |
| 1347 |
case 0x14: |
| 1348 |
case 0x15: |
| 1349 |
case 0x16: |
| 1350 |
case 0x17: |
| 1351 |
case 0x18: |
| 1352 |
ch = reg - 0x10; |
| 1353 |
set_fnumber(opll, ch, data + ((opll->reg[0x20 + ch] & 1) << 8)); |
| 1354 |
break; |
| 1355 |
|
| 1356 |
case 0x20: |
| 1357 |
case 0x21: |
| 1358 |
case 0x22: |
| 1359 |
case 0x23: |
| 1360 |
case 0x24: |
| 1361 |
case 0x25: |
| 1362 |
case 0x26: |
| 1363 |
case 0x27: |
| 1364 |
case 0x28: |
| 1365 |
ch = reg - 0x20; |
| 1366 |
set_fnumber(opll, ch, ((data & 1) << 8) + opll->reg[0x10 + ch]); |
| 1367 |
set_block(opll, ch, (data >> 1) & 7); |
| 1368 |
set_sus_flag(opll, ch, (data >> 5) & 1); |
| 1369 |
update_key_status(opll); |
| 1370 |
break; |
| 1371 |
|
| 1372 |
case 0x30: |
| 1373 |
case 0x31: |
| 1374 |
case 0x32: |
| 1375 |
case 0x33: |
| 1376 |
case 0x34: |
| 1377 |
case 0x35: |
| 1378 |
case 0x36: |
| 1379 |
case 0x37: |
| 1380 |
case 0x38: |
| 1381 |
if ((opll->reg[0x0e] & 32) && (reg >= 0x36)) { |
| 1382 |
switch (reg) { |
| 1383 |
case 0x37: |
| 1384 |
set_slot_volume(MOD(opll, 7), ((data >> 4) & 15) << 2); |
| 1385 |
break; |
| 1386 |
case 0x38: |
| 1387 |
set_slot_volume(MOD(opll, 8), ((data >> 4) & 15) << 2); |
| 1388 |
break; |
| 1389 |
default: |
| 1390 |
break; |
| 1391 |
} |
| 1392 |
} else { |
| 1393 |
set_patch(opll, reg - 0x30, (data >> 4) & 15); |
| 1394 |
} |
| 1395 |
set_volume(opll, reg - 0x30, (data & 15) << 2); |
| 1396 |
break; |
| 1397 |
|
| 1398 |
default: |
| 1399 |
break; |
| 1400 |
} |
| 1401 |
} |
| 1402 |
|
| 1403 |
void OPLL_writeIO(OPLL *opll, uint32_t adr, uint8_t val) { |
| 1404 |
if (adr & 1) |
| 1405 |
OPLL_writeReg(opll, opll->adr, val); |
| 1406 |
else |
| 1407 |
opll->adr = val; |
| 1408 |
} |
| 1409 |
|
| 1410 |
void OPLL_setPan(OPLL *opll, uint32_t ch, uint8_t pan) { opll->pan[ch & 15] = pan; } |
| 1411 |
|
| 1412 |
void OPLL_setPanFine(OPLL *opll, uint32_t ch, float pan[2]) { |
| 1413 |
opll->pan_fine[ch & 15][0] = pan[0]; |
| 1414 |
opll->pan_fine[ch & 15][1] = pan[1]; |
| 1415 |
} |
| 1416 |
|
| 1417 |
void OPLL_dumpToPatch(const uint8_t *dump, OPLL_PATCH *patch) { |
| 1418 |
patch[0].AM = (dump[0] >> 7) & 1; |
| 1419 |
patch[1].AM = (dump[1] >> 7) & 1; |
| 1420 |
patch[0].PM = (dump[0] >> 6) & 1; |
| 1421 |
patch[1].PM = (dump[1] >> 6) & 1; |
| 1422 |
patch[0].EG = (dump[0] >> 5) & 1; |
| 1423 |
patch[1].EG = (dump[1] >> 5) & 1; |
| 1424 |
patch[0].KR = (dump[0] >> 4) & 1; |
| 1425 |
patch[1].KR = (dump[1] >> 4) & 1; |
| 1426 |
patch[0].ML = (dump[0]) & 15; |
| 1427 |
patch[1].ML = (dump[1]) & 15; |
| 1428 |
patch[0].KL = (dump[2] >> 6) & 3; |
| 1429 |
patch[1].KL = (dump[3] >> 6) & 3; |
| 1430 |
patch[0].TL = (dump[2]) & 63; |
| 1431 |
patch[1].TL = 0; |
| 1432 |
patch[0].FB = (dump[3]) & 7; |
| 1433 |
patch[1].FB = 0; |
| 1434 |
patch[0].WS = (dump[3] >> 3) & 1; |
| 1435 |
patch[1].WS = (dump[3] >> 4) & 1; |
| 1436 |
patch[0].AR = (dump[4] >> 4) & 15; |
| 1437 |
patch[1].AR = (dump[5] >> 4) & 15; |
| 1438 |
patch[0].DR = (dump[4]) & 15; |
| 1439 |
patch[1].DR = (dump[5]) & 15; |
| 1440 |
patch[0].SL = (dump[6] >> 4) & 15; |
| 1441 |
patch[1].SL = (dump[7] >> 4) & 15; |
| 1442 |
patch[0].RR = (dump[6]) & 15; |
| 1443 |
patch[1].RR = (dump[7]) & 15; |
| 1444 |
} |
| 1445 |
|
| 1446 |
void OPLL_getDefaultPatch(int32_t type, int32_t num, OPLL_PATCH *patch) { |
| 1447 |
OPLL_dumpToPatch(default_inst[type] + num * 8, patch); |
| 1448 |
} |
| 1449 |
|
| 1450 |
void OPLL_setPatch(OPLL *opll, const uint8_t *dump) { |
| 1451 |
OPLL_PATCH patch[2]; |
| 1452 |
int i; |
| 1453 |
for (i = 0; i < 19; i++) { |
| 1454 |
OPLL_dumpToPatch(dump + i * 8, patch); |
| 1455 |
memcpy(&opll->patch[i * 2 + 0], &patch[0], sizeof(OPLL_PATCH)); |
| 1456 |
memcpy(&opll->patch[i * 2 + 1], &patch[1], sizeof(OPLL_PATCH)); |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
void OPLL_patchToDump(const OPLL_PATCH *patch, uint8_t *dump) { |
| 1461 |
dump[0] = (uint8_t)((patch[0].AM << 7) + (patch[0].PM << 6) + (patch[0].EG << 5) + (patch[0].KR << 4) + patch[0].ML); |
| 1462 |
dump[1] = (uint8_t)((patch[1].AM << 7) + (patch[1].PM << 6) + (patch[1].EG << 5) + (patch[1].KR << 4) + patch[1].ML); |
| 1463 |
dump[2] = (uint8_t)((patch[0].KL << 6) + patch[0].TL); |
| 1464 |
dump[3] = (uint8_t)((patch[1].KL << 6) + (patch[1].WS << 4) + (patch[0].WS << 3) + patch[0].FB); |
| 1465 |
dump[4] = (uint8_t)((patch[0].AR << 4) + patch[0].DR); |
| 1466 |
dump[5] = (uint8_t)((patch[1].AR << 4) + patch[1].DR); |
| 1467 |
dump[6] = (uint8_t)((patch[0].SL << 4) + patch[0].RR); |
| 1468 |
dump[7] = (uint8_t)((patch[1].SL << 4) + patch[1].RR); |
| 1469 |
} |
| 1470 |
|
| 1471 |
void OPLL_copyPatch(OPLL *opll, int32_t num, OPLL_PATCH *patch) { |
| 1472 |
memcpy(&opll->patch[num], patch, sizeof(OPLL_PATCH)); |
| 1473 |
} |
| 1474 |
|
| 1475 |
void OPLL_resetPatch(OPLL *opll, uint8_t type) { |
| 1476 |
int i; |
| 1477 |
for (i = 0; i < 19 * 2; i++) |
| 1478 |
OPLL_copyPatch(opll, i, &default_patch[type % OPLL_TONE_NUM][i]); |
| 1479 |
} |
| 1480 |
|
| 1481 |
int16_t OPLL_calc(OPLL *opll) { |
| 1482 |
while (opll->out_step > opll->out_time) { |
| 1483 |
opll->out_time += opll->inp_step; |
| 1484 |
update_output(opll); |
| 1485 |
mix_output(opll); |
| 1486 |
} |
| 1487 |
opll->out_time -= opll->out_step; |
| 1488 |
if (opll->conv) { |
| 1489 |
opll->mix_out[0] = OPLL_RateConv_getData(opll->conv, 0); |
| 1490 |
} |
| 1491 |
return opll->mix_out[0]; |
| 1492 |
} |
| 1493 |
|
| 1494 |
void OPLL_calcStereo(OPLL *opll, int32_t out[2]) { |
| 1495 |
while (opll->out_step > opll->out_time) { |
| 1496 |
opll->out_time += opll->inp_step; |
| 1497 |
update_output(opll); |
| 1498 |
mix_output_stereo(opll); |
| 1499 |
} |
| 1500 |
opll->out_time -= opll->out_step; |
| 1501 |
if (opll->conv) { |
| 1502 |
out[0] = OPLL_RateConv_getData(opll->conv, 0); |
| 1503 |
out[1] = OPLL_RateConv_getData(opll->conv, 1); |
| 1504 |
} else { |
| 1505 |
out[0] = opll->mix_out[0]; |
| 1506 |
out[1] = opll->mix_out[1]; |
| 1507 |
} |
| 1508 |
} |
| 1509 |
|
| 1510 |
uint32_t OPLL_setMask(OPLL *opll, uint32_t mask) { |
| 1511 |
uint32_t ret; |
| 1512 |
|
| 1513 |
if (opll) { |
| 1514 |
ret = opll->mask; |
| 1515 |
opll->mask = mask; |
| 1516 |
return ret; |
| 1517 |
} else |
| 1518 |
return 0; |
| 1519 |
} |
| 1520 |
|
| 1521 |
uint32_t OPLL_toggleMask(OPLL *opll, uint32_t mask) { |
| 1522 |
uint32_t ret; |
| 1523 |
|
| 1524 |
if (opll) { |
| 1525 |
ret = opll->mask; |
| 1526 |
opll->mask ^= mask; |
| 1527 |
return ret; |
| 1528 |
} else |
| 1529 |
return 0; |
| 1530 |
} |