Debian Codegraph @

src/engine/fileOps/xm.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 void readEnvelope(DivInstrument* ins, int env, unsigned char flags, unsigned char numPoints, unsigned char loopStart, unsigned char loopEnd, unsigned char susPoint, short* points) {
23 if (numPoints>12) numPoints=12;
24
25 if (loopStart>=numPoints) loopStart=numPoints-1;
26 if (loopEnd>=numPoints) loopEnd=numPoints-1;
27 if (susPoint>=numPoints) susPoint=numPoints-1;
28
29 unsigned short pointTime[12];
30 short pointVal[12];
31
32 for (int i=0; i<12; i++) {
33 pointTime[i]=points[i<<1];
34 pointVal[i]=points[1|(i<<1)];
35 }
36
37 // don't process if there aren't any points or if the envelope is disabled
38 if (numPoints<1) return;
39 if (!(flags&1)) return;
40
41 // convert into macro, or try to
42 DivInstrumentMacro* target=NULL;
43 switch (env) {
44 case 0: // volume
45 target=&ins->std.volMacro;
46 break;
47 case 1: // panning (split later)
48 target=&ins->std.panLMacro;
49 break;
50 }
51 target->len=0;
52 // mark loop end as end of envelope
53 if (flags&4) {
54 if (loopEnd<numPoints) numPoints=loopEnd+1;
55 }
56
57 // new freaking algorithm
58 for (int i=0; i<numPoints; i++) {
59 int curPoint=MIN(i,numPoints-1);
60 int nextPoint=MIN(i+1,numPoints-1);
61 int p0=pointVal[curPoint];
62 int p1=pointVal[nextPoint];
63 int t0=pointTime[curPoint];
64 int t1=pointTime[nextPoint];
65
66 if (t0==t1) {
67 if (t0<255) {
68 target->val[t0]=p0;
69 }
70 } else {
71 for (int j=t0; j<t1 && j<255; j++) {
72 target->val[j]=p0+(((p1-p0)*(j-t0))/(t1-t0));
73 }
74 }
75 }
76 if (flags&4) { // loop
77 if (loopStart!=loopEnd && loopStart<numPoints) {
78 target->loop=CLAMP(pointTime[loopStart],0,254);
79 target->len=MIN(pointTime[numPoints-1],255);
80 } else {
81 target->len=MIN(pointTime[numPoints-1]+1,255);
82 }
83 } else {
84 target->len=MIN(pointTime[numPoints-1]+1,255);
85 }
86 if (flags&2 && susPoint<numPoints) { // sustain
87 target->rel=CLAMP(pointTime[susPoint]-1,0,254);
88 }
89 if (((flags&4) && (!(flags&2))) || ((flags&6)==0)) {
90 target->rel=target->len-1;
91 }
92
93 // old crap
94 /*
95 for (int i=0; i<255; i++) {
96 int curPoint=MIN(point,numPoints-1);
97 int nextPoint=MIN(point+1,numPoints-1);
98 int p0=pointVal[curPoint];
99 int p1=pointVal[nextPoint];
100 while (i>pointTime[nextPoint]) {
101 point++;
102 pointJustBegan=true;
103 curPoint=MIN(point,numPoints-1);
104 nextPoint=MIN(point+1,numPoints-1);
105 p0=pointVal[curPoint];
106 p1=pointVal[nextPoint];
107 if ((point+1)>=numPoints) {
108 break;
109 }
110 }
111 if (pointJustBegan) {
112 pointJustBegan=false;
113 if (flags&4) { // loop
114 if (point==loopStart) {
115 if (loopStart!=loopEnd) {
116 target->loop=i;
117 }
118 }
119 }
120 if (flags&2) { // sustain
121 if (point==susPoint) {
122 target->rel=MAX(i-1,0);
123 }
124 }
125 }
126 if ((point+1)>=numPoints) {
127 target->len=i-1;
128 if (((flags&4) && (!(flags&2))) || ((flags&6)==0)) {
129 target->rel=i-2;
130 }
131 //target->val[i]=p0;
132 break;
133 }
134 int timeDiff=pointTime[nextPoint]-pointTime[curPoint];
135 int curTime=i-pointTime[curPoint];
136 if (timeDiff<1) timeDiff=1;
137 if (curTime<0) curTime=0;
138
139 target->len=i+1;
140 target->val[i]=p0+(((p1-p0)*curTime)/timeDiff);
141 }
142 */
143
144 // split L/R
145 if (env==1) {
146 for (int i=0; i<ins->std.panLMacro.len; i++) {
147 int val=ins->std.panLMacro.val[i]-32;
148 if (val==0) {
149 ins->std.panLMacro.val[i]=4095;
150 ins->std.panRMacro.val[i]=4095;
151 } else if (val>0) { // pan right
152 ins->std.panLMacro.val[i]=4095*pow(1.0-((double)val/64.0),0.25);
153 ins->std.panRMacro.val[i]=4095;
154 } else { // pan left
155 ins->std.panLMacro.val[i]=4095;
156 ins->std.panRMacro.val[i]=4095*pow(1.0+((double)val/64.0),0.25);
157 }
158 }
159 ins->std.panRMacro.len=ins->std.panLMacro.len;
160 ins->std.panRMacro.loop=ins->std.panLMacro.loop;
161 ins->std.panRMacro.rel=ins->std.panLMacro.rel;
162 }
163 }
164
165 #define XM_FINISH \
166 delete[] sampleVol; \
167 delete[] samplePan; \
168 delete[] noteMap;
169
170 bool DivEngine::loadXM(unsigned char* file, size_t len) {
171 struct InvalidHeaderException {};
172 bool success=false;
173 char magic[32];
174 unsigned char* sampleVol=new unsigned char[256*256];
175 unsigned char* samplePan=new unsigned char[256*256];
176 unsigned char* noteMap=new unsigned char[256*128];
177
178 unsigned short patLen[256];
179
180 bool doesPitchSlide[128];
181 bool doesVibrato[128];
182 bool doesPanning[128];
183 bool doesVolSlide[128];
184 bool doesPanSlide[128];
185 bool doesArp[128];
186 bool doesTremolo[128];
187 bool doesPanbrello[128];
188
189 SafeReader reader=SafeReader(file,len);
190 warnings="";
191
192 memset(sampleVol,0,256*256);
193 memset(samplePan,0,256*256);
194 memset(noteMap,0,256*128);
195
196 memset(patLen,0,256*sizeof(unsigned short));
197
198 memset(doesPitchSlide,0,128*sizeof(bool));
199 memset(doesVibrato,0,128*sizeof(bool));
200 memset(doesPanning,0,128*sizeof(bool));
201 memset(doesVolSlide,0,128*sizeof(bool));
202 memset(doesPanSlide,0,128*sizeof(bool));
203 memset(doesArp,0,128*sizeof(bool));
204 memset(doesTremolo,0,128*sizeof(bool));
205 memset(doesPanbrello,0,128*sizeof(bool));
206
207 try {
208 DivSong ds;
209 ds.version=DIV_VERSION_XM;
210 //ds.linearPitch=0;
211 //ds.pitchMacroIsLinear=false;
212 ds.noSlidesOnFirstTick=true;
213 ds.rowResetsArpPos=true;
214 ds.ignoreJumpAtEnd=false;
215 ds.pitchSlideSpeed=8;
216
217 logV("Extended Module");
218
219 // load here
220 if (!reader.seek(0,SEEK_SET)) {
221 logE("premature end of file!");
222 lastError="incomplete file";
223 delete[] file;
224 XM_FINISH;
225 return false;
226 }
227 reader.read(magic,17);
228
229 if (memcmp(magic,DIV_XM_MAGIC,17)!=0) {
230 logW("invalid magic");
231 throw EndOfFileException(&reader,reader.tell());
232 }
233
234 ds.name=reader.readStringLatin1(20);
235
236 // 0x1a
237 reader.readC();
238
239 String trackerName=reader.readStringLatin1(20);
240 unsigned short trackerVer=reader.readS();
241
242 if (trackerName!="") logV("made with %s",trackerName);
243 logV("version %x",trackerVer);
244
245 unsigned int headerSeek=reader.tell();
246 headerSeek+=reader.readI();
247
248 ds.subsong[0]->ordersLen=(unsigned short)reader.readS();
249 ds.subsong[0]->patLen=1;
250 unsigned short loopPos=reader.readS();
251 unsigned short totalChans=reader.readS();
252 unsigned short patCount=reader.readS();
253 ds.insLen=(unsigned short)reader.readS();
254 ds.linearPitch=(reader.readS()&1)?2:0;
255 ds.subsong[0]->speeds.val[0]=reader.readS();
256 ds.subsong[0]->speeds.len=1;
257 double bpm=(unsigned short)reader.readS();
258 ds.subsong[0]->hz=(double)bpm/2.5;
259
260 if (ds.subsong[0]->ordersLen>256) {
261 logE("invalid order count!");
262 lastError="invalid order count";
263 delete[] file;
264 XM_FINISH;
265 return false;
266 }
267
268 if (patCount>256) {
269 logE("too many patterns!");
270 lastError="too many patterns";
271 delete[] file;
272 XM_FINISH;
273 return false;
274 }
275
276 if (ds.insLen<0 || ds.insLen>256) {
277 logE("invalid instrument count!");
278 lastError="invalid instrument count";
279 delete[] file;
280 XM_FINISH;
281 return false;
282 }
283
284 logV("channels: %d",totalChans);
285
286 if (totalChans>127) {
287 logE("invalid channel count!");
288 lastError="invalid channel count";
289 delete[] file;
290 XM_FINISH;
291 return false;
292 }
293
294 logV("repeat pos: %d",loopPos);
295
296 logV("reading orders...");
297 for (int i=0; i<256; i++) {
298 unsigned char val=reader.readC();
299 for (int j=0; j<DIV_MAX_CHANS; j++) {
300 ds.subsong[0]->orders.ord[j][i]=val;
301 }
302 }
303
304 for (int i=0; i<(totalChans+31)>>5; i++) {
305 ds.system[i]=DIV_SYSTEM_ES5506;
306 ds.systemFlags[i].set("amigaVol",true);
307 ds.systemFlags[i].set("amigaPitch",(ds.linearPitch==0));
308 ds.systemFlags[i].set("volScale",3900);
309 }
310 ds.systemLen=(totalChans+31)>>5;
311
312 size_t patBegin=headerSeek;
313 logV("seeking to %x...",patBegin);
314
315 if (!reader.seek(patBegin,SEEK_SET)) {
316 logE("premature end of file!");
317 lastError="incomplete file";
318 delete[] file;
319 XM_FINISH;
320 return false;
321 }
322
323 // scan pattern data for effect use
324 logD("scanning patterns...");
325 for (unsigned short i=0; i<patCount; i++) {
326 logV("pattern %d",i);
327 headerSeek=reader.tell();
328 unsigned int headerSeekAdd=reader.readI();
329 logV("seek is %d",headerSeekAdd);
330 headerSeek+=headerSeekAdd;
331
332 unsigned char packType=reader.readC();
333 if (packType!=0) {
334 logE("unknown packing type %d!",packType);
335 lastError="unknown packing type";
336 ds.unload();
337 delete[] file;
338 XM_FINISH;
339 return false;
340 }
341
342 unsigned short totalRows=reader.readS();
343 logV("total rows: %d",totalRows);
344 if (totalRows>ds.subsong[0]->patLen) ds.subsong[0]->patLen=totalRows;
345 patLen[i]=totalRows;
346
347 if (totalRows>256) {
348 logE("too many rows! %d",totalRows);
349 lastError="too many rows";
350 delete[] file;
351 XM_FINISH;
352 return false;
353 }
354
355 unsigned short packedSize=reader.readS();
356 logV("packed size: %d",packedSize);
357
358 unsigned int packedSeek=headerSeek+packedSize;
359
360 logV("seeking to %x...",headerSeek);
361 if (!reader.seek(headerSeek,SEEK_SET)) {
362 logE("premature end of file!");
363 lastError="incomplete file";
364 delete[] file;
365 XM_FINISH;
366 return false;
367 }
368
369 // read data
370 for (int j=0; j<totalRows; j++) {
371 if (reader.tell()>=packedSeek) {
372 logV("end of data - stopping here...");
373 break;
374 }
375 for (int k=0; k<totalChans; k++) {
376 unsigned char note=reader.readC();
377 unsigned char vol=0;
378 unsigned char effect=0;
379 unsigned char effectVal=0;
380 bool hasNote=false;
381 bool hasIns=false;
382 bool hasVol=false;
383 bool hasEffect=false;
384 bool hasEffectVal=false;
385
386 if (note&0x80) { // packed
387 hasNote=note&1;
388 hasIns=note&2;
389 hasVol=note&4;
390 hasEffect=note&8;
391 hasEffectVal=note&16;
392 if (hasNote) {
393 note=reader.readC();
394 }
395 } else { // unpacked
396 hasNote=true;
397 hasIns=true;
398 hasVol=true;
399 hasEffect=true;
400 hasEffectVal=true;
401 }
402
403 if (hasIns) {
404 reader.readC();
405 }
406 if (hasVol) {
407 vol=reader.readC();
408 switch (vol>>4) {
409 case 0x6: // vol slide down
410 doesVolSlide[k]=true;
411 break;
412 case 0x7: // vol slide up
413 doesVolSlide[k]=true;
414 break;
415 case 0x8: // vol slide down (fine)
416 doesVolSlide[k]=true;
417 break;
418 case 0x9: // vol slide up (fine)
419 doesVolSlide[k]=true;
420 break;
421 case 0xa: // vibrato speed
422 doesVibrato[k]=true;
423 break;
424 case 0xb: // vibrato depth
425 doesVibrato[k]=true;
426 break;
427 case 0xc: // panning
428 doesPanning[k]=true;
429 break;
430 case 0xd: // pan slide left
431 doesPanSlide[k]=true;
432 break;
433 case 0xe: // pan slide right
434 doesPanSlide[k]=true;
435 break;
436 case 0xf: // porta
437 doesPitchSlide[k]=true;
438 break;
439 }
440 }
441 if (hasEffect) {
442 effect=reader.readC();
443 switch (effect) {
444 case 0:
445 doesArp[k]=true;
446 break;
447 case 1:
448 doesPitchSlide[k]=true;
449 break;
450 case 2:
451 doesPitchSlide[k]=true;
452 break;
453 case 3:
454 doesPitchSlide[k]=true;
455 break;
456 case 4:
457 doesVibrato[k]=true;
458 break;
459 case 5:
460 doesPitchSlide[k]=true;
461 doesVolSlide[k]=true;
462 break;
463 case 6:
464 doesVibrato[k]=true;
465 doesVolSlide[k]=true;
466 break;
467 case 7:
468 doesTremolo[k]=true;
469 break;
470 case 8:
471 doesPanning[k]=true;
472 break;
473 case 0xe:
474 doesPanning[k]=true;
475 break;
476 case 0x19: // P
477 doesPanSlide[k]=true;
478 break;
479 case 0x21: // X
480 doesPitchSlide[k]=true;
481 break;
482 case 0x22: // Y
483 doesPanbrello[k]=true;
484 break;
485 }
486 }
487 if (hasEffectVal) {
488 if (!hasEffect) {
489 doesArp[k]=true;
490 }
491 effectVal=reader.readC();
492 if (effect==0xe) {
493 switch (effectVal>>4) {
494 case 1:
495 doesPitchSlide[k]=true;
496 break;
497 case 2:
498 doesPitchSlide[k]=true;
499 break;
500 case 0xa:
501 doesVolSlide[k]=true;
502 break;
503 case 0xb:
504 doesVolSlide[k]=true;
505 break;
506 }
507 }
508 }
509 }
510 }
511
512 logV("seeking to %x...",packedSeek);
513 if (!reader.seek(packedSeek,SEEK_SET)) {
514 logE("premature end of file!");
515 lastError="incomplete file";
516 delete[] file;
517 XM_FINISH;
518 return false;
519 }
520 }
521
522 // read instruments
523 for (int i=0; i<ds.insLen; i++) {
524 short volEnv[24];
525 short panEnv[48];
526
527 DivInstrument* ins=new DivInstrument;
528 logD("instrument %d",i);
529 headerSeek=reader.tell();
530 headerSeek+=reader.readI();
531
532 ins->name=reader.readStringLatin1(22);
533 ins->type=DIV_INS_ES5506;
534 ins->amiga.useNoteMap=true;
535
536 unsigned char insType=reader.readC();
537
538 /*
539 if (insType!=0) {
540 logE("unknown instrument type!");
541 lastError="unknown instrument type";
542 delete ins;
543 song.unload();
544 delete[] file;
545 XM_FINISH;
546 return false;
547 }*/
548
549 logV("type: %d",insType);
550
551 unsigned short sampleCount=reader.readS();
552 logV("%d samples",sampleCount);
553
554 if (sampleCount>0) {
555 unsigned int sampleHeaderSize=reader.readI();
556 logV("sample header size: %d",sampleHeaderSize);
557 for (int j=0; j<96; j++) {
558 unsigned char nextMap=reader.readC();
559 ins->amiga.noteMap[j].map=ds.sample.size()+nextMap;
560 noteMap[(i<<7)|j]=nextMap;
561 }
562
563 for (int j=0; j<24; j++) {
564 volEnv[j]=reader.readS();
565 }
566 for (int j=0; j<24; j++) {
567 panEnv[j]=reader.readS();
568 }
569
570 unsigned char volEnvLen=reader.readC();
571 unsigned char panEnvLen=reader.readC();
572 unsigned char volSusPoint=reader.readC();
573 unsigned char volLoopStart=reader.readC();
574 unsigned char volLoopEnd=reader.readC();
575 unsigned char panSusPoint=reader.readC();
576 unsigned char panLoopStart=reader.readC();
577 unsigned char panLoopEnd=reader.readC();
578 unsigned char volType=reader.readC();
579 unsigned char panType=reader.readC();
580
581 unsigned char vibType=reader.readC();
582 unsigned char vibSweep=reader.readC();
583 unsigned char vibDepth=reader.readC();
584 unsigned char vibRate=reader.readC();
585
586 unsigned short volFade=reader.readS();
587 reader.readS(); // reserved
588
589 logV("vibrato: %d %d %d %d",vibType,vibSweep,vibDepth,vibRate);
590
591 // convert envelopes
592 readEnvelope(ins,0,volType,volEnvLen,volLoopStart,volLoopEnd,volSusPoint,volEnv);
593 readEnvelope(ins,1,panType,panEnvLen,panLoopStart,panLoopEnd,panSusPoint,panEnv);
594
595 if (volType&1) {
596 // add fade-out
597 logV("volFade: %d",volFade);
598 if (volFade!=0) {
599 int cur=64;
600 int macroLen=ins->std.volMacro.len;
601 int curPos=ins->std.volMacro.len-1;
602 if (ins->std.volMacro.loop<macroLen) {
603 curPos=ins->std.volMacro.loop;
604 }
605 if (ins->std.volMacro.len>0) {
606 cur=ins->std.volMacro.val[curPos];
607 }
608 for (int fadeOut=32767; fadeOut>0 && ins->std.volMacro.len<254; fadeOut-=volFade) {
609 cur=ins->std.volMacro.val[curPos];
610 ins->std.volMacro.val[ins->std.volMacro.len++]=(cur*fadeOut)>>15;
611 if (++curPos>=macroLen) {
612 if (ins->std.volMacro.loop<macroLen) {
613 curPos=ins->std.volMacro.loop;
614 } else {
615 curPos=macroLen-1;
616 }
617 }
618 }
619 if (ins->std.volMacro.len<255) {
620 ins->std.volMacro.val[ins->std.volMacro.len++]=0;
621 }
622 if (ins->std.volMacro.rel<ins->std.volMacro.len && ins->std.volMacro.rel<ins->std.volMacro.loop) {
623 ins->std.volMacro.loop=255;
624 }
625 }
626 } else {
627 // add a one-tick macro to make note release happy
628 ins->std.volMacro.val[0]=64;
629 ins->std.volMacro.val[1]=0;
630 ins->std.volMacro.rel=0;
631 ins->std.volMacro.loop=255;
632 ins->std.volMacro.len=2;
633 }
634
635 if (!reader.seek(headerSeek,SEEK_SET)) {
636 logE("premature end of file!");
637 lastError="incomplete file";
638 delete[] file;
639 XM_FINISH;
640 return false;
641 }
642
643 // read samples for this instrument
644 std::vector<DivSample*> toAdd;
645 for (int j=0; j<sampleCount; j++) {
646 DivSample* s=new DivSample;
647
648 unsigned int numSamples=reader.readI();
649 if (numSamples>16777216) {
650 logE("abnormal sample size! %x",reader.tell());
651 lastError="bad sample size";
652 delete s;
653 delete[] file;
654 XM_FINISH;
655 return false;
656 }
657
658 s->loopStart=reader.readI();
659 s->loopEnd=reader.readI()+s->loopStart;
660
661 sampleVol[(i<<8)|j]=reader.readC();
662
663 signed char fine=reader.readC();
664 unsigned char flags=reader.readC();
665 samplePan[(i<<8)|j]=reader.readC();
666 signed char note=reader.readC();
667
668 switch (flags&3) {
669 case 0:
670 s->loop=false;
671 break;
672 case 1:
673 s->loop=true;
674 s->loopMode=DIV_SAMPLE_LOOP_FORWARD;
675 break;
676 case 2:
677 s->loop=true;
678 s->loopMode=DIV_SAMPLE_LOOP_PINGPONG;
679 break;
680 }
681
682 if (s->loopStart>s->loopEnd) {
683 s->loopStart^=s->loopEnd;
684 s->loopEnd^=s->loopStart;
685 s->loopStart^=s->loopEnd;
686 }
687
688 if (flags&16) {
689 s->loopStart>>=1;
690 s->loopEnd>>=1;
691 }
692
693 reader.readC(); // reserved
694
695 s->centerRate=8363.0*pow(2.0,((double)note+((double)fine/128.0))/12.0);
696
697 s->name=reader.readStringLatin1(22);
698 s->depth=(flags&16)?DIV_SAMPLE_DEPTH_16BIT:DIV_SAMPLE_DEPTH_8BIT;
699 if (flags&16) {
700 numSamples>>=1;
701 }
702 s->init(numSamples);
703
704 // seek here???
705 toAdd.push_back(s);
706 }
707
708 for (int j=0; j<sampleCount; j++) {
709 DivSample* s=toAdd[j];
710
711 // load sample data
712 if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
713 short next=0;
714 for (unsigned int i=0; i<s->samples; i++) {
715 next+=reader.readS();
716 s->data16[i]=next;
717 }
718 } else {
719 signed char next=0;
720 for (unsigned int i=0; i<s->samples; i++) {
721 next+=reader.readC();
722 s->data8[i]=next;
723 }
724 }
725 }
726
727 for (DivSample* i: toAdd) {
728 ds.sample.push_back(i);
729 }
730 toAdd.clear();
731 } else {
732 if (!reader.seek(headerSeek,SEEK_SET)) {
733 logE("premature end of file!");
734 lastError="incomplete file";
735 delete[] file;
736 XM_FINISH;
737 return false;
738 }
739 }
740
741 ds.ins.push_back(ins);
742 }
743
744 if (!reader.seek(patBegin,SEEK_SET)) {
745 logE("premature end of file!");
746 lastError="incomplete file";
747 delete[] file;
748 XM_FINISH;
749 return false;
750 }
751
752 // read patterns
753 logD("reading patterns...");
754 for (unsigned short i=0; i<patCount; i++) {
755 unsigned char effectCol[128];
756 unsigned char vibStatus[128];
757 bool vibStatusChanged[128];
758 bool vibing[128];
759 bool vibingOld[128];
760 unsigned char volSlideStatus[128];
761 bool volSlideStatusChanged[128];
762 bool volSliding[128];
763 bool volSlidingOld[128];
764 unsigned char portaStatus[128];
765 bool portaStatusChanged[128];
766 bool porting[128];
767 bool portingOld[128];
768 unsigned char portaType[128];
769 unsigned char arpStatus[128];
770 bool arpStatusChanged[128];
771 bool arping[128];
772 bool arpingOld[128];
773 unsigned char panStatus[128];
774 bool panStatusChanged[128];
775 bool panning[128];
776 bool panningOld[128];
777 unsigned char tremStatus[128];
778 bool tremStatusChanged[128];
779 bool treming[128];
780 bool tremingOld[128];
781 unsigned char panSlideStatus[128];
782 bool panSlideStatusChanged[128];
783 bool panSliding[128];
784 bool panSlidingOld[128];
785 unsigned char lastNote[128];
786
787 bool mustCommitInitial=true;
788
789 memset(effectCol,4,128);
790 memset(vibStatus,0,128);
791 memset(vibStatusChanged,0,128*sizeof(bool));
792 memset(vibing,0,128*sizeof(bool));
793 memset(vibingOld,0,128*sizeof(bool));
794 memset(volSlideStatus,0,128);
795 memset(volSlideStatusChanged,0,128*sizeof(bool));
796 memset(volSliding,0,128*sizeof(bool));
797 memset(volSlidingOld,0,128*sizeof(bool));
798 memset(portaStatus,0,128);
799 memset(portaStatusChanged,0,128*sizeof(bool));
800 memset(porting,0,128*sizeof(bool));
801 memset(portingOld,0,128*sizeof(bool));
802 memset(portaType,0,128);
803 memset(arpStatus,0,128);
804 memset(arpStatusChanged,0,128*sizeof(bool));
805 memset(arping,0,128*sizeof(bool));
806 memset(arpingOld,0,128*sizeof(bool));
807 memset(panStatus,0,128);
808 memset(panStatusChanged,0,128*sizeof(bool));
809 memset(panning,0,128*sizeof(bool));
810 memset(panningOld,0,128*sizeof(bool));
811 memset(tremStatus,0,128);
812 memset(tremStatusChanged,0,128*sizeof(bool));
813 memset(treming,0,128*sizeof(bool));
814 memset(tremingOld,0,128*sizeof(bool));
815 memset(panSlideStatus,0,128);
816 memset(panSlideStatusChanged,0,128*sizeof(bool));
817 memset(panSliding,0,128*sizeof(bool));
818 memset(panSlidingOld,0,128*sizeof(bool));
819 memset(lastNote,0,128);
820
821 logV("pattern %d",i);
822 headerSeek=reader.tell();
823 headerSeek+=reader.readI();
824
825 unsigned char packType=reader.readC();
826 if (packType!=0) {
827 logE("unknown packing type %d!",packType);
828 lastError="unknown packing type";
829 ds.unload();
830 delete[] file;
831 XM_FINISH;
832 return false;
833 }
834
835 unsigned short totalRows=reader.readS();
836 logV("total rows: %d",totalRows);
837 if (totalRows>ds.subsong[0]->patLen) ds.subsong[0]->patLen=totalRows;
838 patLen[i]=totalRows;
839
840 if (totalRows>256) {
841 logE("too many rows! %d",totalRows);
842 lastError="too many rows";
843 delete[] file;
844 XM_FINISH;
845 return false;
846 }
847
848 unsigned short packedSize=reader.readS();
849 logV("packed size: %d",packedSize);
850
851 unsigned int packedSeek=headerSeek+packedSize;
852
853 logV("seeking to %x...",headerSeek);
854 if (!reader.seek(headerSeek,SEEK_SET)) {
855 logE("premature end of file!");
856 lastError="incomplete file";
857 delete[] file;
858 XM_FINISH;
859 return false;
860 }
861
862 // read data
863 for (int j=0; j<totalRows; j++) {
864 if (reader.tell()>=packedSeek) {
865 logV("end of data - stopping here...");
866 break;
867 }
868 for (int k=0; k<totalChans; k++) {
869 DivPattern* p=ds.subsong[0]->pat[k].getPattern(i,true);
870
871 unsigned char note=reader.readC();
872 unsigned char ins=0;
873 unsigned char vol=0;
874 unsigned char effect=0;
875 unsigned char effectVal=0;
876 bool hasNote=false;
877 bool hasIns=false;
878 bool hasVol=false;
879 bool hasEffect=false;
880 bool hasEffectVal=false;
881 bool writePanning=false;
882
883 if (note&0x80) { // packed
884 hasNote=note&1;
885 hasIns=note&2;
886 hasVol=note&4;
887 hasEffect=note&8;
888 hasEffectVal=note&16;
889 if (hasNote) {
890 note=reader.readC();
891 }
892 } else { // unpacked
893 hasNote=true;
894 hasIns=true;
895 hasVol=true;
896 hasEffect=true;
897 hasEffectVal=true;
898 }
899
900 if (hasNote) {
901 if (note!=0) {
902 lastNote[k]=note;
903 if (note>96) {
904 p->data[j][0]=101;
905 p->data[j][1]=0;
906 } else {
907 note--;
908 p->data[j][0]=note%12;
909 p->data[j][1]=note/12;
910 if (p->data[j][0]==0) {
911 p->data[j][0]=12;
912 p->data[j][1]=(unsigned char)(p->data[j][1]-1);
913 }
914 }
915 }
916 }
917 if (hasIns) {
918 ins=reader.readC();
919 p->data[j][2]=((int)ins)-1;
920 // default volume
921 if (lastNote[k]<96 && ins>0) {
922 p->data[j][3]=sampleVol[(((ins-1)&255)<<8)|(noteMap[(((ins-1)&255)<<7)|(lastNote[k]&127)])];
923 }
924 writePanning=true;
925 }
926 if (hasVol) {
927 vol=reader.readC();
928 if (vol>=0x10 && vol<=0x50) {
929 p->data[j][3]=vol-0x10;
930 } else { // effects in volume column
931 switch (vol>>4) {
932 case 0x6: // vol slide down
933 if ((vol&15)!=0) {
934 volSlideStatus[k]=vol&15;
935 volSlideStatusChanged[k]=true;
936 }
937 if (hasNote || hasIns) {
938 volSlideStatusChanged[k]=true;
939 }
940 volSliding[k]=true;
941 break;
942 case 0x7: // vol slide up
943 if ((vol&15)!=0) {
944 volSlideStatus[k]=(vol&15)<<4;
945 volSlideStatusChanged[k]=true;
946 }
947 if (hasNote || hasIns) {
948 volSlideStatusChanged[k]=true;
949 }
950 volSliding[k]=true;
951 break;
952 case 0x8: // vol slide down (fine)
953 if ((vol&15)!=0) {
954 volSlideStatus[k]=0xf0|(vol&15);
955 volSlideStatusChanged[k]=true;
956 }
957 if (hasNote || hasIns) {
958 volSlideStatusChanged[k]=true;
959 }
960 volSliding[k]=true;
961 break;
962 case 0x9: // vol slide up (fine)
963 if ((vol&15)!=0) {
964 volSlideStatus[k]=((vol&15)<<4)|0xf;
965 volSlideStatusChanged[k]=true;
966 }
967 if (hasNote || hasIns) {
968 volSlideStatusChanged[k]=true;
969 }
970 volSliding[k]=true;
971 break;
972 case 0xa: // vibrato speed
973 if ((vol&15)!=0) {
974 vibStatus[k]&=0x0f;
975 vibStatus[k]|=(vol&15)<<4;
976 vibStatusChanged[k]=true;
977 }
978 vibing[k]=true;
979 break;
980 case 0xb: // vibrato depth
981 if ((vol&15)!=0) {
982 vibStatus[k]&=0xf0;
983 vibStatus[k]|=vol&15;
984 vibStatusChanged[k]=true;
985 }
986 vibing[k]=true;
987 break;
988 case 0xc: // panning
989 p->data[j][effectCol[k]++]=0x80;
990 if ((vol&15)==8) {
991 p->data[j][effectCol[k]++]=0x80;
992 } else {
993 p->data[j][effectCol[k]++]=(vol&15)|((vol&15)<<4);
994 }
995 writePanning=false;
996 break;
997 case 0xd: // pan slide left
998 if ((vol&15)!=0) {
999 panSlideStatus[k]&=0x0f;
1000 panSlideStatus[k]|=(vol&15)<<4;
1001 panSlideStatusChanged[k]=true;
1002 }
1003 panSliding[k]=true;
1004 break;
1005 case 0xe: // pan slide right
1006 if ((vol&15)!=0) {
1007 panSlideStatus[k]&=0xf0;
1008 panSlideStatus[k]|=vol&15;
1009 panSlideStatusChanged[k]=true;
1010 }
1011 panSliding[k]=true;
1012 break;
1013 case 0xf: // porta
1014 if ((vol&15)!=0) {
1015 portaStatus[k]=(vol&15)<<4;
1016 portaStatusChanged[k]=true;
1017 }
1018 if (portaType[k]!=3 || (hasNote && note>0)) {
1019 portaStatusChanged[k]=true;
1020 }
1021 portaType[k]=3;
1022 porting[k]=true;
1023 break;
1024 }
1025 }
1026 }
1027 if (hasEffect) {
1028 effect=reader.readC();
1029 }
1030 if (hasEffectVal) {
1031 effectVal=reader.readC();
1032 }
1033
1034 if (hasEffect || (hasEffectVal && effectVal!=0)) {
1035 switch (effect) {
1036 case 0: // arp
1037 if (effectVal!=0) {
1038 arpStatus[k]=(effectVal>>4)|(effectVal<<4);
1039 arpStatusChanged[k]=true;
1040 }
1041 arping[k]=true;
1042 break;
1043 case 1: // pitch up
1044 if (effectVal!=0) {
1045 portaStatus[k]=effectVal;
1046 portaStatusChanged[k]=true;
1047 }
1048 if (portaType[k]!=1) {
1049 portaStatusChanged[k]=true;
1050 }
1051 portaType[k]=1;
1052 porting[k]=true;
1053 break;
1054 case 2: // pitch down
1055 if (effectVal!=0) {
1056 portaStatus[k]=effectVal;
1057 portaStatusChanged[k]=true;
1058 }
1059 if (portaType[k]!=2) {
1060 portaStatusChanged[k]=true;
1061 }
1062 portaType[k]=2;
1063 porting[k]=true;
1064 break;
1065 case 3: // porta
1066 if (effectVal!=0) {
1067 portaStatus[k]=effectVal;
1068 portaStatusChanged[k]=true;
1069 }
1070 if (portaType[k]!=3 || (hasNote && note>0)) {
1071 portaStatusChanged[k]=true;
1072 }
1073 portaType[k]=3;
1074 porting[k]=true;
1075 break;
1076 case 4: // vibrato
1077 if (effectVal!=0) {
1078 if ((effectVal&0xf0)==0) { // only change depth
1079 vibStatus[k]&=0xf0;
1080 vibStatus[k]|=effectVal&0x0f;
1081 } else if ((effectVal&0x0f)==0) { // only change speed
1082 vibStatus[k]&=0x0f;
1083 vibStatus[k]|=effectVal&0xf0;
1084 } else {
1085 vibStatus[k]=effectVal;
1086 }
1087 vibStatusChanged[k]=true;
1088 }
1089 vibing[k]=true;
1090 break;
1091 case 5: // vol slide + porta
1092 if (effectVal!=0) {
1093 volSlideStatus[k]=effectVal;
1094 volSlideStatusChanged[k]=true;
1095 }
1096 volSliding[k]=true;
1097 porting[k]=true;
1098 portaType[k]=3;
1099 break;
1100 case 6: // vol slide + vibrato
1101 if (effectVal!=0) {
1102 volSlideStatus[k]=effectVal;
1103 volSlideStatusChanged[k]=true;
1104 }
1105 volSliding[k]=true;
1106 vibing[k]=true;
1107 break;
1108 case 7: // tremolo
1109 if (effectVal!=0) {
1110 tremStatus[k]=effectVal;
1111 tremStatusChanged[k]=true;
1112 }
1113 treming[k]=true;
1114 break;
1115 case 8: // panning
1116 p->data[j][effectCol[k]++]=0x80;
1117 p->data[j][effectCol[k]++]=effectVal;
1118 writePanning=false;
1119 break;
1120 case 9: // offset
1121 if (hasNote) {
1122 p->data[j][effectCol[k]++]=0x91;
1123 p->data[j][effectCol[k]++]=effectVal;
1124 }
1125 break;
1126 case 0xa: // vol slide
1127 if (effectVal!=0) {
1128 volSlideStatus[k]=effectVal;
1129 volSlideStatusChanged[k]=true;
1130 }
1131 if (hasNote || hasIns) {
1132 volSlideStatusChanged[k]=true;
1133 }
1134 volSliding[k]=true;
1135 break;
1136 case 0xb: // go to order
1137 p->data[j][effectCol[k]++]=0x0b;
1138 p->data[j][effectCol[k]++]=effectVal;
1139 break;
1140 case 0xc: // set volume
1141 p->data[j][3]=effectVal;
1142 break;
1143 case 0xd: // next order
1144 p->data[j][effectCol[k]++]=0x0d;
1145 p->data[j][effectCol[k]++]=effectVal;
1146 break;
1147 case 0xe: // special...
1148 // TODO: implement the rest
1149 switch (effectVal>>4) {
1150 case 0x4: // vibrato waveform
1151 switch (effectVal&3) {
1152 case 0x0: // sine
1153 p->data[j][effectCol[k]++]=0xe3;
1154 p->data[j][effectCol[k]++]=0x00;
1155 break;
1156 case 0x1: // ramp down
1157 p->data[j][effectCol[k]++]=0xe3;
1158 p->data[j][effectCol[k]++]=0x05;
1159 break;
1160 case 0x2: // square
1161 case 0x3:
1162 p->data[j][effectCol[k]++]=0xe3;
1163 p->data[j][effectCol[k]++]=0x06;
1164 break;
1165 }
1166 break;
1167 case 0x5: // fine tune
1168 p->data[j][effectCol[k]++]=0xe5;
1169 p->data[j][effectCol[k]++]=(effectVal&15)<<4;
1170 break;
1171 case 0x9: // retrigger
1172 p->data[j][effectCol[k]++]=0x0c;
1173 p->data[j][effectCol[k]++]=(effectVal&15);
1174 break;
1175 case 0xa: // vol slide up (fine)
1176 volSlideStatus[k]=((effectVal&15)<<4)|0xf;
1177 volSlideStatusChanged[k]=true;
1178 if (hasNote || hasIns) {
1179 volSlideStatusChanged[k]=true;
1180 }
1181 volSliding[k]=true;
1182 break;
1183 case 0xb: // vol slide down (fine)
1184 volSlideStatus[k]=0xf0|(effectVal&15);
1185 volSlideStatusChanged[k]=true;
1186 if (hasNote || hasIns) {
1187 volSlideStatusChanged[k]=true;
1188 }
1189 volSliding[k]=true;
1190 break;
1191 case 0xc: // note cut
1192 p->data[j][effectCol[k]++]=0xdc;
1193 p->data[j][effectCol[k]++]=MAX(1,effectVal&15);
1194 break;
1195 case 0xd: // note delay
1196 p->data[j][effectCol[k]++]=0xed;
1197 p->data[j][effectCol[k]++]=MAX(1,effectVal&15);
1198 break;
1199 }
1200 break;
1201 case 0xf: // speed/tempo
1202 if (effectVal>=0x20) {
1203 p->data[j][effectCol[k]++]=0xf0;
1204 } else if (effectVal==0) {
1205 p->data[j][effectCol[k]++]=0xff;
1206 } else {
1207 p->data[j][effectCol[k]++]=0x0f;
1208 }
1209 p->data[j][effectCol[k]++]=effectVal;
1210 break;
1211 case 0x10: // G: global volume (!)
1212 break;
1213 case 0x11: // H: global volume slide (!)
1214 break;
1215 case 0x14: // K: key off
1216 p->data[j][effectCol[k]++]=0xe7;
1217 p->data[j][effectCol[k]++]=effectVal;
1218 break;
1219 case 0x15: // L: set envelope position (!)
1220 break;
1221 case 0x19: // P: pan slide
1222 if (effectVal!=0) {
1223 panSlideStatus[k]=effectVal;
1224 panSlideStatusChanged[k]=true;
1225 }
1226 panSliding[k]=true;
1227 break;
1228 case 0x1b: // R: retrigger
1229 p->data[j][effectCol[k]++]=0x0c;
1230 p->data[j][effectCol[k]++]=effectVal;
1231 break;
1232 case 0x1d: // T: tremor (!)
1233 break;
1234 case 0x21: // X: extra fine volume
1235 break;
1236 case 0x22: // Y: panbrello (extension)
1237 if (effectVal!=0) {
1238 panStatus[k]=effectVal;
1239 panStatusChanged[k]=true;
1240 }
1241 panning[k]=true;
1242 break;
1243 }
1244 }
1245
1246 if (writePanning && hasNote && note<96 && ins>0) {
1247 p->data[j][effectCol[k]++]=0x80;
1248 p->data[j][effectCol[k]++]=samplePan[(((ins-1)&255)<<8)|(noteMap[(((ins-1)&255)<<7)|(note&127)])];
1249 }
1250 }
1251
1252 // commit effects
1253 for (int k=0; k<totalChans; k++) {
1254 DivPattern* p=ds.subsong[0]->pat[k].getPattern(i,true);
1255 if (vibing[k]!=vibingOld[k] || vibStatusChanged[k]) {
1256 p->data[j][effectCol[k]++]=0x04;
1257 p->data[j][effectCol[k]++]=vibing[k]?vibStatus[k]:0;
1258 doesVibrato[k]=true;
1259 } else if (doesVibrato[k] && mustCommitInitial) {
1260 p->data[j][effectCol[k]++]=0x04;
1261 p->data[j][effectCol[k]++]=0;
1262 }
1263
1264 if (volSliding[k]!=volSlidingOld[k] || volSlideStatusChanged[k]) {
1265 if (volSlideStatus[k]>=0xf1 && volSliding[k]) {
1266 p->data[j][effectCol[k]++]=0xf9;
1267 p->data[j][effectCol[k]++]=volSlideStatus[k]&15;
1268 volSliding[k]=false;
1269 } else if ((volSlideStatus[k]&15)==15 && volSlideStatus[k]>=0x10 && volSliding[k]) {
1270 p->data[j][effectCol[k]++]=0xf8;
1271 p->data[j][effectCol[k]++]=volSlideStatus[k]>>4;
1272 volSliding[k]=false;
1273 } else {
1274 p->data[j][effectCol[k]++]=0xfa;
1275 p->data[j][effectCol[k]++]=volSliding[k]?volSlideStatus[k]:0;
1276 }
1277 doesVolSlide[k]=true;
1278 } else if (doesVolSlide[k] && mustCommitInitial) {
1279 p->data[j][effectCol[k]++]=0xfa;
1280 p->data[j][effectCol[k]++]=0;
1281 }
1282
1283 if (porting[k]!=portingOld[k] || portaStatusChanged[k]) {
1284 p->data[j][effectCol[k]++]=portaType[k];
1285 p->data[j][effectCol[k]++]=porting[k]?portaStatus[k]:0;
1286 doesPitchSlide[k]=true;
1287 } else if (doesPitchSlide[k] && mustCommitInitial) {
1288 p->data[j][effectCol[k]++]=0x01;
1289 p->data[j][effectCol[k]++]=0;
1290 }
1291
1292 if (arping[k]!=arpingOld[k] || arpStatusChanged[k]) {
1293 p->data[j][effectCol[k]++]=0x00;
1294 p->data[j][effectCol[k]++]=arping[k]?arpStatus[k]:0;
1295 doesArp[k]=true;
1296 } else if (doesArp[k] && mustCommitInitial) {
1297 p->data[j][effectCol[k]++]=0x00;
1298 p->data[j][effectCol[k]++]=0;
1299 }
1300
1301 if (treming[k]!=tremingOld[k] || tremStatusChanged[k]) {
1302 p->data[j][effectCol[k]++]=0x07;
1303 p->data[j][effectCol[k]++]=treming[k]?tremStatus[k]:0;
1304 doesTremolo[k]=true;
1305 } else if (doesTremolo[k] && mustCommitInitial) {
1306 p->data[j][effectCol[k]++]=0x07;
1307 p->data[j][effectCol[k]++]=0;
1308 }
1309
1310 if (panning[k]!=panningOld[k] || panStatusChanged[k]) {
1311 p->data[j][effectCol[k]++]=0x84;
1312 p->data[j][effectCol[k]++]=panning[k]?panStatus[k]:0;
1313 doesPanbrello[k]=true;
1314 } else if (doesPanbrello[k] && mustCommitInitial) {
1315 p->data[j][effectCol[k]++]=0x84;
1316 p->data[j][effectCol[k]++]=0;
1317 }
1318
1319 if (panSliding[k]!=panSlidingOld[k] || panSlideStatusChanged[k]) {
1320 p->data[j][effectCol[k]++]=0x83;
1321 p->data[j][effectCol[k]++]=panSliding[k]?panSlideStatus[k]:0;
1322 doesPanSlide[k]=true;
1323 } else if (doesPanSlide[k] && mustCommitInitial) {
1324 p->data[j][effectCol[k]++]=0x83;
1325 p->data[j][effectCol[k]++]=0;
1326 }
1327
1328 if ((effectCol[k]>>1)-2>ds.subsong[0]->pat[k].effectCols) {
1329 ds.subsong[0]->pat[k].effectCols=(effectCol[k]>>1)-1;
1330 }
1331 }
1332
1333 memcpy(vibingOld,vibing,64*sizeof(bool));
1334 memcpy(volSlidingOld,volSliding,64*sizeof(bool));
1335 memcpy(portingOld,porting,64*sizeof(bool));
1336 memcpy(arpingOld,arping,64*sizeof(bool));
1337 memset(vibStatusChanged,0,64*sizeof(bool));
1338 memset(volSlideStatusChanged,0,64*sizeof(bool));
1339 memset(portaStatusChanged,0,64*sizeof(bool));
1340 memset(arpStatusChanged,0,64*sizeof(bool));
1341 memset(vibing,0,64*sizeof(bool));
1342 memset(volSliding,0,64*sizeof(bool));
1343 memset(porting,0,64*sizeof(bool));
1344 memset(arping,0,64*sizeof(bool));
1345 mustCommitInitial=false;
1346 if (j==totalRows-1) {
1347 // place end of pattern marker
1348 DivPattern* p=ds.subsong[0]->pat[0].getPattern(i,true);
1349 p->data[j][effectCol[0]++]=0x0d;
1350 p->data[j][effectCol[0]++]=0;
1351
1352 if ((effectCol[0]>>1)-2>ds.subsong[0]->pat[0].effectCols) {
1353 ds.subsong[0]->pat[0].effectCols=(effectCol[0]>>1)-1;
1354 }
1355 }
1356 memset(effectCol,4,64);
1357 }
1358
1359 logV("seeking to %x...",packedSeek);
1360 if (!reader.seek(packedSeek,SEEK_SET)) {
1361 logE("premature end of file!");
1362 lastError="incomplete file";
1363 delete[] file;
1364 XM_FINISH;
1365 return false;
1366 }
1367 }
1368
1369 ds.sampleLen=ds.sample.size();
1370 if (ds.sampleLen>32768) {
1371 logE("too many samples!");
1372 lastError="too many samples";
1373 ds.unload();
1374 delete[] file;
1375 XM_FINISH;
1376 return false;
1377 }
1378
1379 // set channel visibility
1380 for (int i=totalChans; i<((totalChans+32)&(~31)); i++) {
1381 ds.subsong[0]->chanShow[i]=false;
1382 ds.subsong[0]->chanShowChanOsc[i]=false;
1383 }
1384
1385 // find subsongs
1386 ds.findSubSongs(totalChans);
1387
1388 if (active) quitDispatch();
1389 BUSY_BEGIN_SOFT;
1390 saveLock.lock();
1391 song.unload();
1392 song=ds;
1393 changeSong(0);
1394 recalcChans();
1395 saveLock.unlock();
1396 BUSY_END;
1397 if (active) {
1398 initDispatch();
1399 BUSY_BEGIN;
1400 renderSamples();
1401 reset();
1402 BUSY_END;
1403 }
1404 success=true;
1405 } catch (EndOfFileException& e) {
1406 //logE("premature end of file!");
1407 lastError="incomplete file";
1408 } catch (InvalidHeaderException& e) {
1409 //logE("invalid header!");
1410 lastError="invalid header!";
1411 }
1412 XM_FINISH;
1413 return success;
1414 }
1415