dosbox_opl.cpp 64 KB

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