dosbox_opl.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. /*
  2. * Copyright (C) 2002-2013 The DOSBox Team
  3. * OPL2/OPL3 emulation library
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /*
  20. * Originally based on ADLIBEMU.C, an AdLib/OPL2 emulation library by Ken Silverman
  21. * Copyright (C) 1998-2001 Ken Silverman
  22. * Ken Silverman's official web site: "http://www.advsys.net/ken"
  23. */
  24. #include "dosbox_opl.h"
  25. #include <stdlib.h>
  26. #include <memory.h>
  27. #include <math.h>
  28. struct opl_chip_struct {
  29. /*static*/ Bit32u generator_add; // should be a chip parameter
  30. // per-chip variables
  31. op_type op[MAXOPERATORS];
  32. Bit8u status;
  33. Bit32u opl_index;
  34. #if defined(OPLTYPE_IS_OPL3)
  35. Bit8u adlibreg[512]; // adlib register set (including second set)
  36. Bit8u wave_sel[44]; // waveform selection
  37. #else
  38. Bit8u adlibreg[256]; // adlib register set
  39. Bit8u wave_sel[22]; // waveform selection
  40. #endif
  41. // vibrato/tremolo increment/counter
  42. Bit32u vibtab_pos;
  43. Bit32u vibtab_add;
  44. Bit32u tremtab_pos;
  45. Bit32u tremtab_add;
  46. /*static*/ fltype recipsamp; // inverse of sampling rate
  47. static Bit16s wavtable[WAVEPREC * 3]; // wave form table
  48. // vibrato/tremolo tables
  49. /*static*/ Bit32s vib_table[VIBTAB_SIZE];
  50. /*static*/ Bit32s trem_table[TREMTAB_SIZE * 2];
  51. /*static*/ Bit32s vibval_const[BLOCKBUF_SIZE];
  52. /*static*/ Bit32s tremval_const[BLOCKBUF_SIZE];
  53. // vibrato value tables (used per-operator)
  54. /*static*/ Bit32s vibval_var1[BLOCKBUF_SIZE];
  55. /*static*/ Bit32s vibval_var2[BLOCKBUF_SIZE];
  56. //static Bit32s vibval_var3[BLOCKBUF_SIZE];
  57. //static Bit32s vibval_var4[BLOCKBUF_SIZE];
  58. // vibrato/trmolo value table pointers
  59. /*static*/ Bit32s *vibval1, *vibval2, *vibval3, *vibval4;
  60. /*static*/ Bit32s *tremval1, *tremval2, *tremval3, *tremval4;
  61. // calculated frequency multiplication values (depend on sampling rate)
  62. /*static*/ fltype frqmul[16];
  63. // key scale levels
  64. static Bit8u kslev[8][16];
  65. };
  66. Bit16s opl_chip_struct::wavtable[WAVEPREC * 3];
  67. Bit8u opl_chip_struct::kslev[8][16];
  68. // key scale level lookup table
  69. static const fltype kslmul[4] = {
  70. 0.0, 0.5, 0.25, 1.0 // -> 0, 3, 1.5, 6 dB/oct
  71. };
  72. // frequency multiplicator lookup table
  73. static const fltype frqmul_tab[16] = {
  74. 0.5,1,2,3,4,5,6,7,8,9,10,10,12,12,15,15
  75. };
  76. // map a channel number to the register offset of the modulator (=register base)
  77. static const Bit8u modulatorbase[9] = {
  78. 0,1,2,
  79. 8,9,10,
  80. 16,17,18
  81. };
  82. // map a register base to a modulator operator number or operator number
  83. #if defined(OPLTYPE_IS_OPL3)
  84. static const Bit8u regbase2modop[44] = {
  85. 0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8, // first set
  86. 18,19,20,18,19,20,0,0,21,22,23,21,22,23,0,0,24,25,26,24,25,26 // second set
  87. };
  88. static const Bit8u regbase2op[44] = {
  89. 0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17, // first set
  90. 18,19,20,27,28,29,0,0,21,22,23,30,31,32,0,0,24,25,26,33,34,35 // second set
  91. };
  92. #else
  93. static const Bit8u regbase2modop[22] = {
  94. 0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8
  95. };
  96. static const Bit8u regbase2op[22] = {
  97. 0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17
  98. };
  99. #endif
  100. // start of the waveform
  101. static const Bit32u waveform[8] = {
  102. WAVEPREC,
  103. WAVEPREC>>1,
  104. WAVEPREC,
  105. (WAVEPREC*3)>>2,
  106. 0,
  107. 0,
  108. (WAVEPREC*5)>>2,
  109. WAVEPREC<<1
  110. };
  111. // length of the waveform as mask
  112. static const Bit32u wavemask[8] = {
  113. WAVEPREC-1,
  114. WAVEPREC-1,
  115. (WAVEPREC>>1)-1,
  116. (WAVEPREC>>1)-1,
  117. WAVEPREC-1,
  118. ((WAVEPREC*3)>>2)-1,
  119. WAVEPREC>>1,
  120. WAVEPREC-1
  121. };
  122. // where the first entry resides
  123. static const Bit32u wavestart[8] = {
  124. 0,
  125. WAVEPREC>>1,
  126. 0,
  127. WAVEPREC>>2,
  128. 0,
  129. 0,
  130. 0,
  131. WAVEPREC>>3
  132. };
  133. // envelope generator function constants
  134. static const fltype attackconst[4] = {
  135. (fltype)(1/2.82624),
  136. (fltype)(1/2.25280),
  137. (fltype)(1/1.88416),
  138. (fltype)(1/1.59744)
  139. };
  140. static const fltype decrelconst[4] = {
  141. (fltype)(1/39.28064),
  142. (fltype)(1/31.41608),
  143. (fltype)(1/26.17344),
  144. (fltype)(1/22.44608)
  145. };
  146. #pragma region Operators
  147. void operator_advance(opl_chip* opl, op_type* op_pt, Bit32s vib) {
  148. op_pt->wfpos = op_pt->tcount; // waveform position
  149. // advance waveform time
  150. op_pt->tcount += op_pt->tinc;
  151. op_pt->tcount += (Bit32s)(op_pt->tinc)*vib/FIXEDPT;
  152. op_pt->generator_pos += opl->generator_add;
  153. }
  154. void operator_advance_drums(opl_chip* opl, op_type* op_pt1, Bit32s vib1, op_type* op_pt2, Bit32s vib2, op_type* op_pt3, Bit32s vib3) {
  155. Bit32u c1 = op_pt1->tcount/FIXEDPT;
  156. Bit32u c3 = op_pt3->tcount/FIXEDPT;
  157. Bit32u phasebit = (((c1 & 0x88) ^ ((c1<<5) & 0x80)) | ((c3 ^ (c3<<2)) & 0x20)) ? 0x02 : 0x00;
  158. Bit32u noisebit = rand()&1;
  159. Bit32u snare_phase_bit = (((Bitu)((op_pt1->tcount/FIXEDPT) / 0x100))&1);
  160. //Hihat
  161. Bit32u inttm = (phasebit<<8) | (0x34<<(phasebit ^ (noisebit<<1)));
  162. op_pt1->wfpos = inttm*FIXEDPT; // waveform position
  163. // advance waveform time
  164. op_pt1->tcount += op_pt1->tinc;
  165. op_pt1->tcount += (Bit32s)(op_pt1->tinc)*vib1/FIXEDPT;
  166. op_pt1->generator_pos += opl->generator_add;
  167. //Snare
  168. inttm = ((1+snare_phase_bit) ^ noisebit)<<8;
  169. op_pt2->wfpos = inttm*FIXEDPT; // waveform position
  170. // advance waveform time
  171. op_pt2->tcount += op_pt2->tinc;
  172. op_pt2->tcount += (Bit32s)(op_pt2->tinc)*vib2/FIXEDPT;
  173. op_pt2->generator_pos += opl->generator_add;
  174. //Cymbal
  175. inttm = (1+phasebit)<<8;
  176. op_pt3->wfpos = inttm*FIXEDPT; // waveform position
  177. // advance waveform time
  178. op_pt3->tcount += op_pt3->tinc;
  179. op_pt3->tcount += (Bit32s)(op_pt3->tinc)*vib3/FIXEDPT;
  180. op_pt3->generator_pos += opl->generator_add;
  181. }
  182. // output level is sustained, mode changes only when operator is turned off (->release)
  183. // or when the keep-sustained bit is turned off (->sustain_nokeep)
  184. void operator_output(op_type* op_pt, Bit32s modulator, Bit32s trem) {
  185. if (op_pt->op_state != OF_TYPE_OFF) {
  186. op_pt->lastcval = op_pt->cval;
  187. Bit32u i = (Bit32u)((op_pt->wfpos+modulator)/FIXEDPT);
  188. // wform: -16384 to 16383 (0x4000)
  189. // trem : 32768 to 65535 (0x10000)
  190. // step_amp: 0.0 to 1.0
  191. // vol : 1/2^14 to 1/2^29 (/0x4000; /1../0x8000)
  192. op_pt->cval = (Bit32s)(op_pt->step_amp*op_pt->vol*op_pt->cur_wform[i&op_pt->cur_wmask]*trem/16.0);
  193. }
  194. }
  195. // no action, operator is off
  196. void operator_off(op_type* /*op_pt*/) {
  197. }
  198. // output level is sustained, mode changes only when operator is turned off (->release)
  199. // or when the keep-sustained bit is turned off (->sustain_nokeep)
  200. void operator_sustain(op_type* op_pt) {
  201. Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
  202. for (Bit32u ct=0; ct<num_steps_add; ct++) {
  203. op_pt->cur_env_step++;
  204. }
  205. op_pt->generator_pos -= num_steps_add*FIXEDPT;
  206. }
  207. // operator in release mode, if output level reaches zero the operator is turned off
  208. void operator_release(op_type* op_pt) {
  209. // ??? boundary?
  210. if (op_pt->amp > 0.00000001) {
  211. // release phase
  212. op_pt->amp *= op_pt->releasemul;
  213. }
  214. Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
  215. for (Bit32u ct=0; ct<num_steps_add; ct++) {
  216. op_pt->cur_env_step++; // sample counter
  217. if ((op_pt->cur_env_step & op_pt->env_step_r)==0) {
  218. if (op_pt->amp <= 0.00000001) {
  219. // release phase finished, turn off this operator
  220. op_pt->amp = 0.0;
  221. if (op_pt->op_state == OF_TYPE_REL) {
  222. op_pt->op_state = OF_TYPE_OFF;
  223. }
  224. }
  225. op_pt->step_amp = op_pt->amp;
  226. }
  227. }
  228. op_pt->generator_pos -= num_steps_add*FIXEDPT;
  229. }
  230. // operator in decay mode, if sustain level is reached the output level is either
  231. // kept (sustain level keep enabled) or the operator is switched into release mode
  232. void operator_decay(op_type* op_pt) {
  233. if (op_pt->amp > op_pt->sustain_level) {
  234. // decay phase
  235. op_pt->amp *= op_pt->decaymul;
  236. }
  237. Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
  238. for (Bit32u ct=0; ct<num_steps_add; ct++) {
  239. op_pt->cur_env_step++;
  240. if ((op_pt->cur_env_step & op_pt->env_step_d)==0) {
  241. if (op_pt->amp <= op_pt->sustain_level) {
  242. // decay phase finished, sustain level reached
  243. if (op_pt->sus_keep) {
  244. // keep sustain level (until turned off)
  245. op_pt->op_state = OF_TYPE_SUS;
  246. op_pt->amp = op_pt->sustain_level;
  247. } else {
  248. // next: release phase
  249. op_pt->op_state = OF_TYPE_SUS_NOKEEP;
  250. }
  251. }
  252. op_pt->step_amp = op_pt->amp;
  253. }
  254. }
  255. op_pt->generator_pos -= num_steps_add*FIXEDPT;
  256. }
  257. // operator in attack mode, if full output level is reached,
  258. // the operator is switched into decay mode
  259. void operator_attack(op_type* op_pt) {
  260. op_pt->amp = ((op_pt->a3*op_pt->amp + op_pt->a2)*op_pt->amp + op_pt->a1)*op_pt->amp + op_pt->a0;
  261. Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
  262. for (Bit32u ct=0; ct<num_steps_add; ct++) {
  263. op_pt->cur_env_step++; // next sample
  264. if ((op_pt->cur_env_step & op_pt->env_step_a)==0) { // check if next step already reached
  265. if (op_pt->amp > 1.0) {
  266. // attack phase finished, next: decay
  267. op_pt->op_state = OF_TYPE_DEC;
  268. op_pt->amp = 1.0;
  269. op_pt->step_amp = 1.0;
  270. }
  271. op_pt->step_skip_pos_a <<= 1;
  272. if (op_pt->step_skip_pos_a==0) op_pt->step_skip_pos_a = 1;
  273. if (op_pt->step_skip_pos_a & op_pt->env_step_skip_a) { // check if required to skip next step
  274. op_pt->step_amp = op_pt->amp;
  275. }
  276. }
  277. }
  278. op_pt->generator_pos -= num_steps_add*FIXEDPT;
  279. }
  280. typedef void (*optype_fptr)(op_type*);
  281. const optype_fptr opfuncs[6] = {
  282. operator_attack,
  283. operator_decay,
  284. operator_release,
  285. operator_sustain, // sustain phase (keeping level)
  286. operator_release, // sustain_nokeep phase (release-style)
  287. operator_off
  288. };
  289. #pragma endregion
  290. #pragma region Changes
  291. void change_attackrate(opl_chip* opl, Bitu regbase, op_type* op_pt) {
  292. Bits attackrate = opl->adlibreg[ARC_ATTR_DECR + regbase] >> 4;
  293. if (attackrate) {
  294. fltype f = (fltype)(pow(FL2, (fltype)attackrate + (op_pt->toff >> 2) - 1)*attackconst[op_pt->toff & 3] * opl->recipsamp);
  295. // attack rate coefficients
  296. op_pt->a0 = (fltype)(0.0377*f);
  297. op_pt->a1 = (fltype)(10.73*f + 1);
  298. op_pt->a2 = (fltype)(-17.57*f);
  299. op_pt->a3 = (fltype)(7.42*f);
  300. Bits step_skip = attackrate * 4 + op_pt->toff;
  301. Bits steps = step_skip >> 2;
  302. op_pt->env_step_a = (1 << (steps <= 12 ? 12 - steps : 0)) - 1;
  303. Bits step_num = (step_skip <= 48) ? (4 - (step_skip & 3)) : 0;
  304. static Bit8u step_skip_mask[5] = { 0xff, 0xfe, 0xee, 0xba, 0xaa };
  305. op_pt->env_step_skip_a = step_skip_mask[step_num];
  306. #if defined(OPLTYPE_IS_OPL3)
  307. if (step_skip >= 60) {
  308. #else
  309. if (step_skip >= 62) {
  310. #endif
  311. op_pt->a0 = (fltype)(2.0); // something that triggers an immediate transition to amp:=1.0
  312. op_pt->a1 = (fltype)(0.0);
  313. op_pt->a2 = (fltype)(0.0);
  314. op_pt->a3 = (fltype)(0.0);
  315. }
  316. }
  317. else {
  318. // attack disabled
  319. op_pt->a0 = 0.0;
  320. op_pt->a1 = 1.0;
  321. op_pt->a2 = 0.0;
  322. op_pt->a3 = 0.0;
  323. op_pt->env_step_a = 0;
  324. op_pt->env_step_skip_a = 0;
  325. }
  326. }
  327. void change_decayrate(opl_chip* opl, Bitu regbase, op_type* op_pt) {
  328. Bits decayrate = opl->adlibreg[ARC_ATTR_DECR + regbase] & 15;
  329. // decaymul should be 1.0 when decayrate==0
  330. if (decayrate) {
  331. fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff & 3] * opl->recipsamp);
  332. op_pt->decaymul = (fltype)(pow(FL2, f*pow(FL2, (fltype)(decayrate + (op_pt->toff >> 2)))));
  333. Bits steps = (decayrate * 4 + op_pt->toff) >> 2;
  334. op_pt->env_step_d = (1 << (steps <= 12 ? 12 - steps : 0)) - 1;
  335. }
  336. else {
  337. op_pt->decaymul = 1.0;
  338. op_pt->env_step_d = 0;
  339. }
  340. }
  341. void change_releaserate(opl_chip* opl, Bitu regbase, op_type* op_pt) {
  342. Bits releaserate = opl->adlibreg[ARC_SUSL_RELR + regbase] & 15;
  343. // releasemul should be 1.0 when releaserate==0
  344. if (releaserate) {
  345. fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff & 3] * opl->recipsamp);
  346. op_pt->releasemul = (fltype)(pow(FL2, f*pow(FL2, (fltype)(releaserate + (op_pt->toff >> 2)))));
  347. Bits steps = (releaserate * 4 + op_pt->toff) >> 2;
  348. op_pt->env_step_r = (1 << (steps <= 12 ? 12 - steps : 0)) - 1;
  349. }
  350. else {
  351. op_pt->releasemul = 1.0;
  352. op_pt->env_step_r = 0;
  353. }
  354. }
  355. void change_sustainlevel(opl_chip* opl, Bitu regbase, op_type* op_pt) {
  356. Bits sustainlevel = opl->adlibreg[ARC_SUSL_RELR + regbase] >> 4;
  357. // sustainlevel should be 0.0 when sustainlevel==15 (max)
  358. if (sustainlevel < 15) {
  359. op_pt->sustain_level = (fltype)(pow(FL2, (fltype)sustainlevel * (-FL05)));
  360. }
  361. else {
  362. op_pt->sustain_level = 0.0;
  363. }
  364. }
  365. void change_waveform(opl_chip* opl, Bitu regbase, op_type* op_pt) {
  366. #if defined(OPLTYPE_IS_OPL3)
  367. if (regbase>=ARC_SECONDSET) regbase -= (ARC_SECONDSET-22); // second set starts at 22
  368. #endif
  369. // waveform selection
  370. op_pt->cur_wmask = wavemask[opl->wave_sel[regbase]];
  371. op_pt->cur_wform = &opl->wavtable[waveform[opl->wave_sel[regbase]]];
  372. // (might need to be adapted to waveform type here...)
  373. }
  374. void change_keepsustain(opl_chip* opl, Bitu regbase, op_type* op_pt) {
  375. op_pt->sus_keep = (opl->adlibreg[ARC_TVS_KSR_MUL + regbase] & 0x20) > 0;
  376. if (op_pt->op_state == OF_TYPE_SUS) {
  377. if (!op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS_NOKEEP;
  378. }
  379. else if (op_pt->op_state == OF_TYPE_SUS_NOKEEP) {
  380. if (op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS;
  381. }
  382. }
  383. // enable/disable vibrato/tremolo LFO effects
  384. void change_vibrato(opl_chip* opl, Bitu regbase, op_type* op_pt) {
  385. op_pt->vibrato = (opl->adlibreg[ARC_TVS_KSR_MUL + regbase] & 0x40) != 0;
  386. op_pt->tremolo = (opl->adlibreg[ARC_TVS_KSR_MUL + regbase] & 0x80) != 0;
  387. }
  388. // change amount of self-feedback
  389. void change_feedback(opl_chip* opl, Bitu chanbase, op_type* op_pt) {
  390. Bits feedback = opl->adlibreg[ARC_FEEDBACK + chanbase] & 14;
  391. if (feedback) op_pt->mfbi = (Bit32s)(pow(FL2, (fltype)((feedback >> 1) + 8)));
  392. else op_pt->mfbi = 0;
  393. }
  394. void change_frequency(opl_chip* opl, Bitu chanbase, Bitu regbase, op_type* op_pt) {
  395. // frequency
  396. Bit32u frn = ((((Bit32u)opl->adlibreg[ARC_KON_BNUM + chanbase]) & 3) << 8) + (Bit32u)opl->adlibreg[ARC_FREQ_NUM + chanbase];
  397. // block number/octave
  398. Bit32u oct = ((((Bit32u)opl->adlibreg[ARC_KON_BNUM + chanbase]) >> 2) & 7);
  399. op_pt->freq_high = (Bit32s)((frn >> 7) & 7);
  400. // keysplit
  401. Bit32u note_sel = (opl->adlibreg[8] >> 6) & 1;
  402. op_pt->toff = ((frn >> 9)&(note_sel ^ 1)) | ((frn >> 8)&note_sel);
  403. op_pt->toff += (oct << 1);
  404. // envelope scaling (KSR)
  405. if (!(opl->adlibreg[ARC_TVS_KSR_MUL + regbase] & 0x10)) op_pt->toff >>= 2;
  406. // 20+a0+b0:
  407. op_pt->tinc = (Bit32u)((((fltype)(frn << oct))*opl->frqmul[opl->adlibreg[ARC_TVS_KSR_MUL + regbase] & 15]));
  408. // 40+a0+b0:
  409. fltype vol_in = (fltype)((fltype)(opl->adlibreg[ARC_KSL_OUTLEV + regbase] & 63) +
  410. kslmul[opl->adlibreg[ARC_KSL_OUTLEV + regbase] >> 6] * opl->kslev[oct][frn >> 6]);
  411. op_pt->vol = (fltype)(pow(FL2, (fltype)(vol_in * -0.125 - 14)));
  412. // operator frequency changed, care about features that depend on it
  413. change_attackrate(opl, regbase, op_pt);
  414. change_decayrate(opl, regbase, op_pt);
  415. change_releaserate(opl, regbase, op_pt);
  416. }
  417. void enable_operator(opl_chip* opl, Bitu regbase, op_type* op_pt, Bit32u act_type) {
  418. // check if this is really an off-on transition
  419. if (op_pt->act_state == OP_ACT_OFF) {
  420. Bits wselbase = regbase;
  421. if (wselbase >= ARC_SECONDSET) wselbase -= (ARC_SECONDSET - 22); // second set starts at 22
  422. op_pt->tcount = wavestart[opl->wave_sel[wselbase]] * FIXEDPT;
  423. // start with attack mode
  424. op_pt->op_state = OF_TYPE_ATT;
  425. op_pt->act_state |= act_type;
  426. }
  427. }
  428. void disable_operator(op_type* op_pt, Bit32u act_type) {
  429. // check if this is really an on-off transition
  430. if (op_pt->act_state != OP_ACT_OFF) {
  431. op_pt->act_state &= (~act_type);
  432. if (op_pt->act_state == OP_ACT_OFF) {
  433. if (op_pt->op_state != OF_TYPE_OFF) op_pt->op_state = OF_TYPE_REL;
  434. }
  435. }
  436. }
  437. #pragma endregion
  438. opl_chip* adlib_init(Bit32u samplerate) {
  439. opl_chip* opl = new opl_chip;
  440. if (!opl) return NULL;
  441. Bits i, j, oct, int_samplerate = samplerate;
  442. opl->generator_add = (Bit32u)(INTFREQU*FIXEDPT / int_samplerate);
  443. memset((void *)opl->adlibreg, 0, sizeof(opl->adlibreg));
  444. memset((void *)opl->op, 0, sizeof(op_type)*MAXOPERATORS);
  445. memset((void *)opl->wave_sel, 0, sizeof(opl->wave_sel));
  446. for (i = 0; i < MAXOPERATORS; i++) {
  447. opl->op[i].op_state = OF_TYPE_OFF;
  448. opl->op[i].act_state = OP_ACT_OFF;
  449. opl->op[i].amp = 0.0;
  450. opl->op[i].step_amp = 0.0;
  451. opl->op[i].vol = 0.0;
  452. opl->op[i].tcount = 0;
  453. opl->op[i].tinc = 0;
  454. opl->op[i].toff = 0;
  455. opl->op[i].cur_wmask = wavemask[0];
  456. opl->op[i].cur_wform = &opl->wavtable[waveform[0]];
  457. opl->op[i].freq_high = 0;
  458. opl->op[i].generator_pos = 0;
  459. opl->op[i].cur_env_step = 0;
  460. opl->op[i].env_step_a = 0;
  461. opl->op[i].env_step_d = 0;
  462. opl->op[i].env_step_r = 0;
  463. opl->op[i].step_skip_pos_a = 0;
  464. opl->op[i].env_step_skip_a = 0;
  465. #if defined(OPLTYPE_IS_OPL3)
  466. opl->op[i].is_4op = false;
  467. opl->op[i].is_4op_attached = false;
  468. opl->op[i].left_pan = 1;
  469. opl->op[i].right_pan = 1;
  470. #endif
  471. }
  472. opl->recipsamp = 1.0 / (fltype)int_samplerate;
  473. for (i = 15; i >= 0; i--) {
  474. opl->frqmul[i] = (fltype)(frqmul_tab[i] * INTFREQU / (fltype)WAVEPREC*(fltype)FIXEDPT*opl->recipsamp);
  475. }
  476. opl->status = 0;
  477. opl->opl_index = 0;
  478. // create vibrato table
  479. opl->vib_table[0] = 8;
  480. opl->vib_table[1] = 4;
  481. opl->vib_table[2] = 0;
  482. opl->vib_table[3] = -4;
  483. for (i = 4; i < VIBTAB_SIZE; i++) opl->vib_table[i] = opl->vib_table[i - 4] * -1;
  484. // vibrato at ~6.1 ?? (opl3 docs say 6.1, opl4 docs say 6.0, y8950 docs say 6.4)
  485. opl->vibtab_add = static_cast<Bit32u>(VIBTAB_SIZE*FIXEDPT_LFO / 8192 * INTFREQU / int_samplerate);
  486. opl->vibtab_pos = 0;
  487. for (i = 0; i < BLOCKBUF_SIZE; i++) opl->vibval_const[i] = 0;
  488. // create tremolo table
  489. Bit32s trem_table_int[TREMTAB_SIZE];
  490. for (i = 0; i < 14; i++) trem_table_int[i] = i - 13; // upwards (13 to 26 -> -0.5/6 to 0)
  491. for (i = 14; i < 41; i++) trem_table_int[i] = -i + 14; // downwards (26 to 0 -> 0 to -1/6)
  492. for (i = 41; i < 53; i++) trem_table_int[i] = i - 40 - 26; // upwards (1 to 12 -> -1/6 to -0.5/6)
  493. for (i = 0; i < TREMTAB_SIZE; i++) {
  494. // 0.0 .. -26/26*4.8/6 == [0.0 .. -0.8], 4/53 steps == [1 .. 0.57]
  495. fltype trem_val1 = (fltype)(((fltype)trem_table_int[i])*4.8 / 26.0 / 6.0); // 4.8db
  496. fltype trem_val2 = (fltype)((fltype)((Bit32s)(trem_table_int[i] / 4))*1.2 / 6.0 / 6.0); // 1.2db (larger stepping)
  497. opl->trem_table[i] = (Bit32s)(pow(FL2, trem_val1)*FIXEDPT);
  498. opl->trem_table[TREMTAB_SIZE + i] = (Bit32s)(pow(FL2, trem_val2)*FIXEDPT);
  499. }
  500. // tremolo at 3.7hz
  501. opl->tremtab_add = (Bit32u)((fltype)TREMTAB_SIZE * TREM_FREQ * FIXEDPT_LFO / (fltype)int_samplerate);
  502. opl->tremtab_pos = 0;
  503. for (i = 0; i < BLOCKBUF_SIZE; i++) opl->tremval_const[i] = FIXEDPT;
  504. static Bitu initfirstime = 0;
  505. if (!initfirstime) {
  506. initfirstime = 1;
  507. // create waveform tables
  508. for (i = 0; i < (WAVEPREC >> 1); i++) {
  509. opl_chip::wavtable[(i << 1) + WAVEPREC] = (Bit16s)(16384 * sin((fltype)((i << 1))*PI * 2 / WAVEPREC));
  510. opl_chip::wavtable[(i << 1) + 1 + WAVEPREC] = (Bit16s)(16384 * sin((fltype)((i << 1) + 1)*PI * 2 / WAVEPREC));
  511. opl_chip::wavtable[i] = opl_chip::wavtable[(i << 1) + WAVEPREC];
  512. // alternative: (zero-less)
  513. /* wavtable[(i<<1) +WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+1)*PI/WAVEPREC));
  514. wavtable[(i<<1)+1+WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+3)*PI/WAVEPREC));
  515. wavtable[i] = wavtable[(i<<1)-1+WAVEPREC]; */
  516. }
  517. for (i = 0; i < (WAVEPREC >> 3); i++) {
  518. opl_chip::wavtable[i + (WAVEPREC << 1)] = opl_chip::wavtable[i + (WAVEPREC >> 3)] - 16384;
  519. opl_chip::wavtable[i + ((WAVEPREC * 17) >> 3)] = opl_chip::wavtable[i + (WAVEPREC >> 2)] + 16384;
  520. }
  521. // key scale level table verified ([table in book]*8/3)
  522. opl_chip::kslev[7][0] = 0; opl_chip::kslev[7][1] = 24; opl_chip::kslev[7][2] = 32; opl_chip::kslev[7][3] = 37;
  523. opl_chip::kslev[7][4] = 40; opl_chip::kslev[7][5] = 43; opl_chip::kslev[7][6] = 45; opl_chip::kslev[7][7] = 47;
  524. opl_chip::kslev[7][8] = 48;
  525. for (i = 9; i < 16; i++) opl_chip::kslev[7][i] = (Bit8u)(i + 41);
  526. for (j = 6; j >= 0; j--) {
  527. for (i = 0; i < 16; i++) {
  528. oct = (Bits)opl_chip::kslev[j + 1][i] - 8;
  529. if (oct < 0) oct = 0;
  530. opl_chip::kslev[j][i] = (Bit8u)oct;
  531. }
  532. }
  533. }
  534. return opl;
  535. }
  536. void adlib_write(opl_chip* opl, Bitu idx, Bit8u val) {
  537. Bit32u second_set = idx & 0x100;
  538. opl->adlibreg[idx] = val;
  539. switch (idx & 0xf0) {
  540. case ARC_CONTROL:
  541. // here we check for the second set registers, too:
  542. switch (idx) {
  543. case 0x02: // timer1 counter
  544. case 0x03: // timer2 counter
  545. break;
  546. case 0x04:
  547. // IRQ reset, timer mask/start
  548. if (val & 0x80) {
  549. // clear IRQ bits in status register
  550. opl->status &= ~0x60;
  551. }
  552. else {
  553. opl->status = 0;
  554. }
  555. break;
  556. #if defined(OPLTYPE_IS_OPL3)
  557. case 0x04|ARC_SECONDSET:
  558. // 4op enable/disable switches for each possible channel
  559. opl->op[0].is_4op = (val&1)>0;
  560. opl->op[3].is_4op_attached = opl->op[0].is_4op;
  561. opl->op[1].is_4op = (val&2)>0;
  562. opl->op[4].is_4op_attached = opl->op[1].is_4op;
  563. opl->op[2].is_4op = (val&4)>0;
  564. opl->op[5].is_4op_attached = opl->op[2].is_4op;
  565. opl->op[18].is_4op = (val&8)>0;
  566. opl->op[21].is_4op_attached = opl->op[18].is_4op;
  567. opl->op[19].is_4op = (val&16)>0;
  568. opl->op[22].is_4op_attached = opl->op[19].is_4op;
  569. opl->op[20].is_4op = (val&32)>0;
  570. opl->op[23].is_4op_attached = opl->op[20].is_4op;
  571. break;
  572. case 0x05|ARC_SECONDSET:
  573. break;
  574. #endif
  575. case 0x08:
  576. // CSW, note select
  577. break;
  578. default:
  579. break;
  580. }
  581. break;
  582. case ARC_TVS_KSR_MUL:
  583. case ARC_TVS_KSR_MUL + 0x10:
  584. {
  585. // tremolo/vibrato/sustain keeping enabled; key scale rate; frequency multiplication
  586. int num = idx & 7;
  587. Bitu base = (idx - ARC_TVS_KSR_MUL) & 0xff;
  588. if ((num < 6) && (base < 22)) {
  589. Bitu modop = regbase2modop[second_set ? (base + 22) : base];
  590. Bitu regbase = base + second_set;
  591. Bitu chanbase = second_set ? (modop - 18 + ARC_SECONDSET) : modop;
  592. // change tremolo/vibrato and sustain keeping of this operator
  593. op_type* op_ptr = &opl->op[modop + ((num < 3) ? 0 : 9)];
  594. change_keepsustain(opl, regbase, op_ptr);
  595. change_vibrato(opl, regbase, op_ptr);
  596. // change frequency calculations of this operator as
  597. // key scale rate and frequency multiplicator can be changed
  598. #if defined(OPLTYPE_IS_OPL3)
  599. if ((opl->adlibreg[0x105]&1) && (opl->op[modop].is_4op_attached)) {
  600. // operator uses frequency of channel
  601. change_frequency(opl, chanbase-3,regbase,op_ptr);
  602. } else {
  603. change_frequency(opl, chanbase,regbase,op_ptr);
  604. }
  605. #else
  606. change_frequency(opl, chanbase, base, op_ptr);
  607. #endif
  608. }
  609. }
  610. break;
  611. case ARC_KSL_OUTLEV:
  612. case ARC_KSL_OUTLEV + 0x10:
  613. {
  614. // key scale level; output rate
  615. int num = idx & 7;
  616. Bitu base = (idx - ARC_KSL_OUTLEV) & 0xff;
  617. if ((num < 6) && (base < 22)) {
  618. Bitu modop = regbase2modop[second_set ? (base + 22) : base];
  619. Bitu chanbase = second_set ? (modop - 18 + ARC_SECONDSET) : modop;
  620. // change frequency calculations of this operator as
  621. // key scale level and output rate can be changed
  622. op_type* op_ptr = &opl->op[modop + ((num < 3) ? 0 : 9)];
  623. #if defined(OPLTYPE_IS_OPL3)
  624. Bitu regbase = base+second_set;
  625. if ((opl->adlibreg[0x105]&1) && (opl->op[modop].is_4op_attached)) {
  626. // operator uses frequency of channel
  627. change_frequency(opl, chanbase-3,regbase,op_ptr);
  628. } else {
  629. change_frequency(opl, chanbase, regbase, op_ptr);
  630. }
  631. #else
  632. change_frequency(opl, chanbase, base, op_ptr);
  633. #endif
  634. }
  635. }
  636. break;
  637. case ARC_ATTR_DECR:
  638. case ARC_ATTR_DECR + 0x10:
  639. {
  640. // attack/decay rates
  641. int num = idx & 7;
  642. Bitu base = (idx - ARC_ATTR_DECR) & 0xff;
  643. if ((num < 6) && (base < 22)) {
  644. Bitu regbase = base + second_set;
  645. // change attack rate and decay rate of this operator
  646. op_type* op_ptr = &opl->op[regbase2op[second_set ? (base + 22) : base]];
  647. change_attackrate(opl, regbase, op_ptr);
  648. change_decayrate(opl, regbase, op_ptr);
  649. }
  650. }
  651. break;
  652. case ARC_SUSL_RELR:
  653. case ARC_SUSL_RELR + 0x10:
  654. {
  655. // sustain level; release rate
  656. int num = idx & 7;
  657. Bitu base = (idx - ARC_SUSL_RELR) & 0xff;
  658. if ((num < 6) && (base < 22)) {
  659. Bitu regbase = base + second_set;
  660. // change sustain level and release rate of this operator
  661. op_type* op_ptr = &opl->op[regbase2op[second_set ? (base + 22) : base]];
  662. change_releaserate(opl, regbase, op_ptr);
  663. change_sustainlevel(opl, regbase, op_ptr);
  664. }
  665. }
  666. break;
  667. case ARC_FREQ_NUM:
  668. {
  669. // 0xa0-0xa8 low8 frequency
  670. Bitu base = (idx - ARC_FREQ_NUM) & 0xff;
  671. if (base < 9) {
  672. Bits opbase = second_set ? (base + 18) : base;
  673. #if defined(OPLTYPE_IS_OPL3)
  674. if ((opl->adlibreg[0x105] & 1) && opl->op[opbase].is_4op_attached) break;
  675. #endif
  676. // regbase of modulator:
  677. Bits modbase = modulatorbase[base] + second_set;
  678. Bitu chanbase = base + second_set;
  679. change_frequency(opl, chanbase, modbase, &opl->op[opbase]);
  680. change_frequency(opl, chanbase, modbase + 3, &opl->op[opbase + 9]);
  681. #if defined(OPLTYPE_IS_OPL3)
  682. // for 4op channels all four operators are modified to the frequency of the channel
  683. if ((opl->adlibreg[0x105] & 1) && opl->op[second_set ? (base + 18) : base].is_4op) {
  684. change_frequency(opl, chanbase, modbase + 8, &opl->op[opbase + 3]);
  685. change_frequency(opl, chanbase, modbase + 3 + 8, &opl->op[opbase + 3 + 9]);
  686. }
  687. #endif
  688. }
  689. }
  690. break;
  691. case ARC_KON_BNUM:
  692. {
  693. if (idx == ARC_PERC_MODE) {
  694. #if defined(OPLTYPE_IS_OPL3)
  695. if (second_set) return;
  696. #endif
  697. if ((val & 0x30) == 0x30) { // BassDrum active
  698. enable_operator(opl, 16, &opl->op[6], OP_ACT_PERC);
  699. change_frequency(opl, 6, 16, &opl->op[6]);
  700. enable_operator(opl, 16 + 3, &opl->op[6 + 9], OP_ACT_PERC);
  701. change_frequency(opl, 6, 16 + 3, &opl->op[6 + 9]);
  702. }
  703. else {
  704. disable_operator(&opl->op[6], OP_ACT_PERC);
  705. disable_operator(&opl->op[6 + 9], OP_ACT_PERC);
  706. }
  707. if ((val & 0x28) == 0x28) { // Snare active
  708. enable_operator(opl, 17 + 3, &opl->op[16], OP_ACT_PERC);
  709. change_frequency(opl, 7, 17 + 3, &opl->op[16]);
  710. }
  711. else {
  712. disable_operator(&opl->op[16], OP_ACT_PERC);
  713. }
  714. if ((val & 0x24) == 0x24) { // TomTom active
  715. enable_operator(opl, 18, &opl->op[8], OP_ACT_PERC);
  716. change_frequency(opl, 8, 18, &opl->op[8]);
  717. }
  718. else {
  719. disable_operator(&opl->op[8], OP_ACT_PERC);
  720. }
  721. if ((val & 0x22) == 0x22) { // Cymbal active
  722. enable_operator(opl, 18 + 3, &opl->op[8 + 9], OP_ACT_PERC);
  723. change_frequency(opl, 8, 18 + 3, &opl->op[8 + 9]);
  724. }
  725. else {
  726. disable_operator(&opl->op[8 + 9], OP_ACT_PERC);
  727. }
  728. if ((val & 0x21) == 0x21) { // Hihat active
  729. enable_operator(opl, 17, &opl->op[7], OP_ACT_PERC);
  730. change_frequency(opl, 7, 17, &opl->op[7]);
  731. }
  732. else {
  733. disable_operator(&opl->op[7], OP_ACT_PERC);
  734. }
  735. break;
  736. }
  737. // regular 0xb0-0xb8
  738. Bitu base = (idx - ARC_KON_BNUM) & 0xff;
  739. if (base < 9) {
  740. Bits opbase = second_set ? (base + 18) : base;
  741. #if defined(OPLTYPE_IS_OPL3)
  742. if ((opl->adlibreg[0x105] & 1) && opl->op[opbase].is_4op_attached) break;
  743. #endif
  744. // regbase of modulator:
  745. Bits modbase = modulatorbase[base] + second_set;
  746. if (val & 32) {
  747. // operator switched on
  748. enable_operator(opl, modbase, &opl->op[opbase], OP_ACT_NORMAL); // modulator (if 2op)
  749. enable_operator(opl, modbase + 3, &opl->op[opbase + 9], OP_ACT_NORMAL); // carrier (if 2op)
  750. #if defined(OPLTYPE_IS_OPL3)
  751. // for 4op channels all four operators are switched on
  752. if ((opl->adlibreg[0x105] & 1) && opl->op[opbase].is_4op) {
  753. // turn on chan+3 operators as well
  754. enable_operator(opl, modbase + 8, &opl->op[opbase + 3], OP_ACT_NORMAL);
  755. enable_operator(opl, modbase + 3 + 8, &opl->op[opbase + 3 + 9], OP_ACT_NORMAL);
  756. }
  757. #endif
  758. }
  759. else {
  760. // operator switched off
  761. disable_operator(&opl->op[opbase], OP_ACT_NORMAL);
  762. disable_operator(&opl->op[opbase + 9], OP_ACT_NORMAL);
  763. #if defined(OPLTYPE_IS_OPL3)
  764. // for 4op channels all four operators are switched off
  765. if ((opl->adlibreg[0x105] & 1) && opl->op[opbase].is_4op) {
  766. // turn off chan+3 operators as well
  767. disable_operator(&opl->op[opbase + 3], OP_ACT_NORMAL);
  768. disable_operator(&opl->op[opbase + 3 + 9], OP_ACT_NORMAL);
  769. }
  770. #endif
  771. }
  772. Bitu chanbase = base + second_set;
  773. // change frequency calculations of modulator and carrier (2op) as
  774. // the frequency of the channel has changed
  775. change_frequency(opl, chanbase, modbase, &opl->op[opbase]);
  776. change_frequency(opl, chanbase, modbase + 3, &opl->op[opbase + 9]);
  777. #if defined(OPLTYPE_IS_OPL3)
  778. // for 4op channels all four operators are modified to the frequency of the channel
  779. if ((opl->adlibreg[0x105] & 1) && opl->op[second_set ? (base + 18) : base].is_4op) {
  780. // change frequency calculations of chan+3 operators as well
  781. change_frequency(opl, chanbase, modbase + 8, &opl->op[opbase + 3]);
  782. change_frequency(opl, chanbase, modbase + 3 + 8, &opl->op[opbase + 3 + 9]);
  783. }
  784. #endif
  785. }
  786. }
  787. break;
  788. case ARC_FEEDBACK:
  789. {
  790. // 0xc0-0xc8 feedback/modulation type (AM/FM)
  791. Bitu base = (idx - ARC_FEEDBACK) & 0xff;
  792. if (base < 9) {
  793. Bits opbase = second_set ? (base + 18) : base;
  794. Bitu chanbase = base + second_set;
  795. change_feedback(opl, chanbase, &opl->op[opbase]);
  796. #if defined(OPLTYPE_IS_OPL3)
  797. // OPL3 panning
  798. opl->op[opbase].left_pan = ((val & 0x10) >> 4);
  799. opl->op[opbase].right_pan = ((val & 0x20) >> 5);
  800. #endif
  801. }
  802. }
  803. break;
  804. case ARC_WAVE_SEL:
  805. case ARC_WAVE_SEL + 0x10:
  806. {
  807. int num = idx & 7;
  808. Bitu base = (idx - ARC_WAVE_SEL) & 0xff;
  809. if ((num < 6) && (base < 22)) {
  810. #if defined(OPLTYPE_IS_OPL3)
  811. Bits wselbase = second_set ? (base + 22) : base; // for easier mapping onto wave_sel[]
  812. // change waveform
  813. if (opl->adlibreg[0x105] & 1) opl->wave_sel[wselbase] = val & 7; // opl3 mode enabled, all waveforms accessible
  814. else opl->wave_sel[wselbase] = val & 3;
  815. op_type* op_ptr = &opl->op[regbase2modop[wselbase] + ((num < 3) ? 0 : 9)];
  816. change_waveform(opl, wselbase, op_ptr);
  817. #else
  818. if (opl->adlibreg[0x01] & 0x20) {
  819. // wave selection enabled, change waveform
  820. opl->wave_sel[base] = val & 3;
  821. op_type* op_ptr = &opl->op[regbase2modop[base] + ((num < 3) ? 0 : 9)];
  822. change_waveform(opl, base, op_ptr);
  823. }
  824. #endif
  825. }
  826. }
  827. break;
  828. default:
  829. break;
  830. }
  831. }
  832. Bitu adlib_reg_read(opl_chip* opl, Bitu port) {
  833. #if defined(OPLTYPE_IS_OPL3)
  834. // opl3-detection routines require ret&6 to be zero
  835. if ((port&1)==0) {
  836. return opl->status;
  837. }
  838. return 0x00;
  839. #else
  840. // opl2-detection routines require ret&6 to be 6
  841. if ((port & 1) == 0) {
  842. return opl->status | 6;
  843. }
  844. return 0xff;
  845. #endif
  846. }
  847. void adlib_write_index(opl_chip* opl, Bitu port, Bit8u val) {
  848. opl->opl_index = val;
  849. #if defined(OPLTYPE_IS_OPL3)
  850. if ((port&3)!=0) {
  851. // possibly second set
  852. if (((opl->adlibreg[0x105]&1)!=0) || (opl->opl_index==5)) opl->opl_index |= ARC_SECONDSET;
  853. }
  854. #endif
  855. }
  856. static void OPL_INLINE clipit16(Bit32s ival, Bit16s* outval) {
  857. if (ival < 32768) {
  858. if (ival > -32769) {
  859. *outval = (Bit16s)ival;
  860. }
  861. else {
  862. *outval = -32768;
  863. }
  864. }
  865. else {
  866. *outval = 32767;
  867. }
  868. }
  869. // be careful with this
  870. // uses cptr and chanval, outputs into outbufl(/outbufr)
  871. // for opl3 check if opl3-mode is enabled (which uses stereo panning)
  872. #undef CHANVAL_OUT
  873. #if defined(OPLTYPE_IS_OPL3)
  874. # define CHANVAL_OUT \
  875. if (opl->adlibreg[0x105]&1) { \
  876. outbufl[i] += chanval*cptr[0].left_pan; \
  877. outbufr[i] += chanval*cptr[0].right_pan; \
  878. } else { \
  879. outbufl[i] += chanval; \
  880. }
  881. #else
  882. # define CHANVAL_OUT \
  883. outbufl[i] += chanval;
  884. #endif
  885. void adlib_getsample(opl_chip* opl, Bit16s* sndptr, Bits numsamples) {
  886. Bits i, endsamples;
  887. op_type* cptr;
  888. Bit32s outbufl[BLOCKBUF_SIZE];
  889. #if defined(OPLTYPE_IS_OPL3)
  890. // second output buffer (right channel for opl3 stereo)
  891. Bit32s outbufr[BLOCKBUF_SIZE];
  892. #endif
  893. // vibrato/tremolo lookup tables (global, to possibly be used by all operators)
  894. Bit32s vib_lut[BLOCKBUF_SIZE];
  895. Bit32s trem_lut[BLOCKBUF_SIZE];
  896. Bits samples_to_process = numsamples;
  897. for (Bits cursmp = 0; cursmp<samples_to_process; cursmp += endsamples) {
  898. endsamples = samples_to_process - cursmp;
  899. if (endsamples>BLOCKBUF_SIZE) endsamples = BLOCKBUF_SIZE;
  900. memset((void*)&outbufl, 0, endsamples*sizeof(Bit32s));
  901. #if defined(OPLTYPE_IS_OPL3)
  902. // clear second output buffer (opl3 stereo)
  903. if (opl->adlibreg[0x105] & 1) memset((void*)&outbufr, 0, endsamples*sizeof(Bit32s));
  904. #endif
  905. // calculate vibrato/tremolo lookup tables
  906. Bit32s vib_tshift = ((opl->adlibreg[ARC_PERC_MODE] & 0x40) == 0) ? 1 : 0; // 14cents/7cents switching
  907. for (i = 0; i < endsamples; i++) {
  908. // cycle through vibrato table
  909. opl->vibtab_pos += opl->vibtab_add;
  910. if (opl->vibtab_pos / FIXEDPT_LFO >= VIBTAB_SIZE) opl->vibtab_pos -= VIBTAB_SIZE*FIXEDPT_LFO;
  911. vib_lut[i] = opl->vib_table[opl->vibtab_pos / FIXEDPT_LFO] >> vib_tshift; // 14cents (14/100 of a semitone) or 7cents
  912. // cycle through tremolo table
  913. opl->tremtab_pos += opl->tremtab_add;
  914. if (opl->tremtab_pos / FIXEDPT_LFO >= TREMTAB_SIZE) opl->tremtab_pos -= TREMTAB_SIZE*FIXEDPT_LFO;
  915. if (opl->adlibreg[ARC_PERC_MODE] & 0x80) trem_lut[i] = opl->trem_table[opl->tremtab_pos / FIXEDPT_LFO];
  916. else trem_lut[i] = opl->trem_table[TREMTAB_SIZE + opl->tremtab_pos / FIXEDPT_LFO];
  917. }
  918. if (opl->adlibreg[ARC_PERC_MODE] & 0x20) {
  919. //BassDrum
  920. cptr = &opl->op[6];
  921. if (opl->adlibreg[ARC_FEEDBACK + 6] & 1) {
  922. // additive synthesis
  923. if (cptr[9].op_state != OF_TYPE_OFF) {
  924. if (cptr[9].vibrato) {
  925. opl->vibval1 = opl->vibval_var1;
  926. for (i = 0; i < endsamples; i++)
  927. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  928. }
  929. else opl->vibval1 = opl->vibval_const;
  930. if (cptr[9].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  931. else opl->tremval1 = opl->tremval_const;
  932. // calculate channel output
  933. for (i = 0; i < endsamples; i++) {
  934. operator_advance(opl, &cptr[9], opl->vibval1[i]);
  935. opfuncs[cptr[9].op_state](&cptr[9]);
  936. operator_output(&cptr[9], 0, opl->tremval1[i]);
  937. Bit32s chanval = cptr[9].cval * 2;
  938. CHANVAL_OUT
  939. }
  940. }
  941. }
  942. else {
  943. // frequency modulation
  944. if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[0].op_state != OF_TYPE_OFF)) {
  945. if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
  946. opl->vibval1 = opl->vibval_var1;
  947. for (i = 0; i < endsamples; i++)
  948. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[0].freq_high / 8)*FIXEDPT*VIBFAC);
  949. }
  950. else opl->vibval1 = opl->vibval_const;
  951. if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
  952. opl->vibval2 = opl->vibval_var2;
  953. for (i = 0; i < endsamples; i++)
  954. opl->vibval2[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  955. }
  956. else opl->vibval2 = opl->vibval_const;
  957. if (cptr[0].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  958. else opl->tremval1 = opl->tremval_const;
  959. if (cptr[9].tremolo) opl->tremval2 = trem_lut; // tremolo enabled, use table
  960. else opl->tremval2 = opl->tremval_const;
  961. // calculate channel output
  962. for (i = 0; i < endsamples; i++) {
  963. operator_advance(opl, &cptr[0], opl->vibval1[i]);
  964. opfuncs[cptr[0].op_state](&cptr[0]);
  965. operator_output(&cptr[0], (cptr[0].lastcval + cptr[0].cval)*cptr[0].mfbi / 2, opl->tremval1[i]);
  966. operator_advance(opl, &cptr[9], opl->vibval2[i]);
  967. opfuncs[cptr[9].op_state](&cptr[9]);
  968. operator_output(&cptr[9], cptr[0].cval*FIXEDPT, opl->tremval2[i]);
  969. Bit32s chanval = cptr[9].cval * 2;
  970. CHANVAL_OUT
  971. }
  972. }
  973. }
  974. //TomTom (j=8)
  975. if (opl->op[8].op_state != OF_TYPE_OFF) {
  976. cptr = &opl->op[8];
  977. if (cptr[0].vibrato) {
  978. opl->vibval3 = opl->vibval_var1;
  979. for (i = 0; i < endsamples; i++)
  980. opl->vibval3[i] = (Bit32s)((vib_lut[i] * cptr[0].freq_high / 8)*FIXEDPT*VIBFAC);
  981. }
  982. else opl->vibval3 = opl->vibval_const;
  983. if (cptr[0].tremolo) opl->tremval3 = trem_lut; // tremolo enabled, use table
  984. else opl->tremval3 = opl->tremval_const;
  985. // calculate channel output
  986. for (i = 0; i < endsamples; i++) {
  987. operator_advance(opl, &cptr[0], opl->vibval3[i]);
  988. opfuncs[cptr[0].op_state](&cptr[0]); //TomTom
  989. operator_output(&cptr[0], 0, opl->tremval3[i]);
  990. Bit32s chanval = cptr[0].cval * 2;
  991. CHANVAL_OUT
  992. }
  993. }
  994. //Snare/Hihat (j=7), Cymbal (j=8)
  995. if ((opl->op[7].op_state != OF_TYPE_OFF) || (opl->op[16].op_state != OF_TYPE_OFF) ||
  996. (opl->op[17].op_state != OF_TYPE_OFF)) {
  997. cptr = &opl->op[7];
  998. if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
  999. opl->vibval1 = opl->vibval_var1;
  1000. for (i = 0; i < endsamples; i++)
  1001. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[0].freq_high / 8)*FIXEDPT*VIBFAC);
  1002. }
  1003. else opl->vibval1 = opl->vibval_const;
  1004. if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
  1005. opl->vibval2 = opl->vibval_var2;
  1006. for (i = 0; i < endsamples; i++)
  1007. opl->vibval2[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  1008. }
  1009. else opl->vibval2 = opl->vibval_const;
  1010. if (cptr[0].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1011. else opl->tremval1 = opl->tremval_const;
  1012. if (cptr[9].tremolo) opl->tremval2 = trem_lut; // tremolo enabled, use table
  1013. else opl->tremval2 = opl->tremval_const;
  1014. cptr = &opl->op[8];
  1015. if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
  1016. opl->vibval4 = opl->vibval_var2;
  1017. for (i = 0; i < endsamples; i++)
  1018. opl->vibval4[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  1019. }
  1020. else opl->vibval4 = opl->vibval_const;
  1021. if (cptr[9].tremolo) opl->tremval4 = trem_lut; // tremolo enabled, use table
  1022. else opl->tremval4 = opl->tremval_const;
  1023. // calculate channel output
  1024. for (i = 0; i < endsamples; i++) {
  1025. operator_advance_drums(opl, &opl->op[7], opl->vibval1[i], &opl->op[7 + 9], opl->vibval2[i], &opl->op[8 + 9], opl->vibval4[i]);
  1026. opfuncs[opl->op[7].op_state](&opl->op[7]); //Hihat
  1027. operator_output(&opl->op[7], 0, opl->tremval1[i]);
  1028. opfuncs[opl->op[7 + 9].op_state](&opl->op[7 + 9]); //Snare
  1029. operator_output(&opl->op[7 + 9], 0, opl->tremval2[i]);
  1030. opfuncs[opl->op[8 + 9].op_state](&opl->op[8 + 9]); //Cymbal
  1031. operator_output(&opl->op[8 + 9], 0, opl->tremval4[i]);
  1032. Bit32s chanval = (opl->op[7].cval + opl->op[7 + 9].cval + opl->op[8 + 9].cval) * 2;
  1033. CHANVAL_OUT
  1034. }
  1035. }
  1036. }
  1037. Bitu max_channel = NUM_CHANNELS;
  1038. #if defined(OPLTYPE_IS_OPL3)
  1039. if ((opl->adlibreg[0x105] & 1) == 0) max_channel = NUM_CHANNELS / 2;
  1040. #endif
  1041. for (Bits cur_ch = max_channel - 1; cur_ch >= 0; cur_ch--) {
  1042. // skip drum/percussion operators
  1043. if ((opl->adlibreg[ARC_PERC_MODE] & 0x20) && (cur_ch >= 6) && (cur_ch < 9)) continue;
  1044. Bitu k = cur_ch;
  1045. #if defined(OPLTYPE_IS_OPL3)
  1046. if (cur_ch < 9) {
  1047. cptr = &opl->op[cur_ch];
  1048. }
  1049. else {
  1050. cptr = &opl->op[cur_ch + 9]; // second set is operator18-operator35
  1051. k += (-9 + 256); // second set uses registers 0x100 onwards
  1052. }
  1053. // check if this operator is part of a 4-op
  1054. if ((opl->adlibreg[0x105] & 1) && cptr->is_4op_attached) continue;
  1055. #else
  1056. cptr = &opl->op[cur_ch];
  1057. #endif
  1058. // check for FM/AM
  1059. if (opl->adlibreg[ARC_FEEDBACK + k] & 1) {
  1060. #if defined(OPLTYPE_IS_OPL3)
  1061. if ((opl->adlibreg[0x105] & 1) && cptr->is_4op) {
  1062. if (opl->adlibreg[ARC_FEEDBACK + k + 3] & 1) {
  1063. // AM-AM-style synthesis (op1[fb] + (op2 * op3) + op4)
  1064. if (cptr[0].op_state != OF_TYPE_OFF) {
  1065. if (cptr[0].vibrato) {
  1066. opl->vibval1 = opl->vibval_var1;
  1067. for (i = 0; i < endsamples; i++)
  1068. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[0].freq_high / 8)*FIXEDPT*VIBFAC);
  1069. }
  1070. else opl->vibval1 = opl->vibval_const;
  1071. if (cptr[0].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1072. else opl->tremval1 = opl->tremval_const;
  1073. // calculate channel output
  1074. for (i = 0; i < endsamples; i++) {
  1075. operator_advance(&cptr[0], opl->vibval1[i]);
  1076. opfuncs[cptr[0].op_state](&cptr[0]);
  1077. operator_output(&cptr[0], (cptr[0].lastcval + cptr[0].cval)*cptr[0].mfbi / 2, opl->tremval1[i]);
  1078. Bit32s chanval = cptr[0].cval;
  1079. CHANVAL_OUT
  1080. }
  1081. }
  1082. if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
  1083. if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
  1084. opl->vibval1 = opl->vibval_var1;
  1085. for (i = 0; i < endsamples; i++)
  1086. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  1087. }
  1088. else opl->vibval1 = opl->vibval_const;
  1089. if (cptr[9].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1090. else opl->tremval1 = opl->tremval_const;
  1091. if (cptr[3].tremolo) opl->tremval2 = trem_lut; // tremolo enabled, use table
  1092. else opl->tremval2 = opl->tremval_const;
  1093. // calculate channel output
  1094. for (i = 0; i < endsamples; i++) {
  1095. operator_advance(&cptr[9], opl->vibval1[i]);
  1096. opfuncs[cptr[9].op_state](&cptr[9]);
  1097. operator_output(&cptr[9], 0, opl->tremval1[i]);
  1098. operator_advance(&cptr[3], 0);
  1099. opfuncs[cptr[3].op_state](&cptr[3]);
  1100. operator_output(&cptr[3], cptr[9].cval*FIXEDPT, opl->tremval2[i]);
  1101. Bit32s chanval = cptr[3].cval;
  1102. CHANVAL_OUT
  1103. }
  1104. }
  1105. if (cptr[3 + 9].op_state != OF_TYPE_OFF) {
  1106. if (cptr[3 + 9].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1107. else opl->tremval1 = opl->tremval_const;
  1108. // calculate channel output
  1109. for (i = 0; i < endsamples; i++) {
  1110. operator_advance(&cptr[3 + 9], 0);
  1111. opfuncs[cptr[3 + 9].op_state](&cptr[3 + 9]);
  1112. operator_output(&cptr[3 + 9], 0, opl->tremval1[i]);
  1113. Bit32s chanval = cptr[3 + 9].cval;
  1114. CHANVAL_OUT
  1115. }
  1116. }
  1117. }
  1118. else {
  1119. // AM-FM-style synthesis (op1[fb] + (op2 * op3 * op4))
  1120. if (cptr[0].op_state != OF_TYPE_OFF) {
  1121. if (cptr[0].vibrato) {
  1122. opl->vibval1 = opl->vibval_var1;
  1123. for (i = 0; i < endsamples; i++)
  1124. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[0].freq_high / 8)*FIXEDPT*VIBFAC);
  1125. }
  1126. else opl->vibval1 = opl->vibval_const;
  1127. if (cptr[0].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1128. else opl->tremval1 = opl->tremval_const;
  1129. // calculate channel output
  1130. for (i = 0; i < endsamples; i++) {
  1131. operator_advance(&cptr[0], opl->vibval1[i]);
  1132. opfuncs[cptr[0].op_state](&cptr[0]);
  1133. operator_output(&cptr[0], (cptr[0].lastcval + cptr[0].cval)*cptr[0].mfbi / 2, opl->tremval1[i]);
  1134. Bit32s chanval = cptr[0].cval;
  1135. CHANVAL_OUT
  1136. }
  1137. }
  1138. if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3 + 9].op_state != OF_TYPE_OFF)) {
  1139. if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
  1140. opl->vibval1 = opl->vibval_var1;
  1141. for (i = 0; i < endsamples; i++)
  1142. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  1143. }
  1144. else opl->vibval1 = opl->vibval_const;
  1145. if (cptr[9].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1146. else opl->tremval1 = opl->tremval_const;
  1147. if (cptr[3].tremolo) opl->tremval2 = trem_lut; // tremolo enabled, use table
  1148. else opl->tremval2 = opl->tremval_const;
  1149. if (cptr[3 + 9].tremolo) opl->tremval3 = trem_lut; // tremolo enabled, use table
  1150. else opl->tremval3 = opl->tremval_const;
  1151. // calculate channel output
  1152. for (i = 0; i < endsamples; i++) {
  1153. operator_advance(&cptr[9], opl->vibval1[i]);
  1154. opfuncs[cptr[9].op_state](&cptr[9]);
  1155. operator_output(&cptr[9], 0, opl->tremval1[i]);
  1156. operator_advance(&cptr[3], 0);
  1157. opfuncs[cptr[3].op_state](&cptr[3]);
  1158. operator_output(&cptr[3], cptr[9].cval*FIXEDPT, opl->tremval2[i]);
  1159. operator_advance(&cptr[3 + 9], 0);
  1160. opfuncs[cptr[3 + 9].op_state](&cptr[3 + 9]);
  1161. operator_output(&cptr[3 + 9], cptr[3].cval*FIXEDPT, opl->tremval3[i]);
  1162. Bit32s chanval = cptr[3 + 9].cval;
  1163. CHANVAL_OUT
  1164. }
  1165. }
  1166. }
  1167. continue;
  1168. }
  1169. #endif
  1170. // 2op additive synthesis
  1171. if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
  1172. if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
  1173. opl->vibval1 = opl->vibval_var1;
  1174. for (i = 0; i < endsamples; i++)
  1175. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[0].freq_high / 8)*FIXEDPT*VIBFAC);
  1176. }
  1177. else opl->vibval1 = opl->vibval_const;
  1178. if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
  1179. opl->vibval2 = opl->vibval_var2;
  1180. for (i = 0; i < endsamples; i++)
  1181. opl->vibval2[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  1182. }
  1183. else opl->vibval2 = opl->vibval_const;
  1184. if (cptr[0].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1185. else opl->tremval1 = opl->tremval_const;
  1186. if (cptr[9].tremolo) opl->tremval2 = trem_lut; // tremolo enabled, use table
  1187. else opl->tremval2 = opl->tremval_const;
  1188. // calculate channel output
  1189. for (i = 0; i < endsamples; i++) {
  1190. // carrier1
  1191. operator_advance(opl, &cptr[0], opl->vibval1[i]);
  1192. opfuncs[cptr[0].op_state](&cptr[0]);
  1193. operator_output(&cptr[0], (cptr[0].lastcval + cptr[0].cval)*cptr[0].mfbi / 2, opl->tremval1[i]);
  1194. // carrier2
  1195. operator_advance(opl, &cptr[9], opl->vibval2[i]);
  1196. opfuncs[cptr[9].op_state](&cptr[9]);
  1197. operator_output(&cptr[9], 0, opl->tremval2[i]);
  1198. Bit32s chanval = cptr[9].cval + cptr[0].cval;
  1199. CHANVAL_OUT
  1200. }
  1201. }
  1202. else {
  1203. #if defined(OPLTYPE_IS_OPL3)
  1204. if ((opl->adlibreg[0x105] & 1) && cptr->is_4op) {
  1205. if (opl->adlibreg[ARC_FEEDBACK + k + 3] & 1) {
  1206. // FM-AM-style synthesis ((op1[fb] * op2) + (op3 * op4))
  1207. if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
  1208. if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
  1209. opl->vibval1 = opl->vibval_var1;
  1210. for (i = 0; i < endsamples; i++)
  1211. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[0].freq_high / 8)*FIXEDPT*VIBFAC);
  1212. }
  1213. else opl->vibval1 = opl->vibval_const;
  1214. if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
  1215. opl->vibval2 = opl->vibval_var2;
  1216. for (i = 0; i < endsamples; i++)
  1217. opl->vibval2[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  1218. }
  1219. else opl->vibval2 = opl->vibval_const;
  1220. if (cptr[0].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1221. else opl->tremval1 = opl->tremval_const;
  1222. if (cptr[9].tremolo) opl->tremval2 = trem_lut; // tremolo enabled, use table
  1223. else opl->tremval2 = opl->tremval_const;
  1224. // calculate channel output
  1225. for (i = 0; i < endsamples; i++) {
  1226. operator_advance(&cptr[0], opl->vibval1[i]);
  1227. opfuncs[cptr[0].op_state](&cptr[0]);
  1228. operator_output(&cptr[0], (cptr[0].lastcval + cptr[0].cval)*cptr[0].mfbi / 2, opl->tremval1[i]);
  1229. operator_advance(&cptr[9], opl->vibval2[i]);
  1230. opfuncs[cptr[9].op_state](&cptr[9]);
  1231. operator_output(&cptr[9], cptr[0].cval*FIXEDPT, opl->tremval2[i]);
  1232. Bit32s chanval = cptr[9].cval;
  1233. CHANVAL_OUT
  1234. }
  1235. }
  1236. if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[3 + 9].op_state != OF_TYPE_OFF)) {
  1237. if (cptr[3].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1238. else opl->tremval1 = opl->tremval_const;
  1239. if (cptr[3 + 9].tremolo) opl->tremval2 = trem_lut; // tremolo enabled, use table
  1240. else opl->tremval2 = opl->tremval_const;
  1241. // calculate channel output
  1242. for (i = 0; i < endsamples; i++) {
  1243. operator_advance(&cptr[3], 0);
  1244. opfuncs[cptr[3].op_state](&cptr[3]);
  1245. operator_output(&cptr[3], 0, opl->tremval1[i]);
  1246. operator_advance(&cptr[3 + 9], 0);
  1247. opfuncs[cptr[3 + 9].op_state](&cptr[3 + 9]);
  1248. operator_output(&cptr[3 + 9], cptr[3].cval*FIXEDPT, opl->tremval2[i]);
  1249. Bit32s chanval = cptr[3 + 9].cval;
  1250. CHANVAL_OUT
  1251. }
  1252. }
  1253. }
  1254. else {
  1255. // FM-FM-style synthesis (op1[fb] * op2 * op3 * op4)
  1256. if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF) ||
  1257. (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3 + 9].op_state != OF_TYPE_OFF)) {
  1258. if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
  1259. opl->vibval1 = opl->vibval_var1;
  1260. for (i = 0; i < endsamples; i++)
  1261. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[0].freq_high / 8)*FIXEDPT*VIBFAC);
  1262. }
  1263. else opl->vibval1 = opl->vibval_const;
  1264. if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
  1265. opl->vibval2 = opl->vibval_var2;
  1266. for (i = 0; i < endsamples; i++)
  1267. opl->vibval2[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  1268. }
  1269. else opl->vibval2 = opl->vibval_const;
  1270. if (cptr[0].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1271. else opl->tremval1 = opl->tremval_const;
  1272. if (cptr[9].tremolo) opl->tremval2 = trem_lut; // tremolo enabled, use table
  1273. else opl->tremval2 = opl->tremval_const;
  1274. if (cptr[3].tremolo) opl->tremval3 = trem_lut; // tremolo enabled, use table
  1275. else opl->tremval3 = opl->tremval_const;
  1276. if (cptr[3 + 9].tremolo) opl->tremval4 = trem_lut; // tremolo enabled, use table
  1277. else opl->tremval4 = opl->tremval_const;
  1278. // calculate channel output
  1279. for (i = 0; i < endsamples; i++) {
  1280. operator_advance(&cptr[0], opl->vibval1[i]);
  1281. opfuncs[cptr[0].op_state](&cptr[0]);
  1282. operator_output(&cptr[0], (cptr[0].lastcval + cptr[0].cval)*cptr[0].mfbi / 2, opl->tremval1[i]);
  1283. operator_advance(&cptr[9], opl->vibval2[i]);
  1284. opfuncs[cptr[9].op_state](&cptr[9]);
  1285. operator_output(&cptr[9], cptr[0].cval*FIXEDPT, opl->tremval2[i]);
  1286. operator_advance(&cptr[3], 0);
  1287. opfuncs[cptr[3].op_state](&cptr[3]);
  1288. operator_output(&cptr[3], cptr[9].cval*FIXEDPT, opl->tremval3[i]);
  1289. operator_advance(&cptr[3 + 9], 0);
  1290. opfuncs[cptr[3 + 9].op_state](&cptr[3 + 9]);
  1291. operator_output(&cptr[3 + 9], cptr[3].cval*FIXEDPT, opl->tremval4[i]);
  1292. Bit32s chanval = cptr[3 + 9].cval;
  1293. CHANVAL_OUT
  1294. }
  1295. }
  1296. }
  1297. continue;
  1298. }
  1299. #endif
  1300. // 2op frequency modulation
  1301. if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
  1302. if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
  1303. opl->vibval1 = opl->vibval_var1;
  1304. for (i = 0; i < endsamples; i++)
  1305. opl->vibval1[i] = (Bit32s)((vib_lut[i] * cptr[0].freq_high / 8)*FIXEDPT*VIBFAC);
  1306. }
  1307. else opl->vibval1 = opl->vibval_const;
  1308. if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
  1309. opl->vibval2 = opl->vibval_var2;
  1310. for (i = 0; i < endsamples; i++)
  1311. opl->vibval2[i] = (Bit32s)((vib_lut[i] * cptr[9].freq_high / 8)*FIXEDPT*VIBFAC);
  1312. }
  1313. else opl->vibval2 = opl->vibval_const;
  1314. if (cptr[0].tremolo) opl->tremval1 = trem_lut; // tremolo enabled, use table
  1315. else opl->tremval1 = opl->tremval_const;
  1316. if (cptr[9].tremolo) opl->tremval2 = trem_lut; // tremolo enabled, use table
  1317. else opl->tremval2 = opl->tremval_const;
  1318. // calculate channel output
  1319. for (i = 0; i < endsamples; i++) {
  1320. // modulator
  1321. operator_advance(opl, &cptr[0], opl->vibval1[i]);
  1322. opfuncs[cptr[0].op_state](&cptr[0]);
  1323. operator_output(&cptr[0], (cptr[0].lastcval + cptr[0].cval)*cptr[0].mfbi / 2, opl->tremval1[i]);
  1324. // carrier
  1325. operator_advance(opl, &cptr[9], opl->vibval2[i]);
  1326. opfuncs[cptr[9].op_state](&cptr[9]);
  1327. operator_output(&cptr[9], cptr[0].cval*FIXEDPT, opl->tremval2[i]);
  1328. Bit32s chanval = cptr[9].cval;
  1329. CHANVAL_OUT
  1330. }
  1331. }
  1332. }
  1333. #if defined(OPLTYPE_IS_OPL3)
  1334. if (opl->adlibreg[0x105] & 1) {
  1335. // convert to 16bit samples (stereo)
  1336. for (i = 0; i < endsamples; i++) {
  1337. clipit16(outbufl[i], sndptr++);
  1338. clipit16(outbufr[i], sndptr++);
  1339. }
  1340. }
  1341. else {
  1342. // convert to 16bit samples (mono)
  1343. for (i = 0; i < endsamples; i++) {
  1344. clipit16(outbufl[i], sndptr++);
  1345. clipit16(outbufl[i], sndptr++);
  1346. }
  1347. }
  1348. #else
  1349. // convert to 16bit samples
  1350. for (i = 0; i < endsamples; i++)
  1351. clipit16(outbufl[i], sndptr++);
  1352. #endif
  1353. }
  1354. }
  1355. void adlib_release(opl_chip* opl)
  1356. {
  1357. if (opl) delete opl;
  1358. }