Debian Codegraph @

src/engine/cmdStream.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _USE_MATH_DEFINES
21 #include "cmdStream.h"
22 #include "dispatch.h"
23 #include "engine.h"
24 #include "../ta-log.h"
25
26 bool DivCSChannelState::doCall(unsigned int addr) {
27 if (callStackPos>=DIV_MAX_CSSTACK) {
28 readPos=0;
29 return false;
30 }
31
32 callStack[callStackPos++]=readPos;
33 readPos=addr;
34
35 return true;
36 }
37
38 unsigned char* DivCSPlayer::getData() {
39 return b;
40 }
41
42 unsigned short* DivCSPlayer::getDataAccess() {
43 return bAccessTS;
44 }
45
46 size_t DivCSPlayer::getDataLen() {
47 return bLen;
48 }
49
50 DivCSChannelState* DivCSPlayer::getChanState(int ch) {
51 return &chan[ch];
52 }
53
54 unsigned int DivCSPlayer::getFileChans() {
55 return fileChans;
56 }
57
58 unsigned char* DivCSPlayer::getFastDelays() {
59 return fastDelays;
60 }
61
62 unsigned char* DivCSPlayer::getFastCmds() {
63 return fastCmds;
64 }
65
66 unsigned int DivCSPlayer::getCurTick() {
67 return curTick;
68 }
69
70 void DivCSPlayer::cleanup() {
71 delete[] b;
72 b=NULL;
73 bLen=0;
74
75 if (bAccessTS) {
76 delete[] bAccessTS;
77 bAccessTS=NULL;
78 }
79 }
80
81 bool DivCSPlayer::tick() {
82 bool ticked=false;
83 for (int i=0; i<e->getTotalChannelCount(); i++) {
84 bool sendVolume=false;
85 bool sendPitch=false;
86 if (chan[i].readPos==0) continue;
87
88 ticked=true;
89
90 chan[i].waitTicks--;
91 while (chan[i].waitTicks<=0) {
92 if (!stream.seek(chan[i].readPos,SEEK_SET)) {
93 logE("%d: access violation! $%x",i,chan[i].readPos);
94 chan[i].readPos=0;
95 break;
96 }
97
98 unsigned int accessTSBegin=stream.tell();
99
100 chan[i].trace[chan[i].tracePos++]=chan[i].readPos;
101 if (chan[i].tracePos>=DIV_MAX_CSTRACE) {
102 chan[i].tracePos=0;
103 }
104
105 unsigned char next=stream.readC();
106 unsigned char command=0;
107 bool mustTell=true;
108
109 if (next<0xb3) { // note
110 e->dispatchCmd(DivCommand(DIV_CMD_NOTE_ON,i,(int)next-60));
111 chan[i].note=(int)next-60;
112 chan[i].vibratoPos=0;
113 } else if (next>=0xe0 && next<=0xef) {
114 command=fastCmds[next&15];
115 bAccessTS[fastCmdsOff+(next&15)]=curTick;
116 } else if (next>=0xf0) { // preset delay
117 chan[i].waitTicks=fastDelays[next&15];
118 chan[i].lastWaitLen=chan[i].waitTicks;
119 bAccessTS[fastDelaysOff+(next&15)]=curTick;
120 } else switch (next) {
121 case 0xb4: // note on null
122 e->dispatchCmd(DivCommand(DIV_CMD_NOTE_ON,i,DIV_NOTE_NULL));
123 chan[i].vibratoPos=0;
124 break;
125 case 0xb5: // note off
126 e->dispatchCmd(DivCommand(DIV_CMD_NOTE_OFF,i));
127 break;
128 case 0xb6: // note off env
129 e->dispatchCmd(DivCommand(DIV_CMD_NOTE_OFF_ENV,i));
130 break;
131 case 0xb7: // env release
132 e->dispatchCmd(DivCommand(DIV_CMD_ENV_RELEASE,i));
133 break;
134 case 0xb8:
135 command=DIV_CMD_INSTRUMENT;
136 break;
137 case 0xc0:
138 command=DIV_CMD_PRE_PORTA;
139 break;
140 case 0xc1: // arp time
141 arpSpeed=(unsigned char)stream.readC();
142 break;
143 case 0xc2: { // vibrato
144 unsigned char param=stream.readC();
145 chan[i].vibratoDepth=param&15;
146 chan[i].vibratoRate=param>>4;
147 sendPitch=true;
148 break;
149 }
150 case 0xc3: // vibrato range
151 chan[i].vibratoRange=(unsigned char)stream.readC();
152 break;
153 case 0xc4: // vibrato shape
154 chan[i].vibratoShape=(unsigned char)stream.readC();
155 break;
156 case 0xc5: // pitch
157 chan[i].pitch=(signed char)stream.readC();
158 sendPitch=true;
159 break;
160 case 0xc6: // arpeggio
161 chan[i].arp=(unsigned char)stream.readC();
162 break;
163 case 0xc7: // volume
164 chan[i].volume=((unsigned char)stream.readC())<<8;
165 sendVolume=true;
166 break;
167 case 0xc8: // vol slide
168 chan[i].volSpeed=(short)(bigEndian?stream.readS_BE():stream.readS());
169 chan[i].volSpeedTarget=-1;
170 break;
171 case 0xc9: // porta
172 chan[i].portaTarget=(int)((unsigned char)stream.readC())-60;
173 chan[i].portaSpeed=(unsigned char)stream.readC();
174 break;
175 case 0xca: { // legato
176 int arg0=(unsigned char)stream.readC();
177 if (arg0==0xff) {
178 arg0=DIV_NOTE_NULL;
179 } else {
180 arg0-=60;
181 }
182 chan[i].note=arg0;
183 e->dispatchCmd(DivCommand(DIV_CMD_LEGATO,i,chan[i].note));
184 break;
185 }
186 case 0xcb: { // vol slide target
187 int arg0=(short)(bigEndian?stream.readS_BE():stream.readS());
188 int arg1=(short)(bigEndian?stream.readS_BE():stream.readS());
189 chan[i].volSpeed=arg0;
190 chan[i].volSpeedTarget=arg0==0 ? -1 : arg1;
191 break;
192 }
193 case 0xcc: // tremolo (TODO)
194 stream.readC();
195 break;
196 case 0xcd: // panbrello (TODO)
197 stream.readC();
198 break;
199 case 0xce: // pan slide (TODO)
200 stream.readC();
201 break;
202 case 0xcf: { // panning
203 int panL=(unsigned char)stream.readC();
204 int panR=(unsigned char)stream.readC();
205 e->dispatchCmd(DivCommand(DIV_CMD_PANNING,i,panL,panR));
206 break;
207 }
208 case 0xe0: case 0xe1: case 0xe2: case 0xe3:
209 case 0xe4: case 0xe5: case 0xe6: case 0xe7:
210 case 0xe8: case 0xe9: case 0xea: case 0xeb:
211 case 0xec: case 0xed: case 0xee: case 0xef:
212 // TODO: remove as it has no effect
213 command=fastCmds[next&15];
214
215 break;
216 case 0xd0: // placeholder
217 stream.readC();
218 stream.readC();
219 stream.readC();
220 break;
221 case 0xd1: // nop
222 break;
223 case 0xd6: // note off + wait 1
224 e->dispatchCmd(DivCommand(DIV_CMD_NOTE_OFF,i));
225 chan[i].waitTicks=1;
226 chan[i].lastWaitLen=chan[i].waitTicks;
227 break;
228 case 0xd7:
229 command=stream.readC();
230 break;
231 case 0xd8: {
232 unsigned int callAddr=bigEndian?((unsigned short)stream.readS_BE()):((unsigned short)stream.readS());
233 chan[i].readPos=stream.tell();
234 if (!chan[i].doCall(callAddr)) {
235 logE("%d: (call) stack error!",i);
236 chan[i].readPos=0;
237 }
238 mustTell=false;
239 break;
240 }
241 case 0xd5: {
242 unsigned int callAddr=bigEndian?stream.readI_BE():stream.readI();
243 chan[i].readPos=stream.tell();
244 if (!chan[i].doCall(callAddr)) {
245 logE("%d: (calli) stack error!",i);
246 chan[i].readPos=0;
247 }
248 mustTell=false;
249 break;
250 }
251 case 0xd4: {
252 logE("%d: (callsym) not supported here!",i);
253 chan[i].readPos=0;
254 break;
255 }
256 case 0xd9:
257 if (!chan[i].callStackPos) {
258 logE("%d: (ret) stack error!",i);
259 chan[i].readPos=0;
260 break;
261 }
262 chan[i].readPos=chan[i].callStack[--chan[i].callStackPos];
263 mustTell=false;
264 break;
265 case 0xda:
266 chan[i].readPos=bigEndian?stream.readI_BE():stream.readI();
267 mustTell=false;
268 break;
269 case 0xdb:
270 logE("TODO: RATE");
271 stream.readI();
272 break;
273 case 0xdc:
274 chan[i].waitTicks=(unsigned short)(bigEndian?stream.readS_BE():stream.readS());
275 chan[i].lastWaitLen=chan[i].waitTicks;
276 break;
277 case 0xdd:
278 chan[i].waitTicks=(unsigned char)stream.readC();
279 chan[i].lastWaitLen=chan[i].waitTicks;
280 break;
281 case 0xde:
282 chan[i].waitTicks=1;
283 chan[i].lastWaitLen=chan[i].waitTicks;
284 break;
285 case 0xdf:
286 chan[i].readPos=0;
287 mustTell=false;
288 logI("%d: stop",i,chan[i].readPos);
289 break;
290 default:
291 logE("%d: illegal instruction $%.2x! $%.x",i,next,chan[i].readPos);
292 chan[i].readPos=0;
293 break;
294 }
295
296 if (chan[i].readPos==0) break;
297
298 if (command) {
299 int arg0=0;
300 int arg1=0;
301 switch (command) {
302 case DIV_CMD_INSTRUMENT:
303 arg0=(unsigned char)stream.readC();
304 break;
305 case DIV_CMD_PRE_PORTA:
306 arg0=(unsigned char)stream.readC();
307 arg1=(arg0&0x40)?1:0;
308 arg0=(arg0&0x80)?1:0;
309 break;
310 // ONE BYTE COMMANDS
311 case DIV_CMD_SAMPLE_MODE:
312 case DIV_CMD_SAMPLE_FREQ:
313 case DIV_CMD_SAMPLE_BANK:
314 case DIV_CMD_SAMPLE_DIR:
315 case DIV_CMD_FM_HARD_RESET:
316 case DIV_CMD_FM_LFO:
317 case DIV_CMD_FM_LFO_WAVE:
318 case DIV_CMD_FM_LFO2:
319 case DIV_CMD_FM_LFO2_WAVE:
320 case DIV_CMD_FM_FB:
321 case DIV_CMD_FM_EXTCH:
322 case DIV_CMD_FM_AM_DEPTH:
323 case DIV_CMD_FM_PM_DEPTH:
324 case DIV_CMD_STD_NOISE_FREQ:
325 case DIV_CMD_STD_NOISE_MODE:
326 case DIV_CMD_WAVE:
327 case DIV_CMD_GB_SWEEP_TIME:
328 case DIV_CMD_GB_SWEEP_DIR:
329 case DIV_CMD_PCE_LFO_MODE:
330 case DIV_CMD_PCE_LFO_SPEED:
331 case DIV_CMD_NES_DMC:
332 case DIV_CMD_C64_CUTOFF:
333 case DIV_CMD_C64_RESONANCE:
334 case DIV_CMD_C64_FILTER_MODE:
335 case DIV_CMD_C64_RESET_TIME:
336 case DIV_CMD_C64_RESET_MASK:
337 case DIV_CMD_C64_FILTER_RESET:
338 case DIV_CMD_C64_DUTY_RESET:
339 case DIV_CMD_C64_EXTENDED:
340 case DIV_CMD_AY_ENVELOPE_SET:
341 case DIV_CMD_AY_ENVELOPE_LOW:
342 case DIV_CMD_AY_ENVELOPE_HIGH:
343 case DIV_CMD_AY_ENVELOPE_SLIDE:
344 case DIV_CMD_AY_NOISE_MASK_AND:
345 case DIV_CMD_AY_NOISE_MASK_OR:
346 case DIV_CMD_AY_AUTO_ENVELOPE:
347 case DIV_CMD_FDS_MOD_DEPTH:
348 case DIV_CMD_FDS_MOD_HIGH:
349 case DIV_CMD_FDS_MOD_LOW:
350 case DIV_CMD_FDS_MOD_POS:
351 case DIV_CMD_FDS_MOD_WAVE:
352 case DIV_CMD_SAA_ENVELOPE:
353 case DIV_CMD_AMIGA_FILTER:
354 case DIV_CMD_AMIGA_AM:
355 case DIV_CMD_AMIGA_PM:
356 case DIV_CMD_MACRO_OFF:
357 case DIV_CMD_MACRO_ON:
358 case DIV_CMD_MACRO_RESTART:
359 case DIV_CMD_QSOUND_ECHO_FEEDBACK:
360 case DIV_CMD_QSOUND_ECHO_LEVEL:
361 case DIV_CMD_QSOUND_SURROUND:
362 case DIV_CMD_X1_010_ENVELOPE_SHAPE:
363 case DIV_CMD_X1_010_ENVELOPE_ENABLE:
364 case DIV_CMD_X1_010_ENVELOPE_MODE:
365 case DIV_CMD_X1_010_ENVELOPE_PERIOD:
366 case DIV_CMD_X1_010_ENVELOPE_SLIDE:
367 case DIV_CMD_X1_010_AUTO_ENVELOPE:
368 case DIV_CMD_X1_010_SAMPLE_BANK_SLOT:
369 case DIV_CMD_WS_SWEEP_TIME:
370 case DIV_CMD_WS_SWEEP_AMOUNT:
371 case DIV_CMD_N163_WAVE_POSITION:
372 case DIV_CMD_N163_WAVE_LENGTH:
373 case DIV_CMD_N163_WAVE_UNUSED1:
374 case DIV_CMD_N163_WAVE_UNUSED2:
375 case DIV_CMD_N163_WAVE_LOADPOS:
376 case DIV_CMD_N163_WAVE_LOADLEN:
377 case DIV_CMD_N163_WAVE_UNUSED3:
378 case DIV_CMD_N163_CHANNEL_LIMIT:
379 case DIV_CMD_N163_GLOBAL_WAVE_LOAD:
380 case DIV_CMD_N163_GLOBAL_WAVE_LOADPOS:
381 case DIV_CMD_N163_UNUSED4:
382 case DIV_CMD_N163_UNUSED5:
383 case DIV_CMD_SU_SYNC_PERIOD_LOW:
384 case DIV_CMD_SU_SYNC_PERIOD_HIGH:
385 case DIV_CMD_ADPCMA_GLOBAL_VOLUME:
386 case DIV_CMD_SNES_ECHO:
387 case DIV_CMD_SNES_PITCH_MOD:
388 case DIV_CMD_SNES_INVERT:
389 case DIV_CMD_SNES_GAIN_MODE:
390 case DIV_CMD_SNES_GAIN:
391 case DIV_CMD_SNES_ECHO_ENABLE:
392 case DIV_CMD_SNES_ECHO_DELAY:
393 case DIV_CMD_SNES_ECHO_VOL_LEFT:
394 case DIV_CMD_SNES_ECHO_VOL_RIGHT:
395 case DIV_CMD_SNES_ECHO_FEEDBACK:
396 case DIV_CMD_NES_ENV_MODE:
397 case DIV_CMD_NES_LENGTH:
398 case DIV_CMD_NES_COUNT_MODE:
399 case DIV_CMD_FM_AM2_DEPTH:
400 case DIV_CMD_FM_PM2_DEPTH:
401 case DIV_CMD_ES5506_ENVELOPE_LVRAMP:
402 case DIV_CMD_ES5506_ENVELOPE_RVRAMP:
403 case DIV_CMD_ES5506_PAUSE:
404 case DIV_CMD_ES5506_FILTER_MODE:
405 case DIV_CMD_SNES_GLOBAL_VOL_LEFT:
406 case DIV_CMD_SNES_GLOBAL_VOL_RIGHT:
407 case DIV_CMD_NES_LINEAR_LENGTH:
408 case DIV_CMD_EXTERNAL:
409 case DIV_CMD_C64_AD:
410 case DIV_CMD_C64_SR:
411 case DIV_CMD_DAVE_HIGH_PASS:
412 case DIV_CMD_DAVE_RING_MOD:
413 case DIV_CMD_DAVE_SWAP_COUNTERS:
414 case DIV_CMD_DAVE_LOW_PASS:
415 case DIV_CMD_DAVE_CLOCK_DIV:
416 case DIV_CMD_MINMOD_ECHO:
417 case DIV_CMD_FDS_MOD_AUTO:
418 case DIV_CMD_FM_OPMASK:
419 case DIV_CMD_MULTIPCM_MIX_FM:
420 case DIV_CMD_MULTIPCM_MIX_PCM:
421 case DIV_CMD_MULTIPCM_LFO:
422 case DIV_CMD_MULTIPCM_VIB:
423 case DIV_CMD_MULTIPCM_AM:
424 case DIV_CMD_MULTIPCM_AR:
425 case DIV_CMD_MULTIPCM_D1R:
426 case DIV_CMD_MULTIPCM_DL:
427 case DIV_CMD_MULTIPCM_D2R:
428 case DIV_CMD_MULTIPCM_RC:
429 case DIV_CMD_MULTIPCM_RR:
430 case DIV_CMD_MULTIPCM_DAMP:
431 case DIV_CMD_MULTIPCM_PSEUDO_REVERB:
432 case DIV_CMD_MULTIPCM_LFO_RESET:
433 case DIV_CMD_MULTIPCM_LEVEL_DIRECT:
434 case DIV_CMD_SID3_SPECIAL_WAVE:
435 case DIV_CMD_SID3_RING_MOD_SRC:
436 case DIV_CMD_SID3_HARD_SYNC_SRC:
437 case DIV_CMD_SID3_PHASE_MOD_SRC:
438 case DIV_CMD_SID3_WAVE_MIX:
439 case DIV_CMD_SID3_1_BIT_NOISE:
440 case DIV_CMD_SID3_CHANNEL_INVERSION:
441 case DIV_CMD_SID3_FILTER_CONNECTION:
442 case DIV_CMD_SID3_FILTER_MATRIX:
443 case DIV_CMD_SID3_FILTER_ENABLE:
444 case DIV_CMD_SID3_PHASE_RESET:
445 case DIV_CMD_SID3_NOISE_PHASE_RESET:
446 case DIV_CMD_SID3_ENVELOPE_RESET:
447 case DIV_CMD_SID3_CUTOFF_SCALING:
448 case DIV_CMD_SID3_RESONANCE_SCALING:
449 case DIV_CMD_WS_GLOBAL_SPEAKER_VOLUME:
450 case DIV_CMD_FM_ALG:
451 case DIV_CMD_FM_FMS:
452 case DIV_CMD_FM_AMS:
453 case DIV_CMD_FM_FMS2:
454 case DIV_CMD_FM_AMS2:
455 arg0=(unsigned char)stream.readC();
456 break;
457 // TWO BYTE COMMANDS
458 case DIV_CMD_FM_TL:
459 case DIV_CMD_FM_AM:
460 case DIV_CMD_FM_AR:
461 case DIV_CMD_FM_DR:
462 case DIV_CMD_FM_SL:
463 case DIV_CMD_FM_D2R:
464 case DIV_CMD_FM_RR:
465 case DIV_CMD_FM_DT:
466 case DIV_CMD_FM_DT2:
467 case DIV_CMD_FM_RS:
468 case DIV_CMD_FM_KSR:
469 case DIV_CMD_FM_VIB:
470 case DIV_CMD_FM_SUS:
471 case DIV_CMD_FM_WS:
472 case DIV_CMD_FM_SSG:
473 case DIV_CMD_FM_REV:
474 case DIV_CMD_FM_EG_SHIFT:
475 case DIV_CMD_FM_MULT:
476 case DIV_CMD_FM_FINE:
477 case DIV_CMD_AY_IO_WRITE:
478 case DIV_CMD_AY_AUTO_PWM:
479 case DIV_CMD_SURROUND_PANNING:
480 case DIV_CMD_SU_SWEEP_PERIOD_LOW:
481 case DIV_CMD_SU_SWEEP_PERIOD_HIGH:
482 case DIV_CMD_SU_SWEEP_BOUND:
483 case DIV_CMD_SU_SWEEP_ENABLE:
484 case DIV_CMD_SNES_ECHO_FIR:
485 case DIV_CMD_ES5506_FILTER_K1_SLIDE:
486 case DIV_CMD_ES5506_FILTER_K2_SLIDE:
487 case DIV_CMD_ES5506_ENVELOPE_K1RAMP:
488 case DIV_CMD_ES5506_ENVELOPE_K2RAMP:
489 case DIV_CMD_ESFM_OP_PANNING:
490 case DIV_CMD_ESFM_OUTLVL:
491 case DIV_CMD_ESFM_MODIN:
492 case DIV_CMD_ESFM_ENV_DELAY:
493 case DIV_CMD_POWERNOISE_COUNTER_LOAD:
494 case DIV_CMD_POWERNOISE_IO_WRITE:
495 case DIV_CMD_BIFURCATOR_STATE_LOAD:
496 case DIV_CMD_BIFURCATOR_PARAMETER:
497 case DIV_CMD_SID3_LFSR_FEEDBACK_BITS:
498 case DIV_CMD_SID3_FILTER_DISTORTION:
499 case DIV_CMD_SID3_FILTER_OUTPUT_VOLUME:
500 case DIV_CMD_C64_PW_SLIDE:
501 case DIV_CMD_C64_CUTOFF_SLIDE:
502 arg0=(unsigned char)stream.readC();
503 arg1=(unsigned char)stream.readC();
504 break;
505 // ONE SHORT COMMANDS
506 case DIV_CMD_C64_FINE_DUTY:
507 case DIV_CMD_C64_FINE_CUTOFF:
508 case DIV_CMD_LYNX_LFSR_LOAD:
509 case DIV_CMD_QSOUND_ECHO_DELAY:
510 case DIV_CMD_ES5506_ENVELOPE_COUNT:
511 arg0=(unsigned short)(bigEndian?stream.readS_BE():stream.readS());
512 break;
513 // TWO SHORT COMMANDS
514 case DIV_CMD_ES5506_FILTER_K1:
515 case DIV_CMD_ES5506_FILTER_K2:
516 arg0=(unsigned short)(bigEndian?stream.readS_BE():stream.readS());
517 arg1=(unsigned short)(bigEndian?stream.readS_BE():stream.readS());
518 break;
519 case DIV_CMD_FM_FIXFREQ:
520 arg0=(unsigned short)(bigEndian?stream.readS_BE():stream.readS());
521 arg1=arg0&0x7ff;
522 arg0>>=12;
523 break;
524 case DIV_CMD_NES_SWEEP:
525 arg0=(unsigned char)stream.readC();
526 arg1=arg0&0x77;
527 arg0=(arg0&8)?1:0;
528 break;
529 case DIV_CMD_SAMPLE_POS:
530 arg0=(unsigned int)(bigEndian?stream.readI_BE():stream.readI());
531 break;
532 }
533
534 // dispatch it
535 e->dispatchCmd(DivCommand((DivDispatchCmds)command,i,arg0,arg1));
536 }
537
538 for (unsigned int j=accessTSBegin; j<stream.tell(); j++) {
539 if (j<bLen) {
540 bAccessTS[j]=curTick;
541 }
542 }
543
544 if (mustTell) chan[i].readPos=stream.tell();
545 }
546
547 if (sendVolume || chan[i].volSpeed!=0) {
548 int preSpeedVol=chan[i].volume;
549 chan[i].volume+=chan[i].volSpeed;
550 if (chan[i].volSpeedTarget!=-1) {
551 bool atTarget=false;
552 if (chan[i].volSpeed>0) {
553 atTarget=(chan[i].volume>=chan[i].volSpeedTarget);
554 } else if (chan[i].volSpeed<0) {
555 atTarget=(chan[i].volume<=chan[i].volSpeedTarget);
556 } else {
557 atTarget=true;
558 chan[i].volSpeedTarget=chan[i].volume;
559 }
560
561 if (atTarget) {
562 if (chan[i].volSpeed>0) {
563 chan[i].volume=MAX(preSpeedVol,chan[i].volSpeedTarget);
564 } else if (chan[i].volSpeed<0) {
565 chan[i].volume=MIN(preSpeedVol,chan[i].volSpeedTarget);
566 }
567 chan[i].volSpeed=0;
568 chan[i].volSpeedTarget=-1;
569 }
570 }
571 if (chan[i].volume<0) {
572 chan[i].volume=0;
573 }
574 if (chan[i].volume>chan[i].volMax) {
575 chan[i].volume=chan[i].volMax;
576 }
577 e->dispatchCmd(DivCommand(DIV_CMD_VOLUME,i,chan[i].volume>>8));
578 }
579
580 if (sendPitch || chan[i].vibratoDepth!=0) {
581 if (chan[i].vibratoDepth>0) {
582 chan[i].vibratoPos+=chan[i].vibratoRate;
583 if (chan[i].vibratoPos>=64) chan[i].vibratoPos-=64;
584 }
585 e->dispatchCmd(DivCommand(DIV_CMD_PITCH,i,chan[i].pitch+(vibTable[chan[i].vibratoPos&63]*chan[i].vibratoDepth)/15));
586 }
587
588 if (chan[i].portaSpeed) {
589 e->dispatchCmd(DivCommand(DIV_CMD_NOTE_PORTA,i,chan[i].portaSpeed*(e->song.linearPitch==2?e->song.pitchSlideSpeed:1),chan[i].portaTarget));
590 }
591 if (chan[i].arp && !chan[i].portaSpeed) {
592 if (chan[i].arpTicks==0) {
593 switch (chan[i].arpStage) {
594 case 0:
595 e->dispatchCmd(DivCommand(DIV_CMD_LEGATO,i,chan[i].note));
596 break;
597 case 1:
598 e->dispatchCmd(DivCommand(DIV_CMD_LEGATO,i,chan[i].note+(chan[i].arp>>4)));
599 break;
600 case 2:
601 e->dispatchCmd(DivCommand(DIV_CMD_LEGATO,i,chan[i].note+(chan[i].arp&15)));
602 break;
603 }
604 chan[i].arpStage++;
605 if (chan[i].arpStage>=3) chan[i].arpStage=0;
606 chan[i].arpTicks=arpSpeed;
607 }
608 chan[i].arpTicks--;
609 }
610 }
611
612 // cycle over access times in order to ensure deltas are always higher than 256
613 // (and prevent spurious highlights)
614 for (int i=0; i<16; i++) {
615 short delta=(((short)(curTick&0xffff))-(short)bAccessTS[deltaCyclePos]);
616 if (delta>256) {
617 bAccessTS[deltaCyclePos]=curTick-512;
618 }
619 if (++deltaCyclePos>=bLen) {
620 deltaCyclePos=0;
621 }
622 }
623
624 curTick++;
625
626 return ticked;
627 }
628
629 bool DivCSPlayer::init() {
630 unsigned char magic[4];
631 stream.seek(0,SEEK_SET);
632 stream.read(magic,4);
633
634 if (memcmp(magic,"FCS",4)!=0) return false;
635
636 fileChans=(unsigned short)stream.readS();
637 unsigned char flags=stream.readC();
638 stream.readC(); // reserved
639
640 longPointers=flags&1;
641 bigEndian=flags&2;
642
643 if (bigEndian) fileChans=(((fileChans&0xff00)>>8)|((fileChans&0xff)<<8));
644
645 fastDelaysOff=stream.tell();
646 stream.read(fastDelays,16);
647 fastCmdsOff=stream.tell();
648 stream.read(fastCmds,16);
649
650 if (longPointers) {
651 for (unsigned int i=0; i<fileChans; i++) {
652 if (i>=DIV_MAX_CHANS) {
653 stream.readI();
654 continue;
655 }
656 if ((int)i>=e->getTotalChannelCount()) {
657 stream.readI();
658 continue;
659 }
660 if (bigEndian) {
661 chan[i].startPos=stream.readI_BE();
662 } else {
663 chan[i].startPos=stream.readI();
664 }
665 chan[i].readPos=chan[i].startPos;
666 }
667 } else {
668 for (unsigned int i=0; i<fileChans; i++) {
669 if (i>=DIV_MAX_CHANS) {
670 stream.readS();
671 continue;
672 }
673 if ((int)i>=e->getTotalChannelCount()) {
674 stream.readS();
675 continue;
676 }
677 if (bigEndian) {
678 chan[i].startPos=stream.readS_BE();
679 } else {
680 chan[i].startPos=stream.readS();
681 }
682 chan[i].readPos=chan[i].startPos;
683 }
684 }
685
686 // read stack sizes
687 for (unsigned int i=0; i<fileChans; i++) {
688 chan[i].callStackSize=stream.readC();
689 }
690
691 // initialize state
692 for (int i=0; i<e->getTotalChannelCount(); i++) {
693 chan[i].volMax=(e->getDispatch(e->dispatchOfChan[i])->dispatch(DivCommand(DIV_CMD_GET_VOLMAX,e->dispatchChanOfChan[i]))<<8)|0xff;
694 chan[i].volume=chan[i].volMax;
695 }
696
697 for (int i=0; i<64; i++) {
698 vibTable[i]=127*sin(((double)i/64.0)*(2*M_PI));
699 }
700
701 arpSpeed=1;
702 bAccessTS=new unsigned short[bLen];
703 // this value ensures all deltas are higher than 256
704 memset(bAccessTS,0xc0,bLen*sizeof(unsigned short));
705 curTick=0;
706 deltaCyclePos=0;
707
708 return true;
709 }
710
711 // DivEngine
712
713 bool DivEngine::playStream(unsigned char* f, size_t length) {
714 BUSY_BEGIN;
715 // kill the previous player
716 if (cmdStreamInt) {
717 cmdStreamInt->cleanup();
718 delete cmdStreamInt;
719 cmdStreamInt=NULL;
720 }
721 cmdStreamInt=new DivCSPlayer(this,f,length);
722 if (!cmdStreamInt->init()) {
723 logE("not a command stream!");
724 lastError="not a command stream";
725 delete[] f;
726 delete cmdStreamInt;
727 cmdStreamInt=NULL;
728 BUSY_END;
729 return false;
730 }
731
732 if (!playing) {
733 reset();
734 freelance=true;
735 playing=true;
736 }
737 BUSY_END;
738 return true;
739 }
740
741 DivCSPlayer* DivEngine::getStreamPlayer() {
742 return cmdStreamInt;
743 }
744
745 bool DivEngine::killStream() {
746 if (!cmdStreamInt) return false;
747 BUSY_BEGIN;
748 cmdStreamInt->cleanup();
749 delete cmdStreamInt;
750 cmdStreamInt=NULL;
751 BUSY_END;
752 return true;
753 }