Debian Codegraph @

src/engine/fileOps/it.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 #include "fileOpsCommon.h"
21
22 extern "C" {
23 #include "../../../extern/itcompress/compression.h"
24 }
25
26 static const unsigned char volPortaSlide[10]={
27 0, 1, 4, 8, 16, 32, 64, 96, 128, 255
28 };
29
30 void readEnvelope(SafeReader& reader, DivInstrument* ins, int env) {
31 unsigned char flags=reader.readC();
32 unsigned char numPoints=reader.readC();
33 unsigned char loopStart=reader.readC();
34 unsigned char loopEnd=reader.readC();
35 unsigned char susStart=reader.readC();
36 unsigned char susEnd=reader.readC();
37
38 if (numPoints>25) numPoints=25;
39
40 if (loopStart>=numPoints) loopStart=numPoints-1;
41 if (loopEnd>=numPoints) loopEnd=numPoints-1;
42 if (susStart>=numPoints) susStart=numPoints-1;
43 if (susEnd>=numPoints) susEnd=numPoints-1;
44
45 unsigned short pointTime[25];
46 signed char pointVal[25];
47
48 for (int i=0; i<25; i++) {
49 pointVal[i]=reader.readC();
50 pointTime[i]=reader.readS();
51 }
52
53 // x
54 reader.readC();
55
56 // don't process if there aren't any points or if the envelope is disabled
57 if (numPoints<1) return;
58 if (!(flags&1)) return;
59
60 // convert into macro, or try to
61 DivInstrumentMacro* target=NULL;
62 switch (env) {
63 case 0: // volume
64 target=&ins->std.volMacro;
65 break;
66 case 1: // panning (split later)
67 target=&ins->std.panLMacro;
68 break;
69 case 2: // pitch or cutoff
70 if (flags&128) {
71 target=&ins->std.ex1Macro; // ES5506 filter
72 } else {
73 target=&ins->std.pitchMacro;
74 }
75 break;
76 }
77 target->len=0;
78 int point=0;
79 bool pointJustBegan=true;
80 // mark loop end as end of envelope
81 if (flags&2) {
82 if (loopEnd<numPoints) numPoints=loopEnd+1;
83 }
84 for (int i=0; i<255; i++) {
85 int curPoint=MIN(point,numPoints-1);
86 int nextPoint=MIN(point+1,numPoints-1);
87 int p0=pointVal[curPoint];
88 int p1=pointVal[nextPoint];
89 while (i>pointTime[nextPoint]) {
90 point++;
91 pointJustBegan=true;
92 curPoint=MIN(point,numPoints-1);
93 nextPoint=MIN(point+1,numPoints-1);
94 p0=pointVal[curPoint];
95 p1=pointVal[nextPoint];
96 if ((point+1)>=numPoints) {
97 break;
98 }
99 }
100 if (pointJustBegan) {
101 pointJustBegan=false;
102 if (flags&2) { // loop
103 if (point==loopStart && (!(flags&4) || susStart==susEnd || loopStart>=susEnd)) {
104 if (loopStart!=loopEnd) {
105 target->loop=i;
106 }
107 }
108 }
109 if (flags&4) { // sustain
110 if (susStart!=susEnd) { // sustain loop
111 if (point==susStart) {
112 target->loop=i;
113 }
114 }
115 if (point==susEnd) {
116 target->rel=i;
117 }
118 }
119 }
120 if ((point+1)>=numPoints) {
121 target->len=i;
122 //target->val[i]=p0;
123 break;
124 }
125 int timeDiff=pointTime[nextPoint]-pointTime[curPoint];
126 int curTime=i-pointTime[curPoint];
127 if (timeDiff<1) timeDiff=1;
128 if (curTime<0) curTime=0;
129
130 if (env==2) {
131 if (flags&128) {
132 p0+=32;
133 p1+=32;
134 p0*=512;
135 p1*=512;
136 } else {
137 p0*=64;
138 p1*=64;
139 }
140 }
141
142 target->len=i+1;
143 target->val[i]=p0+(((p1-p0)*curTime)/timeDiff);
144 }
145
146 // split L/R
147 if (env==1) {
148 for (int i=0; i<ins->std.panLMacro.len; i++) {
149 int val=ins->std.panLMacro.val[i];
150 if (val==0) {
151 ins->std.panLMacro.val[i]=4095;
152 ins->std.panRMacro.val[i]=4095;
153 } else if (val>0) { // pan right
154 ins->std.panLMacro.val[i]=4095*pow(1.0-((double)val/64.0),0.25);
155 ins->std.panRMacro.val[i]=4095;
156 } else { // pan left
157 ins->std.panLMacro.val[i]=4095;
158 ins->std.panRMacro.val[i]=4095*pow(1.0+((double)val/64.0),0.25);
159 }
160 }
161 ins->std.panRMacro.len=ins->std.panLMacro.len;
162 ins->std.panRMacro.loop=ins->std.panLMacro.loop;
163 ins->std.panRMacro.rel=ins->std.panLMacro.rel;
164 }
165 }
166
167 bool DivEngine::loadIT(unsigned char* file, size_t len) {
168 struct InvalidHeaderException {};
169 bool success=false;
170 char magic[4];
171
172 unsigned char chanPan[64];
173 unsigned char chanVol[64];
174
175 unsigned char orders[256];
176
177 unsigned int insPtr[256];
178 unsigned int samplePtr[256];
179 unsigned int patPtr[256];
180
181 unsigned short patLen[256];
182
183 unsigned char defVol[256];
184 unsigned char defPan[256];
185 unsigned char defPanIns[256];
186 unsigned char noteMap[256][128];
187
188 bool doesPitchSlide[64];
189 bool doesVibrato[64];
190 bool doesPanning[64];
191 bool doesVolSlide[64];
192 bool doesPanSlide[64];
193 bool doesArp[64];
194 bool doesTremolo[64];
195 bool doesPanbrello[64];
196
197 memset(doesPitchSlide,0,64*sizeof(bool));
198 memset(doesVibrato,0,64*sizeof(bool));
199 memset(doesPanning,0,64*sizeof(bool));
200 memset(doesVolSlide,0,64*sizeof(bool));
201 memset(doesPanSlide,0,64*sizeof(bool));
202 memset(doesArp,0,64*sizeof(bool));
203 memset(doesTremolo,0,64*sizeof(bool));
204 memset(doesPanbrello,0,64*sizeof(bool));
205
206 SafeReader reader=SafeReader(file,len);
207 warnings="";
208
209 memset(chanPan,0,64);
210 memset(chanVol,0,64);
211 memset(orders,0,256);
212 memset(patLen,0,256*sizeof(unsigned short));
213
214 memset(defVol,0,256);
215 memset(defPan,0,256);
216 memset(defPanIns,128,256);
217 memset(noteMap,0,256*128);
218
219 try {
220 DivSong ds;
221 ds.version=DIV_VERSION_IT;
222 ds.noSlidesOnFirstTick=true;
223 ds.rowResetsArpPos=true;
224 ds.ignoreJumpAtEnd=false;
225 ds.pitchSlideSpeed=8;
226
227 logV("Impulse Tracker module");
228
229 // load here
230 if (!reader.seek(0,SEEK_SET)) {
231 logE("premature end of file!");
232 lastError="incomplete file";
233 delete[] file;
234 return false;
235 }
236 reader.read(magic,4);
237
238 if (memcmp(magic,DIV_IT_MAGIC,4)!=0) {
239 logW("the magic isn't complete");
240 throw EndOfFileException(&reader,reader.tell());
241 }
242
243 ds.name=reader.readStringLatin1(26);
244
245 unsigned char hilight1=reader.readC();
246 unsigned char hilight2=reader.readC();
247 logV("highlights: %d %d",hilight1,hilight2);
248
249 unsigned short ordersLen=reader.readS();
250 ds.insLen=reader.readS();
251 ds.sampleLen=reader.readS();
252 unsigned short patCount=reader.readS();
253 unsigned short usedTracker=reader.readS();
254 unsigned short compatTracker=reader.readS();
255 unsigned short flags=reader.readS();
256 unsigned short special=reader.readS();
257
258 if (ds.insLen<0 || ds.insLen>256) {
259 logE("too many instruments!");
260 lastError="too many instruments";
261 delete[] file;
262 return false;
263 }
264
265 if (ds.sampleLen<0 || ds.sampleLen>256) {
266 logE("too many samples!");
267 lastError="too many samples";
268 delete[] file;
269 return false;
270 }
271
272 if (patCount>256) {
273 logE("too many patterns!");
274 lastError="too many patterns";
275 delete[] file;
276 return false;
277 }
278
279 if (flags&8) {
280 ds.linearPitch=2;
281 } else {
282 ds.linearPitch=0;
283 }
284
285 unsigned char globalVol=reader.readC();
286 unsigned char masterVol=reader.readC();
287 unsigned char initSpeed=reader.readC();
288 unsigned char initTempo=reader.readC();
289 unsigned char panSep=reader.readC();
290 reader.readC(); // PWD - unused
291
292 ds.subsong[0]->hz=(double)initTempo/2.5;
293 ds.subsong[0]->speeds.val[0]=initSpeed;
294 ds.subsong[0]->speeds.len=1;
295
296 logV("used tracker: %x",usedTracker);
297 logV("compatible with: %x",compatTracker);
298 logV("flags: %x",flags);
299 logV("special: %x",special);
300
301 logV("global vol: %d",globalVol);
302 logV("master vol: %d",masterVol);
303 logV("pan sep: %d",panSep);
304
305 unsigned short commentLen=reader.readS();
306 unsigned int commentPtr=reader.readI();
307
308 logV("comment len: %d",commentLen);
309 logV("comment ptr: %x",commentPtr);
310
311 reader.readI(); // reserved
312
313 reader.read(chanPan,64);
314 reader.read(chanVol,64);
315
316 logD("reading orders...");
317 size_t curSubSong=0;
318 int curOrder1=0;
319 ds.subsong[curSubSong]->ordersLen=0;
320 bool subSongIncreased=false;
321 for (int i=0; i<ordersLen; i++) {
322 unsigned char nextOrder=reader.readC();
323 orders[i]=curOrder1;
324
325 // skip +++ order
326 if (nextOrder==254) {
327 logV("- +++");
328 continue;
329 }
330 // next subsong
331 if (nextOrder==255) {
332 logV("- end of song");
333 if (!subSongIncreased) {
334 curSubSong++;
335 subSongIncreased=true;
336 }
337 curOrder1=0;
338 continue;
339 }
340 subSongIncreased=false;
341 if (ds.subsong.size()<=curSubSong) {
342 ds.subsong.push_back(new DivSubSong);
343 ds.subsong[curSubSong]->ordersLen=0;
344 ds.subsong[curSubSong]->name=fmt::sprintf("Order %.2X",i);
345 }
346 logV("- %.2x",nextOrder);
347 for (int j=0; j<DIV_MAX_CHANS; j++) {
348 ds.subsong[curSubSong]->orders.ord[j][ds.subsong[curSubSong]->ordersLen]=nextOrder;
349 }
350 ds.subsong[curSubSong]->ordersLen++;
351 curOrder1++;
352 }
353
354 for (int i=0; i<ds.insLen; i++) {
355 insPtr[i]=reader.readI();
356 }
357
358 for (int i=0; i<ds.sampleLen; i++) {
359 samplePtr[i]=reader.readI();
360 }
361
362 for (int i=0; i<patCount; i++) {
363 patPtr[i]=reader.readI();
364 }
365
366 // skip edit history if present
367 if (special&2) {
368 logD("skipping edit history...");
369 unsigned short editHistSize=reader.readS();
370 if (editHistSize>0) {
371 if (!reader.seek(editHistSize*8,SEEK_CUR)) {
372 logV("what? I wasn't expecting that from you.");
373 }
374 }
375 }
376
377 // read extension blocks, if any
378 logD("looking for extensions...");
379 bool hasExtensions=true;
380 while (hasExtensions) {
381 char extType[4];
382 unsigned int extSize=0;
383 memset(extType,0,4);
384
385 reader.read(extType,4);
386 extSize=reader.readI();
387
388 if (memcmp(extType,"PNAM",4)==0) {
389 logV("found MPT extension: pattern names");
390 // check whether this block is valid
391 if (extSize>patCount*32) {
392 logV("block may not be valid");
393 break;
394 }
395 // read pattern names
396 logV("reading pattern names...");
397 for (unsigned int i=0; i<(extSize>>5); i++) {
398 DivPattern* p=ds.subsong[0]->pat[0].getPattern(i,true);
399 p->name=reader.readStringLatin1(32);
400 }
401 } else if (memcmp(extType,"CNAM",4)==0) {
402 logV("found MPT extension: channel names");
403 // check whether this block is valid
404 if (extSize>DIV_MAX_CHANS*20) {
405 logV("block may not be valid");
406 break;
407 }
408 // read channel names
409 logV("reading channel names...");
410 for (unsigned int i=0; i<(extSize>>5); i++) {
411 String chanName=reader.readStringLatin1(20);
412 for (DivSubSong* j: ds.subsong) {
413 j->chanName[i]=chanName;
414 }
415 }
416 } else if (memcmp(extType,"CHFX",4)==0) {
417 logV("found MPT extension: channel effects");
418 // skip (stop if we cannot seek)
419 if (!reader.seek(extSize,SEEK_CUR)) {
420 break;
421 }
422 } else if (
423 extType[0]=='F' &&
424 (extType[1]=='X' || (extType[1]>='0' && extType[1]<='9')) &&
425 (extType[2]>='0' && extType[2]<='9') &&
426 (extType[3]>='0' && extType[3]<='9')
427 ) { // effect slot
428 logV("found MPT extension: effect slot");
429 // skip (stop if we cannot seek)
430 if (!reader.seek(extSize,SEEK_CUR)) {
431 break;
432 }
433 } else {
434 logV("no further extensions found... %.2x%.2x%.2x%.2x",extType[0],extType[1],extType[2],extType[3]);
435 hasExtensions=false;
436 }
437 }
438
439 // read song comment
440 logD("reading song comment...");
441 if (reader.seek(commentPtr,SEEK_SET)) {
442 try {
443 String comment=reader.readStringLatin1Special(commentLen);
444
445 ds.notes="";
446 ds.notes.reserve(comment.size());
447
448 for (char& i: comment) {
449 if (i=='\r') {
450 ds.notes+='\n';
451 } else {
452 ds.notes+=i;
453 }
454 }
455 } catch (EndOfFileException& e) {
456 logW("couldn't read song comment due to premature end of file.");
457 }
458 } else {
459 logW("couldn't seek to comment!");
460 }
461
462
463 // read instruments
464 for (int i=0; i<ds.insLen; i++) {
465 DivInstrument* ins=new DivInstrument;
466 ins->type=DIV_INS_ES5506;
467
468 if (insPtr[i]==0) {
469 ds.ins.push_back(ins);
470 continue;
471 }
472
473 logV("reading instrument %d...",i);
474 if (!reader.seek(insPtr[i],SEEK_SET)) {
475 logE("premature end of file!");
476 lastError="incomplete file";
477 delete ins;
478 delete[] file;
479 return false;
480 }
481
482 reader.read(magic,4);
483
484 if (memcmp(magic,"IMPI",4)!=0) {
485 logE("invalid instrument header!");
486 lastError="invalid instrument header";
487 delete ins;
488 delete[] file;
489 return false;
490 }
491
492 String dosName=reader.readStringLatin1(12);
493
494 reader.readC(); // 0
495
496 unsigned char initCut=255;
497 unsigned char initRes=255;
498 unsigned char insVol=128;
499 unsigned short fadeOut=256;
500
501 if (compatTracker<0x200) { // old format
502 unsigned char flags=reader.readC();
503 unsigned char volLoopStart=reader.readC();
504 unsigned char volLoopEnd=reader.readC();
505 unsigned char susLoopStart=reader.readC();
506 unsigned char susLoopEnd=reader.readC();
507
508 logV("flags: %x",flags);
509 logV("volLoopStart: %d",volLoopStart);
510 logV("volLoopEnd: %d",volLoopEnd);
511 logV("susLoopStart: %d",susLoopStart);
512 logV("susLoopEnd: %d",susLoopEnd);
513
514 reader.readS(); // x
515
516 fadeOut=reader.readS();
517
518 logV("fadeOut: %d",fadeOut);
519
520 // NNA/duplicate check - not supported by Furnace...
521 reader.readC();
522 reader.readC();
523
524 // version/sample count/x
525 reader.readI();
526 } else { // new format
527 // NNA/duplicate check - not supported by Furnace...
528 reader.readC();
529 reader.readC();
530 reader.readC();
531
532 fadeOut=reader.readS();
533
534 logV("fadeOut: %d",fadeOut);
535
536 reader.readC();
537 reader.readC();
538
539 insVol=reader.readC();
540 defPanIns[i]=reader.readC();
541
542 // vol/pan randomization
543 reader.readC();
544 reader.readC();
545
546 // version/sample count/x
547 reader.readI();
548 }
549
550 ins->name=reader.readStringLatin1(26);
551
552 if (compatTracker<0x200) { // old format
553 // x
554 reader.readC();
555 reader.readC();
556 reader.readC();
557 reader.readC();
558 reader.readC();
559 reader.readC();
560 } else { // new format
561 // filter params
562 initCut=reader.readC();
563 initRes=reader.readC();
564
565 // MIDI stuff - ignored
566 reader.readI();
567 }
568
569 logV("filter: %d/%d",initCut,initRes);
570
571 // note map
572 ins->amiga.useNoteMap=true;
573 for (int j=0; j<120; j++) {
574 ins->amiga.noteMap[j].freq=(unsigned char)reader.readC();
575 ins->amiga.noteMap[j].map=reader.readC()-1;
576 noteMap[i][j]=ins->amiga.noteMap[j].map;
577 }
578
579 // envelopes...
580 if (compatTracker<0x200) { // old format
581 // TODO
582 } else {
583 readEnvelope(reader,ins,0);
584 readEnvelope(reader,ins,1);
585 readEnvelope(reader,ins,2);
586 }
587
588 // scale envelope to global volume
589 if (insVol>128) insVol=128;
590 if (ins->std.volMacro.len==0) {
591 ins->std.volMacro.len=1;
592 ins->std.volMacro.val[0]=insVol>>1;
593 } else {
594 for (int j=0; j<ins->std.volMacro.len; j++) {
595 ins->std.volMacro.val[i]=(ins->std.volMacro.val[i]*insVol)>>7;
596 }
597 }
598
599 // add note fade if there isn't a release point in the volume envelope
600 if (ins->std.volMacro.len>0 && ins->std.volMacro.rel>=ins->std.volMacro.len) {
601 ins->std.volMacro.len--;
602 int initial=ins->std.volMacro.val[ins->std.volMacro.len];
603 ins->std.volMacro.rel=ins->std.volMacro.len;
604 for (int i=1024; ins->std.volMacro.len<255; i-=fadeOut) {
605 ins->std.volMacro.val[ins->std.volMacro.len++]=(initial*i)>>10;
606 if (i<0) break;
607 }
608 }
609
610 // set filter macro to cutoff value if no filter envelope is defined
611 if (ins->std.ex1Macro.len==0 && initCut&0x80) {
612 if ((initCut&0x7f)==0x7f) {
613 ins->std.ex1Macro.len=1;
614 ins->std.ex1Macro.val[0]=65535;
615 } else {
616 ins->std.ex1Macro.len=1;
617 ins->std.ex1Macro.val[0]=1024+(initCut&0x7f)*400;
618 }
619 } else {
620 ins->std.ex1Macro.len=1;
621 ins->std.ex1Macro.val[0]=65535;
622 }
623
624 ds.ins.push_back(ins);
625 }
626
627 // read samples
628 for (int i=0; i<ds.sampleLen; i++) {
629 DivSample* s=new DivSample;
630
631 if (samplePtr[i]==0) {
632 ds.sample.push_back(s);
633
634 // does the song not use instruments?
635 // create instrument then
636 if (ds.insLen==0) {
637 DivInstrument* ins=new DivInstrument;
638 ins->type=DIV_INS_ES5506;
639 ds.ins.push_back(ins);
640 }
641 continue;
642 }
643
644 logV("reading sample %d...",i);
645 if (!reader.seek(samplePtr[i],SEEK_SET)) {
646 logE("premature end of file!");
647 lastError="incomplete file";
648 delete s;
649 delete[] file;
650 return false;
651 }
652
653 reader.read(magic,4);
654
655 if (memcmp(magic,"IMPS",4)!=0) {
656 logW("invalid sample header!");
657 lastError="invalid sample header";
658 delete s;
659 delete[] file;
660 return false;
661 }
662
663 String dosName=reader.readStringLatin1(12);
664
665 reader.readC(); // 0
666
667 unsigned char sampleVol=reader.readC();
668 unsigned char flags=reader.readC();
669 defVol[i]=reader.readC();
670
671 s->name=reader.readStringLatin1(26);
672
673 unsigned char convert=reader.readC();
674 defPan[i]=reader.readC();
675
676 if (flags&2) {
677 s->depth=DIV_SAMPLE_DEPTH_16BIT;
678 } else {
679 s->depth=DIV_SAMPLE_DEPTH_8BIT;
680 }
681
682 unsigned int sampleLen=reader.readI();
683
684 if (sampleLen>16777216) {
685 logE("abnormal sample size! %x",reader.tell());
686 lastError="bad sample size";
687 delete[] file;
688 return false;
689 }
690
691 s->init(sampleLen);
692 s->loopStart=reader.readI();
693 s->loopEnd=reader.readI();
694 s->centerRate=reader.readI()/2;
695 if (flags&16) {
696 s->loop=true;
697 if (flags&64) {
698 s->loopMode=DIV_SAMPLE_LOOP_PINGPONG;
699 } else {
700 s->loopMode=DIV_SAMPLE_LOOP_FORWARD;
701 }
702 }
703 if (flags&32) {
704 s->loop=true;
705 if (flags&128) {
706 s->loopMode=DIV_SAMPLE_LOOP_PINGPONG;
707 } else {
708 s->loopMode=DIV_SAMPLE_LOOP_FORWARD;
709 }
710 s->loopStart=reader.readI();
711 s->loopEnd=reader.readI();
712 } else {
713 reader.readI();
714 reader.readI();
715 }
716
717 unsigned int dataPtr=reader.readI();
718
719 unsigned char vibSpeed=reader.readC();
720 unsigned char vibDepth=reader.readC();
721 unsigned char vibRate=reader.readC();
722 unsigned char vibShape=reader.readC();
723
724 logV("vibrato: %d %d %d %d",vibSpeed,vibDepth,vibRate,vibShape);
725
726 if (s->samples>0) {
727 logD("seek to %x...",dataPtr);
728 if (!reader.seek(dataPtr,SEEK_SET)) {
729 logE("premature end of file!");
730 lastError="incomplete file";
731 delete s;
732 delete[] file;
733 return false;
734 }
735 } else {
736 logD("seek not needed...");
737 }
738
739 logV("reading sample data (%d)",s->samples);
740
741 if (flags&8) { // compressed sample
742 unsigned int ret=0;
743 logV("decompression begin... (%d)",s->samples);
744 if (flags&4) {
745 logW("STEREO!");
746 if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
747 logV("16-bit");
748 short* outData=new short[s->samples*2];
749 ret=it_decompress16(outData,s->samples,&file[reader.tell()],len-reader.tell(),(convert&4)?1:0,(flags&4)?2:1);
750 for (unsigned int i=0; i<s->samples; i++) {
751 s->data16[i]=(outData[i<<1]+outData[1+(i<<1)])>>1;
752 }
753 delete[] outData;
754 } else {
755 logV("8-bit");
756 signed char* outData=new signed char[s->samples*2];
757 ret=it_decompress8(outData,s->samples,&file[reader.tell()],len-reader.tell(),(convert&4)?1:0,(flags&4)?2:1);
758 for (unsigned int i=0; i<s->samples; i++) {
759 s->data8[i]=(outData[i<<1]+outData[1+(i<<1)])>>1;
760 }
761 delete[] outData;
762 }
763 } else {
764 if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
765 logV("16-bit");
766 ret=it_decompress16(s->data16,s->samples,&file[reader.tell()],len-reader.tell(),(convert&4)?1:0,(flags&4)?2:1);
767 } else {
768 logV("8-bit");
769 ret=it_decompress8(s->data8,s->samples,&file[reader.tell()],len-reader.tell(),(convert&4)?1:0,(flags&4)?2:1);
770 }
771 }
772 logV("got: %d",ret);
773 } else {
774 try {
775 if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
776 if (flags&4) { // downmix stereo
777 for (unsigned int i=0; i<s->samples; i++) {
778 short l;
779 if (convert&2) {
780 l=reader.readS_BE();
781 } else {
782 l=reader.readS();
783 }
784 if (!(convert&1)) {
785 l^=0x8000;
786 }
787 s->data16[i]=l;
788 }
789 for (unsigned int i=0; i<s->samples; i++) {
790 short r;
791 if (convert&2) {
792 r=reader.readS_BE();
793 } else {
794 r=reader.readS();
795 }
796 if (!(convert&1)) {
797 r^=0x8000;
798 }
799 s->data16[i]=(s->data16[i]+r)>>1;
800 }
801 } else {
802 for (unsigned int i=0; i<s->samples; i++) {
803 if (convert&2) {
804 s->data16[i]=reader.readS_BE()^((convert&1)?0:0x8000);
805 } else {
806 s->data16[i]=reader.readS()^((convert&1)?0:0x8000);
807 }
808 }
809 }
810 } else {
811 if (flags&4) { // downmix stereo
812 for (unsigned int i=0; i<s->samples; i++) {
813 signed char l=reader.readC();
814 if (!(convert&1)) {
815 l^=0x80;
816 }
817 s->data8[i]=l;
818 }
819 for (unsigned int i=0; i<s->samples; i++) {
820 signed char r=reader.readC();
821 if (!(convert&1)) {
822 r^=0x80;
823 }
824 s->data8[i]=(s->data8[i]+r)>>1;
825 }
826 } else {
827 for (unsigned int i=0; i<s->samples; i++) {
828 s->data8[i]=reader.readC()^((convert&1)?0:0x80);
829 }
830 }
831 }
832 } catch (EndOfFileException& e) {
833 logW("premature end of file...");
834 }
835 }
836
837 // scale sample if necessary
838 if (s->samples>0) {
839 if (sampleVol>64) sampleVol=64;
840 if (sampleVol<64) {
841 // convert to 16-bit
842 if (s->depth==DIV_SAMPLE_DEPTH_8BIT) {
843 s->convert(DIV_SAMPLE_DEPTH_16BIT,0);
844 }
845
846 // then scale
847 for (unsigned int i=0; i<s->samples; i++) {
848 s->data16[i]=(s->data16[i]*sampleVol)>>6;
849 }
850 }
851 }
852
853 // does the song not use instruments?
854 // create instrument then
855 if (ds.insLen==0) {
856 DivInstrument* ins=new DivInstrument;
857 ins->name=s->name;
858 ins->type=DIV_INS_ES5506;
859 ins->amiga.initSample=i;
860 ds.ins.push_back(ins);
861 }
862
863 ds.sample.push_back(s);
864 }
865
866 // scan pattern data for effect use
867 int maxChan=0;
868 for (int i=0; i<patCount; i++) {
869 if (patPtr[i]==0) continue;
870
871 unsigned char mask[64];
872 unsigned char note[64];
873 unsigned char ins[64];
874 unsigned char vol[64];
875 unsigned char effect[64];
876 unsigned char effectVal[64];
877 int readRow=0;
878
879 memset(mask,0,64);
880 memset(note,0,64);
881 memset(ins,0,64);
882 memset(vol,0,64);
883 memset(effect,0,64);
884 memset(effectVal,0,64);
885
886 logV("scanning pattern %d...",i);
887 if (!reader.seek(patPtr[i],SEEK_SET)) {
888 logE("premature end of file!");
889 lastError="incomplete file";
890 delete[] file;
891 return false;
892 }
893
894 unsigned int dataLen=(unsigned short)reader.readS();
895 unsigned short patRows=reader.readS();
896
897 patLen[i]=patRows;
898
899 if (patRows>DIV_MAX_ROWS) {
900 logE("too many rows! %d",patRows);
901 lastError="too many rows";
902 delete[] file;
903 return false;
904 }
905
906 reader.readI(); // x
907
908 dataLen+=reader.tell();
909
910 // read pattern data
911 while (reader.tell()<dataLen) {
912 unsigned char chan=reader.readC();
913 bool hasVol=false;
914 bool hasEffect=false;
915
916 if (chan==0) {
917 readRow++;
918 if (readRow>=patRows) {
919 break;
920 }
921 continue;
922 }
923
924 if (chan&128) {
925 mask[(chan-1)&63]=reader.readC();
926 }
927 chan=(chan-1)&63;
928
929 if (chan>maxChan) maxChan=chan;
930
931 if (mask[chan]&1) {
932 note[chan]=reader.readC();
933 }
934 if (mask[chan]&2) {
935 ins[chan]=reader.readC();
936 }
937 if (mask[chan]&4) {
938 vol[chan]=reader.readC();
939 hasVol=true;
940 }
941 if (mask[chan]&8) {
942 effect[chan]=reader.readC();
943 effectVal[chan]=reader.readC();
944 hasEffect=true;
945 }
946 if (mask[chan]&64) {
947 hasVol=true;
948 }
949 if (mask[chan]&128) {
950 hasEffect=true;
951 }
952
953 if (hasVol) {
954 if (vol[chan]>64) {
955 if (vol[chan]>=65 && vol[chan]<=74) { // fine vol up
956 doesVolSlide[chan]=true;
957 } else if (vol[chan]>=75 && vol[chan]<=84) { // fine vol down
958 doesVolSlide[chan]=true;
959 } else if (vol[chan]>=85 && vol[chan]<=94) { // vol slide up
960 doesVolSlide[chan]=true;
961 } else if (vol[chan]>=95 && vol[chan]<=104) { // vol slide down
962 doesVolSlide[chan]=true;
963 } else if (vol[chan]>=105 && vol[chan]<=114) { // pitch down
964 doesPitchSlide[chan]=true;
965 } else if (vol[chan]>=115 && vol[chan]<=124) { // pitch up
966 doesPitchSlide[chan]=true;
967 } else if (vol[chan]>=193 && vol[chan]<=202) { // porta
968 doesPitchSlide[chan]=true;
969 } else if (vol[chan]>=203 && vol[chan]<=212) { // vibrato
970 doesVibrato[chan]=true;
971 }
972 }
973 }
974 if (hasEffect) {
975 switch (effect[chan]+'A'-1) {
976 case 'D': // vol slide
977 doesVolSlide[chan]=true;
978 break;
979 case 'E': // pitch down
980 doesPitchSlide[chan]=true;
981 break;
982 case 'F': // pitch up
983 doesPitchSlide[chan]=true;
984 break;
985 case 'G': // porta
986 doesPitchSlide[chan]=true;
987 break;
988 case 'H': // vibrato
989 doesVibrato[chan]=true;
990 break;
991 case 'J': // arp
992 doesArp[chan]=true;
993 break;
994 case 'K': // vol slide + vibrato
995 doesVolSlide[chan]=true;
996 doesVibrato[chan]=true;
997 break;
998 case 'L': // vol slide + porta
999 doesVolSlide[chan]=true;
1000 doesPitchSlide[chan]=true;
1001 break;
1002 case 'P': // pan slide
1003 doesPanSlide[chan]=true;
1004 break;
1005 case 'R': // tremolo
1006 doesTremolo[chan]=true;
1007 break;
1008 case 'U': // fine vibrato
1009 doesVibrato[chan]=true;
1010 break;
1011 case 'Y': // panbrello
1012 doesPanbrello[chan]=true;
1013 break;
1014 }
1015 }
1016 }
1017 }
1018
1019 // read patterns
1020 for (int i=0; i<patCount; i++) {
1021 unsigned char effectCol[64];
1022 unsigned char vibStatus[64];
1023 bool vibStatusChanged[64];
1024 bool vibing[64];
1025 bool vibingOld[64];
1026 unsigned char volSlideStatus[64];
1027 bool volSlideStatusChanged[64];
1028 bool volSliding[64];
1029 bool volSlidingOld[64];
1030 unsigned char portaStatus[64];
1031 bool portaStatusChanged[64];
1032 bool porting[64];
1033 bool portingOld[64];
1034 unsigned char portaType[64];
1035 unsigned char arpStatus[64];
1036 bool arpStatusChanged[64];
1037 bool arping[64];
1038 bool arpingOld[64];
1039 unsigned char panStatus[64];
1040 bool panStatusChanged[64];
1041 bool panning[64];
1042 bool panningOld[64];
1043 unsigned char tremStatus[64];
1044 bool tremStatusChanged[64];
1045 bool treming[64];
1046 bool tremingOld[64];
1047 unsigned char panSlideStatus[64];
1048 bool panSlideStatusChanged[64];
1049 bool panSliding[64];
1050 bool panSlidingOld[64];
1051 bool did[64];
1052 unsigned char lastRetrig[64];
1053
1054 if (patPtr[i]==0) continue;
1055
1056 unsigned char mask[64];
1057 unsigned char note[64];
1058 unsigned char ins[64];
1059 unsigned char vol[64];
1060 unsigned char effect[64];
1061 unsigned char effectVal[64];
1062 int readRow=0;
1063 bool mustCommitInitial=true;
1064
1065 memset(effectCol,4,64);
1066 memset(vibStatus,0,64);
1067 memset(vibStatusChanged,0,64*sizeof(bool));
1068 memset(vibing,0,64*sizeof(bool));
1069 memset(vibingOld,0,64*sizeof(bool));
1070 memset(volSlideStatus,0,64);
1071 memset(volSlideStatusChanged,0,64*sizeof(bool));
1072 memset(volSliding,0,64*sizeof(bool));
1073 memset(volSlidingOld,0,64*sizeof(bool));
1074 memset(portaStatus,0,64);
1075 memset(portaStatusChanged,0,64*sizeof(bool));
1076 memset(porting,0,64*sizeof(bool));
1077 memset(portingOld,0,64*sizeof(bool));
1078 memset(portaType,0,64);
1079 memset(arpStatus,0,64);
1080 memset(arpStatusChanged,0,64*sizeof(bool));
1081 memset(arping,0,64*sizeof(bool));
1082 memset(arpingOld,0,64*sizeof(bool));
1083 memset(panStatus,0,64);
1084 memset(panStatusChanged,0,64*sizeof(bool));
1085 memset(panning,0,64*sizeof(bool));
1086 memset(panningOld,0,64*sizeof(bool));
1087 memset(tremStatus,0,64);
1088 memset(tremStatusChanged,0,64*sizeof(bool));
1089 memset(treming,0,64*sizeof(bool));
1090 memset(tremingOld,0,64*sizeof(bool));
1091 memset(panSlideStatus,0,64);
1092 memset(panSlideStatusChanged,0,64*sizeof(bool));
1093 memset(panSliding,0,64*sizeof(bool));
1094 memset(panSlidingOld,0,64*sizeof(bool));
1095 memset(did,0,64*sizeof(bool));
1096 memset(lastRetrig,0,64);
1097
1098 memset(mask,0,64);
1099 memset(note,0,64);
1100 memset(ins,0,64);
1101 memset(vol,0,64);
1102 memset(effect,0,64);
1103 memset(effectVal,0,64);
1104
1105 logV("reading pattern %d...",i);
1106 if (!reader.seek(patPtr[i],SEEK_SET)) {
1107 logE("premature end of file!");
1108 lastError="incomplete file";
1109 delete[] file;
1110 return false;
1111 }
1112
1113 unsigned int dataLen=(unsigned short)reader.readS();
1114 unsigned short patRows=reader.readS();
1115
1116 patLen[i]=patRows;
1117
1118 if (patRows>DIV_MAX_ROWS) {
1119 logE("too many rows! %d",patRows);
1120 lastError="too many rows";
1121 delete[] file;
1122 return false;
1123 }
1124
1125 reader.readI(); // x
1126
1127 dataLen+=reader.tell();
1128
1129 // read pattern data
1130 while (reader.tell()<dataLen) {
1131 unsigned char chan=reader.readC();
1132 bool hasNote=false;
1133 bool hasIns=false;
1134 bool hasVol=false;
1135 bool hasEffect=false;
1136
1137 if (chan==0) {
1138 // commit effects
1139 for (int j=0; j<64; j++) {
1140 DivPattern* p=ds.subsong[0]->pat[j].getPattern(i,true);
1141 if (vibing[j]!=vibingOld[j] || vibStatusChanged[j]) {
1142 p->data[readRow][effectCol[j]++]=0x04;
1143 p->data[readRow][effectCol[j]++]=vibing[j]?vibStatus[j]:0;
1144 doesVibrato[j]=true;
1145 } else if (doesVibrato[j] && mustCommitInitial) {
1146 p->data[readRow][effectCol[j]++]=0x04;
1147 p->data[readRow][effectCol[j]++]=0;
1148 }
1149
1150 if (volSliding[j]!=volSlidingOld[j] || volSlideStatusChanged[j]) {
1151 if (volSlideStatus[j]>=0xf1 && volSliding[j]) {
1152 p->data[readRow][effectCol[j]++]=0xf9;
1153 p->data[readRow][effectCol[j]++]=volSlideStatus[j]&15;
1154 volSliding[j]=false;
1155 } else if ((volSlideStatus[j]&15)==15 && volSlideStatus[j]>=0x10 && volSliding[j]) {
1156 p->data[readRow][effectCol[j]++]=0xf8;
1157 p->data[readRow][effectCol[j]++]=volSlideStatus[j]>>4;
1158 volSliding[j]=false;
1159 } else {
1160 p->data[readRow][effectCol[j]++]=0xfa;
1161 p->data[readRow][effectCol[j]++]=volSliding[j]?volSlideStatus[j]:0;
1162 }
1163 doesVolSlide[j]=true;
1164 } else if (doesVolSlide[j] && mustCommitInitial) {
1165 p->data[readRow][effectCol[j]++]=0xfa;
1166 p->data[readRow][effectCol[j]++]=0;
1167 }
1168
1169 if (porting[j]!=portingOld[j] || portaStatusChanged[j]) {
1170 if (portaStatus[j]>=0xe0 && portaType[j]!=3 && porting[j]) {
1171 p->data[readRow][effectCol[j]++]=portaType[j]|0xf0;
1172 p->data[readRow][effectCol[j]++]=(portaStatus[j]&15)*((portaStatus[j]>=0xf0)?1:1);
1173 porting[j]=false;
1174 } else {
1175 p->data[readRow][effectCol[j]++]=portaType[j];
1176 p->data[readRow][effectCol[j]++]=porting[j]?portaStatus[j]:0;
1177 }
1178 doesPitchSlide[j]=true;
1179 } else if (doesPitchSlide[j] && mustCommitInitial) {
1180 p->data[readRow][effectCol[j]++]=0x01;
1181 p->data[readRow][effectCol[j]++]=0;
1182 }
1183
1184 if (arping[j]!=arpingOld[j] || arpStatusChanged[j]) {
1185 p->data[readRow][effectCol[j]++]=0x00;
1186 p->data[readRow][effectCol[j]++]=arping[j]?arpStatus[j]:0;
1187 doesArp[j]=true;
1188 } else if (doesArp[j] && mustCommitInitial) {
1189 p->data[readRow][effectCol[j]++]=0x00;
1190 p->data[readRow][effectCol[j]++]=0;
1191 }
1192
1193 if (treming[j]!=tremingOld[j] || tremStatusChanged[j]) {
1194 p->data[readRow][effectCol[j]++]=0x07;
1195 p->data[readRow][effectCol[j]++]=treming[j]?tremStatus[j]:0;
1196 doesTremolo[j]=true;
1197 } else if (doesTremolo[j] && mustCommitInitial) {
1198 p->data[readRow][effectCol[j]++]=0x07;
1199 p->data[readRow][effectCol[j]++]=0;
1200 }
1201
1202 if (panning[j]!=panningOld[j] || panStatusChanged[j]) {
1203 p->data[readRow][effectCol[j]++]=0x84;
1204 p->data[readRow][effectCol[j]++]=panning[j]?panStatus[j]:0;
1205 doesPanbrello[j]=true;
1206 } else if (doesPanbrello[j] && mustCommitInitial) {
1207 p->data[readRow][effectCol[j]++]=0x84;
1208 p->data[readRow][effectCol[j]++]=0;
1209 }
1210
1211 if (panSliding[j]!=panSlidingOld[j] || panSlideStatusChanged[j]) {
1212 p->data[readRow][effectCol[j]++]=0x83;
1213 p->data[readRow][effectCol[j]++]=panSliding[j]?panSlideStatus[j]:0;
1214 doesPanSlide[j]=true;
1215 } else if (doesPanSlide[j] && mustCommitInitial) {
1216 p->data[readRow][effectCol[j]++]=0x83;
1217 p->data[readRow][effectCol[j]++]=0;
1218 }
1219
1220 if ((effectCol[j]>>1)-2>ds.subsong[0]->pat[j].effectCols) {
1221 ds.subsong[0]->pat[j].effectCols=(effectCol[j]>>1)-1;
1222 }
1223 }
1224
1225 readRow++;
1226 memcpy(vibingOld,vibing,64*sizeof(bool));
1227 memcpy(volSlidingOld,volSliding,64*sizeof(bool));
1228 memcpy(portingOld,porting,64*sizeof(bool));
1229 memcpy(arpingOld,arping,64*sizeof(bool));
1230 memcpy(panningOld,panning,64*sizeof(bool));
1231 memcpy(tremingOld,treming,64*sizeof(bool));
1232 memcpy(panSlidingOld,panSliding,64*sizeof(bool));
1233 memset(vibStatusChanged,0,64*sizeof(bool));
1234 memset(volSlideStatusChanged,0,64*sizeof(bool));
1235 memset(portaStatusChanged,0,64*sizeof(bool));
1236 memset(arpStatusChanged,0,64*sizeof(bool));
1237 memset(panStatusChanged,0,64*sizeof(bool));
1238 memset(tremStatusChanged,0,64*sizeof(bool));
1239 memset(panSlideStatusChanged,0,64*sizeof(bool));
1240 memset(vibing,0,64*sizeof(bool));
1241 memset(volSliding,0,64*sizeof(bool));
1242 memset(porting,0,64*sizeof(bool));
1243 memset(arping,0,64*sizeof(bool));
1244 memset(panning,0,64*sizeof(bool));
1245 memset(treming,0,64*sizeof(bool));
1246 memset(panSliding,0,64*sizeof(bool));
1247 memset(did,0,64);
1248 mustCommitInitial=false;
1249 if (readRow>=patRows) {
1250 if (readRow>0) {
1251 // place end of pattern marker
1252 DivPattern* p=ds.subsong[0]->pat[0].getPattern(i,true);
1253 p->data[readRow-1][effectCol[0]++]=0x0d;
1254 p->data[readRow-1][effectCol[0]++]=0;
1255
1256 if ((effectCol[0]>>1)-2>ds.subsong[0]->pat[0].effectCols) {
1257 ds.subsong[0]->pat[0].effectCols=(effectCol[0]>>1)-1;
1258 }
1259 }
1260 break;
1261 }
1262 memset(effectCol,4,64);
1263 continue;
1264 }
1265
1266 if (chan&128) {
1267 mask[(chan-1)&63]=reader.readC();
1268 }
1269 chan=(chan-1)&63;
1270
1271 if (mask[chan]&1) {
1272 note[chan]=reader.readC();
1273 hasNote=true;
1274 }
1275 if (mask[chan]&2) {
1276 ins[chan]=reader.readC();
1277 hasIns=true;
1278 }
1279 if (mask[chan]&4) {
1280 vol[chan]=reader.readC();
1281 hasVol=true;
1282 }
1283 if (mask[chan]&8) {
1284 effect[chan]=reader.readC();
1285 effectVal[chan]=reader.readC();
1286 hasEffect=true;
1287 }
1288 if (mask[chan]&16) {
1289 hasNote=true;
1290 }
1291 if (mask[chan]&32) {
1292 hasIns=true;
1293 }
1294 if (mask[chan]&64) {
1295 hasVol=true;
1296 }
1297 if (mask[chan]&128) {
1298 hasEffect=true;
1299 }
1300
1301 DivPattern* p=ds.subsong[0]->pat[chan].getPattern(i,true);
1302
1303 if (hasNote) {
1304 if (note[chan]==255) { // note release
1305 p->data[readRow][0]=101;
1306 p->data[readRow][1]=0;
1307 } else if (note[chan]==254) { // note off
1308 p->data[readRow][0]=100;
1309 p->data[readRow][1]=0;
1310 } else if (note[chan]<120) {
1311 p->data[readRow][0]=note[chan]%12;
1312 p->data[readRow][1]=note[chan]/12;
1313 if (p->data[readRow][0]==0) {
1314 p->data[readRow][0]=12;
1315 p->data[readRow][1]--;
1316 }
1317 } else { // note fade, but Furnace does not support that
1318 p->data[readRow][0]=102;
1319 p->data[readRow][1]=0;
1320 }
1321 }
1322 if (hasIns) {
1323 p->data[readRow][2]=ins[chan]-1;
1324 if ((note[chan]<120 || ds.insLen==0) && ins[chan]>0) {
1325 unsigned char targetPan=0;
1326 if (ds.insLen==0) {
1327 targetPan=defPan[(ins[chan]-1)&255];
1328 } else {
1329 targetPan=defPan[noteMap[(ins[chan]-1)&255][note[chan]]];
1330 if (!(targetPan&128)) {
1331 targetPan=defPanIns[(ins[chan]-1)&255]^0x80;
1332 }
1333 }
1334 if (targetPan&128) {
1335 p->data[readRow][effectCol[chan]++]=0x80;
1336 p->data[readRow][effectCol[chan]++]=CLAMP((targetPan&127)<<2,0,255);
1337 }
1338 }
1339
1340 if (hasNote && (note[chan]<120 || ds.insLen==0) && ins[chan]>0) {
1341 if (ds.insLen==0) {
1342 p->data[readRow][3]=defVol[(ins[chan]-1)&255];
1343 } else {
1344 p->data[readRow][3]=defVol[noteMap[(ins[chan]-1)&255][note[chan]]];
1345 }
1346 }
1347 }
1348 if (hasVol) {
1349 if (vol[chan]<=64) {
1350 p->data[readRow][3]=vol[chan];
1351 } else { // effects in volume column
1352 if (vol[chan]>=128 && vol[chan]<=192) { // panning
1353 p->data[readRow][effectCol[chan]++]=0x80;
1354 p->data[readRow][effectCol[chan]++]=CLAMP((vol[chan]-128)<<2,0,255);
1355 } else if (vol[chan]>=65 && vol[chan]<=74) { // fine vol up
1356 } else if (vol[chan]>=75 && vol[chan]<=84) { // fine vol down
1357 } else if (vol[chan]>=85 && vol[chan]<=94) { // vol slide up
1358 if ((vol[chan]-85)!=0) {
1359 volSlideStatus[chan]=(vol[chan]-85)<<4;
1360 volSlideStatusChanged[chan]=true;
1361 }
1362 if (hasNote || hasIns) {
1363 volSlideStatusChanged[chan]=true;
1364 }
1365 volSliding[chan]=true;
1366 } else if (vol[chan]>=95 && vol[chan]<=104) { // vol slide down
1367 if ((vol[chan]-95)!=0) {
1368 volSlideStatus[chan]=vol[chan]-95;
1369 volSlideStatusChanged[chan]=true;
1370 }
1371 if (hasNote || hasIns) {
1372 volSlideStatusChanged[chan]=true;
1373 }
1374 volSliding[chan]=true;
1375 } else if (vol[chan]>=105 && vol[chan]<=114) { // pitch down
1376 } else if (vol[chan]>=115 && vol[chan]<=124) { // pitch up
1377 } else if (vol[chan]>=193 && vol[chan]<=202) { // porta
1378 unsigned char portaVal=volPortaSlide[vol[chan]-193];
1379 if (portaVal!=0) {
1380 portaStatus[chan]=portaVal;
1381 portaStatusChanged[chan]=true;
1382 }
1383 if (portaType[chan]!=3) {
1384 portaStatusChanged[chan]=true;
1385 }
1386 portaType[chan]=3;
1387 porting[chan]=true;
1388 } else if (vol[chan]>=203 && vol[chan]<=212) { // vibrato
1389 if ((vol[chan]-203)!=0) {
1390 vibStatus[chan]&=0xf0;
1391 vibStatus[chan]|=(vol[chan]-203);
1392 vibStatusChanged[chan]=true;
1393 }
1394 vibing[chan]=true;
1395 }
1396 }
1397 }
1398 if (hasEffect) {
1399 switch (effect[chan]+'A'-1) {
1400 case 'A': // speed
1401 p->data[readRow][effectCol[chan]++]=0x0f;
1402 p->data[readRow][effectCol[chan]++]=effectVal[chan];
1403 break;
1404 case 'B': // go to order
1405 p->data[readRow][effectCol[chan]++]=0x0b;
1406 p->data[readRow][effectCol[chan]++]=orders[effectVal[chan]];
1407 break;
1408 case 'C': // next order
1409 p->data[readRow][effectCol[chan]++]=0x0d;
1410 p->data[readRow][effectCol[chan]++]=effectVal[chan];
1411 break;
1412 case 'D': // vol slide
1413 if (effectVal[chan]!=0) {
1414 volSlideStatus[chan]=effectVal[chan];
1415 volSlideStatusChanged[chan]=true;
1416 }
1417 if (hasIns) {
1418 volSlideStatusChanged[chan]=true;
1419 }
1420 volSliding[chan]=true;
1421 break;
1422 case 'E': // pitch down
1423 if (effectVal[chan]!=0) {
1424 portaStatus[chan]=effectVal[chan];
1425 portaStatusChanged[chan]=true;
1426 }
1427 if (portaType[chan]!=2) {
1428 portaStatusChanged[chan]=true;
1429 }
1430 portaType[chan]=2;
1431 porting[chan]=true;
1432 break;
1433 case 'F': // pitch up
1434 if (effectVal[chan]!=0) {
1435 portaStatus[chan]=effectVal[chan];
1436 portaStatusChanged[chan]=true;
1437 }
1438 if (portaType[chan]!=1) {
1439 portaStatusChanged[chan]=true;
1440 }
1441 portaType[chan]=1;
1442 porting[chan]=true;
1443 break;
1444 case 'G': // porta
1445 if (effectVal[chan]!=0) {
1446 portaStatus[chan]=effectVal[chan];
1447 portaStatusChanged[chan]=true;
1448 }
1449 if (portaType[chan]!=3 || hasNote) {
1450 portaStatusChanged[chan]=true;
1451 }
1452 portaType[chan]=3;
1453 porting[chan]=true;
1454 break;
1455 case 'H': // vibrato
1456 if (effectVal[chan]!=0) {
1457 vibStatus[chan]=effectVal[chan];
1458 vibStatusChanged[chan]=true;
1459 }
1460 vibing[chan]=true;
1461 break;
1462 case 'I': // tremor (!)
1463 break;
1464 case 'J': // arp
1465 if (effectVal[chan]!=0) {
1466 arpStatus[chan]=effectVal[chan];
1467 arpStatusChanged[chan]=true;
1468 }
1469 arping[chan]=true;
1470 break;
1471 case 'K': // vol slide + vibrato
1472 if (effectVal[chan]!=0) {
1473 volSlideStatus[chan]=effectVal[chan];
1474 volSlideStatusChanged[chan]=true;
1475 }
1476 volSliding[chan]=true;
1477 vibing[chan]=true;
1478 break;
1479 case 'L': // vol slide + porta
1480 if (effectVal[chan]!=0) {
1481 volSlideStatus[chan]=effectVal[chan];
1482 volSlideStatusChanged[chan]=true;
1483 }
1484 volSliding[chan]=true;
1485 porting[chan]=true;
1486 portaType[chan]=3;
1487 break;
1488 case 'M': // channel vol
1489 break;
1490 case 'N': // channel vol slide
1491 break;
1492 case 'O': // offset
1493 p->data[readRow][effectCol[chan]++]=0x91;
1494 p->data[readRow][effectCol[chan]++]=effectVal[chan];
1495 break;
1496 case 'P': // pan slide
1497 if (effectVal[chan]!=0) {
1498 panSlideStatus[chan]=effectVal[chan];
1499 panSlideStatusChanged[chan]=true;
1500 }
1501 panSliding[chan]=true;
1502 break;
1503 case 'Q': // retrigger
1504 if (effectVal[chan]!=0) {
1505 lastRetrig[chan]=effectVal[chan];
1506 }
1507 p->data[readRow][effectCol[chan]++]=0x0c;
1508 p->data[readRow][effectCol[chan]++]=lastRetrig[chan]&15;
1509 break;
1510 case 'R': // tremolo
1511 if (effectVal[chan]!=0) {
1512 tremStatus[chan]=effectVal[chan];
1513 tremStatusChanged[chan]=true;
1514 }
1515 treming[chan]=true;
1516 break;
1517 case 'S': // special...
1518 switch (effectVal[chan]>>4) {
1519 case 0x3: // vibrato waveform
1520 switch (effectVal[chan]&3) {
1521 case 0x0: // sine
1522 p->data[readRow][effectCol[chan]++]=0xe3;
1523 p->data[readRow][effectCol[chan]++]=0x00;
1524 break;
1525 case 0x1: // ramp down
1526 p->data[readRow][effectCol[chan]++]=0xe3;
1527 p->data[readRow][effectCol[chan]++]=0x05;
1528 break;
1529 case 0x2: // square
1530 p->data[readRow][effectCol[chan]++]=0xe3;
1531 p->data[readRow][effectCol[chan]++]=0x06;
1532 break;
1533 case 0x3: // random
1534 p->data[readRow][effectCol[chan]++]=0xe3;
1535 p->data[readRow][effectCol[chan]++]=0x07;
1536 break;
1537 }
1538 break;
1539 case 0x7:
1540 switch (effectVal[chan]&15) {
1541 case 0x7: // volume envelope off
1542 p->data[readRow][effectCol[chan]++]=0xf5;
1543 p->data[readRow][effectCol[chan]++]=0x00;
1544 break;
1545 case 0x8: // volume envelope on
1546 p->data[readRow][effectCol[chan]++]=0xf6;
1547 p->data[readRow][effectCol[chan]++]=0x00;
1548 break;
1549 case 0x9: // panning envelope off
1550 p->data[readRow][effectCol[chan]++]=0xf5;
1551 p->data[readRow][effectCol[chan]++]=0x0c;
1552 p->data[readRow][effectCol[chan]++]=0xf5;
1553 p->data[readRow][effectCol[chan]++]=0x0d;
1554 break;
1555 case 0xa: // panning envelope on
1556 p->data[readRow][effectCol[chan]++]=0xf6;
1557 p->data[readRow][effectCol[chan]++]=0x0c;
1558 p->data[readRow][effectCol[chan]++]=0xf6;
1559 p->data[readRow][effectCol[chan]++]=0x0d;
1560 break;
1561 case 0xb: // pitch envelope off
1562 p->data[readRow][effectCol[chan]++]=0xf5;
1563 p->data[readRow][effectCol[chan]++]=0x04;
1564 break;
1565 case 0xc: //pitch envelope on
1566 p->data[readRow][effectCol[chan]++]=0xf6;
1567 p->data[readRow][effectCol[chan]++]=0x04;
1568 break;
1569 }
1570 break;
1571 case 0x8: // panning
1572 p->data[readRow][effectCol[chan]++]=0x80;
1573 p->data[readRow][effectCol[chan]++]=(effectVal[chan]&15)<<4;
1574 break;
1575 case 0xa: // offset (high nibble)
1576 p->data[readRow][effectCol[chan]++]=0x92;
1577 p->data[readRow][effectCol[chan]++]=effectVal[chan]&15;
1578 break;
1579 case 0xc: // note cut
1580 p->data[readRow][effectCol[chan]++]=0xec;
1581 p->data[readRow][effectCol[chan]++]=effectVal[chan]&15;
1582 break;
1583 case 0xd: // note delay
1584 p->data[readRow][effectCol[chan]++]=0xed;
1585 p->data[readRow][effectCol[chan]++]=effectVal[chan]&15;
1586 break;
1587 }
1588 break;
1589 case 'T': // tempo
1590 if (effectVal[chan]>=0x20) {
1591 p->data[readRow][effectCol[chan]++]=0xf0;
1592 p->data[readRow][effectCol[chan]++]=effectVal[chan];
1593 }
1594 break;
1595 case 'U': // fine vibrato
1596 if (effectVal[chan]!=0) {
1597 vibStatus[chan]=effectVal[chan];
1598 vibStatusChanged[chan]=true;
1599 }
1600 vibing[chan]=true;
1601 break;
1602 case 'V': // global volume (!)
1603 break;
1604 case 'W': // global volume slide (!)
1605 break;
1606 case 'X': // panning
1607 p->data[readRow][effectCol[chan]++]=0x80;
1608 p->data[readRow][effectCol[chan]++]=effectVal[chan];
1609 break;
1610 case 'Y': // panbrello
1611 if (effectVal[chan]!=0) {
1612 panStatus[chan]=effectVal[chan];
1613 panStatusChanged[chan]=true;
1614 }
1615 panning[chan]=true;
1616 break;
1617 case 'Z': // MIDI macro
1618 break;
1619 }
1620 }
1621 }
1622 }
1623
1624 maxChan++;
1625
1626 ds.insLen=ds.ins.size();
1627
1628 logV("maxChan: %d",maxChan);
1629
1630 // set channel visibility
1631 for (int i=maxChan; i<((maxChan+32)&(~31)); i++) {
1632 ds.subsong[0]->chanShow[i]=false;
1633 ds.subsong[0]->chanShowChanOsc[i]=false;
1634 }
1635
1636 // copy patterns to the rest of subsongs
1637 int copiesMade=0;
1638 for (size_t i=1; i<ds.subsong.size(); i++) {
1639 bool usedPat[256];
1640 memset(usedPat,0,256*sizeof(bool));
1641 for (int j=0; j<ds.subsong[i]->ordersLen; j++) {
1642 usedPat[ds.subsong[i]->orders.ord[0][j]]=true;
1643 }
1644 for (int j=0; j<maxChan; j++) {
1645 for (int k=0; k<patCount; k++) {
1646 if (!usedPat[k]) continue;
1647 if (ds.subsong[0]->pat[j].data[k]) {
1648 ds.subsong[0]->pat[j].data[k]->copyOn(ds.subsong[i]->pat[j].getPattern(k,true));
1649 copiesMade++;
1650 }
1651 }
1652 ds.subsong[i]->pat[j].effectCols=ds.subsong[0]->pat[j].effectCols;
1653 }
1654 ds.subsong[i]->speeds=ds.subsong[0]->speeds;
1655 ds.subsong[i]->hz=ds.subsong[0]->hz;
1656 memcpy(ds.subsong[i]->chanShow,ds.subsong[0]->chanShow,DIV_MAX_CHANS*sizeof(bool));
1657 memcpy(ds.subsong[i]->chanShowChanOsc,ds.subsong[0]->chanShowChanOsc,DIV_MAX_CHANS*sizeof(bool));
1658 }
1659
1660 logV("copies made %d",copiesMade);
1661
1662 // set pattern lengths
1663 for (size_t i=0; i<ds.subsong.size(); i++) {
1664 unsigned short patLenMax=0;
1665 for (int j=0; j<ds.subsong[i]->ordersLen; j++) {
1666 unsigned short nextLen=patLen[ds.subsong[i]->orders.ord[0][j]];
1667 if (patLenMax<nextLen) {
1668 patLenMax=nextLen;
1669 }
1670 }
1671 ds.subsong[i]->patLen=patLenMax;
1672 }
1673
1674 // set systems
1675 for (int i=0; i<(maxChan+32)>>5; i++) {
1676 ds.system[i]=DIV_SYSTEM_ES5506;
1677 ds.systemFlags[i].set("amigaVol",true);
1678 if (ds.linearPitch!=2) {
1679 ds.systemFlags[i].set("amigaPitch",true);
1680 }
1681 }
1682 ds.systemLen=(maxChan+32)>>5;
1683 ds.systemName="PC";
1684
1685 // find subsongs
1686 ds.findSubSongs(maxChan);
1687
1688 // populate subsongs with default panning values
1689 for (size_t i=0; i<ds.subsong.size(); i++) {
1690 for (int j=0; j<maxChan; j++) {
1691 DivPattern* p=ds.subsong[i]->pat[j].getPattern(ds.subsong[i]->orders.ord[j][0],true);
1692 for (int k=0; k<DIV_MAX_EFFECTS; k++) {
1693 if (p->data[0][4+(k<<1)]==0x80) {
1694 // give up if there's a panning effect already
1695 break;
1696 }
1697 if (p->data[0][4+(k<<1)]==-1) {
1698 if ((chanPan[j]&127)==100) {
1699 // should be surround...
1700 p->data[0][4+(k<<1)]=0x80;
1701 p->data[0][5+(k<<1)]=0x80;
1702 } else {
1703 p->data[0][4+(k<<1)]=0x80;
1704 p->data[0][5+(k<<1)]=CLAMP((chanPan[j]&127)<<2,0,255);
1705 }
1706 if (ds.subsong[i]->pat[j].effectCols<=k) ds.subsong[i]->pat[j].effectCols=k+1;
1707 break;
1708 }
1709 }
1710 }
1711 }
1712
1713 if (active) quitDispatch();
1714 BUSY_BEGIN_SOFT;
1715 saveLock.lock();
1716 song.unload();
1717 song=ds;
1718 changeSong(0);
1719 recalcChans();
1720 saveLock.unlock();
1721 BUSY_END;
1722 if (active) {
1723 initDispatch();
1724 BUSY_BEGIN;
1725 renderSamples();
1726 reset();
1727 BUSY_END;
1728 }
1729 success=true;
1730 } catch (EndOfFileException& e) {
1731 //logE("premature end of file!");
1732 lastError="incomplete file";
1733 } catch (InvalidHeaderException& e) {
1734 //logE("invalid header!");
1735 lastError="invalid header!";
1736 }
1737 return success;
1738 }
1739