Debian Codegraph @

src/engine/fileOps/ftm.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 // most of this code written by LTVA
21 // ported to Furnace by tildearrow
22
23 // portions apparently taken from FamiTracker source, which is under GPLv2+
24
25 // TODO:
26 // - format code?
27
28 #include "fileOpsCommon.h"
29
30 #define CHECK_BLOCK_VERSION(x) \
31 if (blockVersion>x) { \
32 logW("incompatible block version %d for %s!", blockVersion, blockName); \
33 }
34
35 enum FTEffects {
36 FT_EF_NONE = 0,
37 FT_EF_SPEED, // Speed
38 FT_EF_JUMP, // Jump
39 FT_EF_SKIP, // Skip
40 FT_EF_HALT, // Halt
41 FT_EF_VOLUME, // Volume
42 FT_EF_PORTAMENTO, // Porta on
43 FT_EF_PORTAOFF, // Porta off unused
44 FT_EF_SWEEPUP, // Sweep up
45 FT_EF_SWEEPDOWN, // Sweep down
46 FT_EF_ARPEGGIO, // Arpeggio
47 FT_EF_VIBRATO, // Vibrato
48 FT_EF_TREMOLO, // Tremolo
49 FT_EF_PITCH, // Pitch
50 FT_EF_DELAY, // Note delay
51 FT_EF_DAC, // DAC setting
52 FT_EF_PORTA_UP, // Portamento up
53 FT_EF_PORTA_DOWN, // Portamento down
54 FT_EF_DUTY_CYCLE, // Duty cycle
55 FT_EF_SAMPLE_OFFSET, // Sample offset
56 FT_EF_SLIDE_UP, // Slide up
57 FT_EF_SLIDE_DOWN, // Slide down
58 FT_EF_VOLUME_SLIDE, // Volume slide
59 FT_EF_NOTE_CUT, // Note cut
60 FT_EF_RETRIGGER, // DPCM retrigger
61 FT_EF_DELAYED_VOLUME, // // // Delayed channel volume
62 FT_EF_FDS_MOD_DEPTH, // FDS modulation depth
63 FT_EF_FDS_MOD_SPEED_HI, // FDS modulation speed hi
64 FT_EF_FDS_MOD_SPEED_LO, // FDS modulation speed lo
65 FT_EF_DPCM_PITCH, // DPCM Pitch
66 FT_EF_SUNSOFT_ENV_TYPE, // Sunsoft envelope type
67 FT_EF_SUNSOFT_ENV_HI, // Sunsoft envelope high
68 FT_EF_SUNSOFT_ENV_LO, // Sunsoft envelope low
69 FT_EF_SUNSOFT_NOISE, // // // 050B Sunsoft noise period
70 FT_EF_VRC7_PORT, // // // 050B VRC7 custom patch port
71 FT_EF_VRC7_WRITE, // // // 050B VRC7 custom patch write
72 FT_EF_NOTE_RELEASE, // // // Delayed release
73 FT_EF_GROOVE, // // // Groove
74 FT_EF_TRANSPOSE, // // // Delayed transpose
75 FT_EF_N163_WAVE_BUFFER, // // // N163 wave buffer
76 FT_EF_FDS_VOLUME, // // // FDS volume envelope
77 FT_EF_FDS_MOD_BIAS, // // // FDS auto-FM bias
78 FT_EF_PHASE_RESET, // Reset waveform phase without retriggering note (VRC6-only so far)
79 FT_EF_HARMONIC, // Multiply the note pitch by an integer
80 FT_EF_TARGET_VOLUME_SLIDE, // // !! Target volume slide
81
82 FT_EF_COUNT
83 };
84
85 const int ftEffectMap[]={
86 -1, // none
87 0x0f,
88 0x0b,
89 0x0d,
90 0xff,
91 -1, // volume? not supported in Furnace yet
92 0x03,
93 0x03, // unused?
94 0x13, 0x14, 0x00, 0x04, 0x07, 0xe5, 0xed, 0x11,
95 0x01, // porta up
96 0x02, // porta down
97 0x12,
98 0x90, // sample offset - not supported yet
99 0xe1, // Slide up
100 0xe2, // Slide down
101 0x0a, 0xec, 0x0c,
102 -1, // delayed volume - not supported yet
103 0x11, // FDS modulation depth
104 0x12, // FDS modulation speed hi
105 0x13, // FDS modulation speed lo
106 0x20, // DPCM pitch
107 0x22, // Sunsoft envelope type
108 0x24, // Sunsoft envelope high
109 0x23, // Sunsoft envelope low
110 0x21, // 050B Sunsoft noise period
111 -1, // VRC7 "custom patch port" - not supported?
112 -1, // VRC7 "custom patch write"
113 0xfc, // delayed release
114 0x09, // select groove
115 0xe6, // delayed note transpose
116 0x11, // Namco 163 wave RAM offset
117 -1, // FDS vol env - not supported
118 -1, // FDS auto FM - not supported yet
119 -1, // phase reset - not supported
120 -1, // harmonic - not supported
121 -1, // target volume slide - not supported
122 };
123
124 enum EFTEffects {
125 EFT_EF_NONE = 0,
126 EFT_EF_SPEED, // Speed
127 EFT_EF_JUMP, // Jump
128 EFT_EF_SKIP, // Skip
129 EFT_EF_HALT, // Halt
130 EFT_EF_VOLUME, // Volume
131 EFT_EF_PORTAMENTO, // Porta on
132 EFT_EF_PORTAOFF, // Porta off unused
133 EFT_EF_SWEEPUP, // Sweep up
134 EFT_EF_SWEEPDOWN, // Sweep down
135 EFT_EF_ARPEGGIO, // Arpeggio
136 EFT_EF_VIBRATO, // Vibrato
137 EFT_EF_TREMOLO, // Tremolo
138 EFT_EF_PITCH, // Pitch
139 EFT_EF_DELAY, // Note delay
140 EFT_EF_DAC, // DAC setting
141 EFT_EF_PORTA_UP, // Portamento up
142 EFT_EF_PORTA_DOWN, // Portamento down
143 EFT_EF_DUTY_CYCLE, // Duty cycle
144 EFT_EF_SAMPLE_OFFSET, // Sample offset
145 EFT_EF_SLIDE_UP, // Slide up
146 EFT_EF_SLIDE_DOWN, // Slide down
147 EFT_EF_VOLUME_SLIDE, // Volume slide
148 EFT_EF_NOTE_CUT, // Note cut
149 EFT_EF_RETRIGGER, // DPCM retrigger
150 EFT_EF_DELAYED_VOLUME, // // // Delayed channel volume
151 EFT_EF_FDS_MOD_DEPTH, // FDS modulation depth
152 EFT_EF_FDS_MOD_SPEED_HI, // FDS modulation speed hi
153 EFT_EF_FDS_MOD_SPEED_LO, // FDS modulation speed lo
154 EFT_EF_DPCM_PITCH, // DPCM Pitch
155 EFT_EF_SUNSOFT_ENV_TYPE, // Sunsoft envelope type
156 EFT_EF_SUNSOFT_ENV_HI, // Sunsoft envelope high
157 EFT_EF_SUNSOFT_ENV_LO, // Sunsoft envelope low
158 EFT_EF_SUNSOFT_NOISE, // // // 050B Sunsoft noise period
159 EFT_EF_AY8930_PULSE_WIDTH, // // // AY8930 pulse width
160 EFT_EF_AY8930_AND_MASK, // // // AY8930 noise AND mask
161 EFT_EF_AY8930_OR_MASK, // // // AY8930 noise OR mask
162 EFT_EF_AY8930_VOL, // // // AY8930 extra volume bit
163 EFT_EF_VRC7_PORT, // // // 050B VRC7 custom patch port
164 EFT_EF_VRC7_WRITE, // // // 050B VRC7 custom patch write
165 EFT_EF_NOTE_RELEASE, // // // Delayed release
166 EFT_EF_GROOVE, // // // Groove
167 EFT_EF_TRANSPOSE, // // // Delayed transpose
168 EFT_EF_N163_WAVE_BUFFER, // // // N163 wave buffer
169 EFT_EF_FDS_VOLUME, // // // FDS volume envelope
170 EFT_EF_FDS_MOD_BIAS, // // // FDS auto-FM bias
171 EFT_EF_PHASE_RESET, // Reset waveform phase without retriggering note (VRC6-only so far)
172 EFT_EF_HARMONIC, // Multiply the note pitch by an integer
173 EFT_EF_PWM, // // // Pulse width modulation effect
174 EFT_EF_VOLUME_OFFSET, // // // Relative volume change
175 EFT_EF_SAA_NOISE_MODE, // // // SAA1099 noise mode
176 EFT_EF_SID_FILTER_RESONANCE, // // // SID filter resonance
177 EFT_EF_SID_FILTER_CUTOFF_HI, // // // SID filter cutoff hi
178 EFT_EF_SID_FILTER_CUTOFF_LO, // // // SID filter cutoff lo
179 EFT_EF_SID_FILTER_MODE, // // // SID filter mode
180 EFT_EF_SID_ENVELOPE, // // // SID envelope parameters
181 EFT_EF_SID_RING, // // // SID ringmod
182
183 EFT_EF_COUNT
184 };
185
186 const int eftEffectMap[] = {
187 -1, // none
188 0x0f,
189 0x0b,
190 0x0d,
191 0xff,
192 -1, // volume? not supported in Furnace yet
193 0x03,
194 0x03, // unused?
195 0x13,
196 0x14,
197 0x00,
198 0x04,
199 0x07,
200 0xe5,
201 0xed,
202 0x11,
203 0x01, // porta up
204 0x02, // porta down
205 0x12,
206 0x90, // sample offset - not supported yet
207 0xe1, // Slide up
208 0xe2, // Slide down
209 0x0a,
210 0xec,
211 0x0c,
212 -1, // delayed volume - not supported yet
213 0x11, // FDS modulation depth
214 0x12, // FDS modulation speed hi
215 0x13, // FDS modulation speed lo
216 0x20, // DPCM pitch
217 0x22, // Sunsoft envelope type
218 0x24, // Sunsoft envelope high
219 0x23, // Sunsoft envelope low
220 0x21, // 050B Sunsoft noise period
221 0x12, // // // AY8930 pulse width
222 0x27, // // // AY8930 noise AND mask
223 0x28, // // // AY8930 noise OR mask
224 0x100, // // // AY8930 extra volume bit
225 -1, // VRC7 "custom patch port" - not supported?
226 -1, // VRC7 "custom patch write"
227 0xfc, // delayed release
228 0x09, // select groove
229 0xe6, // delayed note transpose
230 0x11, // Namco 163 wave RAM offset
231 -1, // FDS vol env - not supported
232 -1, // FDS auto FM - not supported yet
233 -1, // phase reset - not supported
234 -1, // harmonic - not supported
235 -1, // // // Pulse width modulation effect
236 -1, // // // Relative volume change
237 -1, // // // SAA1099 noise mode
238 0x13, // // // SID filter resonance
239 0x40, // // // SID filter cutoff hi
240 0x40, // // // SID filter cutoff lo
241 0x14, // // // SID filter mode
242 -1, // // // SID envelope parameters
243 -1, // // // SID ringmod
244 };
245
246 const int eff_conversion_050[][2] = {
247 {FT_EF_SUNSOFT_NOISE, FT_EF_NOTE_RELEASE},
248 {FT_EF_VRC7_PORT, FT_EF_GROOVE},
249 {FT_EF_VRC7_WRITE, FT_EF_TRANSPOSE},
250 {FT_EF_NOTE_RELEASE, FT_EF_N163_WAVE_BUFFER},
251 {FT_EF_GROOVE, FT_EF_FDS_VOLUME},
252 {FT_EF_TRANSPOSE, FT_EF_FDS_MOD_BIAS},
253 {FT_EF_N163_WAVE_BUFFER, FT_EF_SUNSOFT_NOISE},
254 {FT_EF_FDS_VOLUME, FT_EF_VRC7_PORT},
255 {FT_EF_FDS_MOD_BIAS, FT_EF_VRC7_WRITE},
256 {0xFF, 0xFF}, // end mark
257 };
258
259 constexpr int ftEffectMapSize = sizeof(ftEffectMap) / sizeof(int);
260 constexpr int eftEffectMapSize = sizeof(eftEffectMap) / sizeof(int);
261
262 int convertMacros2A03[5] = {(int)DIV_MACRO_VOL, (int)DIV_MACRO_ARP, (int)DIV_MACRO_PITCH, -1, (int)DIV_MACRO_DUTY};
263 int convertMacrosVRC6[5] = {(int)DIV_MACRO_VOL, (int)DIV_MACRO_ARP, (int)DIV_MACRO_PITCH, -1, (int)DIV_MACRO_DUTY};
264 int convertMacrosN163[5] = {(int)DIV_MACRO_VOL, (int)DIV_MACRO_ARP, (int)DIV_MACRO_PITCH, -1, (int)DIV_MACRO_WAVE};
265 int convertMacros5B[5] = {(int)DIV_MACRO_VOL, (int)DIV_MACRO_ARP, (int)DIV_MACRO_PITCH, -1, (int)DIV_MACRO_DUTY};
266
267 int convertMacrosSID[5] = {(int)DIV_MACRO_VOL, (int)DIV_MACRO_ARP, (int)DIV_MACRO_PITCH, -1, (int)DIV_MACRO_WAVE};
268
269 int convert_vrc6_duties[4] = {1, 3, 7, 3};
270
271 int findEmptyFx(short* data) {
272 for (int i=0; i<7; i++) {
273 if (data[4+i*2]==-1) return i;
274 }
275 return -1;
276 }
277
278 void copyMacro(DivInstrument* ins, DivInstrumentMacro* from, int macro_type, int setting) {
279 DivInstrumentMacro* to = NULL;
280
281 switch (ins->type) {
282 case DIV_INS_NES: {
283 if (convertMacros2A03[macro_type] == -1)
284 return;
285 to = ins->std.macroByType((DivMacroType)convertMacros2A03[macro_type]);
286 break;
287 }
288 case DIV_INS_VRC6: {
289 if (convertMacrosVRC6[macro_type] == -1)
290 return;
291 to = ins->std.macroByType((DivMacroType)convertMacros2A03[macro_type]);
292 break;
293 }
294 case DIV_INS_N163: {
295 if (convertMacrosVRC6[macro_type] == -1)
296 return;
297 to = ins->std.macroByType((DivMacroType)convertMacrosN163[macro_type]);
298 break;
299 }
300 case DIV_INS_AY: {
301 if (convertMacrosVRC6[macro_type] == -1)
302 return;
303 to = ins->std.macroByType((DivMacroType)convertMacros5B[macro_type]);
304 break;
305 }
306 case DIV_INS_C64: {
307 if (convertMacrosVRC6[macro_type] == -1)
308 return;
309 to = ins->std.macroByType((DivMacroType)convertMacrosSID[macro_type]);
310 break;
311 }
312 default:
313 break;
314 }
315
316 if (to == NULL)
317 return;
318
319 for (int i = 0; i < 256; i++) {
320 to->val[i] = from->val[i];
321
322 if ((DivMacroType)convertMacros2A03[macro_type] == DIV_MACRO_ARP) {
323 if (setting == 0) // absolute
324 {
325 if (to->val[i] > 0x60) {
326 int temp = to->val[i];
327 to->val[i] = -1 * (0xff - temp + 1); // 2s complement integer my beloved
328 }
329 }
330
331 if (setting == 1) // fixed
332 {
333 to->val[i] |= (1 << 30); // 30th bit in Furnace arp macro marks fixed mode
334 }
335 }
336
337 if ((DivMacroType)convertMacros2A03[macro_type] == DIV_MACRO_PITCH) {
338 if (setting == 0 || setting == 1) // relative/absolute
339 {
340 int temp = to->val[i];
341
342 if (temp < 0x80) {
343 to->val[i] = -1 * temp;
344 } else {
345 to->val[i] = (0x100 - temp);
346 }
347 }
348 }
349
350 if ((DivMacroType)convertMacrosN163[macro_type] == DIV_MACRO_WAVE && ins->type == DIV_INS_N163) {
351 // pfffff
352 }
353
354 if ((DivMacroType)convertMacrosN163[macro_type] == DIV_MACRO_PITCH && (ins->type == DIV_INS_N163 || ins->type == DIV_INS_C64)) {
355 to->val[i] *= -1; // wtf is going on!!!
356 }
357 }
358
359 to->len = from->len;
360 to->delay = from->delay;
361 to->mode = from->mode;
362 to->rel = from->rel;
363 to->speed = from->speed;
364 to->loop = from->loop;
365 to->open = from->open;
366
367 if ((DivMacroType)convertMacros2A03[macro_type] == DIV_MACRO_ARP) {
368 if (setting == 1) // fixed
369 {
370 if (to->loop == 255) // no loop
371 {
372 to->len++;
373 to->val[to->len - 1] = 0; // return to orig pitch (relative mode, 0 offset)
374 }
375 }
376 }
377
378 if ((DivMacroType)convertMacros2A03[macro_type] == DIV_MACRO_PITCH) {
379 if (setting == 0) // relative
380 {
381 to->mode = 1; // setting relative mode
382 }
383 }
384
385 if (ins->type == DIV_INS_AY && macro_type == 4) // S5B noise/mode macro combines noise freq and tone/env/noise settings, so we need to separate them into two macros
386 {
387 DivInstrumentMacro* wave = &ins->std.waveMacro;
388 to = &ins->std.dutyMacro;
389
390 wave->len = to->len;
391 wave->delay = to->delay;
392 wave->mode = to->mode;
393 wave->rel = to->rel;
394 wave->speed = to->speed;
395 wave->loop = to->loop;
396 wave->open = to->open;
397
398 for (int i = 0; i < to->len; i++) {
399 // ? ? ? ?
400
401 logI("%02X", to->val[i]);
402 wave->val[i] = 0;
403
404 int temp = 0;
405
406 if (to->val[i] & 0b10000000) // noise
407 {
408 temp |= 2;
409 }
410 if (to->val[i] & 0b01000000) // tone
411 {
412 temp |= 1;
413 }
414 if (to->val[i] & 0b00100000) // envelope
415 {
416 temp |= 4;
417 }
418
419 wave->val[i] = temp;
420
421 // #define S5B_ENVL 0b10000000
422 // #define S5B_TONE 0b01000000
423 // #define S5B_NOIS 0b00100000
424
425 to->val[i] = to->val[i] & 31;
426 }
427 }
428 }
429
430 bool DivEngine::loadFTM(unsigned char* file, size_t len, bool dnft, bool dnft_sig, bool eft) {
431 SafeReader reader = SafeReader(file, len);
432 warnings = "";
433 try {
434 DivSong ds;
435 String blockName;
436 unsigned int expansions = 0;
437 unsigned int tchans = 0;
438 unsigned int n163Chans = 0;
439 int n163WaveOff[128];
440 bool hasSequence[256][8];
441 unsigned char sequenceIndex[256][8];
442 unsigned char macro_types[256][8];
443 std::vector<DivInstrumentMacro> macros[256];
444 std::vector<String> encounteredBlocks;
445 unsigned char map_channels[DIV_MAX_CHANS];
446 unsigned int hilightA = 4;
447 unsigned int hilightB = 16;
448 double customHz = 60.0;
449
450 unsigned char fds_chan = 0xff;
451 unsigned char vrc6_saw_chan = 0xff;
452 unsigned char n163_chans[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
453 unsigned char vrc7_chans[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
454 unsigned char s5b_chans[3] = {0xff, 0xff, 0xff};
455 unsigned char ay8930_chans[3] = {0xff, 0xff, 0xff};
456
457 // these two seem to go unused, but why?
458 unsigned char vrc6_chans[2] = {0xff, 0xff};
459 unsigned char mmc5_chans[2] = {0xff, 0xff};
460
461 int total_chans = 0;
462
463 memset(n163WaveOff,0,128*sizeof(int));
464 memset(hasSequence, 0, 256 * 8 * sizeof(bool));
465 memset(sequenceIndex, 0, 256 * 8);
466 memset(macro_types, 0, 256 * 8);
467 memset(map_channels, 0xfe, DIV_MAX_CHANS * sizeof(unsigned char));
468
469 for (int i = 0; i < 256; i++) {
470 for (int j = 0; j < 8; j++) {
471 macros[i].push_back(DivInstrumentMacro(DIV_MACRO_VOL));
472 macros[i][j].open|=9;
473 }
474 }
475
476 if (!reader.seek((dnft && dnft_sig) ? 21 : 18, SEEK_SET)) {
477 logE("premature end of file!");
478 lastError = "incomplete file";
479 delete[] file;
480 return false;
481 }
482 ds.version = (unsigned short)reader.readI();
483 logI("module version %d (0x%.4x)", ds.version, ds.version);
484
485 if ((ds.version > 0x0450 && !eft) || (eft && ds.version > 0x0460)) {
486 logE("incompatible version %x!", ds.version);
487 lastError = "incompatible version";
488 delete[] file;
489 return false;
490 }
491
492 for (DivSubSong* i: ds.subsong) {
493 i->clearData();
494 delete i;
495 }
496 ds.subsong.clear();
497
498 ds.linearPitch = 0;
499
500 unsigned int pal = 0;
501
502 while (true) {
503 blockName = reader.readString(3);
504 if (blockName == "END") {
505 // end of module
506 logD("end of data");
507 break;
508 }
509
510 // not the end
511 if (!reader.seek(-3, SEEK_CUR)) {
512 logE("couldn't seek back by 3!");
513 lastError = "couldn't seek back by 3";
514 delete[] file;
515 return false;
516 }
517 blockName = reader.readString(16);
518 unsigned int blockVersion = (unsigned int)reader.readI();
519 unsigned int blockSize = (unsigned int)reader.readI();
520 size_t blockStart = reader.tell();
521
522 logD("reading block %s (version %d, %d bytes, position %x)", blockName, blockVersion, blockSize, reader.tell());
523
524 for (String& i: encounteredBlocks) {
525 if (blockName==i) {
526 logE("duplicate block %s!",blockName);
527 lastError = "duplicate block "+blockName;
528 ds.unload();
529 delete[] file;
530 return false;
531 }
532 }
533 encounteredBlocks.push_back(blockName);
534
535 if (blockName == "PARAMS") {
536 // versions 7-9 don't change anything?
537 CHECK_BLOCK_VERSION(9);
538 unsigned int oldSpeedTempo = 0;
539 if (blockVersion <= 1) {
540 oldSpeedTempo = reader.readI();
541 }
542 if (blockVersion >= 2) {
543 if (eft) {
544 if (blockVersion < 7) {
545 expansions = reader.readC();
546 } else {
547 expansions = reader.readI();
548 }
549 } else {
550 expansions = reader.readC();
551 }
552 }
553
554 tchans = reader.readI();
555
556 if (tchans>=DIV_MAX_CHANS) {
557 logE("invalid channel count! %d",tchans);
558 lastError = "invalid channel count";
559 delete[] file;
560 return false;
561 }
562
563 if (tchans == 5) {
564 expansions = 0; // This is strange. Sometimes expansion chip is set to 0xFF in files
565 }
566
567 pal = reader.readI();
568 if (!eft) {
569 if (blockVersion >= 7) {
570 // advanced Hz control
571 int controlType = reader.readI();
572 int readHz=reader.readI();
573 if (readHz<=0) {
574 customHz=60.0;
575 } else switch (controlType) {
576 case 1:
577 customHz = 1000000.0 / (double)readHz;
578 break;
579 default:
580 logW("unsupported tick rate control type %d",controlType);
581 break;
582 }
583 } else {
584 customHz = reader.readI();
585 }
586 } else {
587 customHz = reader.readI();
588 }
589
590 logV("before clamp: %f",customHz);
591
592 if (customHz>1000.0) customHz=1000.0;
593
594 unsigned int newVibrato = 0;
595 bool sweepReset = false;
596 unsigned int speedSplitPoint = 0;
597 if (blockVersion >= 3) {
598 newVibrato = reader.readI();
599 }
600 if (blockVersion >= 9) {
601 sweepReset = reader.readI();
602 }
603 if (eft) {
604 if (blockVersion >= 4 && blockVersion <= 7) {
605 hilightA = reader.readI();
606 hilightB = reader.readI();
607 }
608 } else {
609 if (blockVersion >= 4 && blockVersion < 7) {
610 hilightA = reader.readI();
611 hilightB = reader.readI();
612 }
613 }
614
615 if ((expansions & 16) && blockVersion >= 5) { // N163 channels
616 n163Chans = reader.readI();
617 if (n163Chans<1 || n163Chans>=9) {
618 logE("invalid Namco 163 channel count! %d",n163Chans);
619 lastError = "invalid Namco 163 channel count";
620 delete[] file;
621 return false;
622 }
623 }
624 if (blockVersion >= 6) {
625 speedSplitPoint = reader.readI();
626 }
627
628 if (blockVersion == 8) {
629 int fineTuneCents = reader.readC() * 100;
630 fineTuneCents += reader.readC();
631
632 ds.tuning = 440.0 * pow(2.0, (double)fineTuneCents / 1200.0);
633
634 logV("detune: %d", fineTuneCents);
635 }
636
637 logV("old speed/tempo: %d", oldSpeedTempo);
638 logV("expansions: %x", expansions);
639 logV("channels: %d", tchans);
640 logV("PAL: %d", pal);
641 logV("custom Hz: %f", customHz);
642 logV("new vibrato: %d", newVibrato);
643 logV("N163 channels: %d", n163Chans);
644 logV("highlight 1: %d", hilightA);
645 logV("highlight 2: %d", hilightB);
646 logV("split point: %d", speedSplitPoint);
647 logV("sweep reset: %d", sweepReset);
648
649 //addWarning("FamiTracker import is experimental.");
650
651 // initialize channels
652 int systemID = 0;
653
654 int curr_chan = 0;
655 int map_ch = 0;
656
657 ds.system[systemID++] = DIV_SYSTEM_NES;
658 ds.systemFlags[0].set("resetSweep",true); // FamiTracker behavior
659
660 if (pal) {
661 ds.systemFlags[0].set("clockSel", 1); // PAL clock
662 }
663
664 for (int ch = 0; ch < 5; ch++) {
665 map_channels[curr_chan] = map_ch;
666 curr_chan++;
667 map_ch++;
668 }
669
670 if (expansions & 1) {
671 ds.system[systemID++] = DIV_SYSTEM_VRC6;
672
673 for (int ch = 0; ch < 3; ch++) {
674 map_channels[curr_chan] = map_ch;
675
676 if (ch < 2) {
677 vrc6_chans[ch] = map_ch;
678 logV("%d",vrc6_chans[ch]);
679 }
680
681 curr_chan++;
682 map_ch++;
683 }
684
685 vrc6_saw_chan = map_ch - 1;
686 }
687 if (expansions & 8) {
688 ds.system[systemID++] = DIV_SYSTEM_MMC5;
689
690 for (int ch = 0; ch < (eft ? 3 : 2); ch++) {
691 map_channels[curr_chan] = map_ch;
692
693 if (ch < 2) {
694 mmc5_chans[ch] = map_ch;
695 logV("%d",mmc5_chans[ch]);
696 }
697
698 curr_chan++;
699 map_ch++;
700 }
701
702 if (!eft) {
703 map_channels[curr_chan] = map_ch; // do not populate and skip MMC5 PCM channel!
704 map_ch++;
705 }
706 }
707 if (expansions & 16) {
708 ds.system[systemID] = DIV_SYSTEM_N163;
709 ds.systemFlags[systemID].set("channels", (int)n163Chans - 1);
710 systemID++;
711
712 for (int ch = 0; ch < (int)n163Chans; ch++) {
713 map_channels[curr_chan] = map_ch;
714 curr_chan++;
715 n163_chans[ch] = map_ch;
716 map_ch++;
717 }
718
719 for (int ch = 0; ch < (8 - (int)n163Chans); ch++) {
720 map_channels[curr_chan] = map_ch; // do not populate and skip the missing N163 channels!
721 map_ch++;
722 }
723 }
724 if (expansions & 4) {
725 ds.system[systemID++] = DIV_SYSTEM_FDS;
726
727 map_channels[curr_chan] = map_ch;
728 fds_chan = map_ch;
729 curr_chan++;
730 map_ch++;
731 }
732 if (expansions & 2) {
733 ds.system[systemID++] = DIV_SYSTEM_VRC7;
734
735 for (int ch = 0; ch < 6; ch++) {
736 map_channels[curr_chan] = map_ch;
737 vrc7_chans[ch] = map_ch;
738 curr_chan++;
739 map_ch++;
740 }
741 }
742 if (expansions & 32) {
743 ds.system[systemID] = DIV_SYSTEM_AY8910;
744 ds.systemFlags[systemID++].set("chipType", 2); // Sunsoft 5B
745
746 for (int ch = 0; ch < 3; ch++) {
747 map_channels[curr_chan] = map_ch;
748 s5b_chans[ch] = map_ch;
749 curr_chan++;
750 map_ch++;
751 }
752 }
753 if (expansions & 64) {
754 ds.system[systemID++] = DIV_SYSTEM_AY8930;
755
756 for (int ch = 0; ch < 3; ch++) {
757 map_channels[curr_chan] = map_ch;
758 ay8930_chans[ch] = map_ch;
759 curr_chan++;
760 map_ch++;
761 }
762 }
763 if (expansions & 128) {
764 ds.system[systemID++] = DIV_SYSTEM_SAA1099;
765
766 for (int ch = 0; ch < 6; ch++) {
767 map_channels[curr_chan] = map_ch;
768 curr_chan++;
769 map_ch++;
770 }
771 }
772 if (expansions & 256) {
773 ds.system[systemID++] = DIV_SYSTEM_5E01;
774
775 for (int ch = 0; ch < 5; ch++) {
776 map_channels[curr_chan] = map_ch;
777 curr_chan++;
778 map_ch++;
779 }
780 }
781 if (expansions & 512) {
782 ds.system[systemID++] = DIV_SYSTEM_C64_6581;
783
784 for (int ch = 0; ch < 3; ch++) {
785 map_channels[curr_chan] = map_ch;
786 curr_chan++;
787 map_ch++;
788 }
789 }
790 if (expansions & 1024) {
791 ds.system[systemID++] = DIV_SYSTEM_C64_8580;
792
793 for (int ch = 0; ch < 3; ch++) {
794 map_channels[curr_chan] = map_ch;
795 curr_chan++;
796 map_ch++;
797 }
798 }
799 if (expansions & 2048) {
800 ds.system[systemID++] = DIV_SYSTEM_POKEY;
801
802 for (int ch = 0; ch < 4; ch++) {
803 map_channels[curr_chan] = map_ch;
804 curr_chan++;
805 map_ch++;
806 }
807 }
808 ds.systemLen = systemID;
809
810 for (int i = 0; i < curr_chan; i++) {
811 logV("map ch: fami ch %d mapped to furnace ch %d", i, map_channels[i]);
812 }
813
814 unsigned int calcChans = 0;
815 for (int i = 0; i < ds.systemLen; i++) {
816 if (ds.system[i] == DIV_SYSTEM_MMC5 && !eft) {
817 calcChans--; // no PCM channel for MMC5 in famitracker
818 }
819
820 calcChans += getChannelCount(ds.system[i]);
821 total_chans += getChannelCount(ds.system[i]);
822
823 if (ds.system[i] == DIV_SYSTEM_N163) {
824 calcChans -= getChannelCount(ds.system[i]);
825 calcChans += (int)n163Chans;
826 }
827 }
828 if (calcChans != tchans) {
829 // TODO: would ignore trigger CVE? too bad if so!
830 if (!eft || (eft && (expansions & 8) == 0)) // ignore since I have no idea how to tell apart E-FT versions which do or do not have PCM chan. Yes, this may lead to all the righer channels to be shifted but at least you still get note data!
831 {
832 logE("channel counts do not match! %d != %d", tchans, calcChans);
833 lastError = "channel counts do not match";
834 delete[] file;
835 return false;
836 }
837 }
838 if (tchans > DIV_MAX_CHANS) {
839 logE("too many channels!");
840 lastError = "too many channels";
841 delete[] file;
842 return false;
843 }
844 if (blockVersion == 9 && blockSize - (reader.tell() - blockStart) == 2) // weird
845 {
846 if (!reader.seek(2, SEEK_CUR)) {
847 logE("could not weird-seek by 2!");
848 lastError = "could not weird-seek by 2";
849 delete[] file;
850 return false;
851 }
852 }
853 } else if (blockName == "INFO") {
854 CHECK_BLOCK_VERSION(1);
855 ds.name = reader.readString(32);
856 ds.author = reader.readString(32);
857 ds.copyright = reader.readString(32);
858 ds.systemName = "NES";
859 } else if (blockName == "HEADER") {
860 CHECK_BLOCK_VERSION(4);
861 unsigned char totalSongs=0;
862 if (blockVersion>=2) totalSongs=reader.readC();
863 logV("%d songs:", totalSongs + 1);
864 ds.subsong.reserve(totalSongs);
865 for (int i = 0; i <= totalSongs; i++) {
866 String subSongName;
867 if (blockVersion>=3) subSongName=reader.readString();
868 ds.subsong.push_back(new DivSubSong);
869 ds.subsong[i]->name = subSongName;
870 ds.subsong[i]->hilightA = hilightA;
871 ds.subsong[i]->hilightB = hilightB;
872 if (customHz != 0) {
873 ds.subsong[i]->hz = customHz;
874 ds.subsong[i]->virtualTempoD = (short)(2.5 * customHz);
875 }
876 logV("- %s", subSongName);
877 }
878 for (unsigned int i = 0; i < tchans; i++) {
879 // TODO: obey channel ID
880 unsigned char chID = reader.readC();
881 logV("for channel ID %d", chID);
882 for (int j = 0; j <= totalSongs; j++) {
883 unsigned char effectCols = reader.readC();
884
885 if (effectCols>7) {
886 logE("too many effect columns!");
887 lastError = "too many effect columns";
888 delete[] file;
889 return false;
890 }
891
892 if (map_channels[i] == 0xfe) {
893 ds.subsong[j]->pat[i].effectCols = 1;
894 logV("- song %d has %d effect columns", j, effectCols);
895 } else {
896 ds.subsong[j]->pat[map_channels[i]].effectCols = effectCols + 1;
897 logV("- song %d has %d effect columns", j, effectCols);
898 }
899 }
900 }
901
902 if (blockVersion >= 4) {
903 for (int i = 0; i <= totalSongs; i++) {
904 ds.subsong[i]->hilightA = (unsigned char)reader.readC();
905 ds.subsong[i]->hilightB = (unsigned char)reader.readC();
906 }
907 }
908 } else if (blockName == "INSTRUMENTS") {
909 CHECK_BLOCK_VERSION(9);
910
911 ds.insLen = reader.readI();
912 if (ds.insLen < 0 || ds.insLen > 256) {
913 logE("too many instruments/out of range!");
914 lastError = "too many instruments/out of range";
915 delete[] file;
916 return false;
917 }
918
919 for (int i = 0; i < 128; i++) {
920 DivInstrument* ins = new DivInstrument;
921 ds.ins.push_back(ins);
922 ds.ins[i]->type = DIV_INS_FM;
923 }
924
925 logV("instruments:");
926 for (int i = 0; i < ds.insLen; i++) {
927 unsigned int insIndex = reader.readI();
928 if (insIndex >= ds.ins.size()) {
929 logE("instrument index %d is out of range!",insIndex);
930 lastError="instrument index out of range";
931 delete[] file;
932 return false;
933 }
934
935 DivInstrument* ins = ds.ins[insIndex];
936 unsigned char insType = reader.readC();
937 switch (insType) {
938 case 1:
939 ins->type = DIV_INS_NES;
940 break;
941 case 2:
942 ins->type = DIV_INS_VRC6;
943 break;
944 case 3: // VRC7
945 ins->type = DIV_INS_OPLL;
946 break;
947 case 4:
948 ins->type = DIV_INS_FDS;
949 break;
950 case 5:
951 ins->type = DIV_INS_N163;
952 break;
953 case 6: // 5B
954 ins->type = DIV_INS_AY;
955 break;
956 case 7: // 6581 SID
957 ins->type = DIV_INS_C64;
958 break;
959 default: {
960 logE("%d: invalid instrument type %d", insIndex, insType);
961 lastError = "invalid instrument type";
962 delete[] file;
963 return false;
964 }
965 }
966
967 // instrument data
968 switch (ins->type) {
969 case DIV_INS_NES: {
970 unsigned int totalSeqs = reader.readI();
971 if (totalSeqs > 5) {
972 logE("%d: too many sequences!", insIndex);
973 lastError = "too many sequences";
974 delete[] file;
975 return false;
976 }
977
978 for (unsigned int j = 0; j < totalSeqs; j++) {
979 hasSequence[insIndex][j] = reader.readC();
980 sequenceIndex[insIndex][j] = reader.readC();
981 }
982
983 int dpcmNotes = (blockVersion >= 2) ? 96 : 72;
984
985 if (blockVersion >= 7) {
986 unsigned int notes = reader.readI();
987 dpcmNotes = notes;
988 }
989
990 // should dpcmNotes be 96 always?
991 for (int j = 0; j < dpcmNotes; j++) {
992 int note = j;
993 if (blockVersion >= 7) {
994 note = reader.readC();
995 }
996 if (note<0 || note>=120) {
997 logE("DPCM note %d out of range!",note);
998 lastError = "DPCM note out of range";
999 delete[] file;
1000 return false;
1001 }
1002 ins->amiga.noteMap[note].map = (short)((unsigned char)reader.readC()) - 1;
1003 unsigned char freq = reader.readC();
1004 ins->amiga.noteMap[note].dpcmFreq = (freq & 15); // 0-15 = 0-15 unlooped, 128-143 = 0-15 looped
1005 ins->amiga.noteMap[note].freq = (freq & 0x80) ? 1 : 0; // loop
1006 if (blockVersion >= 6) {
1007 ins->amiga.noteMap[note].dpcmDelta = (unsigned char)reader.readC(); // DMC value
1008 }
1009 }
1010
1011 ins->amiga.useSample = true;
1012 ins->amiga.useNoteMap = true;
1013
1014 bool empty_note_map = true;
1015
1016 for (int j = 0; j < dpcmNotes; j++) {
1017 if (ins->amiga.noteMap[j].map != -1) {
1018 empty_note_map = false;
1019 }
1020 }
1021
1022 if (empty_note_map) {
1023 ins->amiga.useSample = false;
1024 ins->amiga.useNoteMap = false;
1025 ins->amiga.initSample = -1;
1026 }
1027 break;
1028 }
1029 case DIV_INS_VRC6: {
1030 unsigned int totalSeqs = reader.readI();
1031 if (totalSeqs > 5) {
1032 logE("%d: too many sequences!", insIndex);
1033 lastError = "too many sequences";
1034 delete[] file;
1035 return false;
1036 }
1037
1038 for (unsigned int j = 0; j < totalSeqs; j++) {
1039 hasSequence[insIndex][j] = reader.readC();
1040 sequenceIndex[insIndex][j] = reader.readC();
1041 }
1042 break;
1043 }
1044 case DIV_INS_OPLL: {
1045 ins->fm.opllPreset = (unsigned int)reader.readI();
1046 ins->fm.opllPreset&=15;
1047
1048 unsigned char custom_patch[8];
1049
1050 for (int i = 0; i < 8; i++) {
1051 custom_patch[i] = reader.readC();
1052 }
1053
1054 for (int i = 0; i < 2; i++) {
1055 ins->fm.op[i].am = custom_patch[i] >> 7;
1056 ins->fm.op[i].vib = (custom_patch[i] >> 6) & 1;
1057 ins->fm.op[i].ssgEnv = ((custom_patch[i] >> 5) & 1) << 3;
1058 ins->fm.op[i].ksr = (custom_patch[i] >> 4) & 1;
1059
1060 ins->fm.op[i].mult = custom_patch[i] & 15;
1061
1062 ins->fm.op[i].ksl = custom_patch[i + 2] >> 6;
1063
1064 ins->fm.op[i].ar = custom_patch[i + 4] >> 4;
1065 ins->fm.op[i].dr = custom_patch[i + 4] & 15;
1066
1067 ins->fm.op[i].sl = custom_patch[i + 6] >> 4;
1068 ins->fm.op[i].rr = custom_patch[i + 6] & 15;
1069 }
1070
1071 ins->fm.fms = (custom_patch[3] >> 4) & 1;
1072 ins->fm.ams = (custom_patch[3] >> 3) & 1;
1073 ins->fm.fb = custom_patch[3] & 7;
1074
1075 ins->fm.op[0].tl = custom_patch[2] & 0x3f;
1076 ins->fm.op[1].tl = 0;
1077 break;
1078 }
1079 case DIV_INS_FDS: {
1080 DivWavetable* wave = new DivWavetable;
1081 wave->len = 64;
1082 wave->max = 63;
1083 for (int j = 0; j < 64; j++) {
1084 wave->data[j] = reader.readC();
1085 }
1086 for (int j = 0; j < 32; j++) {
1087 ins->fds.modTable[j] = reader.readC() - 3;
1088 }
1089 ins->fds.modSpeed = reader.readI();
1090 ins->fds.modDepth = reader.readI();
1091 reader.readI(); // this is delay. currently ignored. TODO.
1092 if (ds.wave.size()>=32768) {
1093 logW("too many waves! ignoring...");
1094 delete wave;
1095 } else {
1096 ins->std.waveMacro.len = 1;
1097 ins->std.waveMacro.val[0] = ds.wave.size();
1098 ds.wave.push_back(wave);
1099 ds.waveLen++;
1100 }
1101
1102 unsigned int a = reader.readI();
1103 unsigned int b = reader.readI();
1104
1105 if (!reader.seek(-8, SEEK_CUR)) {
1106 logE("couldn't seek back by 8 reading FDS ins");
1107 lastError = "couldn't seek back by 8 reading FDS ins";
1108 delete[] file;
1109 return false;
1110 }
1111
1112 if (a < 256 && (b & 0xFF) != 0x00) {
1113 // don't look at me like this. I don't know why this should be like this either!
1114 logW("a is less than 256 and b is not zero!");
1115 } else {
1116 ins->std.volMacro.len = reader.readC();
1117 ins->std.volMacro.loop = reader.readI();
1118 ins->std.volMacro.rel = reader.readI();
1119 reader.readI(); // arp mode does not apply here
1120 for (int j = 0; j < ins->std.volMacro.len; j++) {
1121 ins->std.volMacro.val[j] = reader.readC();
1122
1123 if (blockVersion <= 3) {
1124 ins->std.volMacro.val[j] *= 2;
1125 }
1126 }
1127
1128 ins->std.arpMacro.len = reader.readC();
1129 ins->std.arpMacro.loop = reader.readI();
1130 ins->std.arpMacro.rel = reader.readI();
1131 unsigned int mode = reader.readI();
1132 for (int j = 0; j < ins->std.arpMacro.len; j++) {
1133 ins->std.arpMacro.val[j] = reader.readC();
1134
1135 if (mode == 1) // fixed arp
1136 {
1137 ins->std.arpMacro.val[j] |= (1 << 30);
1138 }
1139 }
1140
1141 if (blockVersion >= 3) {
1142 ins->std.pitchMacro.len = reader.readC();
1143 ins->std.pitchMacro.loop = reader.readI();
1144 ins->std.pitchMacro.rel = reader.readI();
1145 reader.readI(); // arp mode does not apply here
1146 for (int j = 0; j < ins->std.pitchMacro.len; j++) {
1147 ins->std.pitchMacro.val[j] = reader.readC();
1148
1149 int temp_val = ins->std.pitchMacro.val[j];
1150 int temp = temp_val;
1151
1152 if (temp_val < 0x80) {
1153 ins->std.pitchMacro.val[j] = temp;
1154 } else {
1155 ins->std.pitchMacro.val[j] = -1 * (0x100 - temp);
1156 }
1157 }
1158
1159 if (mode == 0) // relative
1160 {
1161 ins->std.pitchMacro.mode = 1; // setting relative mode
1162 }
1163 }
1164 }
1165
1166 break;
1167 }
1168 case DIV_INS_N163: {
1169 unsigned int totalSeqs = reader.readI();
1170 if (totalSeqs > 5) {
1171 logE("%d: too many sequences!", insIndex);
1172 lastError = "too many sequences";
1173 delete[] file;
1174 return false;
1175 }
1176
1177 for (unsigned int j = 0; j < totalSeqs; j++) {
1178 hasSequence[insIndex][j] = reader.readC();
1179 sequenceIndex[insIndex][j] = reader.readC();
1180 }
1181
1182 unsigned int wave_size = reader.readI();
1183 unsigned int wave_pos = reader.readI();
1184 ins->n163.waveLen = wave_size;
1185 ins->n163.wavePos = wave_pos;
1186
1187 if (blockVersion >= 8) {
1188 unsigned int autopos = reader.readI();
1189 logV("autopos: %d",autopos);
1190 }
1191
1192 unsigned int wave_count = reader.readI();
1193 n163WaveOff[insIndex] = ds.wave.size();
1194 ins->n163.wave = n163WaveOff[insIndex];
1195
1196 if (wave_size>256) {
1197 logE("wave size %d out of range",wave_size);
1198 lastError = "wave size out of range";
1199 delete[] file;
1200 return false;
1201 }
1202
1203 for (unsigned int ii = 0; ii < wave_count; ii++) {
1204 DivWavetable* wave = new DivWavetable();
1205 wave->len = wave_size;
1206 wave->max = 15;
1207
1208 for (unsigned int jj = 0; jj < wave_size; jj++) {
1209 unsigned char val = reader.readC();
1210 wave->data[jj] = val;
1211 }
1212
1213 if (ds.wave.size()<32768) {
1214 ds.wave.push_back(wave);
1215 } else {
1216 logW("too many waves...");
1217 delete wave;
1218 }
1219 }
1220
1221 break;
1222 }
1223
1224 case DIV_INS_AY: {
1225 unsigned int totalSeqs = reader.readI();
1226 if (totalSeqs > 5) {
1227 logE("%d: too many sequences!", insIndex);
1228 lastError ="too many sequences";
1229 delete[] file;
1230 return false;
1231 }
1232
1233 for (unsigned int j = 0; j < totalSeqs; j++) {
1234 hasSequence[insIndex][j] = reader.readC();
1235 sequenceIndex[insIndex][j] = reader.readC();
1236 }
1237
1238 break;
1239 }
1240
1241 case DIV_INS_C64: {
1242 unsigned int instVersion = reader.readI();
1243 logV("C64 inst version %d", instVersion);
1244
1245 int seek_amount = 94 - 4;
1246
1247 if (instVersion <= 255) {
1248 unsigned char ad = reader.readC();
1249 unsigned char sr = reader.readC();
1250 ins->c64.a = ad >> 4;
1251 ins->c64.d = ad & 15;
1252 ins->c64.s = sr >> 4;
1253 ins->c64.r = sr & 15;
1254
1255 ins->c64.pulseOn = true;
1256 ins->c64.sawOn = false;
1257
1258 seek_amount -= 2;
1259
1260 if (instVersion >= 2) {
1261 unsigned int pwm_start = reader.readI();
1262 unsigned int pwm_end = reader.readI();
1263
1264 unsigned char pwm_speed = reader.readC(); // for LFO mode * 40 / 69 * (diff / (2048 - 311)) (and divide by two for triangle mode)
1265 unsigned char pwm_mode = reader.readC(); // for ADSR mode * 11 / 69
1266
1267 if (pwm_mode != 0) {
1268 ins->c64.dutyIsAbs = true;
1269
1270 logV("pwm mode %d", pwm_mode);
1271
1272 // modes: 1 = saw lfo, 2 = tri lfo, 3 = oneshot ADSR, 4 = set val?
1273
1274 ins->std.dutyMacro.len = 18;
1275
1276 if (pwm_mode != 4) {
1277 ins->std.dutyMacro.val[0] = pwm_start;
1278 ins->std.dutyMacro.val[1] = pwm_end;
1279 } else {
1280 ins->std.dutyMacro.len = 1;
1281 ins->std.dutyMacro.val[0] = pwm_end; // sequence mode, set last value
1282 }
1283
1284 if (pwm_mode == 1 || pwm_mode == 2) {
1285 ins->std.dutyMacro.open = 4 | 1; // LFO
1286 ins->std.dutyMacro.mode = 2; // LFO
1287
1288 ins->std.dutyMacro.val[11] = (int)((uint64_t)pwm_speed * (uint64_t)40 * (uint64_t)abs((int)(pwm_start - pwm_end)) / (2048 - 311) / 69); // LFO speed
1289 ins->std.dutyMacro.val[12] = 1; // sawtooth LFO wave
1290
1291 if (pwm_mode == 2) {
1292 ins->std.dutyMacro.val[11] = (int)((uint64_t)pwm_speed * (uint64_t)40 * (uint64_t)abs((int)(pwm_start - pwm_end)) / (2048 - 311) / 69 / 2); // LFO speed multiplied by 2
1293 ins->std.dutyMacro.val[12] = 0; // triangle LFO wave
1294 }
1295 }
1296
1297 if (pwm_mode == 3) {
1298 ins->std.dutyMacro.open = 2 | 1; // ADSR
1299 ins->std.dutyMacro.mode = 1; // LFO
1300 ins->std.dutyMacro.val[2] = (int)((uint64_t)pwm_speed * (uint64_t)11 * (uint64_t)abs((int)(pwm_start - pwm_end)) / (2048 - 311) / 69); // ADSR attack rate
1301 }
1302 }
1303
1304 seek_amount -= 10;
1305 }
1306
1307 if (instVersion >= 3) {
1308 unsigned int filter_start = reader.readI();
1309 unsigned int filter_end = reader.readI();
1310
1311 unsigned char filter_speed = reader.readC();
1312 unsigned char filter_mode = reader.readC();
1313
1314 if (filter_mode == 0) {
1315 seek_amount -= 10;
1316 } else {
1317 ins->c64.filterIsAbs = true;
1318
1319 // modes: 1 = saw lfo, 2 = tri lfo, 3 = oneshot ADSR, 4 = set val?
1320
1321 ins->std.algMacro.len = 18;
1322
1323 if (filter_mode != 4) {
1324 ins->std.algMacro.val[0] = filter_start;
1325 ins->std.algMacro.val[1] = filter_end;
1326 } else {
1327 ins->std.algMacro.len = 1;
1328 ins->std.algMacro.val[0] = filter_end; // sequence mode, set last value
1329 }
1330
1331 if (filter_mode == 1 || filter_mode == 2) {
1332 ins->std.algMacro.open = 4 | 1; // LFO
1333 ins->std.algMacro.mode = 2; // LFO
1334
1335 ins->std.algMacro.val[11] = (int)((uint64_t)filter_speed * (uint64_t)40 * (uint64_t)abs((int)(filter_start - filter_end)) / (2048 - 311) / 69); // LFO speed
1336 ins->std.algMacro.val[12] = 1; // sawtooth LFO wave
1337
1338 if (filter_mode == 2) {
1339 ins->std.algMacro.val[11] = (int)((uint64_t)filter_speed * (uint64_t)40 * (uint64_t)abs((int)(filter_start - filter_end)) / (2048 - 311) / 69 / 2); // LFO speed multiplied by 2
1340 ins->std.algMacro.val[12] = 0; // triangle LFO wave
1341 }
1342 }
1343
1344 if (filter_mode == 3) {
1345 ins->std.algMacro.open = 2 | 1; // ADSR
1346 ins->std.algMacro.mode = 1; // LFO
1347 ins->std.algMacro.val[2] = (int)((uint64_t)filter_speed * (uint64_t)11 * (uint64_t)abs((int)(filter_start - filter_end)) / (2048 - 311) / 69); // ADSR attack rate
1348 }
1349 }
1350 }
1351
1352 if (instVersion >= 4) {
1353 unsigned int totalSeqs = reader.readI();
1354 seek_amount -= 4;
1355 if (totalSeqs > 5) {
1356 logE("%d: too many sequences!", insIndex);
1357 lastError = "too many sequences";
1358 delete[] file;
1359 return false;
1360 }
1361
1362 for (unsigned int j = 0; j < totalSeqs; j++) {
1363 hasSequence[insIndex][j] = reader.readC();
1364 sequenceIndex[insIndex][j] = reader.readC();
1365
1366 seek_amount -= 2;
1367 }
1368 }
1369
1370 if (instVersion == 2) {
1371 // I know right?
1372 if (!reader.seek(seek_amount, SEEK_CUR)) {
1373 logE("EFT seek fail");
1374 lastError = "EFT seek fail";
1375 delete[] file;
1376 return false;
1377 }
1378 }
1379
1380 // this commented out block left here intentionally.
1381 // total mess of code style... for with no space, UNDEFINED CHAR, escaping the unescapable, silly var names...
1382 // ...whatever.
1383 /*for(int tti = 0; tti < 20; tti++)
1384 {
1385 char aaaa = reader.readC();
1386 logV("\'%c\'", aaaa);
1387 }*/
1388 } else {
1389 if (!reader.seek(-4, SEEK_CUR)) {
1390 logE("EFT -4 seek fail");
1391 lastError = "EFT -4 seek fail";
1392 delete[] file;
1393 return false;
1394 }
1395 }
1396
1397 break;
1398 }
1399
1400 default: {
1401 logE("%d: what's going on here?", insIndex);
1402 lastError = "invalid instrument type";
1403 delete[] file;
1404 return false;
1405 }
1406 }
1407
1408 // name
1409 ins->name = reader.readString((unsigned int)reader.readI());
1410 logV("- %d: %s", insIndex, ins->name);
1411 }
1412
1413 ds.insLen = 128;
1414 } else if (blockName == "SEQUENCES") {
1415 CHECK_BLOCK_VERSION(6);
1416
1417 if (blockVersion < 2) {
1418 lastError = "sequences block version is too old";
1419 delete[] file;
1420 return false;
1421 }
1422
1423 unsigned int seq_count = reader.readI();
1424
1425 if (blockVersion == 2) {
1426 for (unsigned int i = 0; i < seq_count; i++) {
1427 unsigned int index = reader.readI();
1428 unsigned int type = reader.readI();
1429 unsigned char size = reader.readC();
1430
1431 if (index>=256 || type>=8) {
1432 logE("%d: index/type out of range",i);
1433 lastError = "sequence index/type out of range";
1434 delete[] file;
1435 return false;
1436 }
1437
1438 macros[index][type].len = size;
1439
1440 for (int j = 0; j < size; j++) {
1441 unsigned char seq = reader.readC();
1442 reader.readC(); // reserved?
1443 macros[index][type].val[j] = seq;
1444 }
1445
1446 for (int k = 0; k < (int)ds.ins.size(); k++) {
1447 DivInstrument* ins=ds.ins[k];
1448 if (sequenceIndex[k][type] == index && ins->type == DIV_INS_NES && hasSequence[k][type]) {
1449 copyMacro(ins, &macros[index][type], type, 0);
1450 }
1451 }
1452 }
1453 } else {
1454 unsigned char* Indices = new unsigned char[128 * 5];
1455 unsigned char* Types = new unsigned char[128 * 5];
1456
1457 memset(Indices,0,128*5);
1458 memset(Types,0,128*5);
1459
1460 for (unsigned int i = 0; i < seq_count; i++) {
1461 unsigned int index = reader.readI();
1462 if (index>=128*5) {
1463 logE("%d: index out of range",i);
1464 lastError = "sequence index out of range";
1465 delete[] file;
1466 return false;
1467 }
1468 Indices[i] = index;
1469 unsigned int type = reader.readI();
1470 if (type>=128*5) {
1471 logE("%d: type out of range",i);
1472 lastError = "sequence type out of range";
1473 delete[] file;
1474 return false;
1475 }
1476 Types[i] = type;
1477
1478 if (index>=256 || type>=8) {
1479 logE("%d: index/type out of range",i);
1480 lastError = "sequence index/type out of range";
1481 delete[] file;
1482 return false;
1483 }
1484
1485 unsigned char size = reader.readC();
1486 unsigned int setting = 0;
1487
1488 macros[index][type].len = size;
1489
1490 unsigned int loop = reader.readI();
1491
1492 macros[index][type].loop = loop;
1493
1494 if (blockVersion == 4) {
1495 unsigned int release = reader.readI();
1496 setting = reader.readI();
1497
1498 macros[index][type].rel = release;
1499 macro_types[index][type] = setting;
1500 }
1501
1502 for (int j = 0; j < size; j++) {
1503 unsigned char seq = reader.readC();
1504 macros[index][type].val[j] = seq;
1505 }
1506
1507 for (int k = 0; k < (int)ds.ins.size(); k++) {
1508 DivInstrument* ins = ds.ins[k];
1509 if (sequenceIndex[k][Types[i]] == Indices[i] && ins->type == DIV_INS_NES && hasSequence[k][Types[i]]) {
1510 copyMacro(ins, &macros[index][type], Types[i], setting);
1511 }
1512 }
1513 }
1514
1515 if (blockVersion == 5) // Version 5 saved the release points incorrectly, this is fixed in ver 6
1516 {
1517 for (int i = 0; i < 128; i++) {
1518 for (int j = 0; j < 5; j++) {
1519 unsigned int release = reader.readI();
1520 unsigned int setting = reader.readI();
1521
1522 for (int k = 0; k < (int)ds.ins.size(); k++) {
1523 DivInstrument* ins = ds.ins[k];
1524 if (sequenceIndex[k][j] == i && ins->type == DIV_INS_NES && hasSequence[k][j]) {
1525 macros[k][j].rel = release;
1526 macro_types[k][j] = setting;
1527
1528 copyMacro(ins, &macros[sequenceIndex[k][j]][j], j, setting);
1529 }
1530 }
1531 }
1532 }
1533 }
1534
1535 if (blockVersion >= 6) // Read release points correctly stored
1536 {
1537 for (unsigned int i = 0; i < seq_count; i++) {
1538 unsigned int release = reader.readI();
1539 unsigned int setting = reader.readI();
1540
1541 for (int k = 0; k < (int)ds.ins.size(); k++) {
1542 DivInstrument* ins = ds.ins[k];
1543 if (sequenceIndex[k][Types[i]] == Indices[i] && ins->type == DIV_INS_NES && hasSequence[k][Types[i]]) {
1544 macros[sequenceIndex[k][Types[i]]][Types[i]].rel = release;
1545 macro_types[k][Types[i]] = setting;
1546
1547 copyMacro(ins, &macros[sequenceIndex[k][Types[i]]][Types[i]], Types[i], setting);
1548 }
1549 }
1550 }
1551 }
1552
1553 delete[] Indices;
1554 delete[] Types;
1555 }
1556 } else if (blockName == "GROOVES") {
1557 CHECK_BLOCK_VERSION(6);
1558
1559 unsigned char num_grooves = reader.readC();
1560 int max_groove = 0;
1561
1562 for (int i = 0; i < 256; i++) {
1563 ds.grooves.push_back(DivGroovePattern());
1564 }
1565
1566 for (int gr = 0; gr < num_grooves; gr++) {
1567 unsigned char index = reader.readC();
1568 unsigned char size = reader.readC();
1569
1570 if (index > max_groove) {
1571 max_groove = index + 1;
1572 }
1573
1574 DivGroovePattern gp;
1575 gp.len = size;
1576
1577 for (int sz = 0; sz < size; sz++) {
1578 unsigned char value = reader.readC();
1579 if (sz<16) {
1580 gp.val[sz] = value;
1581 }
1582 }
1583
1584 ds.grooves[index] = gp;
1585 }
1586
1587 ds.grooves.resize(max_groove == 0 ? 1 : max_groove);
1588
1589 unsigned char subsongs = reader.readC();
1590
1591 for (int sub = 0; sub < subsongs; sub++) {
1592 unsigned char used = reader.readC();
1593
1594 if (used) {
1595 ds.subsong[sub]->speeds = ds.grooves[0];
1596 }
1597 }
1598
1599 if ((reader.tell() - blockStart) != blockSize) {
1600 logE("block %s size does not match! block size %d curr pos %d", blockName, blockSize, reader.tell() - blockStart);
1601 }
1602 } else if (blockName == "FRAMES") {
1603 CHECK_BLOCK_VERSION(3);
1604
1605 for (size_t i = 0; i < ds.subsong.size(); i++) {
1606 DivSubSong* s = ds.subsong[i];
1607
1608 int framesLen=reader.readI();
1609 if (framesLen<1 || framesLen>256) {
1610 logE("frames out of range (%d)",framesLen);
1611 lastError = "frames out of range";
1612 delete[] file;
1613 return false;
1614 }
1615
1616 s->ordersLen = framesLen;
1617 if (blockVersion >= 3) {
1618 s->speeds.val[0] = reader.readI();
1619 }
1620 if (blockVersion >= 2) {
1621 int tempo = reader.readI();
1622
1623 logV("tempo %d", tempo);
1624
1625 if (tempo == 0) {
1626 s->virtualTempoN = s->virtualTempoD;
1627 } else {
1628 s->virtualTempoN = tempo;
1629 }
1630
1631 int patLen=reader.readI();
1632 if (patLen<1 || patLen>256) {
1633 logE("pattern length out of range");
1634 lastError = "pattern length out of range";
1635 delete[] file;
1636 return false;
1637 }
1638 s->patLen = patLen;
1639 }
1640 int why = tchans;
1641 if (blockVersion == 1) {
1642 why = reader.readI();
1643 if (why<0 || why>=DIV_MAX_CHANS) {
1644 logE("why out of range!");
1645 lastError = "why out of range";
1646 delete[] file;
1647 return false;
1648 }
1649 }
1650 logV("reading %d and %d orders", tchans, s->ordersLen);
1651
1652 for (int j = 0; j < s->ordersLen; j++) {
1653 for (int k = 0; k < why; k++) {
1654 unsigned char o = reader.readC();
1655 if (map_channels[k]>=DIV_MAX_CHANS) continue;
1656 s->orders.ord[map_channels[k]][j] = o;
1657 }
1658 }
1659 }
1660 } else if (blockName == "PATTERNS") {
1661 CHECK_BLOCK_VERSION(6);
1662
1663 size_t blockEnd = reader.tell() + blockSize;
1664
1665 if (blockVersion == 1) {
1666 int patLenOld = reader.readI();
1667 if (patLenOld<1 || patLenOld>=256) {
1668 logE("old pattern length out of range");
1669 lastError = "old pattern length out of range";
1670 delete[] file;
1671 return false;
1672 }
1673 for (DivSubSong* i : ds.subsong) {
1674 i->patLen = patLenOld;
1675 }
1676 }
1677
1678 // so it appears .ftm doesn't keep track of how many patterns are stored in the file....
1679 while (reader.tell() < blockEnd) {
1680 logV("reading pattern at %x...",reader.tell());
1681 int subs = 0;
1682 if (blockVersion >= 2)
1683 subs = reader.readI();
1684 int ch = reader.readI();
1685 int patNum = reader.readI();
1686 int numRows = reader.readI();
1687
1688 logV("ch: %d",ch);
1689 logV("subs: %d. map_channels[ch]: %d",subs,map_channels[ch]);
1690 logV("patNum: %d",patNum);
1691 logV("rows: %d",numRows);
1692
1693 if (subs<0 || subs>=(int)ds.subsong.size()) {
1694 logE("subsong out of range!");
1695 lastError = "subsong out of range";
1696 delete[] file;
1697 return false;
1698 }
1699 if (ch<0 || ch>=DIV_MAX_CHANS) {
1700 logE("channel out of range!");
1701 lastError = "channel out of range";
1702 delete[] file;
1703 return false;
1704 }
1705 if (map_channels[ch]>=DIV_MAX_CHANS) {
1706 logE("mapped channel out of range!");
1707 lastError = "mapped channel out of range";
1708 delete[] file;
1709 return false;
1710 }
1711 if (patNum<0 || patNum>=256) {
1712 logE("pattern number out of range!");
1713 lastError = "pattern number out of range";
1714 delete[] file;
1715 return false;
1716 }
1717 if (numRows<0) {
1718 logE("row count is negative!");
1719 lastError = "row count is negative";
1720 delete[] file;
1721 return false;
1722 }
1723
1724 DivPattern* pat = ds.subsong[subs]->pat[map_channels[ch]].getPattern(patNum, true);
1725 for (int i = 0; i < numRows; i++) {
1726 unsigned int row = 0;
1727 if (ds.version==0x200 || blockVersion >= 6) { // row index
1728 row = (unsigned char)reader.readC();
1729 } else {
1730 row = reader.readI();
1731 }
1732
1733 if (row>=256) {
1734 logE("row index out of range");
1735 lastError = "row index out of range";
1736 delete[] file;
1737 return false;
1738 }
1739
1740 unsigned char nextNote = reader.readC();
1741 unsigned char nextOctave = reader.readC();
1742
1743 if (blockVersion < 5 && map_channels[ch] == fds_chan) // FDS transpose
1744 {
1745 nextOctave += 2;
1746 nextOctave -= 2;
1747 }
1748 if (blockVersion >= 5 && map_channels[ch] == fds_chan) // FDS transpose
1749 {
1750 nextOctave -= 2;
1751 }
1752
1753 if (map_channels[ch] != 0xff) {
1754 if (nextNote == 0x0d) {
1755 pat->data[row][0] = 101;
1756 } else if (nextNote == 0x0e) {
1757 pat->data[row][0] = 100;
1758 } else if (nextNote == 0x01) {
1759 pat->data[row][0] = 12;
1760 pat->data[row][1] = nextOctave - 1;
1761 } else if (nextNote == 0) {
1762 pat->data[row][0] = 0;
1763 } else if (nextNote < 0x0d) {
1764 pat->data[row][0] = nextNote - 1;
1765 pat->data[row][1] = nextOctave;
1766 }
1767 }
1768
1769 unsigned char nextIns = reader.readC();
1770 // TODO: you sure about 0xff?
1771 if (map_channels[ch] != 0xff) {
1772 if (nextIns < 0x40 && nextNote != 0x0d && nextNote != 0x0e) {
1773 pat->data[row][2] = nextIns;
1774 } else {
1775 pat->data[row][2] = -1;
1776 }
1777 }
1778
1779 unsigned char nextVol = reader.readC();
1780 if (map_channels[ch] != 0xff) {
1781 if (nextVol < 0x10) {
1782 pat->data[row][3] = nextVol;
1783 if (map_channels[ch] == vrc6_saw_chan) // scale volume
1784 {
1785 // TODO: shouldn't it be 32?
1786 pat->data[row][3] = (pat->data[row][3] * 42) / 15;
1787 }
1788
1789 if (map_channels[ch] == fds_chan) {
1790 pat->data[row][3] = (pat->data[row][3] * 31) / 15;
1791 }
1792 } else {
1793 pat->data[row][3] = -1;
1794 }
1795 }
1796
1797 int effectCols = ds.subsong[subs]->pat[map_channels[ch]].effectCols;
1798 if (blockVersion >= 6)
1799 effectCols = 4;
1800
1801 if (ds.version == 0x200) {
1802 effectCols = 1;
1803 }
1804
1805 unsigned char nextEffectVal = 0;
1806 unsigned char nextEffect = 0;
1807
1808 for (int j = 0; j < effectCols; j++) {
1809 nextEffect = reader.readC();
1810
1811 if (nextEffect>0 && ((nextEffect < FT_EF_COUNT && !eft) || (nextEffect < EFT_EF_COUNT && eft))) {
1812 nextEffectVal = reader.readC();
1813
1814 if (blockVersion < 3) {
1815 if (nextEffect == FT_EF_PORTAOFF) {
1816 nextEffect = FT_EF_PORTAMENTO;
1817 nextEffectVal = 0;
1818 } else if (nextEffect == FT_EF_PORTAMENTO) {
1819 if (nextEffect < 0xFF)
1820 nextEffectVal++;
1821 }
1822 }
1823 } else if (blockVersion < 6) {
1824 nextEffectVal = reader.readC();
1825 }
1826
1827 // Specific for version 2.0
1828 if (ds.version == 0x0200 && j == 0) {
1829 if (nextEffect == FT_EF_SPEED && nextEffectVal < 20)
1830 nextEffectVal++;
1831
1832 if (pat->data[row][3] == 0)
1833 pat->data[row][3] = 0xf;
1834 else {
1835 pat->data[row][3]--;
1836 pat->data[row][3] &= 0x0F;
1837 }
1838
1839 if (pat->data[row][0] == 0)
1840 pat->data[row][2] = -1;
1841 }
1842
1843 if (blockVersion == 3) {
1844 // Fix for VRC7 portamento
1845 bool is_vrc7 = false;
1846
1847 for (int vrr = 0; vrr < 6; vrr++) {
1848 if (map_channels[ch] == vrc7_chans[vrr]) {
1849 is_vrc7 = true;
1850 }
1851 }
1852
1853 if (is_vrc7) {
1854 switch (nextEffect) {
1855 case FT_EF_PORTA_DOWN:
1856 nextEffect = FT_EF_PORTA_UP;
1857 break;
1858 case FT_EF_PORTA_UP:
1859 nextEffect = FT_EF_PORTA_DOWN;
1860 break;
1861 default:
1862 break;
1863 }
1864 }
1865 // FDS pitch effect fix
1866 else if (map_channels[ch] == fds_chan) {
1867 switch (nextEffect) {
1868 case FT_EF_PITCH:
1869 if (nextEffectVal != 0x80)
1870 nextEffectVal = (0x100 - nextEffectVal) & 0xFF;
1871 break;
1872 default:
1873 break;
1874 }
1875 }
1876 }
1877
1878 for (int v = 0; v < 8; v++) {
1879 if (map_channels[ch] == n163_chans[v]) {
1880 if (nextEffect == FT_EF_SAMPLE_OFFSET) {
1881 nextEffect = FT_EF_N163_WAVE_BUFFER;
1882 }
1883 }
1884 }
1885
1886 if (ds.version < 0x0450 || dnft) {
1887 unsigned char idx = 0;
1888
1889 while (eff_conversion_050[idx][0] != 0xFF) // until the end of the array
1890 {
1891 if (nextEffect == eff_conversion_050[idx][0]) // remap the effects (0CC vs FT effects type order) bruh idk why but it should be done to correctly read some modules
1892 {
1893 nextEffect = eff_conversion_050[idx][1];
1894
1895 break;
1896 }
1897
1898 idx++;
1899 }
1900
1901 }
1902
1903 if (map_channels[ch] != 0xff) {
1904 if (nextEffect == 0 && nextEffectVal == 0) {
1905 pat->data[row][4 + (j * 2)] = -1;
1906 pat->data[row][5 + (j * 2)] = -1;
1907 } else {
1908 if ((eft && nextEffect<eftEffectMapSize) || (!eft && nextEffect<ftEffectMapSize)) {
1909 if (eft) {
1910 pat->data[row][4 + (j * 2)] = eftEffectMap[nextEffect];
1911 pat->data[row][5 + (j * 2)] = eftEffectMap[nextEffect] == -1 ? -1 : nextEffectVal;
1912
1913 if (pat->data[row][4 + (j * 2)] == 0x100) {
1914 pat->data[row][3] += pat->data[row][5 + (j * 2)] ? 0x10 : 0; // extra volume bit for AY8930
1915 pat->data[row][4 + (j * 2)] = -1;
1916 pat->data[row][5 + (j * 2)] = -1;
1917 }
1918
1919 if (eftEffectMap[nextEffect] == 0x0f && nextEffectVal > 0x1f) {
1920 pat->data[row][4 + (j * 2)] = 0xfd; // BPM speed change!
1921 }
1922
1923 if ((eftEffectMap[nextEffect] == 0xe1 || eftEffectMap[nextEffect] == 0xe2) && (nextEffectVal & 0xf0) == 0) {
1924 pat->data[row][5 + (j * 2)] |= 0x10; // in FamiTracker if e1/e2 commands speed is 0 the portamento still has some speed!
1925 }
1926 } else {
1927 pat->data[row][4 + (j * 2)] = ftEffectMap[nextEffect];
1928 pat->data[row][5 + (j * 2)] = ftEffectMap[nextEffect] == -1 ? -1 : nextEffectVal;
1929
1930 if (ftEffectMap[nextEffect] == 0x0f && nextEffectVal > 0x1f) {
1931 pat->data[row][4 + (j * 2)] = 0xfd; // BPM speed change!
1932 }
1933
1934 if ((ftEffectMap[nextEffect] == 0xe1 || ftEffectMap[nextEffect] == 0xe2) && (nextEffectVal & 0xf0) == 0) {
1935 pat->data[row][5 + (j * 2)] |= 0x10; // in FamiTracker if e1/e2 commands speed is 0 the portamento still has some speed!
1936 }
1937 }
1938 for (int v = 0; v < 8; v++) {
1939 if (map_channels[ch] == n163_chans[v]) {
1940 if (pat->data[row][4 + (j * 2)] == 0x12) {
1941 pat->data[row][4 + (j * 2)] = 0x110; // N163 wave change (we'll map this later)
1942 }
1943 }
1944 }
1945
1946 for (int vrr = 0; vrr < 6; vrr++)
1947 {
1948 if (map_channels[ch] == vrc7_chans[vrr])
1949 {
1950 if (pat->data[row][4 + (j * 2)] == 0x12)
1951 {
1952 pat->data[row][4 + (j * 2)] = 0x10; // set VRC7 patch
1953 }
1954 }
1955 }
1956
1957 for (int v = 0; v < 3; v++) {
1958 if (map_channels[ch] == s5b_chans[v] || map_channels[ch] == ay8930_chans[v]) {
1959 if (pat->data[row][4 + (j * 2)] == 0x22 && (pat->data[row][5 + (j * 2)] & 0xf0) != 0) {
1960 pat->data[row][4 + (7 * 2)] = -666; //marker
1961 }
1962 }
1963 }
1964 } else {
1965 pat->data[row][4 + (j * 2)] = -1;
1966 pat->data[row][5 + (j * 2)] = -1;
1967 }
1968 }
1969 }
1970 }
1971 }
1972 }
1973 } else if (blockName == "DPCM SAMPLES") {
1974 CHECK_BLOCK_VERSION(1);
1975 unsigned char num_samples = reader.readC();
1976
1977 for (int i = 0; i < 256; i++) {
1978 DivSample* s = new DivSample();
1979 ds.sample.push_back(s);
1980 }
1981
1982 ds.sampleLen = ds.sample.size();
1983
1984 unsigned int true_size = 0;
1985 unsigned char index = 0;
1986
1987 for (unsigned char i = 0; i < num_samples; i++) {
1988 index = reader.readC();
1989
1990 DivSample* sample = ds.sample[index];
1991
1992 sample->rate = 33144;
1993 sample->centerRate = 33144;
1994 sample->depth = DIV_SAMPLE_DEPTH_1BIT_DPCM;
1995
1996 sample->name = reader.readString((unsigned int)reader.readI());
1997
1998 unsigned int sample_len = reader.readI();
1999
2000 if (sample_len>=2097152) {
2001 logE("%d: sample too large! %d",index,sample_len);
2002 lastError = "sample too large";
2003 delete[] file;
2004 return false;
2005 }
2006
2007 true_size = sample_len + ((1 - (int)sample_len) & 0x0f);
2008 sample->init(true_size * 8);
2009 memset(sample->dataDPCM, 0xAA, true_size);
2010 reader.read(sample->dataDPCM, sample_len);
2011 }
2012
2013 int last_non_empty_sample = 0xff;
2014
2015 for (int i = 255; i > 0; i--) {
2016 DivSample* s = ds.sample[i];
2017
2018 if (s->samples>0) {
2019 last_non_empty_sample = i;
2020 break;
2021 }
2022 }
2023
2024 for (int i = 255; i > last_non_empty_sample; i--) {
2025 ds.sample.erase(ds.sample.begin() + i);
2026 }
2027
2028 ds.sampleLen = ds.sample.size();
2029 } else if (blockName == "SEQUENCES_VRC6") {
2030 CHECK_BLOCK_VERSION(6);
2031
2032 unsigned char* Indices = new unsigned char[128 * 5];
2033 unsigned char* Types = new unsigned char[128 * 5];
2034
2035 memset(Indices,0,128*5);
2036 memset(Types,0,128*5);
2037
2038 unsigned int seq_count = reader.readI();
2039
2040 for (unsigned int i = 0; i < seq_count; i++) {
2041 unsigned int index = reader.readI();
2042 if (index>=128*5) {
2043 logE("%d: index out of range",i);
2044 lastError = "sequence index out of range";
2045 delete[] file;
2046 return false;
2047 }
2048 Indices[i] = index;
2049 unsigned int type = reader.readI();
2050 if (type>=128*5) {
2051 logE("%d: type out of range",i);
2052 lastError = "sequence type out of range";
2053 delete[] file;
2054 return false;
2055 }
2056 Types[i] = type;
2057
2058 if (index>=256 || type>=8) {
2059 logE("%d: index/type out of range",i);
2060 lastError = "sequence index/type out of range";
2061 delete[] file;
2062 return false;
2063 }
2064
2065 unsigned char size = reader.readC();
2066 unsigned int setting = 0;
2067
2068 macros[index][type].len = size;
2069
2070 unsigned int loop = reader.readI();
2071
2072 macros[index][type].loop = loop;
2073
2074 if (blockVersion == 4) {
2075 unsigned int release = reader.readI();
2076 setting = reader.readI();
2077
2078 macros[index][type].rel = release;
2079 macro_types[index][type] = setting;
2080 }
2081
2082 for (int j = 0; j < size; j++) {
2083 unsigned char seq = reader.readC();
2084 macros[index][type].val[j] = seq;
2085 }
2086
2087 for (int k = 0; k < (int)ds.ins.size(); k++) {
2088 DivInstrument* ins = ds.ins[k];
2089 if (sequenceIndex[k][Types[i]] == Indices[i] && ins->type == DIV_INS_VRC6 && hasSequence[k][Types[i]]) {
2090 copyMacro(ins, &macros[index][type], type, setting);
2091
2092 if (type == 0 && setting == 1) {
2093 ins->type = DIV_INS_VRC6_SAW;
2094 }
2095 }
2096 }
2097 }
2098
2099 if (blockVersion == 5) // Version 5 saved the release points incorrectly, this is fixed in ver 6
2100 {
2101 for (int i = 0; i < 128; i++) {
2102 for (int j = 0; j < 5; j++) {
2103 unsigned int release = reader.readI();
2104 unsigned int setting = reader.readI();
2105
2106 for (int k = 0; k < (int)ds.ins.size(); k++) {
2107 DivInstrument* ins = ds.ins[k];
2108 if (sequenceIndex[k][j] == i && ins->type == DIV_INS_VRC6 && hasSequence[k][j]) {
2109 macros[k][j].rel = release;
2110 macro_types[k][j] = setting;
2111
2112 copyMacro(ins, &macros[sequenceIndex[k][j]][j], j, setting);
2113
2114 if (j == 0 && setting == 1) {
2115 ins->type = DIV_INS_VRC6_SAW;
2116 }
2117 }
2118 }
2119 }
2120 }
2121 }
2122
2123 if (blockVersion >= 6) // Read release points correctly stored
2124 {
2125 for (unsigned int i = 0; i < seq_count; i++) {
2126 unsigned int release = reader.readI();
2127 unsigned int setting = reader.readI();
2128
2129 for (int k = 0; k < (int)ds.ins.size(); k++) {
2130 DivInstrument* ins = ds.ins[k];
2131 if (sequenceIndex[k][Types[i]] == Indices[i] && ins->type == DIV_INS_VRC6 && hasSequence[k][Types[i]]) {
2132 macros[sequenceIndex[k][Types[i]]][Types[i]].rel = release;
2133 macro_types[k][Types[i]] = setting;
2134
2135 copyMacro(ins, &macros[sequenceIndex[k][Types[i]]][Types[i]], Types[i], setting);
2136
2137 if (Types[i] == 0 && setting == 1) {
2138 ins->type = DIV_INS_VRC6_SAW;
2139 }
2140 }
2141 }
2142 }
2143 }
2144
2145 delete[] Indices;
2146 delete[] Types;
2147 } else if (blockName == "SEQUENCES_N163" || blockName == "SEQUENCES_N106") {
2148 CHECK_BLOCK_VERSION(1);
2149
2150 unsigned char* Indices = new unsigned char[128 * 5];
2151 unsigned char* Types = new unsigned char[128 * 5];
2152
2153 memset(Indices,0,128*5);
2154 memset(Types,0,128*5);
2155
2156 unsigned int seq_count = reader.readI();
2157
2158 for (unsigned int i = 0; i < seq_count; i++) {
2159 unsigned int index = reader.readI();
2160 if (index>=128*5) {
2161 logE("%d: index out of range",i);
2162 lastError = "sequence index out of range";
2163 delete[] file;
2164 return false;
2165 }
2166 Indices[i] = index;
2167 unsigned int type = reader.readI();
2168 if (type>=128*5) {
2169 logE("%d: type out of range",i);
2170 lastError = "sequence type out of range";
2171 delete[] file;
2172 return false;
2173 }
2174 Types[i] = type;
2175
2176 if (index>=256 || type>=8) {
2177 logE("%d: index/type out of range",i);
2178 lastError = "sequence index/type out of range";
2179 delete[] file;
2180 return false;
2181 }
2182
2183 unsigned char size = reader.readC();
2184 unsigned int setting = 0;
2185
2186 macros[index][type].len = size;
2187
2188 unsigned int loop = reader.readI();
2189
2190 macros[index][type].loop = loop;
2191
2192 unsigned int release = reader.readI();
2193 setting = reader.readI();
2194
2195 macros[index][type].rel = release;
2196 macro_types[index][type] = setting;
2197
2198 for (int j = 0; j < size; j++) {
2199 unsigned char seq = reader.readC();
2200 macros[index][type].val[j] = seq;
2201 }
2202
2203 for (int k = 0; k < (int)ds.ins.size(); k++) {
2204 DivInstrument* ins = ds.ins[k];
2205 if (sequenceIndex[k][Types[i]] == Indices[i] && ins->type == DIV_INS_N163 && hasSequence[k][Types[i]]) {
2206 copyMacro(ins, &macros[index][type], type, setting);
2207 }
2208 }
2209 }
2210
2211 delete[] Indices;
2212 delete[] Types;
2213
2214 } else if (blockName == "SEQUENCES_S5B") {
2215 CHECK_BLOCK_VERSION(1);
2216
2217 unsigned char* Indices = new unsigned char[128 * 5];
2218 unsigned char* Types = new unsigned char[128 * 5];
2219
2220 memset(Indices,0,128*5);
2221 memset(Types,0,128*5);
2222
2223 unsigned int seq_count = reader.readI();
2224
2225 for (unsigned int i = 0; i < seq_count; i++) {
2226 unsigned int index = reader.readI();
2227 if (index>=128*5) {
2228 logE("%d: index out of range",i);
2229 lastError = "sequence index out of range";
2230 delete[] file;
2231 return false;
2232 }
2233 Indices[i] = index;
2234 unsigned int type = reader.readI();
2235 if (type>=128*5) {
2236 logE("%d: type out of range",i);
2237 lastError = "sequence type out of range";
2238 delete[] file;
2239 return false;
2240 }
2241 Types[i] = type;
2242
2243 if (index>=256 || type>=8) {
2244 logE("%d: index/type out of range",i);
2245 lastError = "sequence index/type out of range";
2246 delete[] file;
2247 return false;
2248 }
2249
2250 unsigned char size = reader.readC();
2251 unsigned int setting = 0;
2252
2253 macros[index][type].len = size;
2254
2255 unsigned int loop = reader.readI();
2256
2257 macros[index][type].loop = loop;
2258
2259 unsigned int release = reader.readI();
2260 setting = reader.readI();
2261
2262 macros[index][type].rel = release;
2263 macro_types[index][type] = setting;
2264
2265 for (int j = 0; j < size; j++) {
2266 unsigned char seq = reader.readC();
2267 macros[index][type].val[j] = seq;
2268 }
2269
2270 for (int k = 0; k < (int)ds.ins.size(); k++) {
2271 DivInstrument* ins = ds.ins[k];
2272 if (sequenceIndex[k][type] == Indices[i] && ins->type == DIV_INS_AY && hasSequence[k][type]) {
2273 copyMacro(ins, &macros[index][type], type, setting);
2274 }
2275 }
2276 }
2277
2278 delete[] Indices;
2279 delete[] Types;
2280 } else if (blockName == "SEQUENCES_SID") {
2281 CHECK_BLOCK_VERSION(4);
2282
2283 unsigned char* Indices = new unsigned char[128 * 5];
2284 unsigned char* Types = new unsigned char[128 * 5];
2285
2286 memset(Indices,0,128*5);
2287 memset(Types,0,128*5);
2288
2289 unsigned int seq_count = reader.readI();
2290
2291 for (unsigned int i = 0; i < seq_count; i++) {
2292 unsigned int index = reader.readI();
2293 if (index>=128*5) {
2294 logE("%d: index out of range",i);
2295 lastError = "sequence index out of range";
2296 delete[] file;
2297 return false;
2298 }
2299 Indices[i] = index;
2300 unsigned int type = reader.readI();
2301 if (type>=128*5) {
2302 logE("%d: type out of range",i);
2303 lastError = "sequence type out of range";
2304 delete[] file;
2305 return false;
2306 }
2307 Types[i] = type;
2308
2309 if (index>=256 || type>=8) {
2310 logE("%d: index/type out of range",i);
2311 lastError = "sequence index/type out of range";
2312 delete[] file;
2313 return false;
2314 }
2315
2316 unsigned char size = reader.readC();
2317 unsigned int setting = 0;
2318
2319 macros[index][type].len = size;
2320
2321 unsigned int loop = reader.readI();
2322
2323 macros[index][type].loop = loop;
2324
2325 unsigned int release = reader.readI();
2326 setting = reader.readI();
2327
2328 macros[index][type].rel = release;
2329 macro_types[index][type] = setting;
2330
2331 for (int j = 0; j < size; j++) {
2332 unsigned char seq = reader.readC();
2333 macros[index][type].val[j] = seq;
2334 }
2335
2336 for (int k = 0; k < (int)ds.ins.size(); k++) {
2337 DivInstrument* ins = ds.ins[k];
2338 if (sequenceIndex[k][type] == Indices[i] && ins->type == DIV_INS_C64 && hasSequence[k][type]) {
2339 copyMacro(ins, &macros[index][type], type, setting);
2340 }
2341 }
2342 }
2343
2344 delete[] Indices;
2345 delete[] Types;
2346 } else if (blockName == "JSON") {
2347 CHECK_BLOCK_VERSION(1);
2348 logW("block JSON not supported...");
2349 reader.seek(blockSize, SEEK_CUR);
2350 } else if (blockName == "PARAMS_EMU") {
2351 CHECK_BLOCK_VERSION(1);
2352 logW("block PARAMS_EMU not supported...");
2353 reader.seek(blockSize, SEEK_CUR);
2354 } else if (blockName == "DETUNETABLES") {
2355 CHECK_BLOCK_VERSION(1);
2356 logW("block DETUNETABLES not supported...");
2357 reader.seek(blockSize, SEEK_CUR);
2358 } else if (blockName == "COMMENTS") {
2359 CHECK_BLOCK_VERSION(1);
2360 unsigned int display_comment = reader.readI();
2361
2362 logV("displayComment: %d",display_comment);
2363
2364 char ch = 0;
2365
2366 // why not readString?
2367 while (true) {
2368 ch = reader.readC();
2369 if (ch==0) break;
2370 ds.subsong[0]->notes += ch;
2371 }
2372
2373 } else if (blockName == "PARAMS_EXTRA") {
2374 CHECK_BLOCK_VERSION(3);
2375 unsigned int linear_pitch = reader.readI();
2376
2377 ds.linearPitch = linear_pitch == 0 ? 0 : 2;
2378
2379 if (blockVersion >= 2) {
2380 int fineTuneCents = reader.readC() * 100;
2381 fineTuneCents += reader.readC();
2382
2383 ds.tuning = 440.0 * pow(2.0, (double)fineTuneCents / 1200.0);
2384 }
2385 if (blockVersion >= 3) {
2386 unsigned char flats = reader.readC();
2387 logV("flats: %d",(int)flats);
2388 }
2389 } else if (blockName == "TUNING") {
2390 CHECK_BLOCK_VERSION(1);
2391 if (blockVersion == 1) {
2392 int fineTuneCents = reader.readC() * 100;
2393 fineTuneCents += reader.readC();
2394
2395 ds.tuning = 440.0 * pow(2.0, (double)fineTuneCents / 1200.0);
2396 }
2397 } else if (blockName == "BOOKMARKS") {
2398 CHECK_BLOCK_VERSION(1);
2399 logW("block BOOKMARKS not supported...");
2400 reader.seek(blockSize, SEEK_CUR);
2401 } else {
2402 logE("block %s is unknown!", blockName);
2403 lastError = "unknown block " + blockName;
2404 delete[] file;
2405 return false;
2406 }
2407
2408 if ((reader.tell() - blockStart) != blockSize) {
2409 logE("block %s is incomplete! reader.tell()-blockStart %d blockSize %d", blockName, (reader.tell() - blockStart), blockSize);
2410 lastError = "incomplete block " + blockName;
2411 delete[] file;
2412 return false;
2413 }
2414 }
2415
2416 ds.insLen = ds.ins.size();
2417
2418 if (ds.insLen > 0) {
2419 for (int tries = 0; tries < 5; tries++) // de-duplicating instruments
2420 {
2421 for (int i = 0; i < 128; i++) {
2422 if (ds.ins.empty()) break;
2423 int index = i >= (int)ds.insLen ? ((int)ds.insLen - 1) : i;
2424 if (index < 0)
2425 index = 0;
2426
2427 if (index>=(int)ds.ins.size()) continue;
2428
2429 DivInstrument* ins = ds.ins[index];
2430
2431 if (ins->type == DIV_INS_FM) {
2432 delete ds.ins[index];
2433 ds.ins.erase(ds.ins.begin() + index);
2434 ds.insLen = ds.ins.size();
2435 for (int ii = 0; ii < total_chans; ii++) {
2436 for (size_t j = 0; j < ds.subsong.size(); j++) {
2437 for (int k = 0; k < DIV_MAX_PATTERNS; k++) {
2438 if (ds.subsong[j]->pat[ii].data[k] == NULL)
2439 continue;
2440 for (int l = 0; l < ds.subsong[j]->patLen; l++) {
2441 if (ds.subsong[j]->pat[ii].data[k]->data[l][2] > index) {
2442 ds.subsong[j]->pat[ii].data[k]->data[l][2]--;
2443 }
2444 }
2445 }
2446 }
2447 }
2448 }
2449 }
2450 }
2451 }
2452
2453 for (int ii = 0; ii < total_chans; ii++) {
2454 for (size_t j = 0; j < ds.subsong.size(); j++) {
2455 for (int k = 0; k < DIV_MAX_PATTERNS; k++) {
2456 if (ds.subsong[j]->pat[ii].data[k] == NULL)
2457 continue;
2458 for (int l = 0; l < ds.subsong[j]->patLen; l++) {
2459 if (ds.subsong[j]->pat[ii].data[k]->data[l][4 + 7*2] == -666) {
2460 bool converted = false;
2461 // for()? if()? THESE ARE NOT FUNCTIONS!
2462 for (int hh = 0; hh < 7; hh++) { // oh and now you 1TBS up. oh man...
2463 if (ds.subsong[j]->pat[ii].data[k]->data[l][4 + hh*2] == 0x22 && !converted) {
2464 int slot = findEmptyFx(ds.subsong[j]->pat[ii].data[k]->data[l]);
2465 if (slot != -1) {
2466 // space your comments damn it!
2467 // Hxy - Envelope automatic pitch
2468
2469 // Sets envelope period to the note period shifted by x and envelope type y.
2470 // Approximate envelope frequency is note frequency * (2^|x - 8|) / 32.
2471
2472 int ftAutoEnv = (ds.subsong[j]->pat[ii].data[k]->data[l][5 + hh*2] >> 4) & 15;
2473 int autoEnvDen = 16; // ???? with 32 it's an octave lower...
2474 int autoEnvNum = (1 << (abs(ftAutoEnv - 8)));
2475
2476 while (autoEnvNum >= 2 && autoEnvDen >= 2) {
2477 autoEnvDen /= 2;
2478 autoEnvNum /= 2;
2479 }
2480
2481 if (autoEnvDen < 16 && autoEnvNum < 16) {
2482 ds.subsong[j]->pat[ii].data[k]->data[l][4 + slot*2] = 0x29;
2483 ds.subsong[j]->pat[ii].data[k]->data[l][5 + slot*2] = (autoEnvNum << 4) | autoEnvDen;
2484 }
2485
2486 ds.subsong[j]->pat[ii].data[k]->data[l][5 + hh*2] = (ds.subsong[j]->pat[ii].data[k]->data[l][5 + hh*2] & 0xf) << 4;
2487
2488 converted = true;
2489 }
2490 }
2491 }
2492
2493 ds.subsong[j]->pat[ii].data[k]->data[l][4 + (7 * 2)] = -1; //delete marker
2494 }
2495 }
2496 }
2497 }
2498 }
2499
2500 for (size_t j = 0; j < ds.subsong.size(); j++) { // open hidden effect columns
2501 // what are your rules for spacing, really?
2502 DivSubSong* s = ds.subsong[j];
2503 for (int c = 0; c < total_chans; c++) {
2504 int num_fx = 1;
2505
2506 for (int p = 0; p < s->ordersLen; p++) {
2507 for (int r = 0; r < s->patLen; r++) {
2508 DivPattern* pat = s->pat[c].getPattern(s->orders.ord[c][p], true);
2509 short* s_row_data = pat->data[r];
2510
2511 for (int eff = 0; eff < DIV_MAX_EFFECTS - 1; eff++) {
2512 if (s_row_data[4 + 2 * eff] != -1 && eff + 1 > num_fx) {
2513 num_fx = eff + 1;
2514 }
2515 }
2516 }
2517 }
2518
2519 s->pat[c].effectCols = num_fx;
2520 }
2521 }
2522
2523 // famitracker is not fucking strict with what instrument types can be used on any channel. This leads to e.g. 2A03 instuments being used on VRC6 channels.
2524 // Furnace is way more strict, so NES instrument in VRC6 channel just does not play properly. To fix this, we are creating copies of instruments, changing their type to please Furnace system.
2525 // I kinda did the same in klystrack import, tbh. - LTVA
2526
2527 // actually, this is wrong. Furnace is very lax regarding instrument usage. you can put an OPM instrument in OPL channel and yeah, it'll sound weird but it'll work. - tildearrow
2528
2529 // P.S. Duties conversion is based on what I really hear in FamiTracker when using wrong instrument type (and what I see on Audacity "oscilloscope") - LTVA
2530
2531 // is that the best you could do? paste my rant from that other commit and change a couple words?
2532 // at least be creative next time...
2533
2534 int ins_vrc6_conv[256][2];
2535 int ins_vrc6_saw_conv[256][2];
2536 int ins_nes_conv[256][2]; // vrc6 (or whatever) -> nes
2537
2538 for (int i = 0; i < 256; i++) {
2539 ins_vrc6_conv[i][0] = -1;
2540 ins_vrc6_conv[i][1] = -1;
2541
2542 ins_vrc6_saw_conv[i][0] = -1;
2543 ins_vrc6_saw_conv[i][1] = -1;
2544
2545 ins_nes_conv[i][0] = -1;
2546 ins_nes_conv[i][1] = -1;
2547 }
2548
2549 int init_inst_num = ds.ins.size();
2550
2551 for (int i = 0; i < init_inst_num; i++) {
2552 for (int ii = 0; ii < total_chans; ii++) {
2553 for (size_t j = 0; j < ds.subsong.size(); j++) {
2554 for (int k = 0; k < DIV_MAX_PATTERNS; k++) {
2555 if (ds.subsong[j]->pat[ii].data[k] == NULL)
2556 continue;
2557 for (int l = 0; l < ds.subsong[j]->patLen; l++) {
2558 // 1TBS > GNU
2559 if (ds.subsong[j]->pat[ii].data[k]->data[l][2] == i) { // instrument
2560 DivInstrument* ins = ds.ins[i];
2561 bool go_to_end = false;
2562
2563 if (ins->type != DIV_INS_VRC6 && (ii == vrc6_chans[0] || ii == vrc6_chans[1])) { // we encountered non-VRC6 instrument on VRC6 channel
2564 DivInstrument* insnew = new DivInstrument;
2565 ds.ins.push_back(insnew);
2566
2567 *ds.ins[ds.ins.size() - 1] = *ins;
2568
2569 ds.ins[ds.ins.size() - 1]->name += " [VRC6 copy]";
2570 ds.ins[ds.ins.size() - 1]->amiga.useSample = false;
2571 ds.ins[ds.ins.size() - 1]->amiga.useNoteMap = false;
2572
2573 ds.ins[ds.ins.size() - 1]->type = DIV_INS_VRC6;
2574
2575 if (ins->std.dutyMacro.len > 0) {
2576 for (int mm = 0; mm < ins->std.dutyMacro.len; mm++) {
2577 if (ds.ins[ds.ins.size() - 1]->std.dutyMacro.val[mm] < 4) {
2578 int vall = ins->std.dutyMacro.val[mm];
2579 ds.ins[ds.ins.size() - 1]->std.dutyMacro.val[mm] = convert_vrc6_duties[vall];
2580 }
2581 }
2582 }
2583
2584 ins_vrc6_conv[i][0] = i;
2585 ins_vrc6_conv[i][1] = ds.ins.size() - 1;
2586
2587 go_to_end = true;
2588 }
2589
2590 if (go_to_end) {
2591 goto end1;
2592 }
2593 }
2594 }
2595 }
2596 }
2597 }
2598
2599 end1:;
2600 }
2601
2602 for (int i = 0; i < init_inst_num; i++) {
2603 for (int ii = 0; ii < total_chans; ii++) {
2604 for (size_t j = 0; j < ds.subsong.size(); j++) {
2605 for (int k = 0; k < DIV_MAX_PATTERNS; k++) {
2606 if (ds.subsong[j]->pat[ii].data[k] == NULL)
2607 continue;
2608 for (int l = 0; l < ds.subsong[j]->patLen; l++) {
2609 if (ds.subsong[j]->pat[ii].data[k]->data[l][2] == i) // instrument
2610 {
2611 DivInstrument* ins = ds.ins[i];
2612 bool go_to_end = false;
2613
2614 if (ins->type != DIV_INS_VRC6_SAW && ii == vrc6_saw_chan) // we encountered non-VRC6-saw instrument on VRC6 saw channel
2615 {
2616 DivInstrument* insnew = new DivInstrument;
2617 ds.ins.push_back(insnew);
2618
2619 *ds.ins[ds.ins.size() - 1] = *ins;
2620
2621 ds.ins[ds.ins.size() - 1]->name += " [VRC6 saw copy]";
2622 ds.ins[ds.ins.size() - 1]->amiga.useSample = false;
2623 ds.ins[ds.ins.size() - 1]->amiga.useNoteMap = false;
2624
2625 ds.ins[ds.ins.size() - 1]->type = DIV_INS_VRC6_SAW;
2626
2627 if (ins->std.volMacro.len > 0) {
2628 for (int mm = 0; mm < ins->std.volMacro.len; mm++) {
2629 if (ds.ins[ds.ins.size() - 1]->std.volMacro.val[mm] < 16) {
2630 int vall = ins->std.volMacro.val[mm];
2631 ds.ins[ds.ins.size() - 1]->std.volMacro.val[mm] = vall * 42 / 15;
2632 }
2633 }
2634 }
2635
2636 ins_vrc6_saw_conv[i][0] = i;
2637 ins_vrc6_saw_conv[i][1] = ds.ins.size() - 1;
2638
2639 go_to_end = true;
2640 }
2641
2642 if (go_to_end) {
2643 goto end2;
2644 }
2645 }
2646 }
2647 }
2648 }
2649 }
2650
2651 end2:;
2652 }
2653
2654 for (int i = 0; i < init_inst_num; i++) {
2655 for (int ii = 0; ii < total_chans; ii++) {
2656 for (size_t j = 0; j < ds.subsong.size(); j++) {
2657 for (int k = 0; k < DIV_MAX_PATTERNS; k++) {
2658 if (ds.subsong[j]->pat[ii].data[k] == NULL)
2659 continue;
2660 for (int l = 0; l < ds.subsong[j]->patLen; l++) {
2661 if (ds.subsong[j]->pat[ii].data[k]->data[l][2] == i) // instrument
2662 {
2663 DivInstrument* ins = ds.ins[i];
2664 bool go_to_end = false;
2665
2666 if (ins->type != DIV_INS_NES && (ii == mmc5_chans[0] || ii == mmc5_chans[1] || ii < 5)) // we encountered VRC6 (or whatever?) instrument on NES/MMC5 channel
2667 {
2668 DivInstrument* insnew = new DivInstrument;
2669 ds.ins.push_back(insnew);
2670
2671 *ds.ins[ds.ins.size() - 1] = *ins;
2672
2673 ds.ins[ds.ins.size() - 1]->name += " [NES copy]";
2674
2675 ds.ins[ds.ins.size() - 1]->type = DIV_INS_NES;
2676
2677 if (ins->type == DIV_INS_VRC6) {
2678 if (insnew->std.dutyMacro.len > 0) // convert duties for NES
2679 {
2680 for (int mm = 0; mm < insnew->std.dutyMacro.len; mm++) {
2681 switch (insnew->std.dutyMacro.val[mm]) {
2682 case 0:
2683 case 1: {
2684 insnew->std.dutyMacro.val[mm] = 0;
2685 break;
2686 }
2687 case 2:
2688 case 3:
2689 case 4:
2690 case 5: {
2691 insnew->std.dutyMacro.val[mm] = 1;
2692 break;
2693 }
2694 case 6:
2695 case 7: {
2696 insnew->std.dutyMacro.val[mm] = 2;
2697 break;
2698 }
2699 default:
2700 break;
2701 }
2702 }
2703 }
2704 }
2705
2706 ins_nes_conv[i][0] = i;
2707 ins_nes_conv[i][1] = ds.ins.size() - 1;
2708
2709 go_to_end = true;
2710 }
2711
2712 if (go_to_end) {
2713 goto end3;
2714 }
2715 }
2716 }
2717 }
2718 }
2719 }
2720
2721 end3:;
2722 }
2723
2724 for (int i = 0; i < 256; i++) {
2725 if (ins_vrc6_conv[i][0] != -1 || ins_vrc6_saw_conv[i][0] != -1 || ins_nes_conv[i][0] != -1) {
2726 for (int ii = 0; ii < total_chans; ii++) {
2727 for (size_t j = 0; j < ds.subsong.size(); j++) {
2728 for (int k = 0; k < DIV_MAX_PATTERNS; k++) {
2729 if (ds.subsong[j]->pat[ii].data[k] == NULL)
2730 continue;
2731 for (int l = 0; l < ds.subsong[j]->patLen; l++) {
2732 if (ds.subsong[j]->pat[ii].data[k]->data[l][2] == ins_vrc6_conv[i][0] && (ii == vrc6_chans[0] || ii == vrc6_chans[1])) // change ins index
2733 {
2734 ds.subsong[j]->pat[ii].data[k]->data[l][2] = ins_vrc6_conv[i][1];
2735 }
2736
2737 if (ds.subsong[j]->pat[ii].data[k]->data[l][2] == ins_vrc6_saw_conv[i][0] && ii == vrc6_saw_chan) {
2738 ds.subsong[j]->pat[ii].data[k]->data[l][2] = ins_vrc6_saw_conv[i][1];
2739 }
2740
2741 if (ds.subsong[j]->pat[ii].data[k]->data[l][2] == ins_nes_conv[i][0] && (ii == mmc5_chans[0] || ii == mmc5_chans[1] || ii < 5)) {
2742 ds.subsong[j]->pat[ii].data[k]->data[l][2] = ins_nes_conv[i][1];
2743 }
2744 }
2745 }
2746 }
2747 }
2748 }
2749 }
2750
2751 // why? I thought FamiTracker was lax
2752 //ds.delayBehavior=0;
2753
2754 ds.version=DIV_VERSION_FTM;
2755 ds.insLen = ds.ins.size();
2756 ds.sampleLen = ds.sample.size();
2757 ds.waveLen = ds.wave.size();
2758
2759 // check whether virtual tempo is inside range
2760 for (DivSubSong* i: ds.subsong) {
2761 while (i->virtualTempoD>255) {
2762 i->virtualTempoD>>=1;
2763 i->virtualTempoN>>=1;
2764 }
2765 if (i->virtualTempoN<1) i->virtualTempoN=1;
2766 if (i->virtualTempoD<1) i->virtualTempoD=1;
2767 }
2768
2769 // offset N163 wave macros (local -> global wave conversion)
2770 for (size_t i=0; i<ds.ins.size(); i++) {
2771 DivInstrument* ins=ds.ins[i];
2772 int waveOff=n163WaveOff[i];
2773 if (ins->type==DIV_INS_N163) {
2774 for (int j=0; j<ins->std.waveMacro.len; j++) {
2775 ins->std.waveMacro.val[j]+=waveOff;
2776 }
2777 }
2778 }
2779
2780 // offset N163 wave change effects whether possible
2781 for (DivSubSong* i: ds.subsong) {
2782 for (int j=0; j<total_chans; j++) {
2783 int curWaveOff=0;
2784 for (int k=0; k<i->ordersLen; k++) {
2785 DivPattern* p=i->pat[j].getPattern(i->orders.ord[j][k],true);
2786 for (int l=0; l<i->patLen; l++) {
2787 // check for instrument change
2788 if (p->data[l][2]!=-1) {
2789 curWaveOff=n163WaveOff[p->data[l][2]&127];
2790 }
2791
2792 // check effect columns for 0x110 (dummy wave change)
2793 for (int m=0; m<i->pat[j].effectCols; m++) {
2794 if (p->data[l][4+(m<<1)]==0x110) {
2795 // map wave
2796 p->data[l][4+(m<<1)]=0x10;
2797 if (p->data[l][5+(m<<1)]==-1) {
2798 p->data[l][5+(m<<1)]=curWaveOff&0xff;
2799 } else {
2800 p->data[l][5+(m<<1)]=(p->data[l][5+(m<<1)]+curWaveOff)&0xff;
2801 }
2802 }
2803 }
2804 }
2805 }
2806 }
2807 }
2808
2809 if (active) quitDispatch();
2810 BUSY_BEGIN_SOFT;
2811 saveLock.lock();
2812 song.unload();
2813 song=ds;
2814 changeSong(0);
2815 recalcChans();
2816 saveLock.unlock();
2817 BUSY_END;
2818 if (active) {
2819 initDispatch();
2820 BUSY_BEGIN;
2821 renderSamples();
2822 reset();
2823 BUSY_END;
2824 }
2825 } catch (EndOfFileException& e) {
2826 logE("premature end of file!");
2827 lastError = "incomplete file";
2828 delete[] file;
2829 return false;
2830 }
2831 delete[] file;
2832 return true;
2833 }