opal.h 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. // This is the Opal OPL3 emulator from Reality Adlib Tracker v2.0a (http://www.3eality.com/productions/reality-adlib-tracker).
  2. // It was released by Shayde/Reality into the public domain.
  3. // Minor modifications to silence some warnings and fix a bug in the envelope generator have been applied.
  4. // Additional fixes by JP Cimalando.
  5. /*
  6. The Opal OPL3 emulator.
  7. Note: this is not a complete emulator, just enough for Reality Adlib Tracker tunes.
  8. Missing features compared to a real OPL3:
  9. - Timers/interrupts
  10. - OPL3 enable bit (it defaults to always on)
  11. - CSW mode
  12. - Test register
  13. - Percussion mode
  14. */
  15. #include <cstdint>
  16. //==================================================================================================
  17. // Opal class.
  18. //==================================================================================================
  19. class Opal {
  20. class Channel;
  21. // Various constants
  22. enum {
  23. OPL3SampleRate = 49716,
  24. NumChannels = 18,
  25. NumOperators = 36,
  26. EnvOff = -1,
  27. EnvAtt,
  28. EnvDec,
  29. EnvSus,
  30. EnvRel,
  31. };
  32. // A single FM operator
  33. class Operator {
  34. public:
  35. Operator();
  36. void SetMaster(Opal *opal) { Master = opal; }
  37. void SetChannel(Channel *chan) { Chan = chan; }
  38. int16_t Output(uint16_t keyscalenum, uint32_t phase_step, int16_t vibrato, int16_t mod = 0, int16_t fbshift = 0);
  39. void SetKeyOn(bool on);
  40. void SetTremoloEnable(bool on);
  41. void SetVibratoEnable(bool on);
  42. void SetSustainMode(bool on);
  43. void SetEnvelopeScaling(bool on);
  44. void SetFrequencyMultiplier(uint16_t scale);
  45. void SetKeyScale(uint16_t scale);
  46. void SetOutputLevel(uint16_t level);
  47. void SetAttackRate(uint16_t rate);
  48. void SetDecayRate(uint16_t rate);
  49. void SetSustainLevel(uint16_t level);
  50. void SetReleaseRate(uint16_t rate);
  51. void SetWaveform(uint16_t wave);
  52. void ComputeRates();
  53. void ComputeKeyScaleLevel();
  54. protected:
  55. Opal * Master; // Master object
  56. Channel * Chan; // Owning channel
  57. uint32_t Phase; // The current offset in the selected waveform
  58. uint16_t Waveform; // The waveform id this operator is using
  59. uint16_t FreqMultTimes2; // Frequency multiplier * 2
  60. int EnvelopeStage; // Which stage the envelope is at (see Env* enums above)
  61. int16_t EnvelopeLevel; // 0 - $1FF, 0 being the loudest
  62. uint16_t OutputLevel; // 0 - $FF
  63. uint16_t AttackRate;
  64. uint16_t DecayRate;
  65. uint16_t SustainLevel;
  66. uint16_t ReleaseRate;
  67. uint16_t AttackShift;
  68. uint16_t AttackMask;
  69. uint16_t AttackAdd;
  70. const uint16_t *AttackTab;
  71. uint16_t DecayShift;
  72. uint16_t DecayMask;
  73. uint16_t DecayAdd;
  74. const uint16_t *DecayTab;
  75. uint16_t ReleaseShift;
  76. uint16_t ReleaseMask;
  77. uint16_t ReleaseAdd;
  78. const uint16_t *ReleaseTab;
  79. uint16_t KeyScaleShift;
  80. uint16_t KeyScaleLevel;
  81. int16_t Out[2];
  82. bool KeyOn;
  83. bool KeyScaleRate; // Affects envelope rate scaling
  84. bool SustainMode; // Whether to sustain during the sustain phase, or release instead
  85. bool TremoloEnable;
  86. bool VibratoEnable;
  87. };
  88. // A single channel, which can contain two or more operators
  89. class Channel {
  90. public:
  91. Channel();
  92. void SetMaster(Opal *opal) { Master = opal; }
  93. void SetOperators(Operator *a, Operator *b, Operator *c, Operator *d) {
  94. Op[0] = a;
  95. Op[1] = b;
  96. Op[2] = c;
  97. Op[3] = d;
  98. if (a)
  99. a->SetChannel(this);
  100. if (b)
  101. b->SetChannel(this);
  102. if (c)
  103. c->SetChannel(this);
  104. if (d)
  105. d->SetChannel(this);
  106. }
  107. void Output(int16_t &left, int16_t &right);
  108. void SetEnable(bool on) { Enable = on; }
  109. void SetChannelPair(Channel *pair) { ChannelPair = pair; }
  110. void SetFrequencyLow(uint16_t freq);
  111. void SetFrequencyHigh(uint16_t freq);
  112. void SetKeyOn(bool on);
  113. void SetOctave(uint16_t oct);
  114. void SetLeftEnable(bool on);
  115. void SetRightEnable(bool on);
  116. void SetFeedback(uint16_t val);
  117. void SetModulationType(uint16_t type);
  118. uint16_t GetFreq() const { return Freq; }
  119. uint16_t GetOctave() const { return Octave; }
  120. uint16_t GetKeyScaleNumber() const { return KeyScaleNumber; }
  121. uint16_t GetModulationType() const { return ModulationType; }
  122. Channel * GetChannelPair() const { return ChannelPair; }
  123. void ComputeKeyScaleNumber();
  124. protected:
  125. void ComputePhaseStep();
  126. Operator * Op[4];
  127. Opal * Master; // Master object
  128. uint16_t Freq; // Frequency; actually it's a phase stepping value
  129. uint16_t Octave; // Also known as "block" in Yamaha parlance
  130. uint32_t PhaseStep;
  131. uint16_t KeyScaleNumber;
  132. uint16_t FeedbackShift;
  133. uint16_t ModulationType;
  134. Channel * ChannelPair;
  135. bool Enable;
  136. bool LeftEnable, RightEnable;
  137. };
  138. public:
  139. Opal(int sample_rate);
  140. Opal(const Opal &) = delete;
  141. Opal(Opal &&) = delete;
  142. ~Opal();
  143. void SetSampleRate(int sample_rate);
  144. void Port(uint16_t reg_num, uint8_t val);
  145. void Sample(int16_t *left, int16_t *right);
  146. protected:
  147. void Init(int sample_rate);
  148. void Output(int16_t &left, int16_t &right);
  149. int32_t SampleRate;
  150. int32_t SampleAccum;
  151. int16_t LastOutput[2], CurrOutput[2];
  152. Channel Chan[NumChannels];
  153. Operator Op[NumOperators];
  154. // uint16_t ExpTable[256];
  155. // uint16_t LogSinTable[256];
  156. uint16_t Clock;
  157. uint16_t TremoloClock;
  158. uint16_t TremoloLevel;
  159. uint16_t VibratoTick;
  160. uint16_t VibratoClock;
  161. bool NoteSel;
  162. bool TremoloDepth;
  163. bool VibratoDepth;
  164. static const uint16_t RateTables[4][8];
  165. static const uint16_t ExpTable[256];
  166. static const uint16_t LogSinTable[256];
  167. };
  168. //--------------------------------------------------------------------------------------------------
  169. const uint16_t Opal::RateTables[4][8] = {
  170. { 1, 0, 1, 0, 1, 0, 1, 0 },
  171. { 1, 0, 1, 0, 0, 0, 1, 0 },
  172. { 1, 0, 0, 0, 1, 0, 0, 0 },
  173. { 1, 0, 0, 0, 0, 0, 0, 0 },
  174. };
  175. //--------------------------------------------------------------------------------------------------
  176. const uint16_t Opal::ExpTable[0x100] = {
  177. 1018, 1013, 1007, 1002, 996, 991, 986, 980, 975, 969, 964, 959, 953, 948, 942, 937,
  178. 932, 927, 921, 916, 911, 906, 900, 895, 890, 885, 880, 874, 869, 864, 859, 854,
  179. 849, 844, 839, 834, 829, 824, 819, 814, 809, 804, 799, 794, 789, 784, 779, 774,
  180. 770, 765, 760, 755, 750, 745, 741, 736, 731, 726, 722, 717, 712, 708, 703, 698,
  181. 693, 689, 684, 680, 675, 670, 666, 661, 657, 652, 648, 643, 639, 634, 630, 625,
  182. 621, 616, 612, 607, 603, 599, 594, 590, 585, 581, 577, 572, 568, 564, 560, 555,
  183. 551, 547, 542, 538, 534, 530, 526, 521, 517, 513, 509, 505, 501, 496, 492, 488,
  184. 484, 480, 476, 472, 468, 464, 460, 456, 452, 448, 444, 440, 436, 432, 428, 424,
  185. 420, 416, 412, 409, 405, 401, 397, 393, 389, 385, 382, 378, 374, 370, 367, 363,
  186. 359, 355, 352, 348, 344, 340, 337, 333, 329, 326, 322, 318, 315, 311, 308, 304,
  187. 300, 297, 293, 290, 286, 283, 279, 276, 272, 268, 265, 262, 258, 255, 251, 248,
  188. 244, 241, 237, 234, 231, 227, 224, 220, 217, 214, 210, 207, 204, 200, 197, 194,
  189. 190, 187, 184, 181, 177, 174, 171, 168, 164, 161, 158, 155, 152, 148, 145, 142,
  190. 139, 136, 133, 130, 126, 123, 120, 117, 114, 111, 108, 105, 102, 99, 96, 93,
  191. 90, 87, 84, 81, 78, 75, 72, 69, 66, 63, 60, 57, 54, 51, 48, 45,
  192. 42, 40, 37, 34, 31, 28, 25, 22, 20, 17, 14, 11, 8, 6, 3, 0,
  193. };
  194. //--------------------------------------------------------------------------------------------------
  195. const uint16_t Opal::LogSinTable[0x100] = {
  196. 2137, 1731, 1543, 1419, 1326, 1252, 1190, 1137, 1091, 1050, 1013, 979, 949, 920, 894, 869,
  197. 846, 825, 804, 785, 767, 749, 732, 717, 701, 687, 672, 659, 646, 633, 621, 609,
  198. 598, 587, 576, 566, 556, 546, 536, 527, 518, 509, 501, 492, 484, 476, 468, 461,
  199. 453, 446, 439, 432, 425, 418, 411, 405, 399, 392, 386, 380, 375, 369, 363, 358,
  200. 352, 347, 341, 336, 331, 326, 321, 316, 311, 307, 302, 297, 293, 289, 284, 280,
  201. 276, 271, 267, 263, 259, 255, 251, 248, 244, 240, 236, 233, 229, 226, 222, 219,
  202. 215, 212, 209, 205, 202, 199, 196, 193, 190, 187, 184, 181, 178, 175, 172, 169,
  203. 167, 164, 161, 159, 156, 153, 151, 148, 146, 143, 141, 138, 136, 134, 131, 129,
  204. 127, 125, 122, 120, 118, 116, 114, 112, 110, 108, 106, 104, 102, 100, 98, 96,
  205. 94, 92, 91, 89, 87, 85, 83, 82, 80, 78, 77, 75, 74, 72, 70, 69,
  206. 67, 66, 64, 63, 62, 60, 59, 57, 56, 55, 53, 52, 51, 49, 48, 47,
  207. 46, 45, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30,
  208. 29, 28, 27, 26, 25, 24, 23, 23, 22, 21, 20, 20, 19, 18, 17, 17,
  209. 16, 15, 15, 14, 13, 13, 12, 12, 11, 10, 10, 9, 9, 8, 8, 7,
  210. 7, 7, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2,
  211. 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
  212. };
  213. //==================================================================================================
  214. // This is the temporary code for generating the above tables. Maths and data from this nice
  215. // reverse-engineering effort:
  216. //
  217. // https://docs.google.com/document/d/18IGx18NQY_Q1PJVZ-bHywao9bhsDoAqoIn1rIm42nwo/edit
  218. //==================================================================================================
  219. #if 0
  220. #include <math.h>
  221. void GenerateTables() {
  222. // Build the exponentiation table (reversed from the official OPL3 ROM)
  223. FILE *fd = fopen("exptab.txt", "wb");
  224. if (fd) {
  225. for (int i = 0; i < 0x100; i++) {
  226. int v = (pow(2, (0xFF - i) / 256.0) - 1) * 1024 + 0.5;
  227. if (i & 15)
  228. fprintf(fd, " %4d,", v);
  229. else
  230. fprintf(fd, "\n\t%4d,", v);
  231. }
  232. fclose(fd);
  233. }
  234. // Build the log-sin table
  235. fd = fopen("sintab.txt", "wb");
  236. if (fd) {
  237. for (int i = 0; i < 0x100; i++) {
  238. int v = -log(sin((i + 0.5) * 3.1415926535897933 / 256 / 2)) / log(2) * 256 + 0.5;
  239. if (i & 15)
  240. fprintf(fd, " %4d,", v);
  241. else
  242. fprintf(fd, "\n\t%4d,", v);
  243. }
  244. fclose(fd);
  245. }
  246. }
  247. #endif
  248. //==================================================================================================
  249. // Constructor/destructor.
  250. //==================================================================================================
  251. Opal::Opal(int sample_rate) {
  252. Init(sample_rate);
  253. }
  254. //--------------------------------------------------------------------------------------------------
  255. Opal::~Opal() {
  256. }
  257. //==================================================================================================
  258. // Initialise the emulation.
  259. //==================================================================================================
  260. void Opal::Init(int sample_rate) {
  261. Clock = 0;
  262. TremoloClock = 0;
  263. TremoloLevel = 0;
  264. VibratoTick = 0;
  265. VibratoClock = 0;
  266. NoteSel = false;
  267. TremoloDepth = false;
  268. VibratoDepth = false;
  269. // // Build the exponentiation table (reversed from the official OPL3 ROM)
  270. // for (int i = 0; i < 0x100; i++)
  271. // ExpTable[i] = (pow(2, (0xFF - i) / 256.0) - 1) * 1024 + 0.5;
  272. //
  273. // // Build the log-sin table
  274. // for (int i = 0; i < 0x100; i++)
  275. // LogSinTable[i] = -log(sin((i + 0.5) * 3.1415926535897933 / 256 / 2)) / log(2) * 256 + 0.5;
  276. // Let sub-objects know where to find us
  277. for (int i = 0; i < NumOperators; i++)
  278. Op[i].SetMaster(this);
  279. for (int i = 0; i < NumChannels; i++)
  280. Chan[i].SetMaster(this);
  281. // Add the operators to the channels. Note, some channels can't use all the operators
  282. // FIXME: put this into a separate routine
  283. const int chan_ops[] = {
  284. 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20, 24, 25, 26, 30, 31, 32,
  285. };
  286. for (int i = 0; i < NumChannels; i++) {
  287. Channel *chan = &Chan[i];
  288. int op = chan_ops[i];
  289. if (i < 3 || (i >= 9 && i < 12))
  290. chan->SetOperators(&Op[op], &Op[op + 3], &Op[op + 6], &Op[op + 9]);
  291. else
  292. chan->SetOperators(&Op[op], &Op[op + 3], 0, 0);
  293. }
  294. // Initialise the operator rate data. We can't do this in the Operator constructor as it
  295. // relies on referencing the master and channel objects
  296. for (int i = 0; i < NumOperators; i++)
  297. Op[i].ComputeRates();
  298. SetSampleRate(sample_rate);
  299. }
  300. //==================================================================================================
  301. // Change the sample rate.
  302. //==================================================================================================
  303. void Opal::SetSampleRate(int sample_rate) {
  304. // Sanity
  305. if (sample_rate == 0)
  306. sample_rate = OPL3SampleRate;
  307. SampleRate = sample_rate;
  308. SampleAccum = 0;
  309. LastOutput[0] = LastOutput[1] = 0;
  310. CurrOutput[0] = CurrOutput[1] = 0;
  311. }
  312. //==================================================================================================
  313. // Write a value to an OPL3 register.
  314. //==================================================================================================
  315. void Opal::Port(uint16_t reg_num, uint8_t val) {
  316. static constexpr int8_t op_lookup[] = {
  317. // 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
  318. 0, 1, 2, 3, 4, 5, -1, -1, 6, 7, 8, 9, 10, 11, -1, -1,
  319. // 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
  320. 12, 13, 14, 15, 16, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  321. };
  322. uint16_t type = reg_num & 0xE0;
  323. // Is it BD, the one-off register stuck in the middle of the register array?
  324. if (reg_num == 0xBD) {
  325. TremoloDepth = (val & 0x80) != 0;
  326. VibratoDepth = (val & 0x40) != 0;
  327. return;
  328. }
  329. // Global registers
  330. if (type == 0x00) {
  331. // 4-OP enables
  332. if (reg_num == 0x104) {
  333. // Enable/disable channels based on which 4-op enables
  334. uint8_t mask = 1;
  335. for (int i = 0; i < 6; i++, mask <<= 1) {
  336. // The 4-op channels are 0, 1, 2, 9, 10, 11
  337. uint16_t chan = static_cast<uint16_t>(i < 3 ? i : i + 6);
  338. Channel *primary = &Chan[chan];
  339. Channel *secondary = &Chan[chan + 3];
  340. if (val & mask) {
  341. // Let primary channel know it's controlling the secondary channel
  342. primary->SetChannelPair(secondary);
  343. // Turn off the second channel in the pair
  344. secondary->SetEnable(false);
  345. } else {
  346. // Let primary channel know it's no longer controlling the secondary channel
  347. primary->SetChannelPair(0);
  348. // Turn on the second channel in the pair
  349. secondary->SetEnable(true);
  350. }
  351. }
  352. // CSW / Note-sel
  353. } else if (reg_num == 0x08) {
  354. NoteSel = (val & 0x40) != 0;
  355. // Get the channels to recompute the Key Scale No. as this varies based on NoteSel
  356. for (int i = 0; i < NumChannels; i++)
  357. Chan[i].ComputeKeyScaleNumber();
  358. }
  359. // Channel registers
  360. } else if (type >= 0xA0 && type <= 0xC0) {
  361. // Convert to channel number
  362. int chan_num = reg_num & 15;
  363. // Valid channel?
  364. if (chan_num >= 9)
  365. return;
  366. // Is it the other bank of channels?
  367. if (reg_num & 0x100)
  368. chan_num += 9;
  369. Channel &chan = Chan[chan_num];
  370. // Registers Ax and Bx affect both channels
  371. Channel *chans[2] = {&chan, chan.GetChannelPair()};
  372. int numchans = chans[1] ? 2 : 1;
  373. // Do specific registers
  374. switch (reg_num & 0xF0) {
  375. // Frequency low
  376. case 0xA0: {
  377. for (int i = 0; i < numchans; i++) {
  378. chans[i]->SetFrequencyLow(val);
  379. }
  380. break;
  381. }
  382. // Key-on / Octave / Frequency High
  383. case 0xB0: {
  384. for (int i = 0; i < numchans; i++) {
  385. chans[i]->SetKeyOn((val & 0x20) != 0);
  386. chans[i]->SetOctave(val >> 2 & 7);
  387. chans[i]->SetFrequencyHigh(val & 3);
  388. }
  389. break;
  390. }
  391. // Right Stereo Channel Enable / Left Stereo Channel Enable / Feedback Factor / Modulation Type
  392. case 0xC0: {
  393. chan.SetRightEnable((val & 0x20) != 0);
  394. chan.SetLeftEnable((val & 0x10) != 0);
  395. chan.SetFeedback(val >> 1 & 7);
  396. chan.SetModulationType(val & 1);
  397. break;
  398. }
  399. }
  400. // Operator registers
  401. } else if ((type >= 0x20 && type <= 0x80) || type == 0xE0) {
  402. // Convert to operator number
  403. int op_num = op_lookup[reg_num & 0x1F];
  404. // Valid register?
  405. if (op_num < 0)
  406. return;
  407. // Is it the other bank of operators?
  408. if (reg_num & 0x100)
  409. op_num += 18;
  410. Operator &op = Op[op_num];
  411. // Do specific registers
  412. switch (type) {
  413. // Tremolo Enable / Vibrato Enable / Sustain Mode / Envelope Scaling / Frequency Multiplier
  414. case 0x20: {
  415. op.SetTremoloEnable((val & 0x80) != 0);
  416. op.SetVibratoEnable((val & 0x40) != 0);
  417. op.SetSustainMode((val & 0x20) != 0);
  418. op.SetEnvelopeScaling((val & 0x10) != 0);
  419. op.SetFrequencyMultiplier(val & 15);
  420. break;
  421. }
  422. // Key Scale / Output Level
  423. case 0x40: {
  424. op.SetKeyScale(val >> 6);
  425. op.SetOutputLevel(val & 0x3F);
  426. break;
  427. }
  428. // Attack Rate / Decay Rate
  429. case 0x60: {
  430. op.SetAttackRate(val >> 4);
  431. op.SetDecayRate(val & 15);
  432. break;
  433. }
  434. // Sustain Level / Release Rate
  435. case 0x80: {
  436. op.SetSustainLevel(val >> 4);
  437. op.SetReleaseRate(val & 15);
  438. break;
  439. }
  440. // Waveform
  441. case 0xE0: {
  442. op.SetWaveform(val & 7);
  443. break;
  444. }
  445. }
  446. }
  447. }
  448. //==================================================================================================
  449. // Generate sample. Every time you call this you will get two signed 16-bit samples (one for each
  450. // stereo channel) which will sound correct when played back at the sample rate given when the
  451. // class was constructed.
  452. //==================================================================================================
  453. void Opal::Sample(int16_t *left, int16_t *right) {
  454. // If the destination sample rate is higher than the OPL3 sample rate, we need to skip ahead
  455. while (SampleAccum >= SampleRate) {
  456. LastOutput[0] = CurrOutput[0];
  457. LastOutput[1] = CurrOutput[1];
  458. Output(CurrOutput[0], CurrOutput[1]);
  459. SampleAccum -= SampleRate;
  460. }
  461. // Mix with the partial accumulation
  462. int32_t omblend = SampleRate - SampleAccum;
  463. *left = static_cast<uint16_t>((LastOutput[0] * omblend + CurrOutput[0] * SampleAccum) / SampleRate);
  464. *right = static_cast<uint16_t>((LastOutput[1] * omblend + CurrOutput[1] * SampleAccum) / SampleRate);
  465. SampleAccum += OPL3SampleRate;
  466. }
  467. //==================================================================================================
  468. // Produce final output from the chip. This is at the OPL3 sample-rate.
  469. //==================================================================================================
  470. void Opal::Output(int16_t &left, int16_t &right) {
  471. int32_t leftmix = 0, rightmix = 0;
  472. // Sum the output of each channel
  473. for (int i = 0; i < NumChannels; i++) {
  474. int16_t chanleft, chanright;
  475. Chan[i].Output(chanleft, chanright);
  476. leftmix += chanleft;
  477. rightmix += chanright;
  478. }
  479. // Clamp
  480. if (leftmix < -0x8000)
  481. left = -0x8000;
  482. else if (leftmix > 0x7FFF)
  483. left = 0x7FFF;
  484. else
  485. left = static_cast<uint16_t>(leftmix);
  486. if (rightmix < -0x8000)
  487. right = -0x8000;
  488. else if (rightmix > 0x7FFF)
  489. right = 0x7FFF;
  490. else
  491. right = static_cast<uint16_t>(rightmix);
  492. Clock++;
  493. // Tremolo. According to this post, the OPL3 tremolo is a 13,440 sample length triangle wave
  494. // with a peak at 26 and a trough at 0 and is simply added to the logarithmic level accumulator
  495. // http://forums.submarine.org.uk/phpBB/viewtopic.php?f=9&t=1171
  496. TremoloClock = (TremoloClock + 1) % 13440;
  497. TremoloLevel = ((TremoloClock < 13440 / 2) ? TremoloClock : 13440 - TremoloClock) / 256;
  498. if (!TremoloDepth)
  499. TremoloLevel >>= 2;
  500. // Vibrato. This appears to be a 8 sample long triangle wave with a magnitude of the three
  501. // high bits of the channel frequency, positive and negative, divided by two if the vibrato
  502. // depth is zero. It is only cycled every 1,024 samples.
  503. VibratoTick++;
  504. if (VibratoTick >= 1024) {
  505. VibratoTick = 0;
  506. VibratoClock = (VibratoClock + 1) & 7;
  507. }
  508. }
  509. //==================================================================================================
  510. // Channel constructor.
  511. //==================================================================================================
  512. Opal::Channel::Channel() {
  513. Master = 0;
  514. Freq = 0;
  515. Octave = 0;
  516. PhaseStep = 0;
  517. KeyScaleNumber = 0;
  518. FeedbackShift = 0;
  519. ModulationType = 0;
  520. ChannelPair = 0;
  521. Enable = true;
  522. LeftEnable = true;
  523. RightEnable = true;
  524. }
  525. //==================================================================================================
  526. // Produce output from channel.
  527. //==================================================================================================
  528. void Opal::Channel::Output(int16_t &left, int16_t &right) {
  529. // Has the channel been disabled? This is usually a result of the 4-op enables being used to
  530. // disable the secondary channel in each 4-op pair
  531. if (!Enable) {
  532. left = right = 0;
  533. return;
  534. }
  535. int16_t vibrato = (Freq >> 7) & 7;
  536. if (!Master->VibratoDepth)
  537. vibrato >>= 1;
  538. // 0 3 7 3 0 -3 -7 -3
  539. uint16_t clk = Master->VibratoClock;
  540. if (!(clk & 3))
  541. vibrato = 0; // Position 0 and 4 is zero
  542. else {
  543. if (clk & 1)
  544. vibrato >>= 1; // Odd positions are half the magnitude
  545. vibrato <<= Octave;
  546. if (clk & 4)
  547. vibrato = -vibrato; // The second half positions are negative
  548. }
  549. // Combine individual operator outputs
  550. int16_t out, acc;
  551. // Running in 4-op mode?
  552. if (ChannelPair) {
  553. // Get the secondary channel's modulation type. This is the only thing from the secondary
  554. // channel that is used
  555. if (ChannelPair->GetModulationType() == 0) {
  556. if (ModulationType == 0) {
  557. // feedback -> modulator -> modulator -> modulator -> carrier
  558. out = Op[0]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, FeedbackShift);
  559. out = Op[1]->Output(KeyScaleNumber, PhaseStep, vibrato, out, 0);
  560. out = Op[2]->Output(KeyScaleNumber, PhaseStep, vibrato, out, 0);
  561. out = Op[3]->Output(KeyScaleNumber, PhaseStep, vibrato, out, 0);
  562. } else {
  563. // (feedback -> carrier) + (modulator -> modulator -> carrier)
  564. out = Op[0]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, FeedbackShift);
  565. acc = Op[1]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, 0);
  566. acc = Op[2]->Output(KeyScaleNumber, PhaseStep, vibrato, acc, 0);
  567. out += Op[3]->Output(KeyScaleNumber, PhaseStep, vibrato, acc, 0);
  568. }
  569. } else {
  570. if (ModulationType == 0) {
  571. // (feedback -> modulator -> carrier) + (modulator -> carrier)
  572. out = Op[0]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, FeedbackShift);
  573. out = Op[1]->Output(KeyScaleNumber, PhaseStep, vibrato, out, 0);
  574. acc = Op[2]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, 0);
  575. out += Op[3]->Output(KeyScaleNumber, PhaseStep, vibrato, acc, 0);
  576. } else {
  577. // (feedback -> carrier) + (modulator -> carrier) + carrier
  578. out = Op[0]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, FeedbackShift);
  579. acc = Op[1]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, 0);
  580. out += Op[2]->Output(KeyScaleNumber, PhaseStep, vibrato, acc, 0);
  581. out += Op[3]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, 0);
  582. }
  583. }
  584. } else {
  585. // Standard 2-op mode
  586. if (ModulationType == 0) {
  587. // Frequency modulation (well, phase modulation technically)
  588. out = Op[0]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, FeedbackShift);
  589. out = Op[1]->Output(KeyScaleNumber, PhaseStep, vibrato, out, 0);
  590. } else {
  591. // Additive
  592. out = Op[0]->Output(KeyScaleNumber, PhaseStep, vibrato, 0, FeedbackShift);
  593. out += Op[1]->Output(KeyScaleNumber, PhaseStep, vibrato);
  594. }
  595. }
  596. left = LeftEnable ? out : 0;
  597. right = RightEnable ? out : 0;
  598. }
  599. //==================================================================================================
  600. // Set phase step for operators using this channel.
  601. //==================================================================================================
  602. void Opal::Channel::SetFrequencyLow(uint16_t freq) {
  603. Freq = (Freq & 0x300) | (freq & 0xFF);
  604. ComputePhaseStep();
  605. }
  606. //--------------------------------------------------------------------------------------------------
  607. void Opal::Channel::SetFrequencyHigh(uint16_t freq) {
  608. Freq = (Freq & 0xFF) | ((freq & 3) << 8);
  609. ComputePhaseStep();
  610. // Only the high bits of Freq affect the Key Scale No.
  611. ComputeKeyScaleNumber();
  612. }
  613. //==================================================================================================
  614. // Set the octave of the channel (0 to 7).
  615. //==================================================================================================
  616. void Opal::Channel::SetOctave(uint16_t oct) {
  617. Octave = oct & 7;
  618. ComputePhaseStep();
  619. ComputeKeyScaleNumber();
  620. }
  621. //==================================================================================================
  622. // Keys the channel on/off.
  623. //==================================================================================================
  624. void Opal::Channel::SetKeyOn(bool on) {
  625. Op[0]->SetKeyOn(on);
  626. Op[1]->SetKeyOn(on);
  627. }
  628. //==================================================================================================
  629. // Enable left stereo channel.
  630. //==================================================================================================
  631. void Opal::Channel::SetLeftEnable(bool on) {
  632. LeftEnable = on;
  633. }
  634. //==================================================================================================
  635. // Enable right stereo channel.
  636. //==================================================================================================
  637. void Opal::Channel::SetRightEnable(bool on) {
  638. RightEnable = on;
  639. }
  640. //==================================================================================================
  641. // Set the channel feedback amount.
  642. //==================================================================================================
  643. void Opal::Channel::SetFeedback(uint16_t val) {
  644. FeedbackShift = val ? 9 - val : 0;
  645. }
  646. //==================================================================================================
  647. // Set frequency modulation/additive modulation
  648. //==================================================================================================
  649. void Opal::Channel::SetModulationType(uint16_t type) {
  650. ModulationType = type;
  651. }
  652. //==================================================================================================
  653. // Compute the stepping factor for the operator waveform phase based on the frequency and octave
  654. // values of the channel.
  655. //==================================================================================================
  656. void Opal::Channel::ComputePhaseStep() {
  657. PhaseStep = uint32_t(Freq) << Octave;
  658. }
  659. //==================================================================================================
  660. // Compute the key scale number and key scale levels.
  661. //
  662. // From the Yamaha data sheet this is the block/octave number as bits 3-1, with bit 0 coming from
  663. // the MSB of the frequency if NoteSel is 1, and the 2nd MSB if NoteSel is 0.
  664. //==================================================================================================
  665. void Opal::Channel::ComputeKeyScaleNumber() {
  666. uint16_t lsb = Master->NoteSel ? Freq >> 9 : (Freq >> 8) & 1;
  667. KeyScaleNumber = Octave << 1 | lsb;
  668. // Get the channel operators to recompute their rates as they're dependent on this number. They
  669. // also need to recompute their key scale level
  670. for (int i = 0; i < 4; i++) {
  671. if (!Op[i])
  672. continue;
  673. Op[i]->ComputeRates();
  674. Op[i]->ComputeKeyScaleLevel();
  675. }
  676. }
  677. //==================================================================================================
  678. // Operator constructor.
  679. //==================================================================================================
  680. Opal::Operator::Operator() {
  681. Master = 0;
  682. Chan = 0;
  683. Phase = 0;
  684. Waveform = 0;
  685. FreqMultTimes2 = 1;
  686. EnvelopeStage = EnvOff;
  687. EnvelopeLevel = 0x1FF;
  688. AttackRate = 0;
  689. DecayRate = 0;
  690. SustainLevel = 0;
  691. ReleaseRate = 0;
  692. KeyScaleShift = 0;
  693. KeyScaleLevel = 0;
  694. Out[0] = Out[1] = 0;
  695. KeyOn = false;
  696. KeyScaleRate = false;
  697. SustainMode = false;
  698. TremoloEnable = false;
  699. VibratoEnable = false;
  700. }
  701. //==================================================================================================
  702. // Produce output from operator.
  703. //==================================================================================================
  704. int16_t Opal::Operator::Output(uint16_t /*keyscalenum*/, uint32_t phase_step, int16_t vibrato, int16_t mod, int16_t fbshift) {
  705. // Advance wave phase
  706. if (VibratoEnable)
  707. phase_step += vibrato;
  708. Phase += (phase_step * FreqMultTimes2) / 2;
  709. uint16_t level = (EnvelopeLevel + OutputLevel + KeyScaleLevel + (TremoloEnable ? Master->TremoloLevel : 0)) << 3;
  710. switch (EnvelopeStage) {
  711. // Attack stage
  712. case EnvAtt: {
  713. uint16_t add = ((AttackAdd >> AttackTab[Master->Clock >> AttackShift & 7]) * ~EnvelopeLevel) >> 3;
  714. if (AttackRate == 0)
  715. add = 0;
  716. if (AttackMask && (Master->Clock & AttackMask))
  717. add = 0;
  718. EnvelopeLevel += add;
  719. if (EnvelopeLevel <= 0) {
  720. EnvelopeLevel = 0;
  721. EnvelopeStage = EnvDec;
  722. }
  723. break;
  724. }
  725. // Decay stage
  726. case EnvDec: {
  727. uint16_t add = DecayAdd >> DecayTab[Master->Clock >> DecayShift & 7];
  728. if (DecayRate == 0)
  729. add = 0;
  730. if (DecayMask && (Master->Clock & DecayMask))
  731. add = 0;
  732. EnvelopeLevel += add;
  733. if (EnvelopeLevel >= SustainLevel) {
  734. EnvelopeLevel = SustainLevel;
  735. EnvelopeStage = EnvSus;
  736. }
  737. break;
  738. }
  739. // Sustain stage
  740. case EnvSus: {
  741. if (SustainMode)
  742. break;
  743. // Note: fall-through!
  744. [[fallthrough]];
  745. }
  746. // Release stage
  747. case EnvRel: {
  748. uint16_t add = ReleaseAdd >> ReleaseTab[Master->Clock >> ReleaseShift & 7];
  749. if (ReleaseRate == 0)
  750. add = 0;
  751. if (ReleaseMask && (Master->Clock & ReleaseMask))
  752. add = 0;
  753. EnvelopeLevel += add;
  754. if (EnvelopeLevel >= 0x1FF) {
  755. EnvelopeLevel = 0x1FF;
  756. EnvelopeStage = EnvOff;
  757. Out[0] = Out[1] = 0;
  758. return 0;
  759. }
  760. break;
  761. }
  762. // Envelope, and therefore the operator, is not running
  763. default:
  764. Out[0] = Out[1] = 0;
  765. return 0;
  766. }
  767. // Feedback? In that case we modulate by a blend of the last two samples
  768. if (fbshift)
  769. mod += (Out[0] + Out[1]) >> fbshift;
  770. uint16_t phase = static_cast<uint16_t>(Phase >> 10) + mod;
  771. uint16_t offset = phase & 0xFF;
  772. uint16_t logsin;
  773. bool negate = false;
  774. switch (Waveform) {
  775. //------------------------------------
  776. // Standard sine wave
  777. //------------------------------------
  778. case 0:
  779. if (phase & 0x100)
  780. offset ^= 0xFF;
  781. logsin = Master->LogSinTable[offset];
  782. negate = (phase & 0x200) != 0;
  783. break;
  784. //------------------------------------
  785. // Half sine wave
  786. //------------------------------------
  787. case 1:
  788. if (phase & 0x200)
  789. offset = 0;
  790. else if (phase & 0x100)
  791. offset ^= 0xFF;
  792. logsin = Master->LogSinTable[offset];
  793. break;
  794. //------------------------------------
  795. // Positive sine wave
  796. //------------------------------------
  797. case 2:
  798. if (phase & 0x100)
  799. offset ^= 0xFF;
  800. logsin = Master->LogSinTable[offset];
  801. break;
  802. //------------------------------------
  803. // Quarter positive sine wave
  804. //------------------------------------
  805. case 3:
  806. if (phase & 0x100)
  807. offset = 0;
  808. logsin = Master->LogSinTable[offset];
  809. break;
  810. //------------------------------------
  811. // Double-speed sine wave
  812. //------------------------------------
  813. case 4:
  814. if (phase & 0x200)
  815. offset = 0;
  816. else {
  817. if (phase & 0x80)
  818. offset ^= 0xFF;
  819. offset = (offset + offset) & 0xFF;
  820. negate = (phase & 0x100) != 0;
  821. }
  822. logsin = Master->LogSinTable[offset];
  823. break;
  824. //------------------------------------
  825. // Double-speed positive sine wave
  826. //------------------------------------
  827. case 5:
  828. if (phase & 0x200)
  829. offset = 0;
  830. else {
  831. offset = (offset + offset) & 0xFF;
  832. if (phase & 0x80)
  833. offset ^= 0xFF;
  834. }
  835. logsin = Master->LogSinTable[offset];
  836. break;
  837. //------------------------------------
  838. // Square wave
  839. //------------------------------------
  840. case 6:
  841. logsin = 0;
  842. negate = (phase & 0x200) != 0;
  843. break;
  844. //------------------------------------
  845. // Exponentiation wave
  846. //------------------------------------
  847. default:
  848. logsin = phase & 0x1FF;
  849. if (phase & 0x200) {
  850. logsin ^= 0x1FF;
  851. negate = true;
  852. }
  853. logsin <<= 3;
  854. break;
  855. }
  856. uint16_t mix = logsin + level;
  857. if (mix > 0x1FFF)
  858. mix = 0x1FFF;
  859. // From the OPLx decapsulated docs:
  860. // "When such a table is used for calculation of the exponential, the table is read at the
  861. // position given by the 8 LSB's of the input. The value + 1024 (the hidden bit) is then the
  862. // significand of the floating point output and the yet unused MSB's of the input are the
  863. // exponent of the floating point output."
  864. int16_t v = (Master->ExpTable[mix & 0xFF] + 1024u) >> (mix >> 8u);
  865. v += v;
  866. if (negate)
  867. v = ~v;
  868. // Keep last two results for feedback calculation
  869. Out[1] = Out[0];
  870. Out[0] = v;
  871. return v;
  872. }
  873. //==================================================================================================
  874. // Trigger operator.
  875. //==================================================================================================
  876. void Opal::Operator::SetKeyOn(bool on) {
  877. // Already on/off?
  878. if (KeyOn == on)
  879. return;
  880. KeyOn = on;
  881. if (on) {
  882. // The highest attack rate is instant; it bypasses the attack phase
  883. if (AttackRate == 15) {
  884. EnvelopeStage = EnvDec;
  885. EnvelopeLevel = 0;
  886. } else
  887. EnvelopeStage = EnvAtt;
  888. Phase = 0;
  889. } else {
  890. // Stopping current sound?
  891. if (EnvelopeStage != EnvOff && EnvelopeStage != EnvRel)
  892. EnvelopeStage = EnvRel;
  893. }
  894. }
  895. //==================================================================================================
  896. // Enable amplitude vibrato.
  897. //==================================================================================================
  898. void Opal::Operator::SetTremoloEnable(bool on) {
  899. TremoloEnable = on;
  900. }
  901. //==================================================================================================
  902. // Enable frequency vibrato.
  903. //==================================================================================================
  904. void Opal::Operator::SetVibratoEnable(bool on) {
  905. VibratoEnable = on;
  906. }
  907. //==================================================================================================
  908. // Sets whether we release or sustain during the sustain phase of the envelope. 'true' is to
  909. // sustain, otherwise release.
  910. //==================================================================================================
  911. void Opal::Operator::SetSustainMode(bool on) {
  912. SustainMode = on;
  913. }
  914. //==================================================================================================
  915. // Key scale rate. Sets how much the Key Scaling Number affects the envelope rates.
  916. //==================================================================================================
  917. void Opal::Operator::SetEnvelopeScaling(bool on) {
  918. KeyScaleRate = on;
  919. ComputeRates();
  920. }
  921. //==================================================================================================
  922. // Multiplies the phase frequency.
  923. //==================================================================================================
  924. void Opal::Operator::SetFrequencyMultiplier(uint16_t scale) {
  925. // Needs to be multiplied by two (and divided by two later when we use it) because the first
  926. // entry is actually .5
  927. const uint16_t mul_times_2[] = {
  928. 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 24, 24, 30, 30,
  929. };
  930. FreqMultTimes2 = mul_times_2[scale & 15];
  931. }
  932. //==================================================================================================
  933. // Attenuates output level towards higher pitch.
  934. //==================================================================================================
  935. void Opal::Operator::SetKeyScale(uint16_t scale) {
  936. static constexpr uint8_t kslShift[4] = { 8, 1, 2, 0 };
  937. KeyScaleShift = kslShift[scale];
  938. ComputeKeyScaleLevel();
  939. }
  940. //==================================================================================================
  941. // Sets the output level (volume) of the operator.
  942. //==================================================================================================
  943. void Opal::Operator::SetOutputLevel(uint16_t level) {
  944. OutputLevel = level * 4;
  945. }
  946. //==================================================================================================
  947. // Operator attack rate.
  948. //==================================================================================================
  949. void Opal::Operator::SetAttackRate(uint16_t rate) {
  950. AttackRate = rate;
  951. ComputeRates();
  952. }
  953. //==================================================================================================
  954. // Operator decay rate.
  955. //==================================================================================================
  956. void Opal::Operator::SetDecayRate(uint16_t rate) {
  957. DecayRate = rate;
  958. ComputeRates();
  959. }
  960. //==================================================================================================
  961. // Operator sustain level.
  962. //==================================================================================================
  963. void Opal::Operator::SetSustainLevel(uint16_t level) {
  964. SustainLevel = level < 15 ? level : 31;
  965. SustainLevel *= 16;
  966. }
  967. //==================================================================================================
  968. // Operator release rate.
  969. //==================================================================================================
  970. void Opal::Operator::SetReleaseRate(uint16_t rate) {
  971. ReleaseRate = rate;
  972. ComputeRates();
  973. }
  974. //==================================================================================================
  975. // Assign the waveform this operator will use.
  976. //==================================================================================================
  977. void Opal::Operator::SetWaveform(uint16_t wave) {
  978. Waveform = wave & 7;
  979. }
  980. //==================================================================================================
  981. // Compute actual rate from register rate. From the Yamaha data sheet:
  982. //
  983. // Actual rate = Rate value * 4 + Rof, if Rate value = 0, actual rate = 0
  984. //
  985. // Rof is set as follows depending on the KSR setting:
  986. //
  987. // Key scale 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  988. // KSR = 0 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3
  989. // KSR = 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  990. //
  991. // Note: zero rates are infinite, and are treated separately elsewhere
  992. //==================================================================================================
  993. void Opal::Operator::ComputeRates() {
  994. int combined_rate = AttackRate * 4 + (Chan->GetKeyScaleNumber() >> (KeyScaleRate ? 0 : 2));
  995. int rate_high = combined_rate >> 2;
  996. int rate_low = combined_rate & 3;
  997. AttackShift = static_cast<uint16_t>(rate_high < 12 ? 12 - rate_high : 0);
  998. AttackMask = (1 << AttackShift) - 1;
  999. AttackAdd = (rate_high < 12) ? 1 : 1 << (rate_high - 12);
  1000. AttackTab = Master->RateTables[rate_low];
  1001. // Attack rate of 15 is always instant
  1002. if (AttackRate == 15)
  1003. AttackAdd = 0xFFF;
  1004. combined_rate = DecayRate * 4 + (Chan->GetKeyScaleNumber() >> (KeyScaleRate ? 0 : 2));
  1005. rate_high = combined_rate >> 2;
  1006. rate_low = combined_rate & 3;
  1007. DecayShift = static_cast<uint16_t>(rate_high < 12 ? 12 - rate_high : 0);
  1008. DecayMask = (1 << DecayShift) - 1;
  1009. DecayAdd = (rate_high < 12) ? 1 : 1 << (rate_high - 12);
  1010. DecayTab = Master->RateTables[rate_low];
  1011. combined_rate = ReleaseRate * 4 + (Chan->GetKeyScaleNumber() >> (KeyScaleRate ? 0 : 2));
  1012. rate_high = combined_rate >> 2;
  1013. rate_low = combined_rate & 3;
  1014. ReleaseShift = static_cast<uint16_t>(rate_high < 12 ? 12 - rate_high : 0);
  1015. ReleaseMask = (1 << ReleaseShift) - 1;
  1016. ReleaseAdd = (rate_high < 12) ? 1 : 1 << (rate_high - 12);
  1017. ReleaseTab = Master->RateTables[rate_low];
  1018. }
  1019. //==================================================================================================
  1020. // Compute the operator's key scale level. This changes based on the channel frequency/octave and
  1021. // operator key scale value.
  1022. //==================================================================================================
  1023. void Opal::Operator::ComputeKeyScaleLevel() {
  1024. static constexpr uint8_t levtab[] = {
  1025. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1026. 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 12, 16, 20, 24, 28, 32,
  1027. 0, 0, 0, 0, 0, 12, 20, 28, 32, 40, 44, 48, 52, 56, 60, 64,
  1028. 0, 0, 0, 20, 32, 44, 52, 60, 64, 72, 76, 80, 84, 88, 92, 96,
  1029. 0, 0, 32, 52, 64, 76, 84, 92, 96, 104, 108, 112, 116, 120, 124, 128,
  1030. 0, 32, 64, 84, 96, 108, 116, 124, 128, 136, 140, 144, 148, 152, 156, 160,
  1031. 0, 64, 96, 116, 128, 140, 148, 156, 160, 168, 172, 176, 180, 184, 188, 192,
  1032. 0, 96, 128, 148, 160, 172, 180, 188, 192, 200, 204, 208, 212, 216, 220, 224,
  1033. };
  1034. // This uses a combined value of the top four bits of frequency with the octave/block
  1035. uint16_t i = (Chan->GetOctave() << 4) | (Chan->GetFreq() >> 6);
  1036. KeyScaleLevel = levtab[i] >> KeyScaleShift;
  1037. }