1 /*
2  * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3  * Copyright (C) 1999 - 2004 Simon Peter, <dn.tlp@gmx.net>, et al.
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  * lds.cpp - LOUDNESS Player by Simon Peter <dn.tlp@gmx.net>
20  */
21 
22 #include <string.h>
23 
24 #include "lds.h"
25 #include "debug.h"
26 
27 // Note frequency table (16 notes / octave)
28 const unsigned short CldsPlayer::frequency[] = {
29   343, 344, 345, 347, 348, 349, 350, 352, 353, 354, 356, 357, 358,
30   359, 361, 362, 363, 365, 366, 367, 369, 370, 371, 373, 374, 375,
31   377, 378, 379, 381, 382, 384, 385, 386, 388, 389, 391, 392, 393,
32   395, 396, 398, 399, 401, 402, 403, 405, 406, 408, 409, 411, 412,
33   414, 415, 417, 418, 420, 421, 423, 424, 426, 427, 429, 430, 432,
34   434, 435, 437, 438, 440, 442, 443, 445, 446, 448, 450, 451, 453,
35   454, 456, 458, 459, 461, 463, 464, 466, 468, 469, 471, 473, 475,
36   476, 478, 480, 481, 483, 485, 487, 488, 490, 492, 494, 496, 497,
37   499, 501, 503, 505, 506, 508, 510, 512, 514, 516, 518, 519, 521,
38   523, 525, 527, 529, 531, 533, 535, 537, 538, 540, 542, 544, 546,
39   548, 550, 552, 554, 556, 558, 560, 562, 564, 566, 568, 571, 573,
40   575, 577, 579, 581, 583, 585, 587, 589, 591, 594, 596, 598, 600,
41   602, 604, 607, 609, 611, 613, 615, 618, 620, 622, 624, 627, 629,
42   631, 633, 636, 638, 640, 643, 645, 647, 650, 652, 654, 657, 659,
43   662, 664, 666, 669, 671, 674, 676, 678, 681, 683
44 };
45 
46 // Vibrato (sine) table
47 const unsigned char CldsPlayer::vibtab[] = {
48   0, 13, 25, 37, 50, 62, 74, 86, 98, 109, 120, 131, 142, 152, 162,
49   171, 180, 189, 197, 205, 212, 219, 225, 231, 236, 240, 244, 247,
50   250, 252, 254, 255, 255, 255, 254, 252, 250, 247, 244, 240, 236,
51   231, 225, 219, 212, 205, 197, 189, 180, 171, 162, 152, 142, 131,
52   120, 109, 98, 86, 74, 62, 50, 37, 25, 13
53 };
54 
55 // Tremolo (sine * sine) table
56 const unsigned char CldsPlayer::tremtab[] = {
57   0, 0, 1, 1, 2, 4, 5, 7, 10, 12, 15, 18, 21, 25, 29, 33, 37, 42, 47,
58   52, 57, 62, 67, 73, 79, 85, 90, 97, 103, 109, 115, 121, 128, 134,
59   140, 146, 152, 158, 165, 170, 176, 182, 188, 193, 198, 203, 208,
60   213, 218, 222, 226, 230, 234, 237, 240, 243, 245, 248, 250, 251,
61   253, 254, 254, 255, 255, 255, 254, 254, 253, 251, 250, 248, 245,
62   243, 240, 237, 234, 230, 226, 222, 218, 213, 208, 203, 198, 193,
63   188, 182, 176, 170, 165, 158, 152, 146, 140, 134, 127, 121, 115,
64   109, 103, 97, 90, 85, 79, 73, 67, 62, 57, 52, 47, 42, 37, 33, 29,
65   25, 21, 18, 15, 12, 10, 7, 5, 4, 2, 1, 1, 0
66 };
67 
68 // 'maxsound' is maximum number of patches (instruments)
69 // 'maxpos' is maximum number of entries in position list (orderlist)
70 const unsigned short CldsPlayer::maxsound = 0x3f, CldsPlayer::maxpos = 0xff;
71 
72 /*** public methods *************************************/
73 
CldsPlayer(Copl * newopl)74 CldsPlayer::CldsPlayer(Copl *newopl)
75   : CPlayer(newopl), soundbank(0), positions(0), patterns(0)
76 {
77 }
78 
~CldsPlayer()79 CldsPlayer::~CldsPlayer()
80 {
81   if(soundbank) delete [] soundbank;
82   if(positions) delete [] positions;
83   if(patterns) delete [] patterns;
84 }
85 
load(const std::string & filename,const CFileProvider & fp)86 bool CldsPlayer::load(const std::string &filename, const CFileProvider &fp)
87 {
88   binistream	*f;
89   unsigned int	i, j;
90   SoundBank	*sb;
91 
92   // file validation section (actually just an extension check)
93   if(!fp.extension(filename, ".lds")) return false;
94   f = fp.open(filename); if(!f) return false;
95 
96   // file load section (header)
97   mode = f->readInt(1);
98   if(mode > 2) { fp.close(f); return false; }
99   speed = f->readInt(2);
100   tempo = f->readInt(1);
101   pattlen = f->readInt(1);
102   for(i = 0; i < 9; i++) chandelay[i] = f->readInt(1);
103   regbd = f->readInt(1);
104 
105   // load patches
106   numpatch = f->readInt(2);
107   soundbank = new SoundBank[numpatch];
108   for(i = 0; i < numpatch; i++) {
109     sb = &soundbank[i];
110     sb->mod_misc = f->readInt(1); sb->mod_vol = f->readInt(1);
111     sb->mod_ad = f->readInt(1); sb->mod_sr = f->readInt(1);
112     sb->mod_wave = f->readInt(1); sb->car_misc = f->readInt(1);
113     sb->car_vol = f->readInt(1); sb->car_ad = f->readInt(1);
114     sb->car_sr = f->readInt(1); sb->car_wave = f->readInt(1);
115     sb->feedback = f->readInt(1); sb->keyoff = f->readInt(1);
116     sb->portamento = f->readInt(1); sb->glide = f->readInt(1);
117     sb->finetune = f->readInt(1); sb->vibrato = f->readInt(1);
118     sb->vibdelay = f->readInt(1); sb->mod_trem = f->readInt(1);
119     sb->car_trem = f->readInt(1); sb->tremwait = f->readInt(1);
120     sb->arpeggio = f->readInt(1);
121     for(j = 0; j < 12; j++) sb->arp_tab[j] = f->readInt(1);
122     sb->start = f->readInt(2); sb->size = f->readInt(2);
123     sb->fms = f->readInt(1); sb->transp = f->readInt(2);
124     sb->midinst = f->readInt(1); sb->midvelo = f->readInt(1);
125     sb->midkey = f->readInt(1); sb->midtrans = f->readInt(1);
126     sb->middum1 = f->readInt(1); sb->middum2 = f->readInt(1);
127   }
128 
129   // load positions
130   numposi = f->readInt(2);
131   positions = new Position[9 * numposi];
132   for(i = 0; i < numposi; i++)
133     for(j = 0; j < 9; j++) {
134       /*
135        * patnum is a pointer inside the pattern space, but patterns are 16bit
136        * word fields anyway, so it ought to be an even number (hopefully) and
137        * we can just divide it by 2 to get our array index of 16bit words.
138        */
139       positions[i * 9 + j].patnum = f->readInt(2) / 2;
140       positions[i * 9 + j].transpose = f->readInt(1);
141     }
142 
143   AdPlug_LogWrite("CldsPlayer::load(\"%s\",fp): loading LOUDNESS file: mode = "
144 		  "%d, pattlen = %d, numpatch = %d, numposi = %d\n",
145 		  filename.c_str(), mode, pattlen, numpatch, numposi);
146 
147   // load patterns
148   f->ignore(2);		// ignore # of digital sounds (not played by this player)
149   patterns = new unsigned short[(fp.filesize(f) - f->pos()) / 2 + 1];
150   for(i = 0; !f->eof(); i++)
151     patterns[i] = f->readInt(2);
152 
153   fp.close(f);
154   rewind(0);
155   return true;
156 }
157 
update()158 bool CldsPlayer::update()
159 {
160   unsigned short	comword, freq, octave, chan, tune, wibc, tremc, arpreg;
161   bool			vbreak;
162   unsigned char		level, regnum, comhi, comlo;
163   int			i;
164   Channel		*c;
165 
166   if(!playing) return false;
167 
168   // handle fading
169   if(fadeonoff)
170     if(fadeonoff <= 128) {
171       if(allvolume > fadeonoff || allvolume == 0)
172 	allvolume -= fadeonoff;
173       else {
174 	allvolume = 1;
175 	fadeonoff = 0;
176 	if(hardfade != 0) {
177 	  playing = false;
178 	  hardfade = 0;
179 	  for(i = 0; i < 9; i++)
180 	    channel[i].keycount = 1;
181 	}
182       }
183     } else
184       if(((allvolume + (0x100 - fadeonoff)) & 0xff) <= mainvolume)
185 	allvolume += 0x100 - fadeonoff;
186       else {
187 	allvolume = mainvolume;
188 	fadeonoff = 0;
189       }
190 
191   // handle channel delay
192   for(chan = 0; chan < 9; chan++) {
193     c = &channel[chan];
194     if(c->chancheat.chandelay)
195       if(!(--c->chancheat.chandelay))
196 	playsound(c->chancheat.sound, chan, c->chancheat.high);
197   }
198 
199   // handle notes
200   if(!tempo_now) {
201     vbreak = false;
202     for(chan = 0; chan < 9; chan++) {
203       c = &channel[chan];
204       if(!c->packwait) {
205 	unsigned short	patnum = positions[posplay * 9 + chan].patnum;
206 	unsigned char	transpose = positions[posplay * 9 + chan].transpose;
207 
208 	comword = patterns[patnum + c->packpos];
209 	comhi = comword >> 8; comlo = comword & 0xff;
210 	if(comword)
211 	  if(comhi == 0x80)
212 	    c->packwait = comlo;
213 	  else
214 	    if(comhi >= 0x80) {
215 	      switch(comhi) {
216 	      case 0xff:
217 		c->volcar = (((c->volcar & 0x3f) * comlo) >> 6) & 0x3f;
218 		if(fmchip[0xc0 + chan] & 1)
219 		  c->volmod = (((c->volmod & 0x3f) * comlo) >> 6) & 0x3f;
220 		break;
221 	      case 0xfe:
222 		tempo = comword & 0x3f;
223 		break;
224 	      case 0xfd:
225 		c->nextvol = comlo;
226 		break;
227 	      case 0xfc:
228 		playing = false;
229 		// in real player there's also full keyoff here, but we don't need it
230 		break;
231 	      case 0xfb:
232 		c->keycount = 1;
233 		break;
234 	      case 0xfa:
235 		vbreak = true;
236 		jumppos = (posplay + 1) & maxpos;
237 		break;
238 	      case 0xf9:
239 		vbreak = true;
240 		jumppos = comlo & maxpos;
241 		jumping = 1;
242 		if(jumppos < posplay) songlooped = true;
243 		break;
244 	      case 0xf8:
245 		c->lasttune = 0;
246 		break;
247 	      case 0xf7:
248 		c->vibwait = 0;
249 		// PASCAL: c->vibspeed = ((comlo >> 4) & 15) + 2;
250 		c->vibspeed = (comlo >> 4) + 2;
251 		c->vibrate = (comlo & 15) + 1;
252 		break;
253 	      case 0xf6:
254 		c->glideto = comlo;
255 		break;
256 	      case 0xf5:
257 		c->finetune = comlo;
258 		break;
259 	      case 0xf4:
260 		if(!hardfade) {
261 		  allvolume = mainvolume = comlo;
262 		  fadeonoff = 0;
263 		}
264 		break;
265 	      case 0xf3:
266 		if(!hardfade) fadeonoff = comlo;
267 		break;
268 	      case 0xf2:
269 		c->trmstay = comlo;
270 		break;
271 	      case 0xf1:	// panorama
272 	      case 0xf0:	// progch
273 		// MIDI commands (unhandled)
274 		AdPlug_LogWrite("CldsPlayer(): not handling MIDI command 0x%x, "
275 				"value = 0x%x\n", comhi);
276 		break;
277 	      default:
278 		if(comhi < 0xa0)
279 		  c->glideto = comhi & 0x1f;
280 		else
281 		  AdPlug_LogWrite("CldsPlayer(): unknown command 0x%x encountered!"
282 				  " value = 0x%x\n", comhi, comlo);
283 		break;
284 	      }
285 	    } else {
286 	      unsigned char	sound;
287 	      unsigned short	high;
288 	      signed char	transp = transpose & 127;
289 
290 	      /*
291 	       * Originally, in assembler code, the player first shifted
292 	       * logically left the transpose byte by 1 and then shifted
293 	       * arithmetically right the same byte to achieve the final,
294 	       * signed transpose value. Since we can't do arithmetic shifts
295 	       * in C, we just duplicate the 7th bit into the 8th one and
296 	       * discard the 8th one completely.
297 	       */
298 
299 	      if(transpose & 64) transp |= 128;
300 
301 	      if(transpose & 128) {
302 		sound = (comlo + transp) & maxsound;
303 		high = comhi << 4;
304 	      } else {
305 		sound = comlo & maxsound;
306 		high = (comhi + transp) << 4;
307 	      }
308 
309 	      /*
310 		PASCAL:
311 	      sound = comlo & maxsound;
312 	      high = (comhi + (((transpose + 0x24) & 0xff) - 0x24)) << 4;
313 	      */
314 
315 	      if(!chandelay[chan])
316 		playsound(sound, chan, high);
317 	      else {
318 		c->chancheat.chandelay = chandelay[chan];
319 		c->chancheat.sound = sound;
320 		c->chancheat.high = high;
321 	      }
322 	    }
323 
324 	c->packpos++;
325       } else
326 	c->packwait--;
327     }
328 
329     tempo_now = tempo;
330     /*
331       The continue table is updated here, but this is only used in the
332       original player, which can be paused in the middle of a song and then
333       unpaused. Since AdPlug does all this for us automatically, we don't
334       have a continue table here. The continue table update code is noted
335       here for reference only.
336 
337       if(!pattplay) {
338         conttab[speed & maxcont].position = posplay & 0xff;
339         conttab[speed & maxcont].tempo = tempo;
340       }
341     */
342     pattplay++;
343     if(vbreak) {
344       pattplay = 0;
345       for(i = 0; i < 9; i++) channel[i].packpos = channel[i].packwait = 0;
346       posplay = jumppos;
347     } else
348       if(pattplay >= pattlen) {
349 	pattplay = 0;
350 	for(i = 0; i < 9; i++) channel[i].packpos = channel[i].packwait = 0;
351 	posplay = (posplay + 1) & maxpos;
352       }
353   } else
354     tempo_now--;
355 
356   // make effects
357   for(chan = 0; chan < 9; chan++) {
358     c = &channel[chan];
359     regnum = op_table[chan];
360     if(c->keycount > 0) {
361       if(c->keycount == 1)
362 	setregs_adv(0xb0 + chan, 0xdf, 0);
363       c->keycount--;
364     }
365 
366     // arpeggio
367     if(c->arp_size == 0)
368       arpreg = 0;
369     else {
370       arpreg = c->arp_tab[c->arp_pos] << 4;
371       if(arpreg == 0x800) {
372 	if(c->arp_pos > 0) c->arp_tab[0] = c->arp_tab[c->arp_pos - 1];
373 	c->arp_size = 1; c->arp_pos = 0;
374 	arpreg = c->arp_tab[0] << 4;
375       }
376 
377       if(c->arp_count == c->arp_speed) {
378 	c->arp_pos++;
379 	if(c->arp_pos >= c->arp_size) c->arp_pos = 0;
380 	c->arp_count = 0;
381       } else
382 	c->arp_count++;
383     }
384 
385     // glide & portamento
386     if(c->lasttune && (c->lasttune != c->gototune)) {
387       if(c->lasttune > c->gototune) {
388 	if(c->lasttune - c->gototune < c->portspeed)
389 	  c->lasttune = c->gototune;
390 	else
391 	  c->lasttune -= c->portspeed;
392       } else {
393 	if(c->gototune - c->lasttune < c->portspeed)
394 	  c->lasttune = c->gototune;
395 	else
396 	  c->lasttune += c->portspeed;
397       }
398 
399       if(arpreg >= 0x800)
400 	arpreg = c->lasttune - (arpreg ^ 0xff0) - 16;
401       else
402 	arpreg += c->lasttune;
403 
404       freq = frequency[arpreg % (12 * 16)];
405       octave = arpreg / (12 * 16) - 1;
406       setregs(0xa0 + chan, freq & 0xff);
407       setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf);
408     } else {
409       // vibrato
410       if(!c->vibwait) {
411 	if(c->vibrate) {
412 	  wibc = vibtab[c->vibcount & 0x3f] * c->vibrate;
413 
414 	  if((c->vibcount & 0x40) == 0)
415 	    tune = c->lasttune + (wibc >> 8);
416 	  else
417 	    tune = c->lasttune - (wibc >> 8);
418 
419 	  if(arpreg >= 0x800)
420 	    tune = tune - (arpreg ^ 0xff0) - 16;
421 	  else
422 	    tune += arpreg;
423 
424 	  freq = frequency[tune % (12 * 16)];
425 	  octave = tune / (12 * 16) - 1;
426 	  setregs(0xa0 + chan, freq & 0xff);
427 	  setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf);
428 	  c->vibcount += c->vibspeed;
429 	} else
430 	  if(c->arp_size != 0) {	// no vibrato, just arpeggio
431 	    if(arpreg >= 0x800)
432 	      tune = c->lasttune - (arpreg ^ 0xff0) - 16;
433 	    else
434 	      tune = c->lasttune + arpreg;
435 
436 	    freq = frequency[tune % (12 * 16)];
437 	    octave = tune / (12 * 16) - 1;
438 	    setregs(0xa0 + chan, freq & 0xff);
439 	    setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf);
440 	  }
441       } else {	// no vibrato, just arpeggio
442 	c->vibwait--;
443 
444 	if(c->arp_size != 0) {
445 	  if(arpreg >= 0x800)
446 	    tune = c->lasttune - (arpreg ^ 0xff0) - 16;
447 	  else
448 	    tune = c->lasttune + arpreg;
449 
450 	  freq = frequency[tune % (12 * 16)];
451 	  octave = tune / (12 * 16) - 1;
452 	  setregs(0xa0 + chan, freq & 0xff);
453 	  setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf);
454 	}
455       }
456     }
457 
458     // tremolo (modulator)
459     if(!c->trmwait) {
460       if(c->trmrate) {
461 	tremc = tremtab[c->trmcount & 0x7f] * c->trmrate;
462 	if((tremc >> 8) <= (c->volmod & 0x3f))
463 	  level = (c->volmod & 0x3f) - (tremc >> 8);
464 	else
465 	  level = 0;
466 
467 	if(allvolume != 0 && (fmchip[0xc0 + chan] & 1))
468 	  setregs_adv(0x40 + regnum, 0xc0, ((level * allvolume) >> 8) ^ 0x3f);
469 	else
470 	  setregs_adv(0x40 + regnum, 0xc0, level ^ 0x3f);
471 
472 	c->trmcount += c->trmspeed;
473       } else
474 	if(allvolume != 0 && (fmchip[0xc0 + chan] & 1))
475 	  setregs_adv(0x40 + regnum, 0xc0, ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f);
476 	else
477 	  setregs_adv(0x40 + regnum, 0xc0, (c->volmod ^ 0x3f) & 0x3f);
478     } else {
479       c->trmwait--;
480       if(allvolume != 0 && (fmchip[0xc0 + chan] & 1))
481 	setregs_adv(0x40 + regnum, 0xc0, ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f);
482     }
483 
484     // tremolo (carrier)
485     if(!c->trcwait) {
486       if(c->trcrate) {
487 	tremc = tremtab[c->trccount & 0x7f] * c->trcrate;
488 	if((tremc >> 8) <= (c->volcar & 0x3f))
489 	  level = (c->volcar & 0x3f) - (tremc >> 8);
490 	else
491 	  level = 0;
492 
493 	if(allvolume != 0)
494 	  setregs_adv(0x43 + regnum, 0xc0, ((level * allvolume) >> 8) ^ 0x3f);
495 	else
496 	  setregs_adv(0x43 + regnum, 0xc0, level ^ 0x3f);
497 	c->trccount += c->trcspeed;
498       } else
499 	if(allvolume != 0)
500 	  setregs_adv(0x43 + regnum, 0xc0, ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f);
501 	else
502 	  setregs_adv(0x43 + regnum, 0xc0, (c->volcar ^ 0x3f) & 0x3f);
503     } else {
504       c->trcwait--;
505       if(allvolume != 0)
506 	setregs_adv(0x43 + regnum, 0xc0, ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f);
507     }
508   }
509 
510   return (!playing || songlooped) ? false : true;
511 }
512 
rewind(int subsong)513 void CldsPlayer::rewind(int subsong)
514 {
515   int i;
516 
517   // init all with 0
518   tempo_now = 3; playing = true; songlooped = false;
519   jumping = fadeonoff = allvolume = hardfade = pattplay = posplay = jumppos =
520     mainvolume = 0;
521   memset(channel, 0, sizeof(channel));
522   memset(fmchip, 0, sizeof(fmchip));
523 
524   // OPL2 init
525   opl->init();				// Reset OPL chip
526   opl->write(1, 0x20);
527   opl->write(8, 0);
528   opl->write(0xbd, regbd);
529 
530   for(i = 0; i < 9; i++) {
531     opl->write(0x20 + op_table[i], 0);
532     opl->write(0x23 + op_table[i], 0);
533     opl->write(0x40 + op_table[i], 0x3f);
534     opl->write(0x43 + op_table[i], 0x3f);
535     opl->write(0x60 + op_table[i], 0xff);
536     opl->write(0x63 + op_table[i], 0xff);
537     opl->write(0x80 + op_table[i], 0xff);
538     opl->write(0x83 + op_table[i], 0xff);
539     opl->write(0xe0 + op_table[i], 0);
540     opl->write(0xe3 + op_table[i], 0);
541     opl->write(0xa0 + i, 0);
542     opl->write(0xb0 + i, 0);
543     opl->write(0xc0 + i, 0);
544   }
545 }
546 
547 /*** private methods *************************************/
548 
playsound(int inst_number,int channel_number,int tunehigh)549 void CldsPlayer::playsound(int inst_number, int channel_number, int tunehigh)
550 {
551   Channel		*c = &channel[channel_number];		// current channel
552   SoundBank		*i = &soundbank[inst_number];		// current instrument
553   unsigned int		regnum = op_table[channel_number];	// channel's OPL2 register
554   unsigned char		volcalc, octave;
555   unsigned short	freq;
556 
557   // set fine tune
558   tunehigh += ((i->finetune + c->finetune + 0x80) & 0xff) - 0x80;
559 
560   // arpeggio handling
561   if(!i->arpeggio) {
562     unsigned short	arpcalc = i->arp_tab[0] << 4;
563 
564     if(arpcalc > 0x800)
565       tunehigh = tunehigh - (arpcalc ^ 0xff0) - 16;
566     else
567       tunehigh += arpcalc;
568   }
569 
570   // glide handling
571   if(c->glideto != 0) {
572     c->gototune = tunehigh;
573     c->portspeed = c->glideto;
574     c->glideto = c->finetune = 0;
575     return;
576   }
577 
578   // set modulator registers
579   setregs(0x20 + regnum, i->mod_misc);
580   volcalc = i->mod_vol;
581   if(!c->nextvol || !(i->feedback & 1))
582     c->volmod = volcalc;
583   else
584     c->volmod = (volcalc & 0xc0) | ((((volcalc & 0x3f) * c->nextvol) >> 6));
585 
586   if((i->feedback & 1) == 1 && allvolume != 0)
587     setregs(0x40 + regnum, ((c->volmod & 0xc0) | (((c->volmod & 0x3f) * allvolume) >> 8)) ^ 0x3f);
588   else
589     setregs(0x40 + regnum, c->volmod ^ 0x3f);
590   setregs(0x60 + regnum, i->mod_ad);
591   setregs(0x80 + regnum, i->mod_sr);
592   setregs(0xe0 + regnum, i->mod_wave);
593 
594   // Set carrier registers
595   setregs(0x23 + regnum, i->car_misc);
596   volcalc = i->car_vol;
597   if(!c->nextvol)
598     c->volcar = volcalc;
599   else
600     c->volcar = (volcalc & 0xc0) | ((((volcalc & 0x3f) * c->nextvol) >> 6));
601 
602   if(allvolume)
603     setregs(0x43 + regnum, ((c->volcar & 0xc0) | (((c->volcar & 0x3f) * allvolume) >> 8)) ^ 0x3f);
604   else
605     setregs(0x43 + regnum, c->volcar ^ 0x3f);
606   setregs(0x63 + regnum, i->car_ad);
607   setregs(0x83 + regnum, i->car_sr);
608   setregs(0xe3 + regnum, i->car_wave);
609   setregs(0xc0 + channel_number, i->feedback);
610   setregs_adv(0xb0 + channel_number, 0xdf, 0);		// key off
611 
612   freq = frequency[tunehigh % (12 * 16)];
613   octave = tunehigh / (12 * 16) - 1;
614   if(!i->glide) {
615     if(!i->portamento || !c->lasttune) {
616       setregs(0xa0 + channel_number, freq & 0xff);
617       setregs(0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8));
618       c->lasttune = c->gototune = tunehigh;
619     } else {
620       c->gototune = tunehigh;
621       c->portspeed = i->portamento;
622       setregs_adv(0xb0 + channel_number, 0xdf, 0x20);	// key on
623     }
624   } else {
625     setregs(0xa0 + channel_number, freq & 0xff);
626     setregs(0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8));
627     c->lasttune = tunehigh;
628     c->gototune = tunehigh + ((i->glide + 0x80) & 0xff) - 0x80;	// set destination
629     c->portspeed = i->portamento;
630   }
631 
632   if(!i->vibrato)
633     c->vibwait = c->vibspeed = c->vibrate = 0;
634   else {
635     c->vibwait = i->vibdelay;
636     // PASCAL:    c->vibspeed = ((i->vibrato >> 4) & 15) + 1;
637     c->vibspeed = (i->vibrato >> 4) + 2;
638     c->vibrate = (i->vibrato & 15) + 1;
639   }
640 
641   if(!(c->trmstay & 0xf0)) {
642     c->trmwait = (i->tremwait & 0xf0) >> 3;
643     // PASCAL:    c->trmspeed = (i->mod_trem >> 4) & 15;
644     c->trmspeed = i->mod_trem >> 4;
645     c->trmrate = i->mod_trem & 15;
646     c->trmcount = 0;
647   }
648 
649   if(!(c->trmstay & 0x0f)) {
650     c->trcwait = (i->tremwait & 15) << 1;
651     // PASCAL:    c->trcspeed = (i->car_trem >> 4) & 15;
652     c->trcspeed = i->car_trem >> 4;
653     c->trcrate = i->car_trem & 15;
654     c->trccount = 0;
655   }
656 
657   c->arp_size = i->arpeggio & 15;
658   c->arp_speed = i->arpeggio >> 4;
659   memcpy(c->arp_tab, i->arp_tab, 12);
660   c->keycount = i->keyoff;
661   c->nextvol = c->glideto = c->finetune = c->vibcount = c->arp_pos = c->arp_count = 0;
662 }
663 
setregs(unsigned char reg,unsigned char val)664 inline void CldsPlayer::setregs(unsigned char reg, unsigned char val)
665 {
666   if(fmchip[reg] == val) return;
667 
668   fmchip[reg] = val;
669   opl->write(reg, val);
670 }
671 
setregs_adv(unsigned char reg,unsigned char mask,unsigned char val)672 inline void CldsPlayer::setregs_adv(unsigned char reg, unsigned char mask,
673 				    unsigned char val)
674 {
675   setregs(reg, (fmchip[reg] & mask) | val);
676 }
677