Debian Codegraph @

src/engine/fileOps/s3m.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 static void readSbiOpData(sbi_t& sbi, SafeReader& reader) {
23 sbi.Mcharacteristics = reader.readC();
24 sbi.Ccharacteristics = reader.readC();
25 sbi.Mscaling_output = reader.readC();
26 sbi.Cscaling_output = reader.readC();
27 sbi.Meg_AD = reader.readC();
28 sbi.Ceg_AD = reader.readC();
29 sbi.Meg_SR = reader.readC();
30 sbi.Ceg_SR = reader.readC();
31 sbi.Mwave = reader.readC();
32 sbi.Cwave = reader.readC();
33 sbi.FeedConnect = reader.readC();
34 }
35
36 bool DivEngine::loadS3M(unsigned char* file, size_t len) {
37 struct InvalidHeaderException {};
38 bool success=false;
39 bool opl2=!getConfInt("s3mOPL3",0);
40 char magic[4]={0,0,0,0};
41 SafeReader reader=SafeReader(file,len);
42 warnings="";
43
44 unsigned char chanSettings[32];
45 unsigned int insPtr[256];
46 unsigned int patPtr[256];
47 unsigned char chanPan[16];
48 unsigned char chanMap[32];
49 unsigned char defVol[256];
50
51 unsigned char orders[256];
52
53 bool doesPitchSlide[32];
54 bool doesVibrato[32];
55 bool doesPanning[32];
56 bool doesVolSlide[32];
57 bool doesPanSlide[32];
58 bool doesArp[32];
59 bool doesTremolo[32];
60 bool doesPanbrello[32];
61
62 memset(chanSettings,0,32);
63 memset(insPtr,0,256*sizeof(unsigned int));
64 memset(patPtr,0,256*sizeof(unsigned int));
65 memset(chanPan,0,16);
66 memset(chanMap,0,32);
67 memset(defVol,0,256);
68 memset(orders,0,256);
69
70 memset(doesPitchSlide,0,32*sizeof(bool));
71 memset(doesVibrato,0,32*sizeof(bool));
72 memset(doesPanning,0,32*sizeof(bool));
73 memset(doesVolSlide,0,32*sizeof(bool));
74 memset(doesPanSlide,0,32*sizeof(bool));
75 memset(doesArp,0,32*sizeof(bool));
76 memset(doesTremolo,0,32*sizeof(bool));
77 memset(doesPanbrello,0,32*sizeof(bool));
78
79 try {
80 DivSong ds;
81 ds.version=DIV_VERSION_S3M;
82 ds.linearPitch=0;
83 ds.pitchMacroIsLinear=false;
84 ds.noSlidesOnFirstTick=true;
85 ds.rowResetsArpPos=true;
86 ds.ignoreJumpAtEnd=false;
87 ds.pitchSlideSpeed=12;
88
89 logV("Scream Tracker 3 module");
90
91 // load here
92 if (!reader.seek(0x2c,SEEK_SET)) {
93 logE("premature end of file!");
94 lastError="incomplete file";
95 delete[] file;
96 return false;
97 }
98 reader.read(magic,4);
99
100 if (memcmp(magic,DIV_S3M_MAGIC,4)!=0) {
101 logW("the magic isn't complete");
102 throw EndOfFileException(&reader,reader.tell());
103 }
104
105 if (!reader.seek(0,SEEK_SET)) {
106 logE("premature end of file!");
107 lastError="incomplete file";
108 delete[] file;
109 return false;
110 }
111
112 ds.name=reader.readStringLatin1(28);
113
114 reader.readC(); // 0x1a
115 if (reader.readC()!=16) {
116 logW("type is wrong!");
117 }
118 reader.readS(); // x
119
120 unsigned short ordersLen=reader.readS();
121 ds.insLen=reader.readS();
122
123 if (ordersLen>256) {
124 logE("invalid order count!");
125 lastError="invalid order count!";
126 delete[] file;
127 return false;
128 }
129
130 logV("orders: %d",ordersLen);
131 logV("instruments: %d",ds.insLen);
132
133 if (ds.insLen<0 || ds.insLen>256) {
134 logE("invalid instrument count!");
135 lastError="invalid instrument count!";
136 delete[] file;
137 return false;
138 }
139
140 unsigned short patCount=reader.readS();
141
142 logV("patterns: %d",patCount);
143
144 if (patCount>256) {
145 logE("invalid pattern count!");
146 lastError="invalid pattern count!";
147 delete[] file;
148 return false;
149 }
150
151 unsigned short flags=reader.readS();
152 unsigned short version=reader.readS();
153 bool signedSamples=(reader.readS()==1);
154
155 logV("flags: %x",flags);
156 logV("version: %x",version);
157 if (signedSamples) {
158 logV("signed samples: yes");
159 } else {
160 logV("signed samples: no");
161 }
162
163 /*if ((flags&64) || version==0x1300) {
164 ds.noSlidesOnFirstTick=false;
165 }*/
166
167 reader.readI(); // "SCRM"
168
169 unsigned char globalVol=reader.readC();
170
171 ds.subsong[0]->speeds.val[0]=(unsigned char)reader.readC();
172 ds.subsong[0]->speeds.len=1;
173 unsigned char tempo=reader.readC();
174 ds.subsong[0]->hz=((double)tempo)/2.5;
175
176 unsigned char masterVol=reader.readC();
177
178 logV("masterVol: %d",masterVol);
179 logV("signedSamples: %d",signedSamples);
180 logV("globalVol: %d",globalVol);
181
182 reader.readC(); // UC
183 unsigned char defaultPan=(unsigned char)reader.readC();
184
185 logV("defaultPan: %d",defaultPan);
186
187 reader.readS(); // reserved
188 reader.readI();
189 reader.readI(); // the last 2 bytes is Special. we don't read that.
190
191 reader.read(chanSettings,32);
192
193 logD("channel settings:");
194 for (int i=0; i<32; i++) {
195 logV("%d. %x",i,chanSettings[i]);
196 }
197
198 logD("reading orders...");
199 size_t curSubSong=0;
200 int curOrder1=0;
201 ds.subsong[curSubSong]->ordersLen=0;
202 bool subSongIncreased=false;
203 for (int i=0; i<ordersLen; i++) {
204 unsigned char nextOrder=reader.readC();
205 orders[i]=curOrder1;
206
207 // skip +++ order
208 if (nextOrder==254) {
209 logV("- +++");
210 continue;
211 }
212 // next subsong
213 if (nextOrder==255) {
214 logV("- end of song");
215 if (!subSongIncreased) {
216 curSubSong++;
217 subSongIncreased=true;
218 }
219 curOrder1=0;
220 continue;
221 }
222 subSongIncreased=false;
223 if (ds.subsong.size()<=curSubSong) {
224 ds.subsong.push_back(new DivSubSong);
225 ds.subsong[curSubSong]->ordersLen=0;
226 ds.subsong[curSubSong]->name=fmt::sprintf("Order %.2X",i);
227 }
228 logV("- %.2x",nextOrder);
229 for (int j=0; j<DIV_MAX_CHANS; j++) {
230 ds.subsong[curSubSong]->orders.ord[j][ds.subsong[curSubSong]->ordersLen]=nextOrder;
231 }
232 ds.subsong[curSubSong]->ordersLen++;
233 curOrder1++;
234 }
235
236 logD("reading ins pointers...");
237 for (int i=0; i<ds.insLen; i++) {
238 insPtr[i]=((unsigned short)reader.readS())*16;
239 logV("- %.2x",insPtr[i]);
240 }
241
242 logD("reading pat pointers...");
243 for (int i=0; i<patCount; i++) {
244 patPtr[i]=((unsigned short)reader.readS())*16;
245 logV("- %.2x",patPtr[i]);
246 }
247
248 if (defaultPan==252) {
249 logV("reading channel panning settings");
250 reader.read(chanPan,16);
251 } else {
252 logV("default channel pan");
253 memset(chanPan,16,16);
254 }
255
256 // determine chips to use
257 ds.systemLen=0;
258
259 bool hasPCM=false;
260 bool hasFM=false;
261 int numChans=0;
262 int realNumChans=0;
263
264 for (int ch=0; ch<32; ch++) {
265 if (chanSettings[ch]!=255) realNumChans++;
266 }
267
268 for (int ch=0; ch<32; ch++) {
269 if (chanSettings[ch]==255) continue;
270 if ((chanSettings[ch]&127)>=32) continue;
271 if ((chanSettings[ch]&127)>=16) {
272 hasFM=true;
273 } else {
274 hasPCM=true;
275 numChans++;
276 }
277
278 if (hasFM && hasPCM) break;
279 }
280
281 int pcmChan=hasFM?(opl2 ? 9 : 18):0;
282 int fmChan=hasPCM?32:0;
283 int invalidChan=40;
284
285 for (int ch=0; ch<32; ch++) {
286 if (chanSettings[ch]==255) {
287 chanMap[ch]=invalidChan++;
288 continue;
289 }
290 if ((chanSettings[ch]&127)>=32) {
291 chanMap[ch]=invalidChan++;
292 continue;
293 }
294 if ((chanSettings[ch]&127)>=16) {
295 chanMap[ch]=fmChan++;
296 } else {
297 chanMap[ch]=pcmChan++;
298 }
299 }
300
301 char buffer[40];
302 int chanIndex = 1;
303
304 if (hasPCM) {
305 for(int ch = 0; ch < pcmChan - (realNumChans - (hasFM ? 9 : 0)); ch++)
306 {
307 ds.subsong[0]->chanShow[ch]=false;
308 ds.subsong[0]->chanShowChanOsc[ch]=false;
309 }
310
311 for (int ch=pcmChan; ch<32; ch++) {
312 ds.subsong[0]->chanShow[ch]=false;
313 ds.subsong[0]->chanShowChanOsc[ch]=false;
314 }
315
316 for(int ch = 0; ch < 32; ch++)
317 {
318 if(ds.subsong[0]->chanShow[ch])
319 {
320 snprintf(buffer, 40, _("Channel %d"), chanIndex);
321 ds.subsong[0]->chanName[ch] = buffer;
322 chanIndex++;
323 }
324 }
325 }
326
327 if (hasFM && !opl2) {
328 for (int ch=(hasPCM?32:0) + 9; ch<(hasPCM?32:0) + 18; ch++) {
329 ds.subsong[0]->chanShow[ch]=false;
330 ds.subsong[0]->chanShowChanOsc[ch]=false;
331 }
332
333 chanIndex = 1;
334
335 for (int ch=(hasPCM?32:0); ch<(hasPCM?32:0) + 9; ch++) {
336 snprintf(buffer, 40, _("FM %d"), chanIndex);
337 ds.subsong[0]->chanName[ch] = buffer;
338 chanIndex++;
339 }
340 }
341
342 logV("numChans: %d",numChans);
343 logV("realNumChans: %d",realNumChans);
344
345 ds.systemName="PC";
346 if (hasPCM) {
347 ds.system[ds.systemLen]=DIV_SYSTEM_ES5506;
348 ds.systemVol[ds.systemLen]=(float)globalVol/64.0;
349 ds.systemPan[ds.systemLen]=0;
350 ds.systemFlags[ds.systemLen].set("volScale",3900);
351 ds.systemFlags[ds.systemLen].set("amigaVol",true);
352 ds.systemFlags[ds.systemLen].set("amigaPitch",true);
353 ds.systemLen++;
354 }
355 if (hasFM) {
356 ds.system[ds.systemLen]=opl2 ? DIV_SYSTEM_OPL2 : DIV_SYSTEM_OPL3;
357 ds.systemVol[ds.systemLen]=1.0f;
358 ds.systemPan[ds.systemLen]=0;
359 ds.systemLen++;
360 }
361
362 // load instruments/samples
363 ds.ins.reserve(ds.insLen);
364 for (int i=0; i<ds.insLen; i++) {
365 logV("reading instrument %d...",i);
366 DivInstrument* ins=new DivInstrument;
367 if (insPtr[i]==0) {
368 ins->type=DIV_INS_ES5506;
369 ds.ins.push_back(ins);
370 DivSample* emptySample=new DivSample;
371 ds.sample.push_back(emptySample);
372 continue;
373 }
374
375 if (!reader.seek(insPtr[i]+0x4c,SEEK_SET)) {
376 logE("premature end of file!");
377 lastError="incomplete file";
378 delete ins;
379 delete[] file;
380 return false;
381 }
382
383 reader.read(magic,4);
384
385 if (memcmp(magic,"SCRS",4)==0) {
386 ins->type=DIV_INS_ES5506;
387 } else if (memcmp(magic,"SCRI",4)==0) {
388 ins->type=DIV_INS_OPL;
389 } else {
390 logW("odd magic!");
391 ins->type=DIV_INS_ES5506;
392
393 // read the instrument name anyway
394 if (!reader.seek(insPtr[i]+48,SEEK_SET)) {
395 logE("premature end of file!");
396 lastError="incomplete file";
397 delete ins;
398 delete[] file;
399 return false;
400 }
401
402 String name=reader.readStringLatin1(28);
403 ins->name=name;
404
405 ds.ins.push_back(ins);
406 continue;
407 }
408
409 if (!reader.seek(insPtr[i],SEEK_SET)) {
410 logE("premature end of file!");
411 lastError="incomplete file";
412 delete ins;
413 delete[] file;
414 return false;
415 }
416
417 unsigned char type=reader.readC();
418
419 if (ins->type==DIV_INS_ES5506) {
420 if (type>1) {
421 logE("invalid instrument type! %d",type);
422 lastError="invalid instrument!";
423 delete ins;
424 delete[] file;
425 return false;
426 }
427 } else {
428 if (type<2) {
429 logE("invalid instrument type! %d",type);
430 lastError="invalid instrument!";
431 delete ins;
432 delete[] file;
433 return false;
434 }
435 }
436
437 String dosName=reader.readStringLatin1(12);
438
439 if (ins->type==DIV_INS_ES5506) {
440 unsigned int memSeg=0;
441 memSeg=(unsigned char)reader.readC();
442 memSeg|=((unsigned short)reader.readS())<<8;
443
444 memSeg>>=4; // ???
445
446 logV("memSeg: %x",memSeg);
447
448 unsigned int length=reader.readI();
449
450 if (length>16777216) {
451 logE("abnormal sample size! %x",reader.tell());
452 lastError="bad sample size";
453 delete[] file;
454 return false;
455 }
456
457 DivSample* s=new DivSample;
458
459 s->loopStart=reader.readI();
460 s->loopEnd=reader.readI();
461 defVol[i]=reader.readC();
462
463 reader.readC(); // x
464
465 bool isPacked=reader.readC();
466 unsigned char flags=reader.readC();
467
468 s->centerRate=reader.readI();
469
470 // reserved
471 reader.readI(); // x
472 reader.readI();
473 reader.readI();
474
475 String name=reader.readStringLatin1(28);
476 s->name=dosName;
477 ins->name=name;
478
479 // "SCRS"
480 reader.readI();
481
482 // read sample data
483 if (!reader.seek(memSeg,SEEK_SET)) {
484 logE("premature end of file!");
485 lastError="incomplete file";
486 delete ins;
487 delete s;
488 delete[] file;
489 return false;
490 }
491
492 s->loop=flags&1;
493 s->depth=(flags&4)?DIV_SAMPLE_DEPTH_16BIT:DIV_SAMPLE_DEPTH_8BIT;
494 if (length>0) {
495 s->init(length);
496 }
497
498 if (isPacked) {
499 logE("ADPCM not supported!");
500 lastError="ADPCM sample";
501 delete ins;
502 delete s;
503 delete[] file;
504 return false;
505 }
506
507 if (flags&2) {
508 // downmix stereo
509 if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
510 for (unsigned int i=0; i<s->samples; i++) {
511 short l=reader.readS();
512 if (!signedSamples) {
513 l^=0x8000;
514 }
515 s->data16[i]=l;
516 }
517 for (unsigned int i=0; i<s->samples; i++) {
518 short r=reader.readS();
519 if (!signedSamples) {
520 r^=0x8000;
521 }
522 s->data16[i]=(s->data16[i]+r)>>1;
523 }
524 } else {
525 for (unsigned int i=0; i<s->samples; i++) {
526 signed char l=reader.readC();
527 if (!signedSamples) {
528 l^=0x80;
529 }
530 s->data8[i]=l;
531 }
532 for (unsigned int i=0; i<s->samples; i++) {
533 signed char r=reader.readC();
534 if (!signedSamples) {
535 r^=0x80;
536 }
537 s->data8[i]=(s->data8[i]+r)>>1;
538 }
539 }
540 } else {
541 if (s->depth==DIV_SAMPLE_DEPTH_16BIT) {
542 for (unsigned int i=0; i<s->samples; i++) {
543 s->data16[i]=reader.readS();
544 if (!signedSamples) {
545 s->data16[i]^=0x8000;
546 }
547 }
548 } else {
549 for (unsigned int i=0; i<s->samples; i++) {
550 s->data8[i]=reader.readC();
551 if (!signedSamples) {
552 s->data8[i]^=0x80;
553 }
554 }
555 }
556 }
557
558 ins->amiga.initSample=ds.sample.size();
559 ds.sample.push_back(s);
560 } else {
561 ins->fm.ops=2;
562
563 // reserved
564 reader.readC();
565 reader.readC();
566 reader.readC();
567
568 // OPL data
569 sbi_t s3i;
570 readSbiOpData(s3i,reader);
571
572 // taken from S3I loading code
573 DivInstrumentFM::Operator& opM=ins->fm.op[0];
574 DivInstrumentFM::Operator& opC=ins->fm.op[1];
575 ins->fm.ops = 2;
576 opM.mult=s3i.Mcharacteristics&15;
577 opM.ksr=((s3i.Mcharacteristics>>4)&1);
578 opM.sus=((s3i.Mcharacteristics>>5)&1);
579 opM.vib=((s3i.Mcharacteristics>>6)&1);
580 opM.am=((s3i.Mcharacteristics>>7)&1);
581 opM.tl=s3i.Mscaling_output&63;
582 opM.ksl=((s3i.Mscaling_output>>6)&3);
583 opM.ar=((s3i.Meg_AD >> 4)&15);
584 opM.dr=(s3i.Meg_AD&15);
585 opM.rr=(s3i.Meg_SR&15);
586 opM.sl=((s3i.Meg_SR>>4)&15);
587 opM.ws=s3i.Mwave;
588
589 ins->fm.alg=(s3i.FeedConnect&1);
590 ins->fm.fb=((s3i.FeedConnect>>1)&7);
591
592 opC.mult=s3i.Ccharacteristics&15;
593 opC.ksr=((s3i.Ccharacteristics>>4)&1);
594 opC.sus=((s3i.Ccharacteristics>>5)&1);
595 opC.vib=((s3i.Ccharacteristics>>6)&1);
596 opC.am=((s3i.Ccharacteristics>>7)&1);
597 opC.tl=s3i.Cscaling_output&63;
598 opC.ksl=((s3i.Cscaling_output>>6)&3);
599 opC.ar=((s3i.Ceg_AD>>4)&15);
600 opC.dr=(s3i.Ceg_AD&15);
601 opC.rr=(s3i.Ceg_SR&15);
602 opC.sl=((s3i.Ceg_SR>>4)&15);
603 opC.ws=s3i.Cwave;
604
605 // unused
606 reader.readC();
607
608 defVol[i]=reader.readC();
609 // what?
610 unsigned char dsk=reader.readC();
611
612 // x
613 reader.readS();
614
615 // C-2 speed - convert to macro
616 int centerRate=reader.readI();
617 double centerNote=12.0*log2((double)centerRate/8363.0);
618 if (round(centerNote)!=0) {
619 ins->std.arpMacro.len=1;
620 ins->std.arpMacro.val[0]=round(centerNote);
621 }
622 logV("centerRate: %d (%f)",centerRate,centerNote);
623
624 // x
625 reader.seek(12,SEEK_CUR);
626
627 String name=reader.readStringLatin1(28);
628 ins->name=name;
629
630 // "SCRI"
631 reader.readI();
632
633 logV("dsk: %d",dsk);
634 }
635
636 ds.ins.push_back(ins);
637 }
638 ds.sampleLen=ds.sample.size();
639
640 // scan pattern data for effect use
641 for (int i=0; i<patCount; i++) {
642 logV("scanning pattern %d...",i);
643 if (!reader.seek(patPtr[i],SEEK_SET)) {
644 logE("premature end of file!");
645 lastError="incomplete file";
646 ds.unload();
647 delete[] file;
648 return false;
649 }
650
651 unsigned short dataLen=reader.readS();
652 unsigned int dataEnd=reader.tell()+dataLen;
653 int readRow=0;
654
655 while (reader.tell()<dataEnd) {
656 unsigned char what=reader.readC();
657
658 if (what==0) {
659 readRow++;
660 if (readRow>=64) break;
661 continue;
662 }
663
664 unsigned char chan=what&31;
665 bool hasNoteIns=what&32;
666 bool hasVol=what&64;
667 bool hasEffect=what&128;
668
669 if (hasNoteIns) {
670 reader.readC();
671 reader.readC();
672 }
673 if (hasVol) {
674 reader.readC();
675 }
676 if (hasEffect) {
677 unsigned char effect=reader.readC();
678 reader.readC(); // effect val
679
680 switch (effect+'A'-1) {
681 case 'D': // vol slide
682 doesVolSlide[chan]=true;
683 break;
684 case 'E': // pitch down
685 doesPitchSlide[chan]=true;
686 break;
687 case 'F': // pitch up
688 doesPitchSlide[chan]=true;
689 break;
690 case 'G': // porta
691 doesPitchSlide[chan]=true;
692 break;
693 case 'H': // vibrato
694 doesVibrato[chan]=true;
695 break;
696 case 'J': // arp
697 doesArp[chan]=true;
698 break;
699 case 'K': // vol slide + vibrato
700 doesVolSlide[chan]=true;
701 doesVibrato[chan]=true;
702 break;
703 case 'L': // vol slide + porta
704 doesVolSlide[chan]=true;
705 doesPitchSlide[chan]=true;
706 break;
707 case 'P': // pan slide
708 doesPanSlide[chan]=true;
709 break;
710 case 'R': // tremolo
711 doesTremolo[chan]=true;
712 break;
713 case 'Y': // panbrello
714 doesPanbrello[chan]=true;
715 break;
716 }
717 }
718 }
719 }
720
721 // load pattern data
722 for (int i=0; i<patCount; i++) {
723 unsigned char effectCol[32];
724 unsigned char vibStatus[32];
725 bool vibStatusChanged[32];
726 bool vibing[32];
727 bool vibingOld[32];
728 unsigned char volSlideStatus[32];
729 bool volSlideStatusChanged[32];
730 bool volSliding[32];
731 bool volSlidingOld[32];
732 unsigned char portaStatus[32];
733 bool portaStatusChanged[32];
734 bool porting[32];
735 bool portingOld[32];
736 unsigned char portaType[32];
737 unsigned char arpStatus[32];
738 bool arpStatusChanged[32];
739 bool arping[32];
740 bool arpingOld[32];
741 unsigned char panStatus[32];
742 bool panStatusChanged[32];
743 bool panning[32];
744 bool panningOld[32];
745 unsigned char tremStatus[32];
746 bool tremStatusChanged[32];
747 bool treming[32];
748 bool tremingOld[32];
749 unsigned char panSlideStatus[32];
750 bool panSlideStatusChanged[32];
751 bool panSliding[32];
752 bool panSlidingOld[32];
753 bool did[32];
754
755 if (patPtr[i]==0) continue;
756
757 logV("reading pattern %d...",i);
758 if (!reader.seek(patPtr[i],SEEK_SET)) {
759 logE("premature end of file!");
760 lastError="incomplete file";
761 ds.unload();
762 delete[] file;
763 return false;
764 }
765
766 unsigned short dataLen=reader.readS();
767 unsigned int dataEnd=reader.tell()+dataLen;
768 int readRow=0;
769
770 logV("length: %d",dataLen);
771
772 bool mustCommitInitial=true;
773
774 memset(effectCol,4,32);
775 memset(vibStatus,0,32);
776 memset(vibStatusChanged,0,32*sizeof(bool));
777 memset(vibing,0,32*sizeof(bool));
778 memset(vibingOld,0,32*sizeof(bool));
779 memset(volSlideStatus,0,32);
780 memset(volSlideStatusChanged,0,32*sizeof(bool));
781 memset(volSliding,0,32*sizeof(bool));
782 memset(volSlidingOld,0,32*sizeof(bool));
783 memset(portaStatus,0,32);
784 memset(portaStatusChanged,0,32*sizeof(bool));
785 memset(porting,0,32*sizeof(bool));
786 memset(portingOld,0,32*sizeof(bool));
787 memset(portaType,0,32);
788 memset(arpStatus,0,32);
789 memset(arpStatusChanged,0,32*sizeof(bool));
790 memset(arping,0,32*sizeof(bool));
791 memset(arpingOld,0,32*sizeof(bool));
792 memset(panStatus,0,32);
793 memset(panStatusChanged,0,32*sizeof(bool));
794 memset(panning,0,32*sizeof(bool));
795 memset(panningOld,0,32*sizeof(bool));
796 memset(tremStatus,0,32);
797 memset(tremStatusChanged,0,32*sizeof(bool));
798 memset(treming,0,32*sizeof(bool));
799 memset(tremingOld,0,32*sizeof(bool));
800 memset(panSlideStatus,0,32);
801 memset(panSlideStatusChanged,0,32*sizeof(bool));
802 memset(panSliding,0,32*sizeof(bool));
803 memset(panSlidingOld,0,32*sizeof(bool));
804 memset(did,0,32*sizeof(bool));
805
806 while (reader.tell()<dataEnd) {
807 unsigned char what=reader.readC();
808
809 if (what==0) {
810 // commit effects
811 for (int j=0; j<32; j++) {
812 DivPattern* p=ds.subsong[0]->pat[chanMap[j]].getPattern(i,true);
813 if (vibing[j]!=vibingOld[j] || vibStatusChanged[j]) {
814 p->data[readRow][effectCol[j]++]=0x04;
815 p->data[readRow][effectCol[j]++]=vibing[j]?vibStatus[j]:0;
816 doesVibrato[j]=true;
817 } else if (doesVibrato[j] && mustCommitInitial) {
818 p->data[readRow][effectCol[j]++]=0x04;
819 p->data[readRow][effectCol[j]++]=0;
820 }
821
822 if (volSliding[j]!=volSlidingOld[j] || volSlideStatusChanged[j]) {
823 if (volSlideStatus[j]>=0xf1 && volSliding[j]) {
824 p->data[readRow][effectCol[j]++]=0xf9;
825 p->data[readRow][effectCol[j]++]=volSlideStatus[j]&15;
826 volSliding[j]=false;
827 } else if ((volSlideStatus[j]&15)==15 && volSlideStatus[j]>=0x10 && volSliding[j]) {
828 p->data[readRow][effectCol[j]++]=0xf8;
829 p->data[readRow][effectCol[j]++]=volSlideStatus[j]>>4;
830 volSliding[j]=false;
831 } else {
832 p->data[readRow][effectCol[j]++]=0xfa;
833 p->data[readRow][effectCol[j]++]=volSliding[j]?volSlideStatus[j]:0;
834 }
835 doesVolSlide[j]=true;
836 } else if (doesVolSlide[j] && mustCommitInitial) {
837 p->data[readRow][effectCol[j]++]=0xfa;
838 p->data[readRow][effectCol[j]++]=0;
839 }
840
841 if (porting[j]!=portingOld[j] || portaStatusChanged[j]) {
842 if (portaStatus[j]>=0xe0 && portaType[j]!=3 && porting[j]) {
843 p->data[readRow][effectCol[j]++]=portaType[j]|0xf0;
844 p->data[readRow][effectCol[j]++]=(portaStatus[j]&15)*((portaStatus[j]>=0xf0)?1:1);
845 porting[j]=false;
846 } else {
847 p->data[readRow][effectCol[j]++]=portaType[j];
848 p->data[readRow][effectCol[j]++]=porting[j]?portaStatus[j]:0;
849 }
850 doesPitchSlide[j]=true;
851 } else if (doesPitchSlide[j] && mustCommitInitial) {
852 p->data[readRow][effectCol[j]++]=0x01;
853 p->data[readRow][effectCol[j]++]=0;
854 }
855
856 if (arping[j]!=arpingOld[j] || arpStatusChanged[j]) {
857 p->data[readRow][effectCol[j]++]=0x00;
858 p->data[readRow][effectCol[j]++]=arping[j]?arpStatus[j]:0;
859 doesArp[j]=true;
860 } else if (doesArp[j] && mustCommitInitial) {
861 p->data[readRow][effectCol[j]++]=0x00;
862 p->data[readRow][effectCol[j]++]=0;
863 }
864
865 if (treming[j]!=tremingOld[j] || tremStatusChanged[j]) {
866 p->data[readRow][effectCol[j]++]=0x07;
867 p->data[readRow][effectCol[j]++]=treming[j]?tremStatus[j]:0;
868 doesTremolo[j]=true;
869 } else if (doesTremolo[j] && mustCommitInitial) {
870 p->data[readRow][effectCol[j]++]=0x07;
871 p->data[readRow][effectCol[j]++]=0;
872 }
873
874 if (panning[j]!=panningOld[j] || panStatusChanged[j]) {
875 p->data[readRow][effectCol[j]++]=0x84;
876 p->data[readRow][effectCol[j]++]=panning[j]?panStatus[j]:0;
877 doesPanbrello[j]=true;
878 } else if (doesPanbrello[j] && mustCommitInitial) {
879 p->data[readRow][effectCol[j]++]=0x84;
880 p->data[readRow][effectCol[j]++]=0;
881 }
882
883 if (panSliding[j]!=panSlidingOld[j] || panSlideStatusChanged[j]) {
884 p->data[readRow][effectCol[j]++]=0x83;
885 p->data[readRow][effectCol[j]++]=panSliding[j]?panSlideStatus[j]:0;
886 doesPanSlide[j]=true;
887 } else if (doesPanSlide[j] && mustCommitInitial) {
888 p->data[readRow][effectCol[j]++]=0x83;
889 p->data[readRow][effectCol[j]++]=0;
890 }
891
892 if (effectCol[j]>=4+8*2) {
893 logE("oh crap!");
894 }
895
896 if ((effectCol[j]>>1)-2>ds.subsong[0]->pat[j].effectCols) {
897 ds.subsong[0]->pat[chanMap[j]].effectCols=(effectCol[j]>>1)-1;
898 }
899 }
900
901 readRow++;
902 memset(effectCol,4,32);
903 memcpy(vibingOld,vibing,32*sizeof(bool));
904 memcpy(volSlidingOld,volSliding,32*sizeof(bool));
905 memcpy(portingOld,porting,32*sizeof(bool));
906 memcpy(arpingOld,arping,32*sizeof(bool));
907 memset(vibStatusChanged,0,32*sizeof(bool));
908 memset(volSlideStatusChanged,0,32*sizeof(bool));
909 memset(portaStatusChanged,0,32*sizeof(bool));
910 memset(arpStatusChanged,0,32*sizeof(bool));
911 memset(vibing,0,32*sizeof(bool));
912 memset(volSliding,0,32*sizeof(bool));
913 memset(porting,0,32*sizeof(bool));
914 memset(arping,0,32*sizeof(bool));
915 memset(did,0,32);
916 mustCommitInitial=false;
917 if (readRow>=64) break;
918 continue;
919 }
920
921 unsigned char chan=what&31;
922 bool hasNoteIns=what&32;
923 bool hasVol=what&64;
924 bool hasEffect=what&128;
925
926 if (did[chan]) {
927 logW("pat %d chan %d row %d: we already populated this channel!",i,chan,readRow);
928 } else {
929 did[chan]=true;
930 }
931
932 DivPattern* p=ds.subsong[0]->pat[chanMap[chan]].getPattern(i,true);
933 if (hasNoteIns) {
934 unsigned char note=reader.readC();
935 unsigned char ins=reader.readC();
936
937 if (note==254) { // note off
938 p->data[readRow][0]=100;
939 p->data[readRow][1]=0;
940 } else if (note!=255) {
941 p->data[readRow][0]=note&15;
942 p->data[readRow][1]=note>>4;
943 if ((note&15)==0) {
944 p->data[readRow][0]=12;
945 p->data[readRow][1]--;
946 }
947 }
948 p->data[readRow][2]=(short)ins-1;
949 }
950 if (hasVol) {
951 unsigned char vol=reader.readC();
952 if (vol==255) {
953 p->data[readRow][3]=-1;
954 } else {
955 // check for OPL channel
956 if ((chanSettings[chan]&31)>=16) {
957 if (vol>63) vol=63;
958 } else {
959 if (vol>64) vol=64;
960 }
961 p->data[readRow][3]=vol;
962 }
963 } else if (p->data[readRow][2]!=-1) {
964 // populate with instrument volume
965 unsigned char vol=defVol[p->data[readRow][2]&255];
966 if ((chanSettings[chan]&31)>=16) {
967 if (vol>63) vol=63;
968 } else {
969 if (vol>64) vol=64;
970 }
971 p->data[readRow][3]=vol;
972 }
973 if (hasEffect) {
974 unsigned char effect=reader.readC();
975 unsigned char effectVal=reader.readC();
976
977 switch (effect+'A'-1) {
978 case 'A': // speed
979 p->data[readRow][effectCol[chan]++]=0x0f;
980 p->data[readRow][effectCol[chan]++]=effectVal;
981 break;
982 case 'B': // go to order
983 p->data[readRow][effectCol[chan]++]=0x0b;
984 logD("0B: %x %x",effectVal,orders[effectVal]);
985 p->data[readRow][effectCol[chan]++]=orders[effectVal];
986 break;
987 case 'C': // next order
988 p->data[readRow][effectCol[chan]++]=0x0d;
989 p->data[readRow][effectCol[chan]++]=(effectVal>>4)*10+(effectVal&15);
990 break;
991 case 'D': // vol slide
992 if (effectVal!=0) {
993 volSlideStatus[chan]=effectVal;
994 volSlideStatusChanged[chan]=true;
995 }
996 if (hasNoteIns) {
997 volSlideStatusChanged[chan]=true;
998 }
999 volSliding[chan]=true;
1000 break;
1001 case 'E': // pitch down
1002 if (effectVal!=0) {
1003 portaStatus[chan]=effectVal;
1004 portaStatusChanged[chan]=true;
1005 }
1006 if (hasNoteIns) {
1007 portaStatusChanged[chan]=true;
1008 }
1009 if (portaType[chan]!=2) {
1010 portaStatusChanged[chan]=true;
1011 }
1012 portaType[chan]=2;
1013 porting[chan]=true;
1014 break;
1015 case 'F': // pitch up
1016 if (effectVal!=0) {
1017 portaStatus[chan]=effectVal;
1018 portaStatusChanged[chan]=true;
1019 }
1020 if (hasNoteIns) {
1021 portaStatusChanged[chan]=true;
1022 }
1023 if (portaType[chan]!=1) {
1024 portaStatusChanged[chan]=true;
1025 }
1026 portaType[chan]=1;
1027 porting[chan]=true;
1028 break;
1029 case 'G': // porta
1030 if (effectVal!=0) {
1031 portaStatus[chan]=effectVal;
1032 portaStatusChanged[chan]=true;
1033 }
1034 if (hasNoteIns) {
1035 portaStatusChanged[chan]=true;
1036 }
1037 if (portaType[chan]!=3) {
1038 portaStatusChanged[chan]=true;
1039 }
1040 portaType[chan]=3;
1041 porting[chan]=true;
1042 break;
1043 case 'H': // vibrato
1044 if (effectVal!=0) {
1045 vibStatus[chan]=effectVal;
1046 vibStatusChanged[chan]=true;
1047 }
1048 vibing[chan]=true;
1049 break;
1050 case 'I': // tremor (!)
1051 break;
1052 case 'J': // arp
1053 if (effectVal!=0) {
1054 arpStatus[chan]=effectVal;
1055 arpStatusChanged[chan]=true;
1056 }
1057 arping[chan]=true;
1058 break;
1059 case 'K': // vol slide + vibrato
1060 if (effectVal!=0) {
1061 volSlideStatus[chan]=effectVal;
1062 volSlideStatusChanged[chan]=true;
1063 }
1064 volSliding[chan]=true;
1065 vibing[chan]=true;
1066 break;
1067 case 'L': // vol slide + porta
1068 if (effectVal!=0) {
1069 volSlideStatus[chan]=effectVal;
1070 volSlideStatusChanged[chan]=true;
1071 }
1072 volSliding[chan]=true;
1073 porting[chan]=true;
1074 portaType[chan]=3;
1075 break;
1076 case 'M': // channel vol (extension)
1077 break;
1078 case 'N': // channel vol slide (extension)
1079 break;
1080 case 'O': // offset
1081 p->data[readRow][effectCol[chan]++]=0x91;
1082 p->data[readRow][effectCol[chan]++]=effectVal;
1083 break;
1084 case 'P': // pan slide (extension)
1085 if (effectVal!=0) {
1086 panSlideStatus[chan]=effectVal;
1087 panSlideStatusChanged[chan]=true;
1088 }
1089 panSliding[chan]=true;
1090 break;
1091 case 'Q': // retrigger
1092 p->data[readRow][effectCol[chan]++]=0x0c;
1093 p->data[readRow][effectCol[chan]++]=effectVal&15;
1094 break;
1095 case 'R': // tremolo
1096 if (effectVal!=0) {
1097 tremStatus[chan]=effectVal;
1098 tremStatusChanged[chan]=true;
1099 }
1100 treming[chan]=true;
1101 break;
1102 case 'S': // special...
1103 switch (effectVal>>4) {
1104 case 0x3: // vibrato waveform
1105 switch (effectVal&3) {
1106 case 0x0: // sine
1107 p->data[readRow][effectCol[chan]++]=0xe3;
1108 p->data[readRow][effectCol[chan]++]=0x00;
1109 break;
1110 case 0x1: // ramp down
1111 p->data[readRow][effectCol[chan]++]=0xe3;
1112 p->data[readRow][effectCol[chan]++]=0x05;
1113 break;
1114 case 0x2: // square
1115 p->data[readRow][effectCol[chan]++]=0xe3;
1116 p->data[readRow][effectCol[chan]++]=0x06;
1117 break;
1118 case 0x3: // random
1119 p->data[readRow][effectCol[chan]++]=0xe3;
1120 p->data[readRow][effectCol[chan]++]=0x07;
1121 break;
1122 }
1123 break;
1124 case 0x8: // panning
1125 p->data[readRow][effectCol[chan]++]=0x80;
1126 p->data[readRow][effectCol[chan]++]=(effectVal&15)<<4;
1127 break;
1128 case 0xc: // note cut
1129 p->data[readRow][effectCol[chan]++]=0xec;
1130 p->data[readRow][effectCol[chan]++]=effectVal&15;
1131 break;
1132 case 0xd: // note delay
1133 p->data[readRow][effectCol[chan]++]=0xed;
1134 p->data[readRow][effectCol[chan]++]=effectVal&15;
1135 break;
1136 }
1137 break;
1138 case 'T': // tempo
1139 p->data[readRow][effectCol[chan]++]=0xf0;
1140 p->data[readRow][effectCol[chan]++]=effectVal;
1141 break;
1142 case 'U': // fine vibrato
1143 if (effectVal!=0) {
1144 vibStatus[chan]=effectVal;
1145 vibStatusChanged[chan]=true;
1146 }
1147 vibing[chan]=true;
1148 break;
1149 case 'V': // global volume (!)
1150 break;
1151 case 'W': // global volume slide (!)
1152 break;
1153 case 'X': // panning (extension)
1154 if (effectVal<=0x80) {
1155 p->data[readRow][effectCol[chan]++]=0x80;
1156 p->data[readRow][effectCol[chan]++]=(effectVal&0x80)?0xff:(effectVal<<1);
1157 }
1158 break;
1159 case 'Y': // panbrello (extension)
1160 if (effectVal!=0) {
1161 panStatus[chan]=effectVal;
1162 panStatusChanged[chan]=true;
1163 }
1164 panning[chan]=true;
1165 break;
1166 case 'Z': // MIDI macro (extension)
1167 break;
1168 }
1169 }
1170 }
1171 }
1172
1173 // copy patterns to the rest of subsongs
1174 for (size_t i=1; i<ds.subsong.size(); i++) {
1175 for (int j=0; j<DIV_MAX_CHANS; j++) {
1176 for (int k=0; k<patCount; k++) {
1177 if (ds.subsong[0]->pat[j].data[k]) ds.subsong[0]->pat[j].data[k]->copyOn(ds.subsong[i]->pat[j].getPattern(k,true));
1178 }
1179 ds.subsong[i]->pat[j].effectCols=ds.subsong[0]->pat[j].effectCols;
1180 }
1181 ds.subsong[i]->speeds=ds.subsong[0]->speeds;
1182 ds.subsong[i]->hz=ds.subsong[0]->hz;
1183 memcpy(ds.subsong[i]->chanShow,ds.subsong[0]->chanShow,DIV_MAX_CHANS*sizeof(bool));
1184 memcpy(ds.subsong[i]->chanShowChanOsc,ds.subsong[0]->chanShowChanOsc,DIV_MAX_CHANS*sizeof(bool));
1185 }
1186
1187 // find subsongs
1188 ds.findSubSongs(DIV_MAX_CHANS);
1189
1190 // populate subsongs with default panning values
1191 if (masterVol&128) { // only in stereo mode
1192 for (size_t i=0; i<ds.subsong.size(); i++) {
1193 for (int j=0; j<16; j++) {
1194 DivPattern* p=ds.subsong[i]->pat[chanMap[j]].getPattern(ds.subsong[i]->orders.ord[j][0],true);
1195 for (int k=0; k<DIV_MAX_EFFECTS; k++) {
1196 if (p->data[0][4+(k<<1)]==0x80) {
1197 // give up if there's a panning effect already
1198 break;
1199 }
1200 if (p->data[0][4+(k<<1)]==-1) {
1201 p->data[0][4+(k<<1)]=0x80;
1202 if (chanPan[j]&16) {
1203 p->data[0][5+(k<<1)]=(j&1)?0xcc:0x33;
1204 } else {
1205 p->data[0][5+(k<<1)]=(chanPan[j]&15)|((chanPan[j]&15)<<4);
1206 }
1207 if (ds.subsong[i]->pat[chanMap[j]].effectCols<=k) ds.subsong[i]->pat[chanMap[j]].effectCols=k+1;
1208 break;
1209 }
1210 }
1211 }
1212 }
1213 }
1214
1215 ds.insLen=ds.ins.size();
1216 ds.sampleLen=ds.sample.size();
1217
1218 if (active) quitDispatch();
1219 BUSY_BEGIN_SOFT;
1220 saveLock.lock();
1221 song.unload();
1222 song=ds;
1223 changeSong(0);
1224 recalcChans();
1225 saveLock.unlock();
1226 BUSY_END;
1227 if (active) {
1228 initDispatch();
1229 BUSY_BEGIN;
1230 renderSamples();
1231 reset();
1232 BUSY_END;
1233 }
1234 success=true;
1235 } catch (EndOfFileException& e) {
1236 //logE("premature end of file!");
1237 lastError="incomplete file";
1238 } catch (InvalidHeaderException& e) {
1239 //logE("invalid header!");
1240 lastError="invalid header!";
1241 }
1242 return success;
1243 }
1244