dosbox_opl.cpp 47 KB

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