Debian Codegraph @

src/engine/fileOps/fur.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 short newFormatNotes[180]={
23 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // -5
24 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // -4
25 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // -3
26 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // -2
27 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // -1
28 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 0
29 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 1
30 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 2
31 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 3
32 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 4
33 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 5
34 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 6
35 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 7
36 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 8
37 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 // 9
38 };
39
40 short newFormatOctaves[180]={
41 250, 251, 251, 251, 251, 251, 251, 251, 251, 251, 251, 251, // -5
42 251, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, // -4
43 252, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, // -3
44 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, // -2
45 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // -1
46 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0
47 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1
48 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 2
49 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 3
50 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 4
51 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // 5
52 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, // 6
53 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 7
54 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 8
55 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 9
56 };
57
58 struct PatToWrite {
59 unsigned short subsong, chan, pat;
60 PatToWrite(unsigned short s, unsigned short c, unsigned short p):
61 subsong(s),
62 chan(c),
63 pat(p) {}
64 };
65
66 void DivEngine::putAssetDirData(SafeWriter* w, std::vector<DivAssetDir>& dir) {
67 size_t blockStartSeek, blockEndSeek;
68
69 w->write("ADIR",4);
70 blockStartSeek=w->tell();
71 w->writeI(0);
72
73 w->writeI(dir.size());
74
75 for (DivAssetDir& i: dir) {
76 w->writeString(i.name,false);
77 w->writeS(i.entries.size());
78 for (int j: i.entries) {
79 w->writeC(j);
80 }
81 }
82
83 blockEndSeek=w->tell();
84 w->seek(blockStartSeek,SEEK_SET);
85 w->writeI(blockEndSeek-blockStartSeek-4);
86 w->seek(0,SEEK_END);
87 }
88
89 DivDataErrors DivEngine::readAssetDirData(SafeReader& reader, std::vector<DivAssetDir>& dir) {
90 char magic[4];
91 reader.read(magic,4);
92 if (memcmp(magic,"ADIR",4)!=0) {
93 logV("header is invalid: %c%c%c%c",magic[0],magic[1],magic[2],magic[3]);
94 return DIV_DATA_INVALID_HEADER;
95 }
96 reader.readI(); // reserved
97
98 unsigned int numDirs=reader.readI();
99
100 dir.reserve(numDirs);
101 for (unsigned int i=0; i<numDirs; i++) {
102 DivAssetDir d;
103
104 d.name=reader.readString();
105 unsigned short numEntries=reader.readS();
106
107 d.entries.reserve(numEntries);
108 for (unsigned short j=0; j<numEntries; j++) {
109 d.entries.push_back(((unsigned char)reader.readC()));
110 }
111
112 dir.push_back(d);
113 }
114
115 return DIV_DATA_SUCCESS;
116 }
117
118 void DivEngine::convertOldFlags(unsigned int oldFlags, DivConfig& newFlags, DivSystem sys) {
119 newFlags.clear();
120
121 switch (sys) {
122 case DIV_SYSTEM_SMS:
123 switch (oldFlags&0xff03) {
124 case 0x0000:
125 newFlags.set("clockSel",0);
126 break;
127 case 0x0001:
128 newFlags.set("clockSel",1);
129 break;
130 case 0x0002:
131 newFlags.set("clockSel",2);
132 break;
133 case 0x0003:
134 newFlags.set("clockSel",3);
135 break;
136 case 0x0100:
137 newFlags.set("clockSel",4);
138 break;
139 case 0x0101:
140 newFlags.set("clockSel",5);
141 break;
142 case 0x0102:
143 newFlags.set("clockSel",6);
144 break;
145 }
146 switch (oldFlags&0xcc) {
147 case 0x00:
148 newFlags.set("chipType",0);
149 break;
150 case 0x04:
151 newFlags.set("chipType",1);
152 break;
153 case 0x08:
154 newFlags.set("chipType",2);
155 break;
156 case 0x0c:
157 newFlags.set("chipType",3);
158 break;
159 case 0x40:
160 newFlags.set("chipType",4);
161 break;
162 case 0x44:
163 newFlags.set("chipType",5);
164 break;
165 case 0x48:
166 newFlags.set("chipType",6);
167 break;
168 case 0x4c:
169 newFlags.set("chipType",7);
170 break;
171 case 0x80:
172 newFlags.set("chipType",8);
173 break;
174 case 0x84:
175 newFlags.set("chipType",9);
176 break;
177 }
178 if (oldFlags&16) newFlags.set("noPhaseReset",true);
179 break;
180 case DIV_SYSTEM_GB:
181 newFlags.set("chipType",(int)(oldFlags&3));
182 if (oldFlags&8) newFlags.set("noAntiClick",true);
183 break;
184 case DIV_SYSTEM_PCE:
185 newFlags.set("clockSel",(int)(oldFlags&1));
186 newFlags.set("chipType",(oldFlags&4)?1:0);
187 if (oldFlags&8) newFlags.set("noAntiClick",true);
188 break;
189 case DIV_SYSTEM_NES:
190 case DIV_SYSTEM_VRC6:
191 case DIV_SYSTEM_FDS:
192 case DIV_SYSTEM_MMC5:
193 case DIV_SYSTEM_SAA1099:
194 case DIV_SYSTEM_OPZ:
195 switch (oldFlags) {
196 case 0:
197 newFlags.set("clockSel",0);
198 break;
199 case 1:
200 newFlags.set("clockSel",1);
201 break;
202 case 2:
203 newFlags.set("clockSel",2);
204 break;
205 }
206 break;
207 case DIV_SYSTEM_C64_6581:
208 case DIV_SYSTEM_C64_8580:
209 switch (oldFlags&15) {
210 case 0:
211 newFlags.set("clockSel",0);
212 break;
213 case 1:
214 newFlags.set("clockSel",1);
215 break;
216 case 2:
217 newFlags.set("clockSel",2);
218 break;
219 }
220 break;
221 case DIV_SYSTEM_YM2610:
222 case DIV_SYSTEM_YM2610_EXT:
223 case DIV_SYSTEM_YM2610_FULL:
224 case DIV_SYSTEM_YM2610_FULL_EXT:
225 case DIV_SYSTEM_YM2610B:
226 case DIV_SYSTEM_YM2610B_EXT:
227 switch (oldFlags&0xff) {
228 case 0:
229 newFlags.set("clockSel",0);
230 break;
231 case 1:
232 newFlags.set("clockSel",1);
233 break;
234 }
235 break;
236 case DIV_SYSTEM_AY8910:
237 case DIV_SYSTEM_AY8930:
238 switch (oldFlags&15) {
239 case 0:
240 newFlags.set("clockSel",0);
241 break;
242 case 1:
243 newFlags.set("clockSel",1);
244 break;
245 case 2:
246 newFlags.set("clockSel",2);
247 break;
248 case 3:
249 newFlags.set("clockSel",3);
250 break;
251 case 4:
252 newFlags.set("clockSel",4);
253 break;
254 case 5:
255 newFlags.set("clockSel",5);
256 break;
257 case 6:
258 newFlags.set("clockSel",6);
259 break;
260 case 7:
261 newFlags.set("clockSel",7);
262 break;
263 case 8:
264 newFlags.set("clockSel",8);
265 break;
266 case 9:
267 newFlags.set("clockSel",9);
268 break;
269 case 10:
270 newFlags.set("clockSel",10);
271 break;
272 case 11:
273 newFlags.set("clockSel",11);
274 break;
275 case 12:
276 newFlags.set("clockSel",12);
277 break;
278 case 13:
279 if (sys==DIV_SYSTEM_AY8910) newFlags.set("clockSel",13);
280 break;
281 case 14:
282 if (sys==DIV_SYSTEM_AY8910) newFlags.set("clockSel",14);
283 break;
284 }
285 if (sys==DIV_SYSTEM_AY8910) switch ((oldFlags>>4)&3) {
286 case 0:
287 newFlags.set("chipType",0);
288 break;
289 case 1:
290 newFlags.set("chipType",1);
291 break;
292 case 2:
293 newFlags.set("chipType",2);
294 break;
295 case 3:
296 newFlags.set("chipType",3);
297 break;
298 }
299 if (oldFlags&64) newFlags.set("stereo",true);
300 if (oldFlags&128) newFlags.set("halfClock",true);
301 newFlags.set("stereoSep",(int)((oldFlags>>8)&255));
302 break;
303 case DIV_SYSTEM_AMIGA:
304 if (oldFlags&1) newFlags.set("clockSel",1);
305 if (oldFlags&2) newFlags.set("chipType",1);
306 if (oldFlags&4) newFlags.set("bypassLimits",true);
307 newFlags.set("stereoSep",(int)((oldFlags>>8)&127));
308 break;
309 case DIV_SYSTEM_YM2151:
310 switch (oldFlags&255) {
311 case 0:
312 newFlags.set("clockSel",0);
313 break;
314 case 1:
315 newFlags.set("clockSel",1);
316 break;
317 case 2:
318 newFlags.set("clockSel",2);
319 break;
320 }
321 break;
322 case DIV_SYSTEM_YM2612:
323 case DIV_SYSTEM_YM2612_EXT:
324 case DIV_SYSTEM_YM2612_DUALPCM:
325 case DIV_SYSTEM_YM2612_DUALPCM_EXT:
326 switch (oldFlags&0x7fffffff) {
327 case 0:
328 newFlags.set("clockSel",0);
329 break;
330 case 1:
331 newFlags.set("clockSel",1);
332 break;
333 case 2:
334 newFlags.set("clockSel",2);
335 break;
336 case 3:
337 newFlags.set("clockSel",3);
338 break;
339 case 4:
340 newFlags.set("clockSel",4);
341 break;
342 }
343 if (oldFlags&0x80000000) newFlags.set("ladderEffect",true);
344 break;
345 case DIV_SYSTEM_TIA:
346 newFlags.set("clockSel",(int)(oldFlags&1));
347 switch ((oldFlags>>1)&3) {
348 case 0:
349 newFlags.set("mixingType",0);
350 break;
351 case 1:
352 newFlags.set("mixingType",1);
353 break;
354 case 2:
355 newFlags.set("mixingType",2);
356 break;
357 }
358 break;
359 case DIV_SYSTEM_VIC20:
360 newFlags.set("clockSel",(int)(oldFlags&1));
361 break;
362 case DIV_SYSTEM_SNES:
363 newFlags.set("volScaleL",(int)(oldFlags&127));
364 newFlags.set("volScaleR",(int)((oldFlags>>8)&127));
365 break;
366 case DIV_SYSTEM_OPLL:
367 case DIV_SYSTEM_OPLL_DRUMS:
368 switch (oldFlags&15) {
369 case 0:
370 newFlags.set("clockSel",0);
371 break;
372 case 1:
373 newFlags.set("clockSel",1);
374 break;
375 case 2:
376 newFlags.set("clockSel",2);
377 break;
378 case 3:
379 newFlags.set("clockSel",3);
380 break;
381 }
382 switch (oldFlags>>4) {
383 case 0:
384 newFlags.set("patchSet",0);
385 break;
386 case 1:
387 newFlags.set("patchSet",1);
388 break;
389 case 2:
390 newFlags.set("patchSet",2);
391 break;
392 case 3:
393 newFlags.set("patchSet",3);
394 break;
395 }
396 break;
397 case DIV_SYSTEM_N163:
398 switch (oldFlags&15) {
399 case 0:
400 newFlags.set("clockSel",0);
401 break;
402 case 1:
403 newFlags.set("clockSel",1);
404 break;
405 case 2:
406 newFlags.set("clockSel",2);
407 break;
408 }
409 newFlags.set("channels",(int)((oldFlags>>4)&7));
410 if (oldFlags&128) newFlags.set("multiplex",true);
411 break;
412 case DIV_SYSTEM_YM2203:
413 case DIV_SYSTEM_YM2203_EXT:
414 switch (oldFlags&31) {
415 case 0:
416 newFlags.set("clockSel",0);
417 break;
418 case 1:
419 newFlags.set("clockSel",1);
420 break;
421 case 2:
422 newFlags.set("clockSel",2);
423 break;
424 case 3:
425 newFlags.set("clockSel",3);
426 break;
427 case 4:
428 newFlags.set("clockSel",4);
429 break;
430 case 5:
431 newFlags.set("clockSel",5);
432 break;
433 }
434 switch ((oldFlags>>5)&3) {
435 case 0:
436 newFlags.set("prescale",0);
437 break;
438 case 1:
439 newFlags.set("prescale",1);
440 break;
441 case 2:
442 newFlags.set("prescale",2);
443 break;
444 }
445 break;
446 case DIV_SYSTEM_YM2608:
447 case DIV_SYSTEM_YM2608_EXT:
448 switch (oldFlags&31) {
449 case 0:
450 newFlags.set("clockSel",0);
451 break;
452 case 1:
453 newFlags.set("clockSel",1);
454 break;
455 }
456 switch ((oldFlags>>5)&3) {
457 case 0:
458 newFlags.set("prescale",0);
459 break;
460 case 1:
461 newFlags.set("prescale",1);
462 break;
463 case 2:
464 newFlags.set("prescale",2);
465 break;
466 }
467 break;
468 case DIV_SYSTEM_OPL:
469 case DIV_SYSTEM_OPL2:
470 case DIV_SYSTEM_Y8950:
471 case DIV_SYSTEM_OPL_DRUMS:
472 case DIV_SYSTEM_OPL2_DRUMS:
473 case DIV_SYSTEM_Y8950_DRUMS:
474 case DIV_SYSTEM_YMZ280B:
475 switch (oldFlags&0xff) {
476 case 0:
477 newFlags.set("clockSel",0);
478 break;
479 case 1:
480 newFlags.set("clockSel",1);
481 break;
482 case 2:
483 newFlags.set("clockSel",2);
484 break;
485 case 3:
486 newFlags.set("clockSel",3);
487 break;
488 case 4:
489 newFlags.set("clockSel",4);
490 break;
491 case 5:
492 newFlags.set("clockSel",5);
493 break;
494 }
495 break;
496 case DIV_SYSTEM_OPL3:
497 case DIV_SYSTEM_OPL3_DRUMS:
498 switch (oldFlags&0xff) {
499 case 0:
500 newFlags.set("clockSel",0);
501 break;
502 case 1:
503 newFlags.set("clockSel",1);
504 break;
505 case 2:
506 newFlags.set("clockSel",2);
507 break;
508 case 3:
509 newFlags.set("clockSel",3);
510 break;
511 case 4:
512 newFlags.set("clockSel",4);
513 break;
514 }
515 break;
516 case DIV_SYSTEM_PCSPKR:
517 newFlags.set("speakerType",(int)(oldFlags&3));
518 break;
519 case DIV_SYSTEM_RF5C68:
520 switch (oldFlags&15) {
521 case 0:
522 newFlags.set("clockSel",0);
523 break;
524 case 1:
525 newFlags.set("clockSel",1);
526 break;
527 case 2:
528 newFlags.set("clockSel",2);
529 break;
530 }
531 switch (oldFlags>>4) {
532 case 0:
533 newFlags.set("chipType",0);
534 break;
535 case 1:
536 newFlags.set("chipType",1);
537 break;
538 }
539 break;
540 case DIV_SYSTEM_VRC7:
541 switch (oldFlags&15) {
542 case 0:
543 newFlags.set("clockSel",0);
544 break;
545 case 1:
546 newFlags.set("clockSel",1);
547 break;
548 case 2:
549 newFlags.set("clockSel",2);
550 break;
551 case 3:
552 newFlags.set("clockSel",3);
553 break;
554 }
555 break;
556 case DIV_SYSTEM_SFX_BEEPER:
557 case DIV_SYSTEM_SFX_BEEPER_QUADTONE:
558 newFlags.set("clockSel",(int)(oldFlags&1));
559 break;
560 case DIV_SYSTEM_SCC:
561 case DIV_SYSTEM_SCC_PLUS:
562 switch (oldFlags&63) {
563 case 0:
564 newFlags.set("clockSel",0);
565 break;
566 case 1:
567 newFlags.set("clockSel",1);
568 break;
569 case 2:
570 newFlags.set("clockSel",2);
571 break;
572 case 3:
573 newFlags.set("clockSel",3);
574 break;
575 }
576 break;
577 case DIV_SYSTEM_MSM6295:
578 switch (oldFlags&63) {
579 case 0:
580 newFlags.set("clockSel",0);
581 break;
582 case 1:
583 newFlags.set("clockSel",1);
584 break;
585 case 2:
586 newFlags.set("clockSel",2);
587 break;
588 case 3:
589 newFlags.set("clockSel",3);
590 break;
591 case 4:
592 newFlags.set("clockSel",4);
593 break;
594 case 5:
595 newFlags.set("clockSel",5);
596 break;
597 case 6:
598 newFlags.set("clockSel",6);
599 break;
600 case 7:
601 newFlags.set("clockSel",7);
602 break;
603 case 8:
604 newFlags.set("clockSel",8);
605 break;
606 case 9:
607 newFlags.set("clockSel",9);
608 break;
609 case 10:
610 newFlags.set("clockSel",10);
611 break;
612 case 11:
613 newFlags.set("clockSel",11);
614 break;
615 case 12:
616 newFlags.set("clockSel",12);
617 break;
618 case 13:
619 newFlags.set("clockSel",13);
620 break;
621 case 14:
622 newFlags.set("clockSel",14);
623 break;
624 }
625 if (oldFlags&128) newFlags.set("rateSel",true);
626 break;
627 case DIV_SYSTEM_MSM6258:
628 switch (oldFlags) {
629 case 0:
630 newFlags.set("clockSel",0);
631 break;
632 case 1:
633 newFlags.set("clockSel",1);
634 break;
635 case 2:
636 newFlags.set("clockSel",2);
637 break;
638 case 3:
639 newFlags.set("clockSel",3);
640 break;
641 }
642 break;
643 case DIV_SYSTEM_OPL4:
644 case DIV_SYSTEM_OPL4_DRUMS:
645 switch (oldFlags&0xff) {
646 case 0:
647 newFlags.set("clockSel",0);
648 break;
649 case 1:
650 newFlags.set("clockSel",1);
651 break;
652 case 2:
653 newFlags.set("clockSel",2);
654 break;
655 }
656 break;
657 case DIV_SYSTEM_X1_010:
658 switch (oldFlags&15) {
659 case 0:
660 newFlags.set("clockSel",0);
661 break;
662 case 1:
663 newFlags.set("clockSel",1);
664 break;
665 }
666 if (oldFlags&16) newFlags.set("stereo",true);
667 break;
668 case DIV_SYSTEM_SOUND_UNIT:
669 newFlags.set("clockSel",(int)(oldFlags&1));
670 if (oldFlags&4) newFlags.set("echo",true);
671 if (oldFlags&8) newFlags.set("swapEcho",true);
672 newFlags.set("sampleMemSize",(int)((oldFlags>>4)&1));
673 if (oldFlags&32) newFlags.set("pdm",true);
674 newFlags.set("echoDelay",(int)((oldFlags>>8)&63));
675 newFlags.set("echoFeedback",(int)((oldFlags>>16)&15));
676 newFlags.set("echoResolution",(int)((oldFlags>>20)&15));
677 newFlags.set("echoVol",(int)((oldFlags>>24)&255));
678 break;
679 case DIV_SYSTEM_PCM_DAC:
680 if (!oldFlags) oldFlags=0x1f0000|44099;
681 newFlags.set("rate",(int)((oldFlags&0xffff)+1));
682 newFlags.set("outDepth",(int)((oldFlags>>16)&15));
683 if (oldFlags&0x100000) newFlags.set("stereo",true);
684 break;
685 case DIV_SYSTEM_QSOUND:
686 newFlags.set("echoDelay",(int)(oldFlags&0xfff));
687 newFlags.set("echoFeedback",(int)((oldFlags>>12)&255));
688 break;
689 default:
690 break;
691 }
692 }
693
694 bool DivEngine::loadFur(unsigned char* file, size_t len, int variantID) {
695 std::vector<unsigned int> insPtr;
696 std::vector<unsigned int> wavePtr;
697 std::vector<unsigned int> samplePtr;
698 unsigned int subSongPtr[256];
699 unsigned int sysFlagsPtr[DIV_MAX_CHIPS];
700 unsigned int assetDirPtr[3];
701 std::vector<unsigned int> patPtr;
702 int numberOfSubSongs=0;
703 char magic[5];
704 memset(magic,0,5);
705 SafeReader reader=SafeReader(file,len);
706 warnings="";
707 assetDirPtr[0]=0;
708 assetDirPtr[1]=0;
709 assetDirPtr[2]=0;
710 try {
711 DivSong ds;
712 DivSubSong* subSong=ds.subsong[0];
713
714 if (!reader.seek(16,SEEK_SET)) {
715 logE("premature end of file!");
716 lastError="incomplete file";
717 delete[] file;
718 return false;
719 }
720 ds.version=reader.readS();
721 logI("module version %d (0x%.2x)",ds.version,ds.version);
722
723 if (variantID!=DIV_FUR_VARIANT_VANILLA) {
724 logW("Furnace variant detected: %d",variantID);
725 addWarning("this module was created with a downstream version of Furnace. certain features may not be compatible.");
726 }
727
728 if (ds.version>DIV_ENGINE_VERSION) {
729 logW("this module was created with a more recent version of Furnace!");
730 addWarning("this module was created with a more recent version of Furnace!");
731 }
732
733 if (ds.version<37) { // compat flags not stored back then
734 ds.limitSlides=true;
735 ds.linearPitch=1;
736 ds.loopModality=0;
737 }
738 if (ds.version<43) {
739 ds.properNoiseLayout=false;
740 ds.waveDutyIsVol=false;
741 }
742 if (ds.version<45) {
743 ds.resetMacroOnPorta=true;
744 ds.legacyVolumeSlides=true;
745 ds.compatibleArpeggio=true;
746 ds.noteOffResetsSlides=true;
747 ds.targetResetsSlides=true;
748 }
749 if (ds.version<46) {
750 ds.arpNonPorta=true;
751 ds.algMacroBehavior=true;
752 } else {
753 ds.arpNonPorta=false;
754 ds.algMacroBehavior=false;
755 }
756 if (ds.version<49) {
757 ds.brokenShortcutSlides=true;
758 }
759 if (ds.version<50) {
760 ds.ignoreDuplicateSlides=false;
761 }
762 if (ds.version<62) {
763 ds.stopPortaOnNoteOff=true;
764 }
765 if (ds.version<64) {
766 ds.brokenDACMode=false;
767 }
768 if (ds.version<65) {
769 ds.oneTickCut=false;
770 }
771 if (ds.version<66) {
772 ds.newInsTriggersInPorta=false;
773 }
774 if (ds.version<69) {
775 ds.arp0Reset=false;
776 }
777 if (ds.version<71) {
778 ds.noSlidesOnFirstTick=false;
779 ds.rowResetsArpPos=false;
780 ds.ignoreJumpAtEnd=true;
781 }
782 if (ds.version<72) {
783 ds.buggyPortaAfterSlide=true;
784 ds.gbInsAffectsEnvelope=false;
785 }
786 if (ds.version<78) {
787 ds.sharedExtStat=false;
788 }
789 if (ds.version<83) {
790 ds.ignoreDACModeOutsideIntendedChannel=true;
791 ds.e1e2AlsoTakePriority=false;
792 }
793 if (ds.version<84) {
794 ds.newSegaPCM=false;
795 }
796 if (ds.version<85) {
797 ds.fbPortaPause=true;
798 }
799 if (ds.version<86) {
800 ds.snDutyReset=true;
801 }
802 if (ds.version<90) {
803 ds.pitchMacroIsLinear=false;
804 }
805 if (ds.version<97) {
806 ds.oldOctaveBoundary=true;
807 }
808 if (ds.version<97) { // actually should be 98 but yky uses this feature ahead of time
809 ds.noOPN2Vol=true;
810 }
811 if (ds.version<99) {
812 ds.newVolumeScaling=false;
813 ds.volMacroLinger=false;
814 ds.brokenOutVol=true;
815 }
816 if (ds.version<100) {
817 ds.e1e2StopOnSameNote=false;
818 }
819 if (ds.version<101) {
820 ds.brokenPortaArp=true;
821 }
822 if (ds.version<108) {
823 ds.snNoLowPeriods=true;
824 }
825 if (ds.version<110) {
826 ds.delayBehavior=1;
827 }
828 if (ds.version<113) {
829 ds.jumpTreatment=1;
830 }
831 if (ds.version<115) {
832 ds.autoSystem=false;
833 }
834 if (ds.version<117) {
835 ds.disableSampleMacro=true;
836 }
837 if (ds.version<121) {
838 ds.brokenOutVol2=false;
839 }
840 if (ds.version<130) {
841 ds.oldArpStrategy=true;
842 }
843 if (ds.version<138) {
844 ds.brokenPortaLegato=true;
845 }
846 if (ds.version<155) {
847 ds.brokenFMOff=true;
848 }
849 if (ds.version<168) {
850 ds.preNoteNoEffect=true;
851 }
852 if (ds.version<183) {
853 ds.oldDPCM=true;
854 }
855 if (ds.version<184) {
856 ds.resetArpPhaseOnNewNote=false;
857 }
858 if (ds.version<188) {
859 ds.ceilVolumeScaling=false;
860 }
861 if (ds.version<191) {
862 ds.oldAlwaysSetVolume=true;
863 }
864 if (ds.version<200) {
865 ds.oldSampleOffset=true;
866 }
867 ds.isDMF=false;
868
869 reader.readS(); // reserved
870 int infoSeek=reader.readI();
871
872 if (!reader.seek(infoSeek,SEEK_SET)) {
873 logE("couldn't seek to info header at %d!",infoSeek);
874 lastError="couldn't seek to info header!";
875 delete[] file;
876 return false;
877 }
878
879 // read header
880 reader.read(magic,4);
881 if (strcmp(magic,"INFO")!=0) {
882 logE("invalid info header!");
883 lastError="invalid info header!";
884 delete[] file;
885 return false;
886 }
887 reader.readI();
888
889 subSong->timeBase=reader.readC();
890 subSong->speeds.len=2;
891 subSong->speeds.val[0]=reader.readC();
892 subSong->speeds.val[1]=reader.readC();
893 subSong->arpLen=reader.readC();
894 subSong->hz=reader.readF();
895
896 subSong->patLen=reader.readS();
897 subSong->ordersLen=reader.readS();
898
899 subSong->hilightA=reader.readC();
900 subSong->hilightB=reader.readC();
901
902 ds.insLen=reader.readS();
903 ds.waveLen=reader.readS();
904 ds.sampleLen=reader.readS();
905 int numberOfPats=reader.readI();
906
907 if (subSong->patLen<0) {
908 logE("pattern length is negative!");
909 lastError="pattern lengrh is negative!";
910 delete[] file;
911 return false;
912 }
913 if (subSong->patLen>DIV_MAX_ROWS) {
914 logE("pattern length is too large!");
915 lastError="pattern length is too large!";
916 delete[] file;
917 return false;
918 }
919 if (subSong->ordersLen<0) {
920 logE("song length is negative!");
921 lastError="song length is negative!";
922 delete[] file;
923 return false;
924 }
925 if (subSong->ordersLen>DIV_MAX_PATTERNS) {
926 logE("song is too long!");
927 lastError="song is too long!";
928 delete[] file;
929 return false;
930 }
931 if (ds.insLen<0 || ds.insLen>256) {
932 logE("invalid instrument count!");
933 lastError="invalid instrument count!";
934 delete[] file;
935 return false;
936 }
937 if (ds.waveLen<0 || ds.waveLen>32768) {
938 logE("invalid wavetable count!");
939 lastError="invalid wavetable count!";
940 delete[] file;
941 return false;
942 }
943 if (ds.sampleLen<0 || ds.sampleLen>32768) {
944 logE("invalid sample count!");
945 lastError="invalid sample count!";
946 delete[] file;
947 return false;
948 }
949 if (numberOfPats<0) {
950 logE("invalid pattern count!");
951 lastError="invalid pattern count!";
952 delete[] file;
953 return false;
954 }
955
956 logD("systems:");
957 ds.systemLen=0;
958 for (int i=0; i<DIV_MAX_CHIPS; i++) {
959 unsigned char sysID=reader.readC();
960 ds.system[i]=systemFromFileFur(sysID);
961 logD("- %d: %.2x (%s)",i,sysID,getSystemName(ds.system[i]));
962 if (sysID!=0 && systemToFileFur(ds.system[i])==0) {
963 logE("unrecognized system ID %.2x",sysID);
964 lastError=fmt::sprintf("unrecognized system ID %.2x!",sysID);
965 delete[] file;
966 return false;
967 }
968 if (ds.system[i]!=DIV_SYSTEM_NULL) ds.systemLen=i+1;
969 }
970 int tchans=0;
971 for (int i=0; i<ds.systemLen; i++) {
972 tchans+=getChannelCount(ds.system[i]);
973 }
974 if (tchans>DIV_MAX_CHANS) {
975 tchans=DIV_MAX_CHANS;
976 logW("too many channels!");
977 }
978 logV("system len: %d",ds.systemLen);
979 if (ds.systemLen<1) {
980 logE("zero chips!");
981 lastError="zero chips!";
982 delete[] file;
983 return false;
984 }
985
986 // system volume
987 for (int i=0; i<DIV_MAX_CHIPS; i++) {
988 signed char oldSysVol=reader.readC();
989 ds.systemVol[i]=(float)oldSysVol/64.0f;
990 if (ds.version<59 && ds.system[i]==DIV_SYSTEM_NES) {
991 ds.systemVol[i]/=4;
992 }
993 }
994
995 // system panning
996 for (int i=0; i<DIV_MAX_CHIPS; i++) {
997 signed char oldSysPan=reader.readC();
998 ds.systemPan[i]=(float)oldSysPan/127.0f;
999 }
1000
1001 // system props
1002 for (int i=0; i<DIV_MAX_CHIPS; i++) {
1003 sysFlagsPtr[i]=reader.readI();
1004 }
1005
1006 // handle compound systems
1007 for (int i=0; i<DIV_MAX_CHIPS; i++) {
1008 if (ds.system[i]==DIV_SYSTEM_GENESIS ||
1009 ds.system[i]==DIV_SYSTEM_GENESIS_EXT ||
1010 ds.system[i]==DIV_SYSTEM_ARCADE) {
1011 for (int j=31; j>i; j--) {
1012 ds.system[j]=ds.system[j-1];
1013 ds.systemVol[j]=ds.systemVol[j-1];
1014 ds.systemPan[j]=ds.systemPan[j-1];
1015 }
1016 if (++ds.systemLen>DIV_MAX_CHIPS) ds.systemLen=DIV_MAX_CHIPS;
1017
1018 if (ds.system[i]==DIV_SYSTEM_GENESIS) {
1019 ds.system[i]=DIV_SYSTEM_YM2612;
1020 if (i<31) {
1021 ds.system[i+1]=DIV_SYSTEM_SMS;
1022 ds.systemVol[i+1]=ds.systemVol[i]*0.375f;
1023 }
1024 }
1025 if (ds.system[i]==DIV_SYSTEM_GENESIS_EXT) {
1026 ds.system[i]=DIV_SYSTEM_YM2612_EXT;
1027 if (i<31) {
1028 ds.system[i+1]=DIV_SYSTEM_SMS;
1029 ds.systemVol[i+1]=ds.systemVol[i]*0.375f;
1030 }
1031 }
1032 if (ds.system[i]==DIV_SYSTEM_ARCADE) {
1033 ds.system[i]=DIV_SYSTEM_YM2151;
1034 if (i<31) {
1035 ds.system[i+1]=DIV_SYSTEM_SEGAPCM_COMPAT;
1036 }
1037 }
1038 i++;
1039 }
1040 }
1041
1042 ds.name=reader.readString();
1043 ds.author=reader.readString();
1044 logI("%s by %s",ds.name.c_str(),ds.author.c_str());
1045
1046 if (ds.version>=33) {
1047 ds.tuning=reader.readF();
1048 } else {
1049 reader.readI();
1050 }
1051
1052 // compatibility flags
1053 if (ds.version>=37) {
1054 ds.limitSlides=reader.readC();
1055 ds.linearPitch=reader.readC();
1056 ds.loopModality=reader.readC();
1057 if (ds.version>=43) {
1058 ds.properNoiseLayout=reader.readC();
1059 } else {
1060 reader.readC();
1061 }
1062 if (ds.version>=43) {
1063 ds.waveDutyIsVol=reader.readC();
1064 } else {
1065 reader.readC();
1066 }
1067
1068 if (ds.version>=45) {
1069 ds.resetMacroOnPorta=reader.readC();
1070 } else {
1071 reader.readC();
1072 }
1073 if (ds.version>=45) {
1074 ds.legacyVolumeSlides=reader.readC();
1075 } else {
1076 reader.readC();
1077 }
1078 if (ds.version>=45) {
1079 ds.compatibleArpeggio=reader.readC();
1080 } else {
1081 reader.readC();
1082 }
1083 if (ds.version>=45) {
1084 ds.noteOffResetsSlides=reader.readC();
1085 } else {
1086 reader.readC();
1087 }
1088 if (ds.version>=45) {
1089 ds.targetResetsSlides=reader.readC();
1090 } else {
1091 reader.readC();
1092 }
1093 if (ds.version>=47) {
1094 ds.arpNonPorta=reader.readC();
1095 } else {
1096 reader.readC();
1097 }
1098 if (ds.version>=47) {
1099 ds.algMacroBehavior=reader.readC();
1100 } else {
1101 reader.readC();
1102 }
1103 if (ds.version>=49) {
1104 ds.brokenShortcutSlides=reader.readC();
1105 } else {
1106 reader.readC();
1107 }
1108 if (ds.version>=50) {
1109 ds.ignoreDuplicateSlides=reader.readC();
1110 } else {
1111 reader.readC();
1112 }
1113 if (ds.version>=62) {
1114 ds.stopPortaOnNoteOff=reader.readC();
1115 ds.continuousVibrato=reader.readC();
1116 } else {
1117 reader.readC();
1118 reader.readC();
1119 }
1120 if (ds.version>=64) {
1121 ds.brokenDACMode=reader.readC();
1122 } else {
1123 reader.readC();
1124 }
1125 if (ds.version>=65) {
1126 ds.oneTickCut=reader.readC();
1127 } else {
1128 reader.readC();
1129 }
1130 if (ds.version>=66) {
1131 ds.newInsTriggersInPorta=reader.readC();
1132 } else {
1133 reader.readC();
1134 }
1135 if (ds.version>=69) {
1136 ds.arp0Reset=reader.readC();
1137 } else {
1138 reader.readC();
1139 }
1140 } else {
1141 for (int i=0; i<20; i++) reader.readC();
1142 }
1143
1144 // pointers
1145 insPtr.reserve(ds.insLen);
1146 for (int i=0; i<ds.insLen; i++) {
1147 insPtr.push_back(reader.readI());
1148 }
1149 wavePtr.reserve(ds.waveLen);
1150 for (int i=0; i<ds.waveLen; i++) {
1151 wavePtr.push_back(reader.readI());
1152 }
1153 samplePtr.reserve(ds.sampleLen);
1154 for (int i=0; i<ds.sampleLen; i++) {
1155 samplePtr.push_back(reader.readI());
1156 }
1157 patPtr.reserve(numberOfPats);
1158 for (int i=0; i<numberOfPats; i++) patPtr.push_back(reader.readI());
1159
1160 logD("reading orders (%d)...",subSong->ordersLen);
1161 for (int i=0; i<tchans; i++) {
1162 for (int j=0; j<subSong->ordersLen; j++) {
1163 subSong->orders.ord[i][j]=reader.readC();
1164 }
1165 }
1166
1167 for (int i=0; i<tchans; i++) {
1168 subSong->pat[i].effectCols=reader.readC();
1169 if (subSong->pat[i].effectCols<1 || subSong->pat[i].effectCols>DIV_MAX_EFFECTS) {
1170 logE("channel %d has zero or too many effect columns! (%d)",i,subSong->pat[i].effectCols);
1171 lastError=fmt::sprintf("channel %d has too many effect columns! (%d)",i,subSong->pat[i].effectCols);
1172 delete[] file;
1173 return false;
1174 }
1175 }
1176
1177 if (ds.version>=39) {
1178 for (int i=0; i<tchans; i++) {
1179 if (ds.version<189) {
1180 subSong->chanShow[i]=reader.readC();
1181 subSong->chanShowChanOsc[i]=subSong->chanShow[i];
1182 } else {
1183 unsigned char tempchar=reader.readC();
1184 subSong->chanShow[i]=tempchar&1;
1185 subSong->chanShowChanOsc[i]=(tempchar&2);
1186 }
1187 }
1188
1189 for (int i=0; i<tchans; i++) {
1190 subSong->chanCollapse[i]=reader.readC();
1191 }
1192
1193 if (ds.version<92) {
1194 for (int i=0; i<tchans; i++) {
1195 if (subSong->chanCollapse[i]>0) subSong->chanCollapse[i]=3;
1196 }
1197 }
1198
1199 for (int i=0; i<tchans; i++) {
1200 subSong->chanName[i]=reader.readString();
1201 }
1202
1203 for (int i=0; i<tchans; i++) {
1204 subSong->chanShortName[i]=reader.readString();
1205 }
1206
1207 ds.notes=reader.readString();
1208 }
1209
1210 if (ds.version>=59) {
1211 ds.masterVol=reader.readF();
1212 } else {
1213 ds.masterVol=2.0f;
1214 }
1215
1216 if (ds.version>=70) {
1217 // extended compat flags
1218 ds.brokenSpeedSel=reader.readC();
1219 if (ds.version>=71) {
1220 ds.noSlidesOnFirstTick=reader.readC();
1221 ds.rowResetsArpPos=reader.readC();
1222 ds.ignoreJumpAtEnd=reader.readC();
1223 } else {
1224 reader.readC();
1225 reader.readC();
1226 reader.readC();
1227 }
1228 if (ds.version>=72) {
1229 ds.buggyPortaAfterSlide=reader.readC();
1230 ds.gbInsAffectsEnvelope=reader.readC();
1231 } else {
1232 reader.readC();
1233 reader.readC();
1234 }
1235 if (ds.version>=78) {
1236 ds.sharedExtStat=reader.readC();
1237 } else {
1238 reader.readC();
1239 }
1240 if (ds.version>=83) {
1241 ds.ignoreDACModeOutsideIntendedChannel=reader.readC();
1242 ds.e1e2AlsoTakePriority=reader.readC();
1243 } else {
1244 reader.readC();
1245 reader.readC();
1246 }
1247 if (ds.version>=84) {
1248 ds.newSegaPCM=reader.readC();
1249 } else {
1250 reader.readC();
1251 }
1252 if (ds.version>=85) {
1253 ds.fbPortaPause=reader.readC();
1254 } else {
1255 reader.readC();
1256 }
1257 if (ds.version>=86) {
1258 ds.snDutyReset=reader.readC();
1259 } else {
1260 reader.readC();
1261 }
1262 if (ds.version>=90) {
1263 ds.pitchMacroIsLinear=reader.readC();
1264 } else {
1265 reader.readC();
1266 }
1267 if (ds.version>=94) {
1268 ds.pitchSlideSpeed=reader.readC();
1269 } else {
1270 reader.readC();
1271 }
1272 if (ds.version>=97) {
1273 ds.oldOctaveBoundary=reader.readC();
1274 } else {
1275 reader.readC();
1276 }
1277 if (ds.version>=98) {
1278 ds.noOPN2Vol=reader.readC();
1279 } else {
1280 reader.readC();
1281 }
1282 if (ds.version>=99) {
1283 ds.newVolumeScaling=reader.readC();
1284 ds.volMacroLinger=reader.readC();
1285 ds.brokenOutVol=reader.readC();
1286 } else {
1287 reader.readC();
1288 reader.readC();
1289 reader.readC();
1290 }
1291 if (ds.version>=100) {
1292 ds.e1e2StopOnSameNote=reader.readC();
1293 } else {
1294 reader.readC();
1295 }
1296 if (ds.version>=101) {
1297 ds.brokenPortaArp=reader.readC();
1298 } else {
1299 reader.readC();
1300 }
1301 if (ds.version>=108) {
1302 ds.snNoLowPeriods=reader.readC();
1303 } else {
1304 reader.readC();
1305 }
1306 if (ds.version>=110) {
1307 ds.delayBehavior=reader.readC();
1308 } else {
1309 reader.readC();
1310 }
1311 if (ds.version>=113) {
1312 ds.jumpTreatment=reader.readC();
1313 } else {
1314 reader.readC();
1315 }
1316 if (ds.version>=115) {
1317 ds.autoSystem=reader.readC();
1318 } else {
1319 reader.readC();
1320 }
1321 if (ds.version>=117) {
1322 ds.disableSampleMacro=reader.readC();
1323 } else {
1324 reader.readC();
1325 }
1326 if (ds.version>=121) {
1327 ds.brokenOutVol2=reader.readC();
1328 } else {
1329 reader.readC();
1330 }
1331 if (ds.version>=130) {
1332 ds.oldArpStrategy=reader.readC();
1333 } else {
1334 reader.readC();
1335 }
1336 }
1337
1338 // first song virtual tempo
1339 if (ds.version>=96) {
1340 subSong->virtualTempoN=reader.readS();
1341 subSong->virtualTempoD=reader.readS();
1342 } else {
1343 reader.readI();
1344 }
1345
1346 // subsongs
1347 if (ds.version>=95) {
1348 subSong->name=reader.readString();
1349 subSong->notes=reader.readString();
1350 numberOfSubSongs=(unsigned char)reader.readC();
1351 reader.readC(); // reserved
1352 reader.readC();
1353 reader.readC();
1354 // pointers
1355 for (int i=0; i<numberOfSubSongs; i++) {
1356 subSongPtr[i]=reader.readI();
1357 }
1358 }
1359
1360 // additional metadata
1361 if (ds.version>=103) {
1362 ds.systemName=reader.readString();
1363 ds.category=reader.readString();
1364 ds.nameJ=reader.readString();
1365 ds.authorJ=reader.readString();
1366 ds.systemNameJ=reader.readString();
1367 ds.categoryJ=reader.readString();
1368 } else {
1369 ds.systemName=getSongSystemLegacyName(ds,!getConfInt("noMultiSystem",0));
1370 ds.autoSystem=true;
1371 }
1372
1373 // system output config
1374 if (ds.version>=135) {
1375 for (int i=0; i<ds.systemLen; i++) {
1376 ds.systemVol[i]=reader.readF();
1377 ds.systemPan[i]=reader.readF();
1378 ds.systemPanFR[i]=reader.readF();
1379 }
1380
1381 // patchbay
1382 unsigned int conns=reader.readI();
1383 if (conns>0) ds.patchbay.reserve(conns);
1384 for (unsigned int i=0; i<conns; i++) {
1385 ds.patchbay.push_back((unsigned int)reader.readI());
1386 }
1387 }
1388
1389 if (ds.version>=136) ds.patchbayAuto=reader.readC();
1390
1391 if (ds.version>=138) {
1392 ds.brokenPortaLegato=reader.readC();
1393 if (ds.version>=155) {
1394 ds.brokenFMOff=reader.readC();
1395 } else {
1396 reader.readC();
1397 }
1398 if (ds.version>=168) {
1399 ds.preNoteNoEffect=reader.readC();
1400 } else {
1401 reader.readC();
1402 }
1403 if (ds.version>=183) {
1404 ds.oldDPCM=reader.readC();
1405 } else {
1406 reader.readC();
1407 }
1408 if (ds.version>=184) {
1409 ds.resetArpPhaseOnNewNote=reader.readC();
1410 } else {
1411 reader.readC();
1412 }
1413 if (ds.version>=188) {
1414 ds.ceilVolumeScaling=reader.readC();
1415 } else {
1416 reader.readC();
1417 }
1418 if (ds.version>=191) {
1419 ds.oldAlwaysSetVolume=reader.readC();
1420 } else {
1421 reader.readC();
1422 }
1423 if (ds.version>=200) {
1424 ds.oldSampleOffset=reader.readC();
1425 } else {
1426 reader.readC();
1427 }
1428 }
1429
1430 if (ds.version>=139) {
1431 subSong->speeds.len=reader.readC();
1432 for (int i=0; i<16; i++) {
1433 subSong->speeds.val[i]=reader.readC();
1434 }
1435
1436 // grooves
1437 unsigned char grooveCount=reader.readC();
1438 ds.grooves.reserve(grooveCount);
1439 for (int i=0; i<grooveCount; i++) {
1440 DivGroovePattern gp;
1441 gp.len=reader.readC();
1442 for (int j=0; j<16; j++) {
1443 gp.val[j]=reader.readC();
1444 }
1445
1446 ds.grooves.push_back(gp);
1447 }
1448 }
1449
1450 if (ds.version>=156) {
1451 assetDirPtr[0]=reader.readI();
1452 assetDirPtr[1]=reader.readI();
1453 assetDirPtr[2]=reader.readI();
1454 }
1455
1456 // read system flags
1457 if (ds.version>=119) {
1458 logD("reading chip flags...");
1459 for (int i=0; i<DIV_MAX_CHIPS; i++) {
1460 if (sysFlagsPtr[i]==0) continue;
1461
1462 if (!reader.seek(sysFlagsPtr[i],SEEK_SET)) {
1463 logE("couldn't seek to chip %d flags!",i+1);
1464 lastError=fmt::sprintf("couldn't seek to chip %d flags!",i+1);
1465 ds.unload();
1466 delete[] file;
1467 return false;
1468 }
1469
1470 reader.read(magic,4);
1471 if (strcmp(magic,"FLAG")!=0) {
1472 logE("%d: invalid flag header!",i);
1473 lastError="invalid flag header!";
1474 ds.unload();
1475 delete[] file;
1476 return false;
1477 }
1478 reader.readI();
1479
1480 String data=reader.readString();
1481 ds.systemFlags[i].loadFromMemory(data.c_str());
1482 }
1483 } else {
1484 logD("reading old chip flags...");
1485 for (int i=0; i<ds.systemLen; i++) {
1486 convertOldFlags(sysFlagsPtr[i],ds.systemFlags[i],ds.system[i]);
1487 }
1488 }
1489
1490 // read asset directories
1491 if (ds.version>=156) {
1492 logD("reading asset directories...");
1493
1494 if (!reader.seek(assetDirPtr[0],SEEK_SET)) {
1495 logE("couldn't seek to ins dir!");
1496 lastError=fmt::sprintf("couldn't read instrument directory");
1497 ds.unload();
1498 delete[] file;
1499 return false;
1500 }
1501 if (readAssetDirData(reader,ds.insDir)!=DIV_DATA_SUCCESS) {
1502 lastError="invalid instrument directory data!";
1503 ds.unload();
1504 delete[] file;
1505 return false;
1506 }
1507
1508 if (!reader.seek(assetDirPtr[1],SEEK_SET)) {
1509 logE("couldn't seek to wave dir!");
1510 lastError=fmt::sprintf("couldn't read wavetable directory");
1511 ds.unload();
1512 delete[] file;
1513 return false;
1514 }
1515 if (readAssetDirData(reader,ds.waveDir)!=DIV_DATA_SUCCESS) {
1516 lastError="invalid wavetable directory data!";
1517 ds.unload();
1518 delete[] file;
1519 return false;
1520 }
1521
1522 if (!reader.seek(assetDirPtr[2],SEEK_SET)) {
1523 logE("couldn't seek to sample dir!");
1524 lastError=fmt::sprintf("couldn't read sample directory");
1525 ds.unload();
1526 delete[] file;
1527 return false;
1528 }
1529 if (readAssetDirData(reader,ds.sampleDir)!=DIV_DATA_SUCCESS) {
1530 lastError="invalid sample directory data!";
1531 ds.unload();
1532 delete[] file;
1533 return false;
1534 }
1535 }
1536
1537 // read subsongs
1538 if (ds.version>=95) {
1539 ds.subsong.reserve(numberOfSubSongs);
1540 for (int i=0; i<numberOfSubSongs; i++) {
1541 ds.subsong.push_back(new DivSubSong);
1542 if (!reader.seek(subSongPtr[i],SEEK_SET)) {
1543 logE("couldn't seek to subsong %d!",i+1);
1544 lastError=fmt::sprintf("couldn't seek to subsong %d!",i+1);
1545 ds.unload();
1546 delete[] file;
1547 return false;
1548 }
1549
1550 reader.read(magic,4);
1551 if (strcmp(magic,"SONG")!=0) {
1552 logE("%d: invalid subsong header!",i);
1553 lastError="invalid subsong header!";
1554 ds.unload();
1555 delete[] file;
1556 return false;
1557 }
1558 reader.readI();
1559
1560 subSong=ds.subsong[i+1];
1561 subSong->timeBase=reader.readC();
1562 subSong->speeds.len=2;
1563 subSong->speeds.val[0]=reader.readC();
1564 subSong->speeds.val[1]=reader.readC();
1565 subSong->arpLen=reader.readC();
1566 subSong->hz=reader.readF();
1567
1568 subSong->patLen=reader.readS();
1569 subSong->ordersLen=reader.readS();
1570
1571 subSong->hilightA=reader.readC();
1572 subSong->hilightB=reader.readC();
1573
1574 if (ds.version>=96) {
1575 subSong->virtualTempoN=reader.readS();
1576 subSong->virtualTempoD=reader.readS();
1577 } else {
1578 reader.readI();
1579 }
1580
1581 subSong->name=reader.readString();
1582 subSong->notes=reader.readString();
1583
1584 logD("reading orders of subsong %d (%d)...",i+1,subSong->ordersLen);
1585 for (int j=0; j<tchans; j++) {
1586 for (int k=0; k<subSong->ordersLen; k++) {
1587 subSong->orders.ord[j][k]=reader.readC();
1588 }
1589 }
1590
1591 for (int i=0; i<tchans; i++) {
1592 subSong->pat[i].effectCols=reader.readC();
1593 }
1594
1595 for (int i=0; i<tchans; i++) {
1596 if (ds.version<189) {
1597 subSong->chanShow[i]=reader.readC();
1598 subSong->chanShowChanOsc[i]=subSong->chanShow[i];
1599 } else {
1600 unsigned char tempchar=reader.readC();
1601 subSong->chanShow[i]=tempchar&1;
1602 subSong->chanShowChanOsc[i]=tempchar&2;
1603 }
1604 }
1605
1606 for (int i=0; i<tchans; i++) {
1607 subSong->chanCollapse[i]=reader.readC();
1608 }
1609
1610 for (int i=0; i<tchans; i++) {
1611 subSong->chanName[i]=reader.readString();
1612 }
1613
1614 for (int i=0; i<tchans; i++) {
1615 subSong->chanShortName[i]=reader.readString();
1616 }
1617
1618 if (ds.version>=139) {
1619 subSong->speeds.len=reader.readC();
1620 for (int i=0; i<16; i++) {
1621 subSong->speeds.val[i]=reader.readC();
1622 }
1623 }
1624 }
1625 }
1626
1627 // read instruments
1628 ds.ins.reserve(ds.insLen);
1629 for (int i=0; i<ds.insLen; i++) {
1630 DivInstrument* ins=new DivInstrument;
1631 logD("reading instrument %d at %x...",i,insPtr[i]);
1632 if (!reader.seek(insPtr[i],SEEK_SET)) {
1633 logE("couldn't seek to instrument %d!",i);
1634 lastError=fmt::sprintf("couldn't seek to instrument %d!",i);
1635 ds.unload();
1636 delete ins;
1637 delete[] file;
1638 return false;
1639 }
1640
1641 if (ins->readInsData(reader,ds.version)!=DIV_DATA_SUCCESS) {
1642 lastError="invalid instrument header/data!";
1643 ds.unload();
1644 delete ins;
1645 delete[] file;
1646 return false;
1647 }
1648
1649 ds.ins.push_back(ins);
1650 }
1651
1652 // read wavetables
1653 ds.wave.reserve(ds.waveLen);
1654 for (int i=0; i<ds.waveLen; i++) {
1655 DivWavetable* wave=new DivWavetable;
1656 logD("reading wavetable %d at %x...",i,wavePtr[i]);
1657 if (!reader.seek(wavePtr[i],SEEK_SET)) {
1658 logE("couldn't seek to wavetable %d!",i);
1659 lastError=fmt::sprintf("couldn't seek to wavetable %d!",i);
1660 ds.unload();
1661 delete wave;
1662 delete[] file;
1663 return false;
1664 }
1665
1666 if (wave->readWaveData(reader,ds.version)!=DIV_DATA_SUCCESS) {
1667 lastError="invalid wavetable header/data!";
1668 ds.unload();
1669 delete wave;
1670 delete[] file;
1671 return false;
1672 }
1673
1674 ds.wave.push_back(wave);
1675 }
1676
1677 // read samples
1678 ds.sample.reserve(ds.sampleLen);
1679 for (int i=0; i<ds.sampleLen; i++) {
1680 DivSample* sample=new DivSample;
1681
1682 if (!reader.seek(samplePtr[i],SEEK_SET)) {
1683 logE("couldn't seek to sample %d!",i);
1684 lastError=fmt::sprintf("couldn't seek to sample %d!",i);
1685 ds.unload();
1686 delete sample;
1687 delete[] file;
1688 return false;
1689 }
1690
1691 if (sample->readSampleData(reader,ds.version)!=DIV_DATA_SUCCESS) {
1692 lastError="invalid sample header/data!";
1693 ds.unload();
1694 delete sample;
1695 delete[] file;
1696 return false;
1697 }
1698
1699 ds.sample.push_back(sample);
1700 }
1701
1702 // read patterns
1703 for (unsigned int i: patPtr) {
1704 bool isNewFormat=false;
1705 if (!reader.seek(i,SEEK_SET)) {
1706 logE("couldn't seek to pattern in %x!",i);
1707 lastError=fmt::sprintf("couldn't seek to pattern in %x!",i);
1708 ds.unload();
1709 delete[] file;
1710 return false;
1711 }
1712 reader.read(magic,4);
1713 logD("reading pattern in %x...",i);
1714 if (strcmp(magic,"PATR")!=0) {
1715 if (strcmp(magic,"PATN")!=0 || ds.version<157) {
1716 logE("%x: invalid pattern header!",i);
1717 lastError="invalid pattern header!";
1718 ds.unload();
1719 delete[] file;
1720 return false;
1721 } else {
1722 isNewFormat=true;
1723 }
1724 }
1725 reader.readI();
1726
1727 if (isNewFormat) {
1728 int subs=(unsigned char)reader.readC();
1729 int chan=(unsigned char)reader.readC();
1730 int index=reader.readS();
1731
1732 logD("- %d, %d, %d (new)",subs,chan,index);
1733
1734 if (chan<0 || chan>=tchans) {
1735 logE("pattern channel out of range!",i);
1736 lastError="pattern channel out of range!";
1737 ds.unload();
1738 delete[] file;
1739 return false;
1740 }
1741 if (index<0 || index>(DIV_MAX_PATTERNS-1)) {
1742 logE("pattern index out of range!",i);
1743 lastError="pattern index out of range!";
1744 ds.unload();
1745 delete[] file;
1746 return false;
1747 }
1748 if (subs<0 || subs>=(int)ds.subsong.size()) {
1749 logE("pattern subsong out of range!",i);
1750 lastError="pattern subsong out of range!";
1751 ds.unload();
1752 delete[] file;
1753 return false;
1754 }
1755
1756 DivPattern* pat=ds.subsong[subs]->pat[chan].getPattern(index,true);
1757 pat->name=reader.readString();
1758
1759 // read new pattern
1760 for (int j=0; j<ds.subsong[subs]->patLen; j++) {
1761 unsigned char mask=reader.readC();
1762 unsigned short effectMask=0;
1763
1764 if (mask==0xff) break;
1765 if (mask&128) {
1766 j+=(mask&127)+1;
1767 continue;
1768 }
1769
1770 if (mask&32) {
1771 effectMask|=(unsigned char)reader.readC();
1772 }
1773 if (mask&64) {
1774 effectMask|=((unsigned short)reader.readC()&0xff)<<8;
1775 }
1776 if (mask&8) effectMask|=1;
1777 if (mask&16) effectMask|=2;
1778
1779 if (mask&1) { // note
1780 unsigned char note=reader.readC();
1781 if (note==180) {
1782 pat->data[j][0]=100;
1783 pat->data[j][1]=0;
1784 } else if (note==181) {
1785 pat->data[j][0]=101;
1786 pat->data[j][1]=0;
1787 } else if (note==182) {
1788 pat->data[j][0]=102;
1789 pat->data[j][1]=0;
1790 } else if (note<180) {
1791 pat->data[j][0]=newFormatNotes[note];
1792 pat->data[j][1]=newFormatOctaves[note];
1793 } else {
1794 pat->data[j][0]=0;
1795 pat->data[j][1]=0;
1796 }
1797 }
1798 if (mask&2) { // instrument
1799 pat->data[j][2]=(unsigned char)reader.readC();
1800 }
1801 if (mask&4) { // volume
1802 pat->data[j][3]=(unsigned char)reader.readC();
1803 }
1804 for (unsigned char k=0; k<16; k++) {
1805 if (effectMask&(1<<k)) {
1806 pat->data[j][4+k]=(unsigned char)reader.readC();
1807 }
1808 }
1809 }
1810 } else {
1811 int chan=reader.readS();
1812 int index=reader.readS();
1813 int subs=0;
1814 if (ds.version>=95) {
1815 subs=reader.readS();
1816 } else {
1817 reader.readS();
1818 }
1819 reader.readS();
1820
1821 logD("- %d, %d, %d (old)",subs,chan,index);
1822
1823 if (chan<0 || chan>=tchans) {
1824 logE("pattern channel out of range!",i);
1825 lastError="pattern channel out of range!";
1826 ds.unload();
1827 delete[] file;
1828 return false;
1829 }
1830 if (index<0 || index>(DIV_MAX_PATTERNS-1)) {
1831 logE("pattern index out of range!",i);
1832 lastError="pattern index out of range!";
1833 ds.unload();
1834 delete[] file;
1835 return false;
1836 }
1837 if (subs<0 || subs>=(int)ds.subsong.size()) {
1838 logE("pattern subsong out of range!",i);
1839 lastError="pattern subsong out of range!";
1840 ds.unload();
1841 delete[] file;
1842 return false;
1843 }
1844
1845 DivPattern* pat=ds.subsong[subs]->pat[chan].getPattern(index,true);
1846 for (int j=0; j<ds.subsong[subs]->patLen; j++) {
1847 pat->data[j][0]=reader.readS();
1848 pat->data[j][1]=reader.readS();
1849 pat->data[j][2]=reader.readS();
1850 pat->data[j][3]=reader.readS();
1851 for (int k=0; k<ds.subsong[subs]->pat[chan].effectCols; k++) {
1852 pat->data[j][4+(k<<1)]=reader.readS();
1853 pat->data[j][5+(k<<1)]=reader.readS();
1854 }
1855 if (pat->data[j][0]==0 && pat->data[j][1]!=0) {
1856 logD("what? %d:%d:%d note %d octave %d",chan,i,j,pat->data[j][0],pat->data[j][1]);
1857 pat->data[j][0]=12;
1858 pat->data[j][1]--;
1859 }
1860 }
1861
1862 if (ds.version>=51) {
1863 pat->name=reader.readString();
1864 }
1865 }
1866 }
1867
1868 if (reader.tell()<reader.size()) {
1869 if ((reader.tell()+1)!=reader.size()) {
1870 logW("premature end of song (we are at %x, but size is %x)",reader.tell(),reader.size());
1871 }
1872 }
1873
1874 // convert OPM/NES instrument types
1875 if (ds.version<117) {
1876 int opnCount=0;
1877 int opmCount=0;
1878 int snCount=0;
1879 int nesCount=0;
1880 for (int i=0; i<ds.systemLen; i++) {
1881 switch (ds.system[i]) {
1882 case DIV_SYSTEM_NES:
1883 case DIV_SYSTEM_MMC5:
1884 nesCount++;
1885 break;
1886 case DIV_SYSTEM_SMS:
1887 snCount++;
1888 break;
1889 case DIV_SYSTEM_YM2151:
1890 case DIV_SYSTEM_OPZ:
1891 opmCount++;
1892 break;
1893 case DIV_SYSTEM_YM2610:
1894 case DIV_SYSTEM_YM2610_EXT:
1895 case DIV_SYSTEM_YM2610_FULL:
1896 case DIV_SYSTEM_YM2610_FULL_EXT:
1897 case DIV_SYSTEM_YM2610B:
1898 case DIV_SYSTEM_YM2610B_EXT:
1899 case DIV_SYSTEM_YM2203:
1900 case DIV_SYSTEM_YM2203_EXT:
1901 case DIV_SYSTEM_YM2608:
1902 case DIV_SYSTEM_YM2608_EXT:
1903 case DIV_SYSTEM_YM2612:
1904 case DIV_SYSTEM_YM2612_EXT:
1905 case DIV_SYSTEM_YM2612_DUALPCM:
1906 case DIV_SYSTEM_YM2612_DUALPCM_EXT:
1907 opnCount++;
1908 break;
1909 default:
1910 break;
1911 }
1912 }
1913 if (opmCount>opnCount) {
1914 for (DivInstrument* i: ds.ins) {
1915 if (i->type==DIV_INS_FM) i->type=DIV_INS_OPM;
1916 }
1917 }
1918 if (nesCount>snCount) {
1919 for (DivInstrument* i: ds.ins) {
1920 if (i->type==DIV_INS_STD) i->type=DIV_INS_NES;
1921 }
1922 }
1923 }
1924
1925 // ExtCh compat flag
1926 for (int i=0; i<ds.systemLen; i++) {
1927 if (ds.system[i]==DIV_SYSTEM_YM2612_EXT ||
1928 ds.system[i]==DIV_SYSTEM_YM2612_DUALPCM_EXT ||
1929 ds.system[i]==DIV_SYSTEM_YM2610_EXT ||
1930 ds.system[i]==DIV_SYSTEM_YM2610_FULL_EXT ||
1931 ds.system[i]==DIV_SYSTEM_YM2610B_EXT ||
1932 ds.system[i]==DIV_SYSTEM_YM2203_EXT ||
1933 ds.system[i]==DIV_SYSTEM_YM2608_EXT ||
1934 ds.system[i]==DIV_SYSTEM_YM2612_CSM ||
1935 ds.system[i]==DIV_SYSTEM_YM2203_CSM ||
1936 ds.system[i]==DIV_SYSTEM_YM2608_CSM ||
1937 ds.system[i]==DIV_SYSTEM_YM2610_CSM ||
1938 ds.system[i]==DIV_SYSTEM_YM2610B_CSM) {
1939 if (ds.version<125) {
1940 ds.systemFlags[i].set("noExtMacros",true);
1941 }
1942 if (ds.version<133) {
1943 ds.systemFlags[i].set("fbAllOps",true);
1944 }
1945 }
1946 }
1947
1948 // SN noise compat
1949 if (ds.version<128) {
1950 for (int i=0; i<ds.systemLen; i++) {
1951 if (ds.system[i]==DIV_SYSTEM_SMS ||
1952 ds.system[i]==DIV_SYSTEM_T6W28) {
1953 ds.systemFlags[i].set("noEasyNoise",true);
1954 }
1955 }
1956 }
1957
1958 // OPL3 pan compat
1959 if (ds.version<134) {
1960 for (int i=0; i<ds.systemLen; i++) {
1961 if (ds.system[i]==DIV_SYSTEM_OPL3 ||
1962 ds.system[i]==DIV_SYSTEM_OPL3_DRUMS) {
1963 ds.systemFlags[i].set("compatPan",true);
1964 }
1965 }
1966 }
1967
1968 // new YM2612/SN/X1-010 volumes
1969 if (ds.version<137) {
1970 for (int i=0; i<ds.systemLen; i++) {
1971 switch (ds.system[i]) {
1972 case DIV_SYSTEM_YM2612:
1973 case DIV_SYSTEM_YM2612_EXT:
1974 case DIV_SYSTEM_YM2612_DUALPCM:
1975 case DIV_SYSTEM_YM2612_DUALPCM_EXT:
1976 case DIV_SYSTEM_YM2612_CSM:
1977 ds.systemVol[i]/=2.0;
1978 break;
1979 case DIV_SYSTEM_SMS:
1980 case DIV_SYSTEM_T6W28:
1981 case DIV_SYSTEM_OPLL:
1982 case DIV_SYSTEM_OPLL_DRUMS:
1983 ds.systemVol[i]/=1.5;
1984 break;
1985 case DIV_SYSTEM_X1_010:
1986 ds.systemVol[i]/=4.0;
1987 break;
1988 default:
1989 break;
1990 }
1991 }
1992 }
1993
1994 // Namco C30 noise compat
1995 if (ds.version<145) {
1996 for (int i=0; i<ds.systemLen; i++) {
1997 if (ds.system[i]==DIV_SYSTEM_NAMCO_CUS30) {
1998 ds.systemFlags[i].set("newNoise",false);
1999 }
2000 }
2001 }
2002
2003 // SrgaPCM slide compat
2004 if (ds.version<153) {
2005 for (int i=0; i<ds.systemLen; i++) {
2006 if (ds.system[i]==DIV_SYSTEM_SEGAPCM || ds.system[i]==DIV_SYSTEM_SEGAPCM_COMPAT) {
2007 ds.systemFlags[i].set("oldSlides",true);
2008 }
2009 }
2010 }
2011
2012 // NES PCM compat
2013 if (ds.version<154) {
2014 for (int i=0; i<ds.systemLen; i++) {
2015 if (ds.system[i]==DIV_SYSTEM_NES) {
2016 ds.systemFlags[i].set("dpcmMode",false);
2017 }
2018 }
2019 }
2020
2021 // C64 key priority compat
2022 if (ds.version<160) {
2023 for (int i=0; i<ds.systemLen; i++) {
2024 if (ds.system[i]==DIV_SYSTEM_C64_8580 || ds.system[i]==DIV_SYSTEM_C64_6581) {
2025 ds.systemFlags[i].set("keyPriority",false);
2026 }
2027 }
2028 }
2029
2030 // Namco 163 pitch compensation compat
2031 if (ds.version<165) {
2032 for (int i=0; i<ds.systemLen; i++) {
2033 if (ds.system[i]==DIV_SYSTEM_N163) {
2034 ds.systemFlags[i].set("lenCompensate",true);
2035 }
2036 }
2037 }
2038
2039 // OPM/OPZ slide compat
2040 if (ds.version<176) {
2041 for (int i=0; i<ds.systemLen; i++) {
2042 if (ds.system[i]==DIV_SYSTEM_YM2151 ||
2043 ds.system[i]==DIV_SYSTEM_OPZ) {
2044 ds.systemFlags[i].set("brokenPitch",true);
2045 }
2046 }
2047 }
2048
2049 // C64 1Exy compat
2050 if (ds.version<186) {
2051 for (int i=0; i<ds.systemLen; i++) {
2052 if (ds.system[i]==DIV_SYSTEM_C64_8580 || ds.system[i]==DIV_SYSTEM_C64_6581) {
2053 ds.systemFlags[i].set("no1EUpdate",true);
2054 }
2055 }
2056 }
2057
2058 // C64 original reset time and multiply relative
2059 if (ds.version<187) {
2060 for (int i=0; i<ds.systemLen; i++) {
2061 if (ds.system[i]==DIV_SYSTEM_C64_8580 || ds.system[i]==DIV_SYSTEM_C64_6581) {
2062 ds.systemFlags[i].set("initResetTime",1);
2063 ds.systemFlags[i].set("multiplyRel",true);
2064 }
2065 }
2066 }
2067
2068 // OPLL fixedAll compat
2069 if (ds.version<194) {
2070 for (int i=0; i<ds.systemLen; i++) {
2071 if (ds.system[i]==DIV_SYSTEM_OPLL ||
2072 ds.system[i]==DIV_SYSTEM_OPLL_DRUMS) {
2073 if (!ds.systemFlags[i].has("fixedAll")) {
2074 ds.systemFlags[i].set("fixedAll",false);
2075 }
2076 }
2077 }
2078 }
2079
2080 // C64 macro race
2081 if (ds.version<195) {
2082 for (int i=0; i<ds.systemLen; i++) {
2083 if (ds.system[i]==DIV_SYSTEM_C64_8580 || ds.system[i]==DIV_SYSTEM_C64_6581) {
2084 ds.systemFlags[i].set("macroRace",true);
2085 }
2086 }
2087 }
2088
2089 // VERA old chip revision
2090 // TIA old tuning
2091 if (ds.version<213) {
2092 for (int i=0; i<ds.systemLen; i++) {
2093 if (ds.system[i]==DIV_SYSTEM_VERA) {
2094 ds.systemFlags[i].set("chipType",0);
2095 }
2096 if (ds.system[i]==DIV_SYSTEM_TIA) {
2097 ds.systemFlags[i].set("oldPitch",true);
2098 }
2099 }
2100 } else if (ds.version<217) {
2101 for (int i=0; i<ds.systemLen; i++) {
2102 if (ds.system[i]==DIV_SYSTEM_VERA) {
2103 ds.systemFlags[i].set("chipType",1);
2104 }
2105 }
2106 } else if (ds.version<229) {
2107 for (int i=0; i<ds.systemLen; i++) {
2108 if (ds.system[i]==DIV_SYSTEM_VERA) {
2109 ds.systemFlags[i].set("chipType",2);
2110 }
2111 }
2112 }
2113
2114 // SNES no anti-click
2115 if (ds.version<220) {
2116 for (int i=0; i<ds.systemLen; i++) {
2117 if (ds.system[i]==DIV_SYSTEM_SNES) {
2118 ds.systemFlags[i].set("antiClick",false);
2119 }
2120 }
2121 }
2122
2123 // Y8950 broken ADPCM pitch
2124 if (ds.version<223) {
2125 for (int i=0; i<ds.systemLen; i++) {
2126 if (ds.system[i]==DIV_SYSTEM_Y8950 || ds.system[i]==DIV_SYSTEM_Y8950_DRUMS) {
2127 ds.systemFlags[i].set("compatYPitch",true);
2128 }
2129 }
2130 }
2131
2132 // YM2612 chip type
2133 if (ds.version<231) {
2134 for (int i=0; i<ds.systemLen; i++) {
2135 if (ds.system[i]==DIV_SYSTEM_YM2612 ||
2136 ds.system[i]==DIV_SYSTEM_YM2612_EXT ||
2137 ds.system[i]==DIV_SYSTEM_YM2612_EXT ||
2138 ds.system[i]==DIV_SYSTEM_YM2612_DUALPCM ||
2139 ds.system[i]==DIV_SYSTEM_YM2612_DUALPCM_EXT ||
2140 ds.system[i]==DIV_SYSTEM_YM2612_CSM) {
2141 if (!ds.systemFlags[i].has("chipType") && !ds.systemFlags[i].has("ladderEffect")) {
2142 ds.systemFlags[i].set("chipType",0);
2143 }
2144 }
2145 }
2146 }
2147
2148 if (active) quitDispatch();
2149 BUSY_BEGIN_SOFT;
2150 saveLock.lock();
2151 song.unload();
2152 song=ds;
2153 changeSong(0);
2154 recalcChans();
2155 saveLock.unlock();
2156 BUSY_END;
2157 if (active) {
2158 initDispatch();
2159 BUSY_BEGIN;
2160 renderSamples();
2161 reset();
2162 BUSY_END;
2163 }
2164 } catch (EndOfFileException& e) {
2165 logE("premature end of file!");
2166 lastError="incomplete file";
2167 delete[] file;
2168 return false;
2169 }
2170 delete[] file;
2171 return true;
2172 }
2173
2174 SafeWriter* DivEngine::saveFur(bool notPrimary, bool newPatternFormat) {
2175 saveLock.lock();
2176 std::vector<int> subSongPtr;
2177 std::vector<int> sysFlagsPtr;
2178 std::vector<int> insPtr;
2179 std::vector<int> wavePtr;
2180 std::vector<int> samplePtr;
2181 std::vector<int> patPtr;
2182 int assetDirPtr[3];
2183 size_t ptrSeek, subSongPtrSeek, sysFlagsPtrSeek, blockStartSeek, blockEndSeek, assetDirPtrSeek;
2184 size_t subSongIndex=0;
2185 DivSubSong* subSong=song.subsong[subSongIndex];
2186 warnings="";
2187
2188 // fail if values are out of range
2189 /*
2190 if (subSong->ordersLen>DIV_MAX_PATTERNS) {
2191 logE("maximum song length is %d!",DIV_MAX_PATTERNS);
2192 lastError=fmt::sprintf("maximum song length is %d",DIV_MAX_PATTERNS);
2193 return NULL;
2194 }
2195 if (subSong->patLen>DIV_MAX_ROWS) {
2196 logE("maximum pattern length is %d!",DIV_MAX_ROWS);
2197 lastError=fmt::sprintf("maximum pattern length is %d",DIV_MAX_ROWS);
2198 return NULL;
2199 }
2200 */
2201 if (song.ins.size()>256) {
2202 logE("maximum number of instruments is 256!");
2203 lastError="maximum number of instruments is 256";
2204 saveLock.unlock();
2205 return NULL;
2206 }
2207 if (song.wave.size()>32768) {
2208 logE("maximum number of wavetables is 32768!");
2209 lastError="maximum number of wavetables is 32768";
2210 saveLock.unlock();
2211 return NULL;
2212 }
2213 if (song.sample.size()>32768) {
2214 logE("maximum number of samples is 32768!");
2215 lastError="maximum number of samples is 32768";
2216 saveLock.unlock();
2217 return NULL;
2218 }
2219
2220 if (!notPrimary) {
2221 song.isDMF=false;
2222 song.version=DIV_ENGINE_VERSION;
2223 }
2224
2225 SafeWriter* w=new SafeWriter;
2226 w->init();
2227 /// HEADER
2228 // write magic
2229 w->write(DIV_FUR_MAGIC,16);
2230
2231 // write version
2232 w->writeS(DIV_ENGINE_VERSION);
2233
2234 // reserved
2235 w->writeS(0);
2236
2237 // song info pointer
2238 w->writeI(32);
2239
2240 // reserved
2241 w->writeI(0);
2242 w->writeI(0);
2243
2244 // high short is channel
2245 // low short is pattern number
2246 std::vector<PatToWrite> patsToWrite;
2247 if (getConfInt("saveUnusedPatterns",0)==1) {
2248 for (int i=0; i<chans; i++) {
2249 for (size_t j=0; j<song.subsong.size(); j++) {
2250 DivSubSong* subs=song.subsong[j];
2251 for (int k=0; k<DIV_MAX_PATTERNS; k++) {
2252 if (subs->pat[i].data[k]==NULL) continue;
2253 patsToWrite.push_back(PatToWrite(j,i,k));
2254 }
2255 }
2256 }
2257 } else {
2258 bool alreadyAdded[DIV_MAX_PATTERNS];
2259 for (int i=0; i<chans; i++) {
2260 for (size_t j=0; j<song.subsong.size(); j++) {
2261 DivSubSong* subs=song.subsong[j];
2262 memset(alreadyAdded,0,DIV_MAX_PATTERNS*sizeof(bool));
2263 for (int k=0; k<subs->ordersLen; k++) {
2264 if (alreadyAdded[subs->orders.ord[i][k]]) continue;
2265 patsToWrite.push_back(PatToWrite(j,i,subs->orders.ord[i][k]));
2266 alreadyAdded[subs->orders.ord[i][k]]=true;
2267 }
2268 }
2269 }
2270 }
2271
2272 /// SONG INFO
2273 w->write("INFO",4);
2274 blockStartSeek=w->tell();
2275 w->writeI(0);
2276
2277 w->writeC(subSong->timeBase);
2278 // these are for compatibility
2279 w->writeC(subSong->speeds.val[0]);
2280 w->writeC((subSong->speeds.len>=2)?subSong->speeds.val[1]:subSong->speeds.val[0]);
2281 w->writeC(subSong->arpLen);
2282 w->writeF(subSong->hz);
2283 w->writeS(subSong->patLen);
2284 w->writeS(subSong->ordersLen);
2285 w->writeC(subSong->hilightA);
2286 w->writeC(subSong->hilightB);
2287 w->writeS(song.insLen);
2288 w->writeS(song.waveLen);
2289 w->writeS(song.sampleLen);
2290 w->writeI(patsToWrite.size());
2291
2292 for (int i=0; i<DIV_MAX_CHIPS; i++) {
2293 if (i>=song.systemLen) {
2294 w->writeC(0);
2295 } else {
2296 w->writeC(systemToFileFur(song.system[i]));
2297 }
2298 }
2299
2300 for (int i=0; i<DIV_MAX_CHIPS; i++) {
2301 w->writeC(song.systemVol[i]*64.0f);
2302 }
2303
2304 for (int i=0; i<DIV_MAX_CHIPS; i++) {
2305 w->writeC(song.systemPan[i]*127.0f);
2306 }
2307
2308 // chip flags (we'll seek here later)
2309 sysFlagsPtrSeek=w->tell();
2310 for (int i=0; i<DIV_MAX_CHIPS; i++) {
2311 w->writeI(0);
2312 }
2313
2314 // song name
2315 w->writeString(song.name,false);
2316 // song author
2317 w->writeString(song.author,false);
2318
2319 w->writeF(song.tuning);
2320
2321 // compatibility flags
2322 w->writeC(song.limitSlides);
2323 w->writeC(song.linearPitch);
2324 w->writeC(song.loopModality);
2325 w->writeC(song.properNoiseLayout);
2326 w->writeC(song.waveDutyIsVol);
2327 w->writeC(song.resetMacroOnPorta);
2328 w->writeC(song.legacyVolumeSlides);
2329 w->writeC(song.compatibleArpeggio);
2330 w->writeC(song.noteOffResetsSlides);
2331 w->writeC(song.targetResetsSlides);
2332 w->writeC(song.arpNonPorta);
2333 w->writeC(song.algMacroBehavior);
2334 w->writeC(song.brokenShortcutSlides);
2335 w->writeC(song.ignoreDuplicateSlides);
2336 w->writeC(song.stopPortaOnNoteOff);
2337 w->writeC(song.continuousVibrato);
2338 w->writeC(song.brokenDACMode);
2339 w->writeC(song.oneTickCut);
2340 w->writeC(song.newInsTriggersInPorta);
2341 w->writeC(song.arp0Reset);
2342
2343 ptrSeek=w->tell();
2344 // instrument pointers (we'll seek here later)
2345 for (int i=0; i<song.insLen; i++) {
2346 w->writeI(0);
2347 }
2348
2349 // wavetable pointers (we'll seek here later)
2350 for (int i=0; i<song.waveLen; i++) {
2351 w->writeI(0);
2352 }
2353
2354 // sample pointers (we'll seek here later)
2355 for (int i=0; i<song.sampleLen; i++) {
2356 w->writeI(0);
2357 }
2358
2359 // pattern pointers (we'll seek here later)
2360 for (size_t i=0; i<patsToWrite.size(); i++) {
2361 w->writeI(0);
2362 }
2363
2364 for (int i=0; i<chans; i++) {
2365 for (int j=0; j<subSong->ordersLen; j++) {
2366 w->writeC(subSong->orders.ord[i][j]);
2367 }
2368 }
2369
2370 for (int i=0; i<chans; i++) {
2371 w->writeC(subSong->pat[i].effectCols);
2372 }
2373
2374 for (int i=0; i<chans; i++) {
2375 w->writeC(
2376 (subSong->chanShow[i]?1:0)|
2377 (subSong->chanShowChanOsc[i]?2:0)
2378 );
2379 }
2380
2381 for (int i=0; i<chans; i++) {
2382 w->writeC(subSong->chanCollapse[i]);
2383 }
2384
2385 for (int i=0; i<chans; i++) {
2386 w->writeString(subSong->chanName[i],false);
2387 }
2388
2389 for (int i=0; i<chans; i++) {
2390 w->writeString(subSong->chanShortName[i],false);
2391 }
2392
2393 w->writeString(song.notes,false);
2394
2395 w->writeF(song.masterVol);
2396
2397 // extended compat flags
2398 w->writeC(song.brokenSpeedSel);
2399 w->writeC(song.noSlidesOnFirstTick);
2400 w->writeC(song.rowResetsArpPos);
2401 w->writeC(song.ignoreJumpAtEnd);
2402 w->writeC(song.buggyPortaAfterSlide);
2403 w->writeC(song.gbInsAffectsEnvelope);
2404 w->writeC(song.sharedExtStat);
2405 w->writeC(song.ignoreDACModeOutsideIntendedChannel);
2406 w->writeC(song.e1e2AlsoTakePriority);
2407 w->writeC(song.newSegaPCM);
2408 w->writeC(song.fbPortaPause);
2409 w->writeC(song.snDutyReset);
2410 w->writeC(song.pitchMacroIsLinear);
2411 w->writeC(song.pitchSlideSpeed);
2412 w->writeC(song.oldOctaveBoundary);
2413 w->writeC(song.noOPN2Vol);
2414 w->writeC(song.newVolumeScaling);
2415 w->writeC(song.volMacroLinger);
2416 w->writeC(song.brokenOutVol);
2417 w->writeC(song.e1e2StopOnSameNote);
2418 w->writeC(song.brokenPortaArp);
2419 w->writeC(song.snNoLowPeriods);
2420 w->writeC(song.delayBehavior);
2421 w->writeC(song.jumpTreatment);
2422 w->writeC(song.autoSystem);
2423 w->writeC(song.disableSampleMacro);
2424 w->writeC(song.brokenOutVol2);
2425 w->writeC(song.oldArpStrategy);
2426
2427 // first subsong virtual tempo
2428 w->writeS(subSong->virtualTempoN);
2429 w->writeS(subSong->virtualTempoD);
2430
2431 // subsong list
2432 w->writeString(subSong->name,false);
2433 w->writeString(subSong->notes,false);
2434 w->writeC((unsigned char)(song.subsong.size()-1));
2435 w->writeC(0); // reserved
2436 w->writeC(0);
2437 w->writeC(0);
2438 subSongPtrSeek=w->tell();
2439 // subsong pointers (we'll seek here later)
2440 for (size_t i=0; i<(song.subsong.size()-1); i++) {
2441 w->writeI(0);
2442 }
2443
2444 // additional metadata
2445 w->writeString(song.systemName,false);
2446 w->writeString(song.category,false);
2447 w->writeString(song.nameJ,false);
2448 w->writeString(song.authorJ,false);
2449 w->writeString(song.systemNameJ,false);
2450 w->writeString(song.categoryJ,false);
2451
2452 // system output config
2453 for (int i=0; i<song.systemLen; i++) {
2454 w->writeF(song.systemVol[i]);
2455 w->writeF(song.systemPan[i]);
2456 w->writeF(song.systemPanFR[i]);
2457 }
2458 w->writeI(song.patchbay.size());
2459 for (unsigned int i: song.patchbay) {
2460 w->writeI(i);
2461 }
2462 w->writeC(song.patchbayAuto);
2463
2464 // even more compat flags
2465 w->writeC(song.brokenPortaLegato);
2466 w->writeC(song.brokenFMOff);
2467 w->writeC(song.preNoteNoEffect);
2468 w->writeC(song.oldDPCM);
2469 w->writeC(song.resetArpPhaseOnNewNote);
2470 w->writeC(song.ceilVolumeScaling);
2471 w->writeC(song.oldAlwaysSetVolume);
2472 w->writeC(song.oldSampleOffset);
2473
2474 // speeds of first song
2475 w->writeC(subSong->speeds.len);
2476 for (int i=0; i<16; i++) {
2477 w->writeC(subSong->speeds.val[i]);
2478 }
2479
2480 // groove list
2481 w->writeC((unsigned char)song.grooves.size());
2482 for (const DivGroovePattern& i: song.grooves) {
2483 w->writeC(i.len);
2484 for (int j=0; j<16; j++) {
2485 w->writeC(i.val[j]);
2486 }
2487 }
2488
2489 // asset dir pointers (we'll seek here later)
2490 assetDirPtrSeek=w->tell();
2491 w->writeI(0);
2492 w->writeI(0);
2493 w->writeI(0);
2494
2495 blockEndSeek=w->tell();
2496 w->seek(blockStartSeek,SEEK_SET);
2497 w->writeI(blockEndSeek-blockStartSeek-4);
2498 w->seek(0,SEEK_END);
2499
2500 /// SUBSONGS
2501 subSongPtr.reserve(song.subsong.size() - 1);
2502 for (subSongIndex=1; subSongIndex<song.subsong.size(); subSongIndex++) {
2503 subSong=song.subsong[subSongIndex];
2504 subSongPtr.push_back(w->tell());
2505 w->write("SONG",4);
2506 blockStartSeek=w->tell();
2507 w->writeI(0);
2508
2509 w->writeC(subSong->timeBase);
2510 w->writeC(subSong->speeds.val[0]);
2511 w->writeC((subSong->speeds.len>=2)?subSong->speeds.val[1]:subSong->speeds.val[0]);
2512 w->writeC(subSong->arpLen);
2513 w->writeF(subSong->hz);
2514 w->writeS(subSong->patLen);
2515 w->writeS(subSong->ordersLen);
2516 w->writeC(subSong->hilightA);
2517 w->writeC(subSong->hilightB);
2518 w->writeS(subSong->virtualTempoN);
2519 w->writeS(subSong->virtualTempoD);
2520
2521 w->writeString(subSong->name,false);
2522 w->writeString(subSong->notes,false);
2523
2524 for (int i=0; i<chans; i++) {
2525 for (int j=0; j<subSong->ordersLen; j++) {
2526 w->writeC(subSong->orders.ord[i][j]);
2527 }
2528 }
2529
2530 for (int i=0; i<chans; i++) {
2531 w->writeC(subSong->pat[i].effectCols);
2532 }
2533
2534 for (int i=0; i<chans; i++) {
2535 w->writeC(
2536 (subSong->chanShow[i]?1:0)|
2537 (subSong->chanShowChanOsc[i]?2:0)
2538 );
2539 }
2540
2541 for (int i=0; i<chans; i++) {
2542 w->writeC(subSong->chanCollapse[i]);
2543 }
2544
2545 for (int i=0; i<chans; i++) {
2546 w->writeString(subSong->chanName[i],false);
2547 }
2548
2549 for (int i=0; i<chans; i++) {
2550 w->writeString(subSong->chanShortName[i],false);
2551 }
2552
2553 // speeds
2554 w->writeC(subSong->speeds.len);
2555 for (int i=0; i<16; i++) {
2556 w->writeC(subSong->speeds.val[i]);
2557 }
2558
2559 blockEndSeek=w->tell();
2560 w->seek(blockStartSeek,SEEK_SET);
2561 w->writeI(blockEndSeek-blockStartSeek-4);
2562 w->seek(0,SEEK_END);
2563 }
2564
2565 /// CHIP FLAGS
2566 sysFlagsPtr.reserve(song.systemLen);
2567 for (int i=0; i<song.systemLen; i++) {
2568 String data=song.systemFlags[i].toString();
2569 if (data.empty()) {
2570 sysFlagsPtr.push_back(0);
2571 continue;
2572 }
2573
2574 sysFlagsPtr.push_back(w->tell());
2575 w->write("FLAG",4);
2576 blockStartSeek=w->tell();
2577 w->writeI(0);
2578
2579 w->writeString(data,false);
2580
2581 blockEndSeek=w->tell();
2582 w->seek(blockStartSeek,SEEK_SET);
2583 w->writeI(blockEndSeek-blockStartSeek-4);
2584 w->seek(0,SEEK_END);
2585 }
2586
2587 /// ASSET DIRECTORIES
2588 assetDirPtr[0]=w->tell();
2589 putAssetDirData(w,song.insDir);
2590 assetDirPtr[1]=w->tell();
2591 putAssetDirData(w,song.waveDir);
2592 assetDirPtr[2]=w->tell();
2593 putAssetDirData(w,song.sampleDir);
2594
2595 /// INSTRUMENT
2596 insPtr.reserve(song.insLen);
2597 for (int i=0; i<song.insLen; i++) {
2598 DivInstrument* ins=song.ins[i];
2599 insPtr.push_back(w->tell());
2600 ins->putInsData2(w,false);
2601 }
2602
2603 /// WAVETABLE
2604 wavePtr.reserve(song.waveLen);
2605 for (int i=0; i<song.waveLen; i++) {
2606 DivWavetable* wave=song.wave[i];
2607 wavePtr.push_back(w->tell());
2608 wave->putWaveData(w);
2609 }
2610
2611 /// SAMPLE
2612 samplePtr.reserve(song.sampleLen);
2613 for (int i=0; i<song.sampleLen; i++) {
2614 DivSample* sample=song.sample[i];
2615 samplePtr.push_back(w->tell());
2616 sample->putSampleData(w);
2617 }
2618
2619 /// PATTERN
2620 patPtr.reserve(patsToWrite.size());
2621 for (PatToWrite& i: patsToWrite) {
2622 DivPattern* pat=song.subsong[i.subsong]->pat[i.chan].getPattern(i.pat,false);
2623 patPtr.push_back(w->tell());
2624
2625 if (newPatternFormat) {
2626 w->write("PATN",4);
2627 blockStartSeek=w->tell();
2628 w->writeI(0);
2629
2630 w->writeC(i.subsong);
2631 w->writeC(i.chan);
2632 w->writeS(i.pat);
2633 w->writeString(pat->name,false);
2634
2635 unsigned char emptyRows=0;
2636
2637 for (int j=0; j<song.subsong[i.subsong]->patLen; j++) {
2638 unsigned char mask=0;
2639 unsigned char finalNote=255;
2640 unsigned short effectMask=0;
2641
2642 if (pat->data[j][0]==100) {
2643 finalNote=180;
2644 } else if (pat->data[j][0]==101) { // note release
2645 finalNote=181;
2646 } else if (pat->data[j][0]==102) { // macro release
2647 finalNote=182;
2648 } else if (pat->data[j][1]==0 && pat->data[j][0]==0) {
2649 finalNote=255;
2650 } else {
2651 int seek=(pat->data[j][0]+(signed char)pat->data[j][1]*12)+60;
2652 if (seek<0 || seek>=180) {
2653 finalNote=255;
2654 } else {
2655 finalNote=seek;
2656 }
2657 }
2658
2659 if (finalNote!=255) mask|=1; // note
2660 if (pat->data[j][2]!=-1) mask|=2; // instrument
2661 if (pat->data[j][3]!=-1) mask|=4; // volume
2662 for (int k=0; k<song.subsong[i.subsong]->pat[i.chan].effectCols*2; k+=2) {
2663 if (k==0) {
2664 if (pat->data[j][4+k]!=-1) mask|=8;
2665 if (pat->data[j][5+k]!=-1) mask|=16;
2666 } else if (k<8) {
2667 if (pat->data[j][4+k]!=-1 || pat->data[j][5+k]!=-1) mask|=32;
2668 } else {
2669 if (pat->data[j][4+k]!=-1 || pat->data[j][5+k]!=-1) mask|=64;
2670 }
2671
2672 if (pat->data[j][4+k]!=-1) effectMask|=(1<<k);
2673 if (pat->data[j][5+k]!=-1) effectMask|=(2<<k);
2674 }
2675
2676 if (mask==0) {
2677 emptyRows++;
2678 if (emptyRows>127) {
2679 w->writeC(128|(emptyRows-2));
2680 emptyRows=0;
2681 }
2682 } else {
2683 if (emptyRows>1) {
2684 w->writeC(128|(emptyRows-2));
2685 emptyRows=0;
2686 } else if (emptyRows) {
2687 w->writeC(0);
2688 emptyRows=0;
2689 }
2690
2691 w->writeC(mask);
2692
2693 if (mask&32) w->writeC(effectMask&0xff);
2694 if (mask&64) w->writeC((effectMask>>8)&0xff);
2695
2696 if (mask&1) w->writeC(finalNote);
2697 if (mask&2) w->writeC(pat->data[j][2]);
2698 if (mask&4) w->writeC(pat->data[j][3]);
2699 if (mask&8) w->writeC(pat->data[j][4]);
2700 if (mask&16) w->writeC(pat->data[j][5]);
2701 if (mask&32) {
2702 if (effectMask&4) w->writeC(pat->data[j][6]);
2703 if (effectMask&8) w->writeC(pat->data[j][7]);
2704 if (effectMask&16) w->writeC(pat->data[j][8]);
2705 if (effectMask&32) w->writeC(pat->data[j][9]);
2706 if (effectMask&64) w->writeC(pat->data[j][10]);
2707 if (effectMask&128) w->writeC(pat->data[j][11]);
2708 }
2709 if (mask&64) {
2710 if (effectMask&256) w->writeC(pat->data[j][12]);
2711 if (effectMask&512) w->writeC(pat->data[j][13]);
2712 if (effectMask&1024) w->writeC(pat->data[j][14]);
2713 if (effectMask&2048) w->writeC(pat->data[j][15]);
2714 if (effectMask&4096) w->writeC(pat->data[j][16]);
2715 if (effectMask&8192) w->writeC(pat->data[j][17]);
2716 if (effectMask&16384) w->writeC(pat->data[j][18]);
2717 if (effectMask&32768) w->writeC(pat->data[j][19]);
2718 }
2719 }
2720 }
2721
2722 // stop
2723 w->writeC(0xff);
2724 } else {
2725 w->write("PATR",4);
2726 blockStartSeek=w->tell();
2727 w->writeI(0);
2728
2729 w->writeS(i.chan);
2730 w->writeS(i.pat);
2731 w->writeS(i.subsong);
2732
2733 w->writeS(0); // reserved
2734
2735 for (int j=0; j<song.subsong[i.subsong]->patLen; j++) {
2736 w->writeS(pat->data[j][0]); // note
2737 w->writeS(pat->data[j][1]); // octave
2738 w->writeS(pat->data[j][2]); // instrument
2739 w->writeS(pat->data[j][3]); // volume
2740 #ifdef TA_BIG_ENDIAN
2741 for (int k=0; k<song.subsong[i.subsong]->pat[i.chan].effectCols*2; k++) {
2742 w->writeS(pat->data[j][4+k]);
2743 }
2744 #else
2745 w->write(&pat->data[j][4],2*song.subsong[i.subsong]->pat[i.chan].effectCols*2); // effects
2746 #endif
2747 }
2748
2749 w->writeString(pat->name,false);
2750 }
2751
2752 blockEndSeek=w->tell();
2753 w->seek(blockStartSeek,SEEK_SET);
2754 w->writeI(blockEndSeek-blockStartSeek-4);
2755 w->seek(0,SEEK_END);
2756 }
2757
2758 /// POINTERS
2759 w->seek(ptrSeek,SEEK_SET);
2760
2761 for (int i=0; i<song.insLen; i++) {
2762 w->writeI(insPtr[i]);
2763 }
2764
2765 // wavetable pointers
2766 for (int i=0; i<song.waveLen; i++) {
2767 w->writeI(wavePtr[i]);
2768 }
2769
2770 // sample pointers
2771 for (int i=0; i<song.sampleLen; i++) {
2772 w->writeI(samplePtr[i]);
2773 }
2774
2775 // pattern pointers
2776 for (int i: patPtr) {
2777 w->writeI(i);
2778 }
2779
2780 // subsong pointers
2781 w->seek(subSongPtrSeek,SEEK_SET);
2782 for (size_t i=0; i<(song.subsong.size()-1); i++) {
2783 w->writeI(subSongPtr[i]);
2784 }
2785
2786 // flag pointers
2787 w->seek(sysFlagsPtrSeek,SEEK_SET);
2788 for (size_t i=0; i<sysFlagsPtr.size(); i++) {
2789 w->writeI(sysFlagsPtr[i]);
2790 }
2791
2792 // asset dir pointers
2793 w->seek(assetDirPtrSeek,SEEK_SET);
2794 for (size_t i=0; i<3; i++) {
2795 w->writeI(assetDirPtr[i]);
2796 }
2797
2798 saveLock.unlock();
2799 return w;
2800 }
2801