359
|
1 /*
|
|
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
|
|
3 * Copyright (C) 1999 - 2002 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 *
|
|
19 * protrack.cpp - Generic Protracker Player
|
|
20 * Copyright (C) 2000 - 2002 Simon Peter <dn.tlp@gmx.net>
|
|
21 *
|
|
22 * NOTES:
|
|
23 * This is a generic Protracker-based formats player. It offers all Protracker
|
|
24 * features, plus a good set of extensions to be compatible to other Protracker
|
|
25 * derivatives. It is derived from the original SA2 player by me. If you got a
|
|
26 * Protracker-like format, this is most certainly the player you want to use.
|
|
27 *
|
|
28 * USAGE:
|
|
29 * Read the file 'Protracker.txt' in the 'doc' subdirectory.
|
|
30 */
|
|
31
|
|
32 #include "protrack.h"
|
|
33 #include "debug.h"
|
|
34
|
|
35 #define SPECIALARPLEN 256 // Standard length of special arpeggio lists
|
|
36 #define JUMPMARKER 0x80 // Orderlist jump marker
|
|
37
|
|
38 // SA2 compatible adlib note table
|
|
39 const unsigned short CmodPlayer::sa2_notetable[12] =
|
|
40 {340,363,385,408,432,458,485,514,544,577,611,647};
|
|
41
|
|
42 // SA2 compatible vibrato rate table
|
|
43 const unsigned char CmodPlayer::vibratotab[32] =
|
|
44 {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
|
|
45
|
|
46 /*** public methods *************************************/
|
|
47
|
|
48 CmodPlayer::CmodPlayer(Copl *newopl)
|
|
49 : CPlayer(newopl), inst(0), order(0), arplist(0), arpcmd(0), initspeed(6),
|
|
50 activechan(0xffff), flags(Standard), nop(0), nrows(0), npats(0), nchans(0)
|
|
51 {
|
|
52 realloc_order(128);
|
|
53 realloc_patterns(64, 64, 9);
|
|
54 realloc_instruments(250);
|
|
55 init_notetable(sa2_notetable);
|
|
56 }
|
|
57
|
|
58 CmodPlayer::~CmodPlayer()
|
|
59 {
|
|
60 dealloc();
|
|
61 }
|
|
62
|
|
63 bool CmodPlayer::update()
|
|
64 {
|
|
65 unsigned char pattbreak=0,donote; // remember vars
|
|
66 unsigned char pattnr,chan,info1,info2,info; // cache vars
|
|
67 unsigned short track;
|
|
68 unsigned long row;
|
|
69
|
|
70 if(!speed) // song full stop
|
|
71 return !songend;
|
|
72
|
|
73 // effect handling (timer dependant)
|
|
74 for(chan=0;chan<nchans;chan++) {
|
|
75 if(arplist && arpcmd && inst[channel[chan].inst].arpstart) // special arpeggio
|
|
76 if(channel[chan].arpspdcnt)
|
|
77 channel[chan].arpspdcnt--;
|
|
78 else
|
|
79 if(arpcmd[channel[chan].arppos] != 255) {
|
|
80 switch(arpcmd[channel[chan].arppos]) {
|
|
81 case 252: channel[chan].vol1 = arplist[channel[chan].arppos]; // set volume
|
|
82 if(channel[chan].vol1 > 63) // ?????
|
|
83 channel[chan].vol1 = 63;
|
|
84 channel[chan].vol2 = channel[chan].vol1;
|
|
85 setvolume(chan);
|
|
86 break;
|
|
87 case 253: channel[chan].key = 0; setfreq(chan); break; // release sustaining note
|
|
88 case 254: channel[chan].arppos = arplist[channel[chan].arppos]; break; // arpeggio loop
|
|
89 default: if(arpcmd[channel[chan].arppos]) {
|
|
90 if(arpcmd[channel[chan].arppos] / 10)
|
|
91 opl->write(0xe3 + op_table[chan], arpcmd[channel[chan].arppos] / 10 - 1);
|
|
92 if(arpcmd[channel[chan].arppos] % 10)
|
|
93 opl->write(0xe0 + op_table[chan], (arpcmd[channel[chan].arppos] % 10) - 1);
|
|
94 if(arpcmd[channel[chan].arppos] < 10) // ?????
|
|
95 opl->write(0xe0 + op_table[chan], arpcmd[channel[chan].arppos] - 1);
|
|
96 }
|
|
97 }
|
|
98 if(arpcmd[channel[chan].arppos] != 252) {
|
|
99 if(arplist[channel[chan].arppos] <= 96)
|
|
100 setnote(chan,channel[chan].note + arplist[channel[chan].arppos]);
|
|
101 if(arplist[channel[chan].arppos] >= 100)
|
|
102 setnote(chan,arplist[channel[chan].arppos] - 100);
|
|
103 } else
|
|
104 setnote(chan,channel[chan].note);
|
|
105 setfreq(chan);
|
|
106 if(arpcmd[channel[chan].arppos] != 255)
|
|
107 channel[chan].arppos++;
|
|
108 channel[chan].arpspdcnt = inst[channel[chan].inst].arpspeed - 1;
|
|
109 }
|
|
110
|
|
111 info1 = channel[chan].info1;
|
|
112 info2 = channel[chan].info2;
|
|
113 if(flags & Decimal)
|
|
114 info = channel[chan].info1 * 10 + channel[chan].info2;
|
|
115 else
|
|
116 info = (channel[chan].info1 << 4) + channel[chan].info2;
|
|
117 switch(channel[chan].fx) {
|
|
118 case 0: if(info) { // arpeggio
|
|
119 if(channel[chan].trigger < 2)
|
|
120 channel[chan].trigger++;
|
|
121 else
|
|
122 channel[chan].trigger = 0;
|
|
123 switch(channel[chan].trigger) {
|
|
124 case 0: setnote(chan,channel[chan].note); break;
|
|
125 case 1: setnote(chan,channel[chan].note + info1); break;
|
|
126 case 2: setnote(chan,channel[chan].note + info2);
|
|
127 }
|
|
128 setfreq(chan);
|
|
129 }
|
|
130 break;
|
|
131 case 1: slide_up(chan,info); setfreq(chan); break; // slide up
|
|
132 case 2: slide_down(chan,info); setfreq(chan); break; // slide down
|
|
133 case 3: tone_portamento(chan,channel[chan].portainfo); break; // tone portamento
|
|
134 case 4: vibrato(chan,channel[chan].vibinfo1,channel[chan].vibinfo2); break; // vibrato
|
|
135 case 5: // tone portamento & volume slide
|
|
136 case 6: if(channel[chan].fx == 5) // vibrato & volume slide
|
|
137 tone_portamento(chan,channel[chan].portainfo);
|
|
138 else
|
|
139 vibrato(chan,channel[chan].vibinfo1,channel[chan].vibinfo2);
|
|
140 case 10: if(del % 4) // SA2 volume slide
|
|
141 break;
|
|
142 if(info1)
|
|
143 vol_up(chan,info1);
|
|
144 else
|
|
145 vol_down(chan,info2);
|
|
146 setvolume(chan);
|
|
147 break;
|
|
148 case 14: if(info1 == 3) // retrig note
|
|
149 if(!(del % (info2+1)))
|
|
150 playnote(chan);
|
|
151 break;
|
|
152 case 16: if(del % 4) // AMD volume slide
|
|
153 break;
|
|
154 if(info1)
|
|
155 vol_up_alt(chan,info1);
|
|
156 else
|
|
157 vol_down_alt(chan,info2);
|
|
158 setvolume(chan);
|
|
159 break;
|
|
160 case 20: // RAD volume slide
|
|
161 if(info < 50)
|
|
162 vol_down_alt(chan,info);
|
|
163 else
|
|
164 vol_up_alt(chan,info - 50);
|
|
165 setvolume(chan);
|
|
166 break;
|
|
167 case 26: // volume slide
|
|
168 if(info1)
|
|
169 vol_up(chan,info1);
|
|
170 else
|
|
171 vol_down(chan,info2);
|
|
172 setvolume(chan);
|
|
173 break;
|
|
174 case 28:
|
|
175 if (info1) {
|
|
176 slide_up(chan,1); channel[chan].info1--;
|
|
177 }
|
|
178 if (info2) {
|
|
179 slide_down(chan,1); channel[chan].info2--;
|
|
180 }
|
|
181 setfreq(chan);
|
|
182 break;
|
|
183 }
|
|
184 }
|
|
185
|
|
186 if(del) { // speed compensation
|
|
187 del--;
|
|
188 return !songend;
|
|
189 }
|
|
190
|
|
191 // arrangement handling
|
|
192 if(ord >= length) {
|
|
193 songend = 1; // set end-flag
|
|
194 ord = restartpos;
|
|
195 }
|
|
196 pattnr = order[ord];
|
|
197
|
|
198 if(!rw) AdPlug_LogWrite("\nCmodPlayer::update(): Pattern: %d, Order: %d\n", pattnr, ord);
|
|
199 AdPlug_LogWrite("CmodPlayer::update():%3d|", rw);
|
|
200
|
|
201 // play row
|
|
202 row = rw;
|
|
203 for(chan=0;chan<nchans;chan++) {
|
|
204 if(!(activechan >> (15 - chan)) & 1) { // channel active?
|
|
205 AdPlug_LogWrite("N/A|");
|
|
206 continue;
|
|
207 }
|
|
208 if(!(track = trackord[pattnr][chan])) { // resolve track
|
|
209 AdPlug_LogWrite("------------|");
|
|
210 continue;
|
|
211 } else
|
|
212 track--;
|
|
213
|
|
214 AdPlug_LogWrite("%3d%3d%2X%2X%2X|", tracks[track][row].note,
|
|
215 tracks[track][row].inst, tracks[track][row].command,
|
|
216 tracks[track][row].param1, tracks[track][row].param2);
|
|
217
|
|
218 donote = 0;
|
|
219 if(tracks[track][row].inst) {
|
|
220 channel[chan].inst = tracks[track][row].inst - 1;
|
|
221 if (!(flags & Faust)) {
|
|
222 channel[chan].vol1 = 63 - (inst[channel[chan].inst].data[10] & 63);
|
|
223 channel[chan].vol2 = 63 - (inst[channel[chan].inst].data[9] & 63);
|
|
224 setvolume(chan);
|
|
225 }
|
|
226 }
|
|
227
|
|
228 if(tracks[track][row].note && tracks[track][row].command != 3) { // no tone portamento
|
|
229 channel[chan].note = tracks[track][row].note;
|
|
230 setnote(chan,tracks[track][row].note);
|
|
231 channel[chan].nextfreq = channel[chan].freq;
|
|
232 channel[chan].nextoct = channel[chan].oct;
|
|
233 channel[chan].arppos = inst[channel[chan].inst].arpstart;
|
|
234 channel[chan].arpspdcnt = 0;
|
|
235 if(tracks[track][row].note != 127) // handle key off
|
|
236 donote = 1;
|
|
237 }
|
|
238 channel[chan].fx = tracks[track][row].command;
|
|
239 channel[chan].info1 = tracks[track][row].param1;
|
|
240 channel[chan].info2 = tracks[track][row].param2;
|
|
241
|
|
242 if(donote)
|
|
243 playnote(chan);
|
|
244
|
|
245 // command handling (row dependant)
|
|
246 info1 = channel[chan].info1;
|
|
247 info2 = channel[chan].info2;
|
|
248 if(flags & Decimal)
|
|
249 info = channel[chan].info1 * 10 + channel[chan].info2;
|
|
250 else
|
|
251 info = (channel[chan].info1 << 4) + channel[chan].info2;
|
|
252 switch(channel[chan].fx) {
|
|
253 case 3: if(tracks[track][row].note) { // tone portamento
|
|
254 if(tracks[track][row].note < 13)
|
|
255 channel[chan].nextfreq = notetable[tracks[track][row].note - 1];
|
|
256 else
|
|
257 if(tracks[track][row].note % 12 > 0)
|
|
258 channel[chan].nextfreq = notetable[(tracks[track][row].note % 12) - 1];
|
|
259 else
|
|
260 channel[chan].nextfreq = notetable[11];
|
|
261 channel[chan].nextoct = (tracks[track][row].note - 1) / 12;
|
|
262 if(tracks[track][row].note == 127) { // handle key off
|
|
263 channel[chan].nextfreq = channel[chan].freq;
|
|
264 channel[chan].nextoct = channel[chan].oct;
|
|
265 }
|
|
266 }
|
|
267 if(info) // remember vars
|
|
268 channel[chan].portainfo = info;
|
|
269 break;
|
|
270 case 4: if(info) { // vibrato (remember vars)
|
|
271 channel[chan].vibinfo1 = info1;
|
|
272 channel[chan].vibinfo2 = info2;
|
|
273 }
|
|
274 break;
|
|
275 case 7: tempo = info; break; // set tempo
|
|
276 case 8: channel[chan].key = 0; setfreq(chan); break; // release sustaining note
|
|
277 case 9: // set carrier/modulator volume
|
|
278 if(info1)
|
|
279 channel[chan].vol1 = info1 * 7;
|
|
280 else
|
|
281 channel[chan].vol2 = info2 * 7;
|
|
282 setvolume(chan);
|
|
283 break;
|
|
284 case 11: pattbreak = 1; rw = 0; if(info < ord) songend = 1; ord = info; break; // position jump
|
|
285 case 12: // set volume
|
|
286 channel[chan].vol1 = info;
|
|
287 channel[chan].vol2 = info;
|
|
288 if(channel[chan].vol1 > 63)
|
|
289 channel[chan].vol1 = 63;
|
|
290 if(channel[chan].vol2 > 63)
|
|
291 channel[chan].vol2 = 63;
|
|
292 setvolume(chan);
|
|
293 break;
|
|
294 case 13: if(!pattbreak) { pattbreak = 1; rw = info; ord++; } break; // pattern break
|
|
295 case 14: // extended command
|
|
296 switch(info1) {
|
|
297 case 0: if(info2) // define cell-tremolo
|
|
298 regbd |= 128;
|
|
299 else
|
|
300 regbd &= 127;
|
|
301 opl->write(0xbd,regbd);
|
|
302 break;
|
|
303 case 1: if(info2) // define cell-vibrato
|
|
304 regbd |= 64;
|
|
305 else
|
|
306 regbd &= 191;
|
|
307 opl->write(0xbd,regbd);
|
|
308 break;
|
|
309 case 4: vol_up_alt(chan,info2); // increase volume fine
|
|
310 setvolume(chan);
|
|
311 break;
|
|
312 case 5: vol_down_alt(chan,info2); // decrease volume fine
|
|
313 setvolume(chan);
|
|
314 break;
|
|
315 case 6: slide_up(chan,info2); // manual slide up
|
|
316 setfreq(chan);
|
|
317 break;
|
|
318 case 7: slide_down(chan,info2); // manual slide down
|
|
319 setfreq(chan);
|
|
320 break;
|
|
321 }
|
|
322 break;
|
|
323 case 15: // SA2 set speed
|
|
324 if(info <= 0x1f)
|
|
325 speed = info;
|
|
326 if(info >= 0x32)
|
|
327 tempo = info;
|
|
328 if(!info)
|
|
329 songend = 1;
|
|
330 break;
|
|
331 case 17: // alternate set volume
|
|
332 channel[chan].vol1 = info;
|
|
333 if(channel[chan].vol1 > 63)
|
|
334 channel[chan].vol1 = 63;
|
|
335 if(inst[channel[chan].inst].data[0] & 1) {
|
|
336 channel[chan].vol2 = info;
|
|
337 if(channel[chan].vol2 > 63)
|
|
338 channel[chan].vol2 = 63;
|
|
339 }
|
|
340 setvolume(chan);
|
|
341 break;
|
|
342 case 18: // AMD set speed
|
|
343 if(info <= 31 && info > 0)
|
|
344 speed = info;
|
|
345 if(info > 31 || !info)
|
|
346 tempo = info;
|
|
347 break;
|
|
348 case 19: // RAD/A2M set speed
|
|
349 speed = (info ? info : info + 1);
|
|
350 break;
|
|
351 case 21: // set modulator volume
|
|
352 if(info <= 63)
|
|
353 channel[chan].vol2 = info;
|
|
354 else
|
|
355 channel[chan].vol2 = 63;
|
|
356 setvolume(chan);
|
|
357 break;
|
|
358 case 22: // set carrier volume
|
|
359 if(info <= 63)
|
|
360 channel[chan].vol1 = info;
|
|
361 else
|
|
362 channel[chan].vol1 = 63;
|
|
363 setvolume(chan);
|
|
364 break;
|
|
365 case 23: // fine frequency slide up
|
|
366 slide_up(chan,info);
|
|
367 setfreq(chan);
|
|
368 break;
|
|
369 case 24: // fine frequency slide down
|
|
370 slide_down(chan,info);
|
|
371 setfreq(chan);
|
|
372 break;
|
|
373 case 25: // set carrier/modulator waveform
|
|
374 if(info1 != 0x0f)
|
|
375 opl->write(0xe3 + op_table[chan],info1);
|
|
376 if(info2 != 0x0f)
|
|
377 opl->write(0xe0 + op_table[chan],info2);
|
|
378 break;
|
|
379 case 27: // set chip tremolo/vibrato
|
|
380 if (info1)
|
|
381 regbd |= 128;
|
|
382 else
|
|
383 regbd &= 127;
|
|
384 if (info2)
|
|
385 regbd |= 64;
|
|
386 else
|
|
387 regbd &= 191;
|
|
388 opl->write(0xbd,regbd);
|
|
389 break;
|
|
390 }
|
|
391 }
|
|
392
|
|
393 del = speed - 1; // speed compensation
|
|
394 if(!pattbreak) { // next row (only if no manual advance)
|
|
395 rw++;
|
|
396 if(rw >= nrows) {
|
|
397 rw = 0;
|
|
398 ord++;
|
|
399 }
|
|
400 }
|
|
401 if(ord < length) {
|
|
402 if(order[ord] >= JUMPMARKER) { // jump to order
|
|
403 ord = order[ord] - JUMPMARKER;
|
|
404 songend = 1;
|
|
405 }
|
|
406 } else
|
|
407 songend = 1;
|
|
408
|
|
409 AdPlug_LogWrite("\n");
|
|
410 return !songend;
|
|
411 }
|
|
412
|
|
413 void CmodPlayer::rewind(int subsong)
|
|
414 {
|
|
415 unsigned long i;
|
|
416
|
|
417 // Reset playing variables
|
|
418 songend = del = ord = rw = regbd = 0;
|
|
419 tempo = bpm; speed = initspeed;
|
|
420
|
|
421 // Reset channel data
|
|
422 memset(channel,0,sizeof(Channel)*nchans);
|
|
423
|
|
424 // Compute number of patterns, if needed
|
|
425 if(!nop)
|
|
426 for(i=0;i<length;i++)
|
|
427 nop = (order[i] > nop ? order[i] : nop);
|
|
428
|
|
429 opl->init(); // Reset OPL chip
|
|
430 opl->write(1,32); // Go to ym3812 mode
|
|
431 }
|
|
432
|
|
433 float CmodPlayer::getrefresh()
|
|
434 {
|
|
435 return (float) (tempo / 2.5);
|
|
436 }
|
|
437
|
|
438 void CmodPlayer::init_trackord()
|
|
439 {
|
|
440 unsigned long i;
|
|
441
|
|
442 for(i=0;i<npats*nchans;i++)
|
|
443 trackord[i / nchans][i % nchans] = i + 1;
|
|
444 }
|
|
445
|
|
446 bool CmodPlayer::init_specialarp()
|
|
447 {
|
|
448 arplist = new unsigned char[SPECIALARPLEN];
|
|
449 arpcmd = new unsigned char[SPECIALARPLEN];
|
|
450
|
|
451 return true;
|
|
452 }
|
|
453
|
|
454 void CmodPlayer::init_notetable(const unsigned short *newnotetable)
|
|
455 {
|
|
456 memcpy(notetable, newnotetable, 12 * 2);
|
|
457 }
|
|
458
|
|
459 bool CmodPlayer::realloc_order(unsigned long len)
|
|
460 {
|
|
461 if(order) delete [] order;
|
|
462 order = new unsigned char[len];
|
|
463 return true;
|
|
464 }
|
|
465
|
|
466 bool CmodPlayer::realloc_patterns(unsigned long pats, unsigned long rows, unsigned long chans)
|
|
467 {
|
|
468 unsigned long i;
|
|
469
|
|
470 dealloc_patterns();
|
|
471
|
|
472 // set new number of tracks, rows and channels
|
|
473 npats = pats; nrows = rows; nchans = chans;
|
|
474
|
|
475 // alloc new patterns
|
|
476 tracks = new Tracks *[pats * chans];
|
|
477 for(i=0;i<pats*chans;i++) tracks[i] = new Tracks[rows];
|
|
478 trackord = new unsigned short *[pats];
|
|
479 for(i=0;i<pats;i++) trackord[i] = new unsigned short[chans];
|
|
480 channel = new Channel[chans];
|
|
481
|
|
482 // initialize new patterns
|
|
483 for(i=0;i<pats*chans;i++) memset(tracks[i],0,sizeof(Tracks)*rows);
|
|
484 for(i=0;i<pats;i++) memset(trackord[i],0,chans*2);
|
|
485
|
|
486 return true;
|
|
487 }
|
|
488
|
|
489 void CmodPlayer::dealloc_patterns()
|
|
490 {
|
|
491 unsigned long i;
|
|
492
|
|
493 // dealloc everything previously allocated
|
|
494 if(npats && nrows && nchans) {
|
|
495 for(i=0;i<npats*nchans;i++) delete [] tracks[i];
|
|
496 delete [] tracks;
|
|
497 for(i=0;i<npats;i++) delete [] trackord[i];
|
|
498 delete [] trackord;
|
|
499 delete [] channel;
|
|
500 }
|
|
501 }
|
|
502
|
|
503 bool CmodPlayer::realloc_instruments(unsigned long len)
|
|
504 {
|
|
505 // dealloc previous instance, if any
|
|
506 if(inst) delete [] inst;
|
|
507
|
|
508 inst = new Instrument[len];
|
|
509 memset(inst,0,sizeof(Instrument)*len); // reset instruments
|
|
510 return true;
|
|
511 }
|
|
512
|
|
513 void CmodPlayer::dealloc()
|
|
514 {
|
|
515 if(inst) delete [] inst;
|
|
516 if(order) delete [] order;
|
|
517 if(arplist) delete [] arplist;
|
|
518 if(arpcmd) delete [] arpcmd;
|
|
519 dealloc_patterns();
|
|
520 }
|
|
521
|
|
522 /*** private methods *************************************/
|
|
523
|
|
524 void CmodPlayer::setvolume(unsigned char chan)
|
|
525 {
|
|
526 if (flags & Faust)
|
|
527 setvolume_alt(chan);
|
|
528 else {
|
|
529 opl->write(0x40 + op_table[chan], 63-channel[chan].vol2 + (inst[channel[chan].inst].data[9] & 192));
|
|
530 opl->write(0x43 + op_table[chan], 63-channel[chan].vol1 + (inst[channel[chan].inst].data[10] & 192));
|
|
531 }
|
|
532 }
|
|
533
|
|
534 void CmodPlayer::setvolume_alt(unsigned char chan)
|
|
535 {
|
|
536 unsigned char ivol2 = inst[channel[chan].inst].data[9] & 63;
|
|
537 unsigned char ivol1 = inst[channel[chan].inst].data[10] & 63;
|
|
538
|
|
539 opl->write(0x40 + op_table[chan], (((63 - channel[chan].vol2 & 63) + ivol2) >> 1) + (inst[channel[chan].inst].data[9] & 192));
|
|
540 opl->write(0x43 + op_table[chan], (((63 - channel[chan].vol1 & 63) + ivol1) >> 1) + (inst[channel[chan].inst].data[10] & 192));
|
|
541 }
|
|
542
|
|
543 void CmodPlayer::setfreq(unsigned char chan)
|
|
544 {
|
|
545 opl->write(0xa0 + chan, channel[chan].freq & 255);
|
|
546 if(channel[chan].key)
|
|
547 opl->write(0xb0 + chan, ((channel[chan].freq & 768) >> 8) + (channel[chan].oct << 2) | 32);
|
|
548 else
|
|
549 opl->write(0xb0 + chan, ((channel[chan].freq & 768) >> 8) + (channel[chan].oct << 2));
|
|
550 }
|
|
551
|
|
552 void CmodPlayer::playnote(unsigned char chan)
|
|
553 {
|
|
554 unsigned char op = op_table[chan], insnr = channel[chan].inst;
|
|
555
|
|
556 if(!(flags & NoKeyOn))
|
|
557 opl->write(0xb0 + chan, 0); // stop old note
|
|
558
|
|
559 // set instrument data
|
|
560 opl->write(0x20 + op, inst[insnr].data[1]);
|
|
561 opl->write(0x23 + op, inst[insnr].data[2]);
|
|
562 opl->write(0x60 + op, inst[insnr].data[3]);
|
|
563 opl->write(0x63 + op, inst[insnr].data[4]);
|
|
564 opl->write(0x80 + op, inst[insnr].data[5]);
|
|
565 opl->write(0x83 + op, inst[insnr].data[6]);
|
|
566 opl->write(0xe0 + op, inst[insnr].data[7]);
|
|
567 opl->write(0xe3 + op, inst[insnr].data[8]);
|
|
568 opl->write(0xc0 + chan, inst[insnr].data[0]);
|
|
569 opl->write(0xbd, inst[insnr].misc); // set misc. register
|
|
570
|
|
571 // set frequency, volume & play
|
|
572 channel[chan].key = 1;
|
|
573 setfreq(chan);
|
|
574
|
|
575 if (flags & Faust) {
|
|
576 channel[chan].vol2 = 63;
|
|
577 channel[chan].vol1 = 63;
|
|
578 }
|
|
579 setvolume(chan);
|
|
580 }
|
|
581
|
|
582 void CmodPlayer::setnote(unsigned char chan, int note)
|
|
583 {
|
|
584 if(note > 96)
|
|
585 if(note == 127) { // key off
|
|
586 channel[chan].key = 0;
|
|
587 setfreq(chan);
|
|
588 return;
|
|
589 } else
|
|
590 note = 96;
|
|
591
|
|
592 if(note < 13)
|
|
593 channel[chan].freq = notetable[note - 1];
|
|
594 else
|
|
595 if(note % 12 > 0)
|
|
596 channel[chan].freq = notetable[(note % 12) - 1];
|
|
597 else
|
|
598 channel[chan].freq = notetable[11];
|
|
599 channel[chan].oct = (note - 1) / 12;
|
|
600 channel[chan].freq += inst[channel[chan].inst].slide; // apply pre-slide
|
|
601 }
|
|
602
|
|
603 void CmodPlayer::slide_down(unsigned char chan, int amount)
|
|
604 {
|
|
605 channel[chan].freq -= amount;
|
|
606 if(channel[chan].freq <= 342)
|
|
607 if(channel[chan].oct) {
|
|
608 channel[chan].oct--;
|
|
609 channel[chan].freq <<= 1;
|
|
610 } else
|
|
611 channel[chan].freq = 342;
|
|
612 }
|
|
613
|
|
614 void CmodPlayer::slide_up(unsigned char chan, int amount)
|
|
615 {
|
|
616 channel[chan].freq += amount;
|
|
617 if(channel[chan].freq >= 686)
|
|
618 if(channel[chan].oct < 7) {
|
|
619 channel[chan].oct++;
|
|
620 channel[chan].freq >>= 1;
|
|
621 } else
|
|
622 channel[chan].freq = 686;
|
|
623 }
|
|
624
|
|
625 void CmodPlayer::tone_portamento(unsigned char chan, unsigned char info)
|
|
626 {
|
|
627 if(channel[chan].freq + (channel[chan].oct << 10) < channel[chan].nextfreq +
|
|
628 (channel[chan].nextoct << 10)) {
|
|
629 slide_up(chan,info);
|
|
630 if(channel[chan].freq + (channel[chan].oct << 10) > channel[chan].nextfreq +
|
|
631 (channel[chan].nextoct << 10)) {
|
|
632 channel[chan].freq = channel[chan].nextfreq;
|
|
633 channel[chan].oct = channel[chan].nextoct;
|
|
634 }
|
|
635 }
|
|
636 if(channel[chan].freq + (channel[chan].oct << 10) > channel[chan].nextfreq +
|
|
637 (channel[chan].nextoct << 10)) {
|
|
638 slide_down(chan,info);
|
|
639 if(channel[chan].freq + (channel[chan].oct << 10) < channel[chan].nextfreq +
|
|
640 (channel[chan].nextoct << 10)) {
|
|
641 channel[chan].freq = channel[chan].nextfreq;
|
|
642 channel[chan].oct = channel[chan].nextoct;
|
|
643 }
|
|
644 }
|
|
645 setfreq(chan);
|
|
646 }
|
|
647
|
|
648 void CmodPlayer::vibrato(unsigned char chan, unsigned char speed, unsigned char depth)
|
|
649 {
|
|
650 int i;
|
|
651
|
|
652 if(!speed || !depth)
|
|
653 return;
|
|
654
|
|
655 if(depth > 14)
|
|
656 depth = 14;
|
|
657
|
|
658 for(i=0;i<speed;i++) {
|
|
659 channel[chan].trigger++;
|
|
660 while(channel[chan].trigger >= 64)
|
|
661 channel[chan].trigger -= 64;
|
|
662 if(channel[chan].trigger >= 16 && channel[chan].trigger < 48)
|
|
663 slide_down(chan,vibratotab[channel[chan].trigger - 16] / (16-depth));
|
|
664 if(channel[chan].trigger < 16)
|
|
665 slide_up(chan,vibratotab[channel[chan].trigger + 16] / (16-depth));
|
|
666 if(channel[chan].trigger >= 48)
|
|
667 slide_up(chan,vibratotab[channel[chan].trigger - 48] / (16-depth));
|
|
668 }
|
|
669 setfreq(chan);
|
|
670 }
|
|
671
|
|
672 void CmodPlayer::vol_up(unsigned char chan, int amount)
|
|
673 {
|
|
674 if(channel[chan].vol1 + amount < 63)
|
|
675 channel[chan].vol1 += amount;
|
|
676 else
|
|
677 channel[chan].vol1 = 63;
|
|
678
|
|
679 if(channel[chan].vol2 + amount < 63)
|
|
680 channel[chan].vol2 += amount;
|
|
681 else
|
|
682 channel[chan].vol2 = 63;
|
|
683 }
|
|
684
|
|
685 void CmodPlayer::vol_down(unsigned char chan, int amount)
|
|
686 {
|
|
687 if(channel[chan].vol1 - amount > 0)
|
|
688 channel[chan].vol1 -= amount;
|
|
689 else
|
|
690 channel[chan].vol1 = 0;
|
|
691
|
|
692 if(channel[chan].vol2 - amount > 0)
|
|
693 channel[chan].vol2 -= amount;
|
|
694 else
|
|
695 channel[chan].vol2 = 0;
|
|
696 }
|
|
697
|
|
698 void CmodPlayer::vol_up_alt(unsigned char chan, int amount)
|
|
699 {
|
|
700 if(channel[chan].vol1 + amount < 63)
|
|
701 channel[chan].vol1 += amount;
|
|
702 else
|
|
703 channel[chan].vol1 = 63;
|
|
704 if(inst[channel[chan].inst].data[0] & 1)
|
|
705 if(channel[chan].vol2 + amount < 63)
|
|
706 channel[chan].vol2 += amount;
|
|
707 else
|
|
708 channel[chan].vol2 = 63;
|
|
709 }
|
|
710
|
|
711 void CmodPlayer::vol_down_alt(unsigned char chan, int amount)
|
|
712 {
|
|
713 if(channel[chan].vol1 - amount > 0)
|
|
714 channel[chan].vol1 -= amount;
|
|
715 else
|
|
716 channel[chan].vol1 = 0;
|
|
717 if(inst[channel[chan].inst].data[0] & 1)
|
|
718 if(channel[chan].vol2 - amount > 0)
|
|
719 channel[chan].vol2 -= amount;
|
|
720 else
|
|
721 channel[chan].vol2 = 0;
|
|
722 }
|