dosbox_opl.cpp 51 KB

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