dosbox_opl.cpp 51 KB

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