Debian Codegraph @

src/engine/instrument.h

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 #ifndef _INSTRUMENT_H
21 #define _INSTRUMENT_H
22 #include "safeWriter.h"
23 #include "dataErrors.h"
24 #include "../ta-utils.h"
25 #include "../pch.h"
26 #include "../fixedQueue.h"
27
28 struct DivSong;
29 struct DivInstrument;
30
31 // NOTICE!
32 // before adding new instrument types to this struct, please ask me first.
33 // absolutely zero support granted to conflicting formats.
34 enum DivInstrumentType: unsigned short {
35 DIV_INS_STD=0,
36 DIV_INS_FM=1,
37 DIV_INS_GB=2,
38 DIV_INS_C64=3,
39 DIV_INS_AMIGA=4,
40 DIV_INS_PCE=5,
41 DIV_INS_AY=6,
42 DIV_INS_AY8930=7,
43 DIV_INS_TIA=8,
44 DIV_INS_SAA1099=9,
45 DIV_INS_VIC=10,
46 DIV_INS_PET=11,
47 DIV_INS_VRC6=12,
48 DIV_INS_OPLL=13,
49 DIV_INS_OPL=14,
50 DIV_INS_FDS=15,
51 DIV_INS_VBOY=16,
52 DIV_INS_N163=17,
53 DIV_INS_SCC=18,
54 DIV_INS_OPZ=19,
55 DIV_INS_POKEY=20,
56 DIV_INS_BEEPER=21,
57 DIV_INS_SWAN=22,
58 DIV_INS_MIKEY=23,
59 DIV_INS_VERA=24,
60 DIV_INS_X1_010=25,
61 DIV_INS_VRC6_SAW=26,
62 DIV_INS_ES5506=27,
63 DIV_INS_MULTIPCM=28,
64 DIV_INS_SNES=29,
65 DIV_INS_SU=30,
66 DIV_INS_NAMCO=31,
67 DIV_INS_OPL_DRUMS=32,
68 DIV_INS_OPM=33,
69 DIV_INS_NES=34,
70 DIV_INS_MSM6258=35,
71 DIV_INS_MSM6295=36,
72 DIV_INS_ADPCMA=37,
73 DIV_INS_ADPCMB=38,
74 DIV_INS_SEGAPCM=39,
75 DIV_INS_QSOUND=40,
76 DIV_INS_YMZ280B=41,
77 DIV_INS_RF5C68=42,
78 DIV_INS_MSM5232=43,
79 DIV_INS_T6W28=44,
80 DIV_INS_K007232=45,
81 DIV_INS_GA20=46,
82 DIV_INS_POKEMINI=47,
83 DIV_INS_SM8521=48,
84 DIV_INS_PV1000=49,
85 DIV_INS_K053260=50,
86 // DIV_INS_YMF292=51,
87 DIV_INS_TED=52,
88 DIV_INS_C140=53,
89 DIV_INS_C219=54,
90 DIV_INS_ESFM=55,
91 DIV_INS_POWERNOISE=56,
92 DIV_INS_POWERNOISE_SLOPE=57,
93 DIV_INS_DAVE=58,
94 DIV_INS_NDS=59,
95 DIV_INS_GBA_DMA=60,
96 DIV_INS_GBA_MINMOD=61,
97 DIV_INS_BIFURCATOR=62,
98 DIV_INS_SID2=63, // coincidence!
99 DIV_INS_SUPERVISION=64,
100 DIV_INS_UPD1771C=65,
101 DIV_INS_SID3=66,
102 DIV_INS_MAX,
103 DIV_INS_NULL
104 };
105
106 enum DivMacroType: unsigned char {
107 DIV_MACRO_VOL=0,
108 DIV_MACRO_ARP,
109 DIV_MACRO_DUTY,
110 DIV_MACRO_WAVE,
111 DIV_MACRO_PITCH,
112 DIV_MACRO_EX1,
113 DIV_MACRO_EX2,
114 DIV_MACRO_EX3,
115 DIV_MACRO_ALG,
116 DIV_MACRO_FB,
117 DIV_MACRO_FMS,
118 DIV_MACRO_AMS,
119 DIV_MACRO_PAN_LEFT,
120 DIV_MACRO_PAN_RIGHT,
121 DIV_MACRO_PHASE_RESET,
122 DIV_MACRO_EX4,
123 DIV_MACRO_EX5,
124 DIV_MACRO_EX6,
125 DIV_MACRO_EX7,
126 DIV_MACRO_EX8,
127 DIV_MACRO_EX9,
128 DIV_MACRO_EX10
129 };
130
131 enum DivMacroTypeOp: unsigned char {
132 DIV_MACRO_OP_AM=32,
133 DIV_MACRO_OP_AR,
134 DIV_MACRO_OP_DR,
135 DIV_MACRO_OP_MULT,
136 DIV_MACRO_OP_RR,
137 DIV_MACRO_OP_SL,
138 DIV_MACRO_OP_TL,
139 DIV_MACRO_OP_DT2,
140 DIV_MACRO_OP_RS,
141 DIV_MACRO_OP_DT,
142 DIV_MACRO_OP_D2R,
143 DIV_MACRO_OP_SSG,
144 DIV_MACRO_OP_DAM,
145 DIV_MACRO_OP_DVB,
146 DIV_MACRO_OP_EGT,
147 DIV_MACRO_OP_KSL,
148 DIV_MACRO_OP_SUS,
149 DIV_MACRO_OP_VIB,
150 DIV_MACRO_OP_WS,
151 DIV_MACRO_OP_KSR,
152 };
153
154 // FM operator structure:
155 // - OPN:
156 // - AM, AR, DR, MULT, RR, SL, TL, RS, DT, D2R, SSG-EG
157 // - OPM:
158 // - AM, AR, DR, MULT, RR, SL, TL, DT2, RS, DT, D2R
159 // - OPLL:
160 // - AM, AR, DR, MULT, RR, SL, TL, SSG-EG&8 = EG-S
161 // - KSL, VIB, KSR
162 // - OPL:
163 // - AM, AR, DR, MULT, RR, SL, TL, SSG-EG&8 = EG-S
164 // - KSL, VIB, WS (OPL2/3), KSR
165 // - OPZ:
166 // - AM, AR, DR, MULT (CRS), RR, SL, TL, DT2, RS, DT, D2R
167 // - WS, DVB = MULT (FINE), DAM = REV, KSL = EGShift, EGT = Fixed
168
169 struct DivInstrumentFM {
170 unsigned char alg, fb, fms, ams, fms2, ams2, ops, opllPreset, block;
171 bool fixedDrums;
172 unsigned short kickFreq, snareHatFreq, tomTopFreq;
173
174 bool operator==(const DivInstrumentFM& other);
175 bool operator!=(const DivInstrumentFM& other) {
176 return !(*this==other);
177 }
178 struct Operator {
179 bool enable;
180 unsigned char am, ar, dr, mult, rr, sl, tl, dt2, rs, dt, d2r, ssgEnv;
181 unsigned char dam, dvb, egt, ksl, sus, vib, ws, ksr; // YMU759/OPL/OPZ
182 unsigned char kvs;
183
184 bool operator==(const Operator& other);
185 bool operator!=(const Operator& other) {
186 return !(*this==other);
187 }
188 Operator():
189 enable(true),
190 am(0),
191 ar(0),
192 dr(0),
193 mult(0),
194 rr(0),
195 sl(0),
196 tl(0),
197 dt2(0),
198 rs(0),
199 dt(0),
200 d2r(0),
201 ssgEnv(0),
202 dam(0),
203 dvb(0),
204 egt(0),
205 ksl(0),
206 sus(0),
207 vib(0),
208 ws(0),
209 ksr(0),
210 kvs(2) {}
211 } op[4];
212 DivInstrumentFM():
213 alg(0),
214 fb(0),
215 fms(0),
216 ams(0),
217 fms2(0),
218 ams2(0),
219 ops(2),
220 opllPreset(0),
221 block(0),
222 fixedDrums(false),
223 kickFreq(0x520),
224 snareHatFreq(0x550),
225 tomTopFreq(0x1c0) {
226 // default instrument
227 fb=4;
228 op[0].tl=42;
229 op[0].ar=31;
230 op[0].dr=8;
231 op[0].sl=15;
232 op[0].rr=3;
233 op[0].mult=5;
234 op[0].dt=5;
235
236 op[2].tl=18;
237 op[2].ar=31;
238 op[2].dr=10;
239 op[2].sl=15;
240 op[2].rr=4;
241 op[2].mult=1;
242 op[2].dt=0;
243
244 op[1].tl=48;
245 op[1].ar=31;
246 op[1].dr=4;
247 op[1].sl=11;
248 op[1].rr=1;
249 op[1].mult=1;
250 op[1].dt=5;
251
252 op[3].tl=2;
253 op[3].ar=31;
254 op[3].dr=9;
255 op[3].sl=15;
256 op[3].rr=9;
257 op[3].mult=1;
258 op[3].dt=0;
259 }
260 };
261
262 // this is getting out of hand
263 struct DivInstrumentMacro {
264 int val[256];
265 unsigned int mode;
266 unsigned char open;
267 unsigned char len, delay, speed, loop, rel;
268 // 0-31: normal
269 // 32+: operator (top 3 bits select operator, starting from 1)
270 unsigned char macroType;
271
272 explicit DivInstrumentMacro(unsigned char initType, bool initOpen=false):
273 mode(0),
274 open(initOpen),
275 len(0),
276 delay(0),
277 speed(1),
278 loop(255),
279 rel(255),
280 macroType(initType) {
281 memset(val,0,256*sizeof(int));
282 }
283 };
284
285 struct DivInstrumentSTD {
286 DivInstrumentMacro volMacro;
287 DivInstrumentMacro arpMacro;
288 DivInstrumentMacro dutyMacro;
289 DivInstrumentMacro waveMacro;
290 DivInstrumentMacro pitchMacro;
291 DivInstrumentMacro ex1Macro;
292 DivInstrumentMacro ex2Macro;
293 DivInstrumentMacro ex3Macro;
294 DivInstrumentMacro algMacro;
295 DivInstrumentMacro fbMacro;
296 DivInstrumentMacro fmsMacro;
297 DivInstrumentMacro amsMacro;
298 DivInstrumentMacro panLMacro;
299 DivInstrumentMacro panRMacro;
300 DivInstrumentMacro phaseResetMacro;
301 DivInstrumentMacro ex4Macro;
302 DivInstrumentMacro ex5Macro;
303 DivInstrumentMacro ex6Macro;
304 DivInstrumentMacro ex7Macro;
305 DivInstrumentMacro ex8Macro;
306 DivInstrumentMacro ex9Macro;
307 DivInstrumentMacro ex10Macro;
308
309 struct OpMacro {
310 // ar, dr, mult, rr, sl, tl, dt2, rs, dt, d2r, ssgEnv;
311 DivInstrumentMacro amMacro;
312 DivInstrumentMacro arMacro;
313 DivInstrumentMacro drMacro;
314 DivInstrumentMacro multMacro;
315 DivInstrumentMacro rrMacro;
316 DivInstrumentMacro slMacro;
317 DivInstrumentMacro tlMacro;
318 DivInstrumentMacro dt2Macro;
319 DivInstrumentMacro rsMacro;
320 DivInstrumentMacro dtMacro;
321 DivInstrumentMacro d2rMacro;
322 DivInstrumentMacro ssgMacro;
323 DivInstrumentMacro damMacro;
324 DivInstrumentMacro dvbMacro;
325 DivInstrumentMacro egtMacro;
326 DivInstrumentMacro kslMacro;
327 DivInstrumentMacro susMacro;
328 DivInstrumentMacro vibMacro;
329 DivInstrumentMacro wsMacro;
330 DivInstrumentMacro ksrMacro;
331 OpMacro():
332 amMacro(DIV_MACRO_OP_AM), arMacro(DIV_MACRO_OP_AR), drMacro(DIV_MACRO_OP_DR), multMacro(DIV_MACRO_OP_MULT),
333 rrMacro(DIV_MACRO_OP_RR), slMacro(DIV_MACRO_OP_SL), tlMacro(DIV_MACRO_OP_TL,true), dt2Macro(DIV_MACRO_OP_DT2),
334 rsMacro(DIV_MACRO_OP_RS), dtMacro(DIV_MACRO_OP_DT), d2rMacro(DIV_MACRO_OP_D2R), ssgMacro(DIV_MACRO_OP_SSG),
335 damMacro(DIV_MACRO_OP_DAM), dvbMacro(DIV_MACRO_OP_DVB), egtMacro(DIV_MACRO_OP_EGT), kslMacro(DIV_MACRO_OP_KSL),
336 susMacro(DIV_MACRO_OP_SUS), vibMacro(DIV_MACRO_OP_VIB), wsMacro(DIV_MACRO_OP_WS), ksrMacro(DIV_MACRO_OP_KSR) {}
337 } opMacros[4];
338
339 DivInstrumentMacro* macroByType(DivMacroType type);
340
341 DivInstrumentSTD():
342 volMacro(DIV_MACRO_VOL,true),
343 arpMacro(DIV_MACRO_ARP),
344 dutyMacro(DIV_MACRO_DUTY),
345 waveMacro(DIV_MACRO_WAVE),
346 pitchMacro(DIV_MACRO_PITCH),
347 ex1Macro(DIV_MACRO_EX1),
348 ex2Macro(DIV_MACRO_EX2),
349 ex3Macro(DIV_MACRO_EX3),
350 algMacro(DIV_MACRO_ALG),
351 fbMacro(DIV_MACRO_FB),
352 fmsMacro(DIV_MACRO_FMS),
353 amsMacro(DIV_MACRO_AMS),
354 panLMacro(DIV_MACRO_PAN_LEFT),
355 panRMacro(DIV_MACRO_PAN_RIGHT),
356 phaseResetMacro(DIV_MACRO_PHASE_RESET),
357 ex4Macro(DIV_MACRO_EX4),
358 ex5Macro(DIV_MACRO_EX5),
359 ex6Macro(DIV_MACRO_EX6),
360 ex7Macro(DIV_MACRO_EX7),
361 ex8Macro(DIV_MACRO_EX8),
362 ex9Macro(DIV_MACRO_EX9),
363 ex10Macro(DIV_MACRO_EX10) {
364 for (int i=0; i<4; i++) {
365 opMacros[i].amMacro.macroType=DIV_MACRO_OP_AM+(i<<5);
366 opMacros[i].arMacro.macroType=DIV_MACRO_OP_AR+(i<<5);
367 opMacros[i].drMacro.macroType=DIV_MACRO_OP_DR+(i<<5);
368 opMacros[i].multMacro.macroType=DIV_MACRO_OP_MULT+(i<<5);
369 opMacros[i].rrMacro.macroType=DIV_MACRO_OP_RR+(i<<5);
370 opMacros[i].slMacro.macroType=DIV_MACRO_OP_SL+(i<<5);
371 opMacros[i].tlMacro.macroType=DIV_MACRO_OP_TL+(i<<5);
372 opMacros[i].dt2Macro.macroType=DIV_MACRO_OP_DT2+(i<<5);
373 opMacros[i].rsMacro.macroType=DIV_MACRO_OP_RS+(i<<5);
374 opMacros[i].dtMacro.macroType=DIV_MACRO_OP_DT+(i<<5);
375 opMacros[i].d2rMacro.macroType=DIV_MACRO_OP_D2R+(i<<5);
376 opMacros[i].ssgMacro.macroType=DIV_MACRO_OP_SSG+(i<<5);
377
378 opMacros[i].damMacro.macroType=DIV_MACRO_OP_DAM+(i<<5);
379 opMacros[i].dvbMacro.macroType=DIV_MACRO_OP_DVB+(i<<5);
380 opMacros[i].egtMacro.macroType=DIV_MACRO_OP_EGT+(i<<5);
381 opMacros[i].kslMacro.macroType=DIV_MACRO_OP_KSL+(i<<5);
382 opMacros[i].susMacro.macroType=DIV_MACRO_OP_SUS+(i<<5);
383 opMacros[i].vibMacro.macroType=DIV_MACRO_OP_VIB+(i<<5);
384 opMacros[i].wsMacro.macroType=DIV_MACRO_OP_WS+(i<<5);
385 opMacros[i].ksrMacro.macroType=DIV_MACRO_OP_KSR+(i<<5);
386 }
387 }
388 };
389
390 struct DivInstrumentGB {
391 unsigned char envVol, envDir, envLen, soundLen, hwSeqLen;
392 bool softEnv, alwaysInit, doubleWave;
393 enum HWSeqCommands: unsigned char {
394 DIV_GB_HWCMD_ENVELOPE=0,
395 DIV_GB_HWCMD_SWEEP,
396 DIV_GB_HWCMD_WAIT,
397 DIV_GB_HWCMD_WAIT_REL,
398 DIV_GB_HWCMD_LOOP,
399 DIV_GB_HWCMD_LOOP_REL,
400
401 DIV_GB_HWCMD_MAX
402 };
403 struct HWSeqCommandGB {
404 unsigned char cmd;
405 unsigned short data;
406 } hwSeq[256];
407
408 bool operator==(const DivInstrumentGB& other);
409 bool operator!=(const DivInstrumentGB& other) {
410 return !(*this==other);
411 }
412
413 DivInstrumentGB():
414 envVol(15),
415 envDir(0),
416 envLen(2),
417 soundLen(64),
418 hwSeqLen(0),
419 softEnv(false),
420 alwaysInit(false),
421 doubleWave(false) {
422 memset(hwSeq,0,256*sizeof(HWSeqCommandGB));
423 }
424 };
425
426 struct DivInstrumentC64 {
427 bool triOn, sawOn, pulseOn, noiseOn;
428 unsigned char a, d, s, r;
429 unsigned short duty;
430 unsigned char ringMod, oscSync;
431 bool toFilter, initFilter, dutyIsAbs, filterIsAbs, noTest, resetDuty;
432 unsigned char res;
433 unsigned short cut;
434 bool hp, lp, bp, ch3off;
435
436 bool operator==(const DivInstrumentC64& other);
437 bool operator!=(const DivInstrumentC64& other) {
438 return !(*this==other);
439 }
440
441 DivInstrumentC64():
442 triOn(false),
443 sawOn(true),
444 pulseOn(false),
445 noiseOn(false),
446 a(0),
447 d(8),
448 s(0),
449 r(0),
450 duty(2048),
451 ringMod(0),
452 oscSync(0),
453 toFilter(false),
454 initFilter(false),
455 dutyIsAbs(false),
456 filterIsAbs(false),
457 noTest(false),
458 resetDuty(true),
459 res(0),
460 cut(0),
461 hp(false),
462 lp(false),
463 bp(false),
464 ch3off(false) {}
465 };
466
467 struct DivInstrumentAmiga {
468 struct SampleMap {
469 int freq;
470 short map;
471 signed char dpcmFreq;
472 signed char dpcmDelta;
473 SampleMap(int f=0, short m=-1, signed char df=15, signed char dd=-1):
474 freq(f),
475 map(m),
476 dpcmFreq(df),
477 dpcmDelta(dd) {}
478 };
479 short initSample;
480 bool useNoteMap;
481 bool useSample;
482 bool useWave;
483 unsigned char waveLen;
484 SampleMap noteMap[120];
485
486 bool operator==(const DivInstrumentAmiga& other);
487 bool operator!=(const DivInstrumentAmiga& other) {
488 return !(*this==other);
489 }
490
491 /**
492 * get the sample at specified note.
493 * @return the sample.
494 */
495 inline short getSample(int note) {
496 if (useNoteMap) {
497 if (note<0) note=0;
498 if (note>119) note=119;
499 return noteMap[note].map;
500 }
501 return initSample;
502 }
503
504 /**
505 * get the sample playback note at specified note.
506 * @return the note, or -1 if not using note map.
507 */
508 inline int getFreq(int note) {
509 if (useNoteMap) {
510 if (note<0) note=0;
511 if (note>119) note=119;
512 return noteMap[note].freq;
513 }
514 return note;
515 }
516
517 /**
518 * get the DPCM pitch at specified note.
519 * @return the pitch, or -1 if not using note map.
520 */
521 inline signed char getDPCMFreq(int note) {
522 if (useNoteMap) {
523 if (note<0) note=0;
524 if (note>119) note=119;
525 return noteMap[note].dpcmFreq;
526 }
527 return -1;
528 }
529
530 /**
531 * get the DPCM delta counter value at specified note.
532 * @return the delta counter value, or -1 if not using note map.
533 */
534 inline signed char getDPCMDelta(int note) {
535 if (useNoteMap) {
536 if (note<0) note=0;
537 if (note>119) note=119;
538 return noteMap[note].dpcmDelta;
539 }
540 return -1;
541 }
542
543 DivInstrumentAmiga():
544 initSample(0),
545 useNoteMap(false),
546 useSample(false),
547 useWave(false),
548 waveLen(31) {
549 for (int i=0; i<120; i++) {
550 noteMap[i].map=-1;
551 noteMap[i].freq=i;
552 }
553 }
554 };
555
556 struct DivInstrumentX1_010 {
557 int bankSlot;
558
559 bool operator==(const DivInstrumentX1_010& other);
560 bool operator!=(const DivInstrumentX1_010& other) {
561 return !(*this==other);
562 }
563
564 DivInstrumentX1_010():
565 bankSlot(0) {}
566 };
567
568 struct DivInstrumentN163 {
569 int wave, wavePos, waveLen;
570 unsigned char waveMode;
571 bool perChanPos;
572 int wavePosCh[8];
573 int waveLenCh[8];
574
575 bool operator==(const DivInstrumentN163& other);
576 bool operator!=(const DivInstrumentN163& other) {
577 return !(*this==other);
578 }
579
580 DivInstrumentN163():
581 wave(-1),
582 wavePos(0),
583 waveLen(32),
584 waveMode(3),
585 perChanPos(false) {
586 for (int i=0; i<8; i++) {
587 wavePosCh[i]=(i&3)<<5;
588 waveLenCh[i]=32;
589 }
590 }
591 };
592
593 struct DivInstrumentFDS {
594 signed char modTable[32];
595 int modSpeed, modDepth;
596 // this is here for compatibility.
597 bool initModTableWithFirstWave;
598
599 bool operator==(const DivInstrumentFDS& other);
600 bool operator!=(const DivInstrumentFDS& other) {
601 return !(*this==other);
602 }
603
604 DivInstrumentFDS():
605 modSpeed(0),
606 modDepth(0),
607 initModTableWithFirstWave(false) {
608 memset(modTable,0,32);
609 }
610 };
611
612 struct DivInstrumentMultiPCM {
613 unsigned char ar, d1r, dl, d2r, rr, rc;
614 unsigned char lfo, vib, am;
615 bool damp, pseudoReverb, lfoReset, levelDirect;
616
617 bool operator==(const DivInstrumentMultiPCM& other);
618 bool operator!=(const DivInstrumentMultiPCM& other) {
619 return !(*this==other);
620 }
621
622 DivInstrumentMultiPCM():
623 ar(15), d1r(15), dl(0), d2r(0), rr(15), rc(15),
624 lfo(0), vib(0), am(0),
625 damp(false),
626 pseudoReverb(false),
627 lfoReset(false),
628 levelDirect(true) {
629 }
630 };
631
632 enum DivWaveSynthEffects {
633 DIV_WS_NONE=0,
634 // one waveform effects
635 DIV_WS_INVERT,
636 DIV_WS_ADD,
637 DIV_WS_SUBTRACT,
638 DIV_WS_AVERAGE,
639 DIV_WS_PHASE,
640 DIV_WS_CHORUS,
641
642 DIV_WS_SINGLE_MAX,
643
644 // two waveform effects
645 DIV_WS_NONE_DUAL=128,
646 DIV_WS_WIPE,
647 DIV_WS_FADE,
648 DIV_WS_PING_PONG,
649 DIV_WS_OVERLAY,
650 DIV_WS_NEGATIVE_OVERLAY,
651 DIV_WS_SLIDE,
652 DIV_WS_MIX,
653 DIV_WS_PHASE_MOD,
654
655 DIV_WS_DUAL_MAX
656 };
657
658 struct DivInstrumentWaveSynth {
659 int wave1, wave2;
660 unsigned char rateDivider;
661 unsigned char effect;
662 bool oneShot, enabled, global;
663 unsigned char speed, param1, param2, param3, param4;
664
665 bool operator==(const DivInstrumentWaveSynth& other);
666 bool operator!=(const DivInstrumentWaveSynth& other) {
667 return !(*this==other);
668 }
669
670 DivInstrumentWaveSynth():
671 wave1(0),
672 wave2(0),
673 rateDivider(1),
674 effect(DIV_WS_NONE),
675 oneShot(false),
676 enabled(false),
677 global(false),
678 speed(0),
679 param1(0),
680 param2(0),
681 param3(0),
682 param4(0) {}
683 };
684
685 struct DivInstrumentSoundUnit {
686 bool switchRoles;
687 unsigned char hwSeqLen;
688 enum HWSeqCommands: unsigned char {
689 DIV_SU_HWCMD_VOL=0,
690 DIV_SU_HWCMD_PITCH,
691 DIV_SU_HWCMD_CUT,
692 DIV_SU_HWCMD_WAIT,
693 DIV_SU_HWCMD_WAIT_REL,
694 DIV_SU_HWCMD_LOOP,
695 DIV_SU_HWCMD_LOOP_REL,
696
697 DIV_SU_HWCMD_MAX
698 };
699 struct HWSeqCommandSU {
700 unsigned char cmd;
701 unsigned char bound;
702 unsigned char val;
703 unsigned short speed;
704 unsigned short padding;
705 } hwSeq[256];
706
707 bool operator==(const DivInstrumentSoundUnit& other);
708 bool operator!=(const DivInstrumentSoundUnit& other) {
709 return !(*this==other);
710 }
711
712 DivInstrumentSoundUnit():
713 switchRoles(false),
714 hwSeqLen(0) {
715 memset(hwSeq,0,256*sizeof(HWSeqCommandSU));
716 }
717 };
718
719 struct DivInstrumentES5506 {
720 struct Filter {
721 enum FilterMode: unsigned char { // filter mode for pole 4,3
722 FILTER_MODE_HPK2_HPK2=0,
723 FILTER_MODE_HPK2_LPK1,
724 FILTER_MODE_LPK2_LPK2,
725 FILTER_MODE_LPK2_LPK1,
726 };
727 FilterMode mode;
728 unsigned short k1, k2;
729 Filter():
730 mode(FILTER_MODE_LPK2_LPK1),
731 k1(0xffff),
732 k2(0xffff) {}
733 };
734 struct Envelope {
735 unsigned short ecount;
736 signed char lVRamp, rVRamp;
737 signed char k1Ramp, k2Ramp;
738 bool k1Slow, k2Slow;
739 Envelope():
740 ecount(0),
741 lVRamp(0),
742 rVRamp(0),
743 k1Ramp(0),
744 k2Ramp(0),
745 k1Slow(false),
746 k2Slow(false) {}
747 };
748 Filter filter;
749 Envelope envelope;
750
751 bool operator==(const DivInstrumentES5506& other);
752 bool operator!=(const DivInstrumentES5506& other) {
753 return !(*this==other);
754 }
755
756 DivInstrumentES5506():
757 filter(Filter()),
758 envelope(Envelope()) {}
759 };
760
761 struct DivInstrumentSNES {
762 enum GainMode: unsigned char {
763 GAIN_MODE_DIRECT=0,
764 GAIN_MODE_DEC_LINEAR=4,
765 GAIN_MODE_DEC_LOG=5,
766 GAIN_MODE_INC_LINEAR=6,
767 GAIN_MODE_INC_INVLOG=7
768 };
769 bool useEnv;
770 // 0: no sustain (key off = cut)
771 // 1: sustain (R = d2; key off = dec linear to r)
772 // 2: sustain (R = d2; key off = dec exponential to r)
773 // 3: sustain (R = d2; key off = R to r)
774 unsigned char sus;
775 GainMode gainMode;
776 unsigned char gain;
777 unsigned char a, d, s, r, d2;
778
779 bool operator==(const DivInstrumentSNES& other);
780 bool operator!=(const DivInstrumentSNES& other) {
781 return !(*this==other);
782 }
783
784 DivInstrumentSNES():
785 useEnv(true),
786 sus(0),
787 gainMode(GAIN_MODE_DIRECT),
788 gain(127),
789 a(15),
790 d(7),
791 s(7),
792 r(0),
793 d2(0) {}
794 };
795
796 // ESFM operator structure:
797 // - DELAY, OUT, MOD, L, R, NOISE
798 // - Virtual: CT, DT, FIXED
799 // - In FM struct: AM, DAM, AR, DR, MULT, RR, SL, TL
800 // - In FM struct: KSL, VIB, DVB, WS, SUS, KSR
801 // - Not in struct: FNUML, FNUMH, BLOCK
802
803 struct DivInstrumentESFM {
804 bool operator==(const DivInstrumentESFM& other);
805 bool operator!=(const DivInstrumentESFM& other) {
806 return !(*this==other);
807 }
808
809 // Only works on OP4, so putting it outside the Operator struct instead
810 unsigned char noise;
811 struct Operator {
812 unsigned char delay, outLvl, modIn, left, right, fixed;
813 signed char ct, dt;
814
815 bool operator==(const Operator& other);
816 bool operator!=(const Operator& other) {
817 return !(*this==other);
818 }
819 Operator():
820 delay(0),
821 outLvl(0),
822 modIn(0),
823 left(1),
824 right(1),
825 fixed(0),
826 ct(0),
827 dt(0) {}
828 } op[4];
829 DivInstrumentESFM():
830 noise(0)
831 {
832 op[0].modIn=4;
833 op[0].outLvl=0;
834
835 op[1].modIn=7;
836 op[1].outLvl=0;
837
838 op[2].modIn=7;
839 op[2].outLvl=0;
840
841 op[3].modIn=7;
842 op[3].outLvl=7;
843 }
844 };
845
846 struct DivInstrumentPowerNoise {
847 unsigned char octave;
848
849 bool operator==(const DivInstrumentPowerNoise& other);
850 bool operator!=(const DivInstrumentPowerNoise& other) {
851 return !(*this==other);
852 }
853 DivInstrumentPowerNoise():
854 octave(0) {}
855 };
856
857 struct DivInstrumentSID2 {
858 unsigned char volume;
859 unsigned char mixMode;
860 unsigned char noiseMode;
861
862 bool operator==(const DivInstrumentSID2& other);
863 bool operator!=(const DivInstrumentSID2& other) {
864 return !(*this==other);
865 }
866 DivInstrumentSID2():
867 volume(15),
868 mixMode(0),
869 noiseMode(0) {}
870 };
871
872 struct DivInstrumentSID3 {
873 bool triOn, sawOn, pulseOn, noiseOn;
874 unsigned char a, d, s, r;
875 unsigned char sr;
876 unsigned short duty;
877 unsigned char ringMod, oscSync;
878 bool phase_mod;
879 unsigned char phase_mod_source, ring_mod_source, sync_source;
880 bool specialWaveOn;
881 bool oneBitNoise;
882 bool separateNoisePitch;
883 unsigned char special_wave;
884 bool doWavetable;
885 bool dutyIsAbs;
886 bool resetDuty;
887 unsigned char phaseInv;
888 unsigned char feedback;
889 unsigned char mixMode;
890
891 struct Filter {
892 unsigned short cutoff;
893 unsigned char resonance;
894 unsigned char output_volume;
895 unsigned char distortion_level;
896 unsigned char mode;
897 bool enabled;
898 bool init;
899 unsigned char filter_matrix;
900
901 // this is done purely in software
902 bool absoluteCutoff;
903 bool bindCutoffToNote;
904 unsigned char bindCutoffToNoteStrength; // how much cutoff changes over e.g. 1 semitone
905 unsigned char bindCutoffToNoteCenter; // central note of the cutoff change
906 bool bindCutoffToNoteDir; // if we decrease or increase cutoff if e.g. we go upper in note space
907 bool bindCutoffOnNote; // only do cutoff scaling once, on new note
908
909 bool bindResonanceToNote;
910 unsigned char bindResonanceToNoteStrength; // how much resonance changes over e.g. 1 semitone
911 unsigned char bindResonanceToNoteCenter; // central note of the resonance change
912 bool bindResonanceToNoteDir; // if we decrease or increase resonance if e.g. we go upper in note space
913 bool bindResonanceOnNote; // only do resonance scaling once, on new note
914
915 bool operator==(const Filter& other);
916 bool operator!=(const Filter& other) {
917 return !(*this==other);
918 }
919 Filter():
920 cutoff(0),
921 resonance(0),
922 output_volume(0),
923 distortion_level(0),
924 mode(0),
925 enabled(false),
926 init(false),
927 filter_matrix(0),
928 absoluteCutoff(false),
929 bindCutoffToNote(false),
930 bindCutoffToNoteStrength(0),
931 bindCutoffToNoteCenter(0),
932 bindCutoffToNoteDir(false),
933 bindCutoffOnNote(false),
934 bindResonanceToNote(false),
935 bindResonanceToNoteStrength(0),
936 bindResonanceToNoteCenter(0),
937 bindResonanceToNoteDir(false),
938 bindResonanceOnNote(false) {}
939 } filt[4];
940
941 bool operator==(const DivInstrumentSID3& other);
942 bool operator!=(const DivInstrumentSID3& other) {
943 return !(*this==other);
944 }
945 DivInstrumentSID3():
946 triOn(false),
947 sawOn(true),
948 pulseOn(false),
949 noiseOn(false),
950 a(0),
951 d(64),
952 s(0),
953 r(0),
954 sr(0),
955 duty(32768),
956 ringMod(0),
957 oscSync(0),
958 phase_mod(false),
959 phase_mod_source(0),
960 ring_mod_source(0),
961 sync_source(0),
962 specialWaveOn(false),
963 oneBitNoise(false),
964 separateNoisePitch(false),
965 special_wave(0),
966 doWavetable(false),
967 dutyIsAbs(true),
968 resetDuty(false),
969 phaseInv(0),
970 feedback(0),
971 mixMode(0) {
972 filt[0].mode=16|32; // default settings so filter just works, connect to input and channel output
973 filt[0].output_volume=0xff;
974 }
975 };
976
977 struct DivInstrumentPOD {
978 DivInstrumentType type;
979 DivInstrumentFM fm;
980 DivInstrumentSTD std;
981 DivInstrumentGB gb;
982 DivInstrumentC64 c64;
983 DivInstrumentAmiga amiga;
984 DivInstrumentX1_010 x1_010;
985 DivInstrumentN163 n163;
986 DivInstrumentFDS fds;
987 DivInstrumentMultiPCM multipcm;
988 DivInstrumentWaveSynth ws;
989 DivInstrumentSoundUnit su;
990 DivInstrumentES5506 es5506;
991 DivInstrumentSNES snes;
992 DivInstrumentESFM esfm;
993 DivInstrumentPowerNoise powernoise;
994 DivInstrumentSID2 sid2;
995 DivInstrumentSID3 sid3;
996
997 DivInstrumentPOD() :
998 type(DIV_INS_FM) {
999 }
1000 };
1001
1002 struct DivInstrumentTemp {
1003 // the following variables are used by the GUI and not saved in the file
1004 int vScroll[160];
1005 int vZoom[160];
1006 int typeMemory[160][16];
1007 unsigned char lenMemory[160];
1008 DivInstrumentTemp() {
1009 memset(vScroll,0,160*sizeof(int));
1010 memset(vZoom,-1,160*sizeof(int));
1011 memset(typeMemory,0,160*16*sizeof(int));
1012 memset(lenMemory,0,160*sizeof(int));
1013 }
1014 };
1015
1016 struct MemPatch {
1017 MemPatch() :
1018 data(NULL)
1019 , offset(0)
1020 , size(0) {
1021 }
1022
1023 ~MemPatch() {
1024 if (data) {
1025 delete[] data;
1026 data=NULL;
1027 }
1028 }
1029
1030 bool calcDiff(const void* pre, const void* post, size_t size);
1031 void applyAndReverse(void* target, size_t inputSize);
1032 bool isValid() const { return size>0; }
1033
1034 unsigned char* data;
1035 size_t offset;
1036 size_t size;
1037 };
1038
1039 struct DivInstrumentUndoStep {
1040 DivInstrumentUndoStep() :
1041 name(""),
1042 nameValid(false),
1043 processTime(0) {
1044 }
1045
1046 MemPatch podPatch;
1047 String name;
1048 bool nameValid;
1049 size_t processTime;
1050
1051 void applyAndReverse(DivInstrument* target);
1052 bool makeUndoPatch(size_t processTime_, const DivInstrument* pre, const DivInstrument* post);
1053 };
1054
1055 struct DivInstrument : DivInstrumentPOD {
1056 String name;
1057
1058 DivInstrumentTemp temp;
1059
1060 DivInstrument() :
1061 name("") {
1062 // clear and construct DivInstrumentPOD so it doesn't have any garbage in the padding
1063 memset((unsigned char*)(DivInstrumentPOD*)this, 0, sizeof(DivInstrumentPOD));
1064 new ((DivInstrumentPOD*)this) DivInstrumentPOD;
1065 }
1066
1067 ~DivInstrument();
1068
1069 /**
1070 * copy/assignment to specifically avoid leaking or dangling pointers to undo step
1071 */
1072 DivInstrument( const DivInstrument& ins );
1073 DivInstrument& operator=( const DivInstrument& ins );
1074
1075 /**
1076 * undo stuff
1077 */
1078 FixedQueue<DivInstrumentUndoStep*, 128> undoHist;
1079 FixedQueue<DivInstrumentUndoStep*, 128> redoHist;
1080 bool recordUndoStepIfChanged(size_t processTime, const DivInstrument* old);
1081 int undo();
1082 int redo();
1083
1084 /**
1085 * these are internal functions.
1086 */
1087 void writeMacro(SafeWriter* w, const DivInstrumentMacro& m);
1088 void writeFeatureNA(SafeWriter* w);
1089 void writeFeatureFM(SafeWriter* w, bool fui);
1090 void writeFeatureMA(SafeWriter* w);
1091 void writeFeature64(SafeWriter* w);
1092 void writeFeatureGB(SafeWriter* w);
1093 void writeFeatureSM(SafeWriter* w);
1094 void writeFeatureOx(SafeWriter* w, int op);
1095 void writeFeatureLD(SafeWriter* w);
1096 void writeFeatureSN(SafeWriter* w);
1097 void writeFeatureN1(SafeWriter* w);
1098 void writeFeatureFD(SafeWriter* w);
1099 void writeFeatureWS(SafeWriter* w);
1100 size_t writeFeatureLS(SafeWriter* w, std::vector<int>& list, const DivSong* song);
1101 size_t writeFeatureLW(SafeWriter* w, std::vector<int>& list, const DivSong* song);
1102 void writeFeatureMP(SafeWriter* w);
1103 void writeFeatureSU(SafeWriter* w);
1104 void writeFeatureES(SafeWriter* w);
1105 void writeFeatureX1(SafeWriter* w);
1106 void writeFeatureNE(SafeWriter* w);
1107 void writeFeatureEF(SafeWriter* w);
1108 void writeFeaturePN(SafeWriter* w);
1109 void writeFeatureS2(SafeWriter* w);
1110 void writeFeatureS3(SafeWriter* w);
1111
1112 void readFeatureNA(SafeReader& reader, short version);
1113 void readFeatureFM(SafeReader& reader, short version);
1114 void readFeatureMA(SafeReader& reader, short version);
1115 void readFeature64(SafeReader& reader, bool& volIsCutoff, short version);
1116 void readFeatureGB(SafeReader& reader, short version);
1117 void readFeatureSM(SafeReader& reader, short version);
1118 void readFeatureOx(SafeReader& reader, int op, short version);
1119 void readFeatureLD(SafeReader& reader, short version);
1120 void readFeatureSN(SafeReader& reader, short version);
1121 void readFeatureN1(SafeReader& reader, short version);
1122 void readFeatureFD(SafeReader& reader, short version);
1123 void readFeatureWS(SafeReader& reader, short version);
1124 void readFeatureSL(SafeReader& reader, DivSong* song, short version);
1125 void readFeatureWL(SafeReader& reader, DivSong* song, short version);
1126 void readFeatureLS(SafeReader& reader, DivSong* song, short version);
1127 void readFeatureLW(SafeReader& reader, DivSong* song, short version);
1128 void readFeatureMP(SafeReader& reader, short version);
1129 void readFeatureSU(SafeReader& reader, short version);
1130 void readFeatureES(SafeReader& reader, short version);
1131 void readFeatureX1(SafeReader& reader, short version);
1132 void readFeatureNE(SafeReader& reader, short version);
1133 void readFeatureEF(SafeReader& reader, short version);
1134 void readFeaturePN(SafeReader& reader, short version);
1135 void readFeatureS2(SafeReader& reader, short version);
1136 void readFeatureS3(SafeReader& reader, short version);
1137
1138 DivDataErrors readInsDataOld(SafeReader& reader, short version);
1139 DivDataErrors readInsDataNew(SafeReader& reader, short version, bool fui, DivSong* song);
1140
1141 void convertC64SpecialMacro();
1142
1143 /**
1144 * save the instrument to a SafeWriter.
1145 * @param w the SafeWriter in question.
1146 */
1147 void putInsData2(SafeWriter* w, bool fui=false, const DivSong* song=NULL, bool insName=true);
1148
1149 /**
1150 * read instrument data in .fui format.
1151 * @param reader the reader.
1152 * @param version the format version.
1153 * @return a DivDataErrors.
1154 */
1155 DivDataErrors readInsData(SafeReader& reader, short version, DivSong* song=NULL);
1156
1157 /**
1158 * save this instrument to a file.
1159 * @param path file path.
1160 * @param song if new format, a DivSong to read wavetables and samples.
1161 * @param writeInsName whether to write the instrument name or not. ignored if old format.
1162 * @return whether it was successful.
1163 */
1164 bool save(const char* path, DivSong* song=NULL, bool writeInsName=true);
1165
1166 /**
1167 * save this instrument to a file in .dmp format.
1168 * @param path file path.
1169 * @return whether it was successful.
1170 */
1171 bool saveDMP(const char* path);
1172 };
1173 #endif