| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "fileOpsCommon.h" |
| 21 |
|
| 22 |
static const char* trueFalse[2]={ |
| 23 |
"no", "yes" |
| 24 |
}; |
| 25 |
|
| 26 |
static const char* gbEnvDir[2]={ |
| 27 |
"down", "up" |
| 28 |
}; |
| 29 |
|
| 30 |
static const char* notes[12]={ |
| 31 |
"C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-" |
| 32 |
}; |
| 33 |
|
| 34 |
static const char* notesNegative[12]={ |
| 35 |
"c_", "c+", "d_", "d+", "e_", "f_", "f+", "g_", "g+", "a_", "a+", "b_" |
| 36 |
}; |
| 37 |
|
| 38 |
static const char* sampleLoopModes[4]={ |
| 39 |
"forward", "backward", "ping-pong", "invalid" |
| 40 |
}; |
| 41 |
|
| 42 |
void writeTextMacro(SafeWriter* w, DivInstrumentMacro& m, const char* name, bool& wroteMacroHeader) { |
| 43 |
if ((m.open&6)==0 && m.len<1) return; |
| 44 |
if (!wroteMacroHeader) { |
| 45 |
w->writeText("- macros:\n"); |
| 46 |
wroteMacroHeader=true; |
| 47 |
} |
| 48 |
w->writeText(fmt::sprintf(" - %s:",name)); |
| 49 |
int len=m.len; |
| 50 |
switch (m.open&6) { |
| 51 |
case 2: |
| 52 |
len=16; |
| 53 |
w->writeText(" [ADSR]"); |
| 54 |
break; |
| 55 |
case 4: |
| 56 |
len=16; |
| 57 |
w->writeText(" [LFO]"); |
| 58 |
break; |
| 59 |
} |
| 60 |
if (m.mode) { |
| 61 |
w->writeText(fmt::sprintf(" [MODE %d]",m.mode)); |
| 62 |
} |
| 63 |
if (m.delay>0) { |
| 64 |
w->writeText(fmt::sprintf(" [DELAY %d]",m.delay)); |
| 65 |
} |
| 66 |
if (m.speed>1) { |
| 67 |
w->writeText(fmt::sprintf(" [SPEED %d]",m.speed)); |
| 68 |
} |
| 69 |
for (int i=0; i<len; i++) { |
| 70 |
if (i==m.loop) { |
| 71 |
w->writeText(" |"); |
| 72 |
} |
| 73 |
if (i==m.rel) { |
| 74 |
w->writeText(" /"); |
| 75 |
} |
| 76 |
w->writeText(fmt::sprintf(" %d",m.val[i])); |
| 77 |
} |
| 78 |
w->writeText("\n"); |
| 79 |
} |
| 80 |
|
| 81 |
SafeWriter* DivEngine::saveText(bool separatePatterns) { |
| 82 |
saveLock.lock(); |
| 83 |
|
| 84 |
SafeWriter* w=new SafeWriter; |
| 85 |
w->init(); |
| 86 |
|
| 87 |
w->writeText(fmt::sprintf("# Furnace Text Export\n\ngenerated by Furnace %s (%d)\n\n# Song Information\n\n",DIV_VERSION,DIV_ENGINE_VERSION)); |
| 88 |
w->writeText(fmt::sprintf("- name: %s\n",song.name)); |
| 89 |
w->writeText(fmt::sprintf("- author: %s\n",song.author)); |
| 90 |
w->writeText(fmt::sprintf("- album: %s\n",song.category)); |
| 91 |
w->writeText(fmt::sprintf("- system: %s\n",song.systemName)); |
| 92 |
w->writeText(fmt::sprintf("- tuning: %g\n\n",song.tuning)); |
| 93 |
|
| 94 |
w->writeText(fmt::sprintf("- instruments: %d\n",song.insLen)); |
| 95 |
w->writeText(fmt::sprintf("- wavetables: %d\n",song.waveLen)); |
| 96 |
w->writeText(fmt::sprintf("- samples: %d\n\n",song.sampleLen)); |
| 97 |
|
| 98 |
w->writeText("# Sound Chips\n\n"); |
| 99 |
|
| 100 |
for (int i=0; i<song.systemLen; i++) { |
| 101 |
w->writeText(fmt::sprintf("- %s\n",getSystemName(song.system[i]))); |
| 102 |
w->writeText(fmt::sprintf(" - id: %.2X\n",(int)song.system[i])); |
| 103 |
w->writeText(fmt::sprintf(" - volume: %g\n",song.systemVol[i])); |
| 104 |
w->writeText(fmt::sprintf(" - panning: %g\n",song.systemPan[i])); |
| 105 |
w->writeText(fmt::sprintf(" - front/rear: %g\n",song.systemPanFR[i])); |
| 106 |
|
| 107 |
String sysFlags=song.systemFlags[i].toString(); |
| 108 |
|
| 109 |
if (!sysFlags.empty()) { |
| 110 |
w->writeText(fmt::sprintf(" - flags:\n```\n%s\n```\n",sysFlags)); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if (!song.notes.empty()) { |
| 115 |
w->writeText("\n# Song Comments\n\n"); |
| 116 |
w->writeText(song.notes); |
| 117 |
} |
| 118 |
|
| 119 |
w->writeText("\n# Instruments\n\n"); |
| 120 |
|
| 121 |
for (int i=0; i<song.insLen; i++) { |
| 122 |
DivInstrument* ins=song.ins[i]; |
| 123 |
|
| 124 |
w->writeText(fmt::sprintf("## %.2X: %s\n\n",i,ins->name)); |
| 125 |
|
| 126 |
w->writeText(fmt::sprintf("- type: %d\n",(int)ins->type)); |
| 127 |
|
| 128 |
if (ins->type==DIV_INS_FM || ins->type==DIV_INS_OPL || ins->type==DIV_INS_OPLL || ins->type==DIV_INS_OPZ || ins->type==DIV_INS_OPL_DRUMS || ins->type==DIV_INS_OPM || ins->type==DIV_INS_ESFM) { |
| 129 |
int opCount=4; |
| 130 |
if (ins->type==DIV_INS_OPLL) { |
| 131 |
opCount=2; |
| 132 |
} else if (ins->type==DIV_INS_OPL) { |
| 133 |
opCount=(ins->fm.ops==4)?4:2; |
| 134 |
} |
| 135 |
|
| 136 |
w->writeText("- FM parameters:\n"); |
| 137 |
w->writeText(fmt::sprintf(" - ALG: %d\n",ins->fm.alg)); |
| 138 |
w->writeText(fmt::sprintf(" - FB: %d\n",ins->fm.fb)); |
| 139 |
w->writeText(fmt::sprintf(" - FMS: %d\n",ins->fm.fms)); |
| 140 |
w->writeText(fmt::sprintf(" - AMS: %d\n",ins->fm.ams)); |
| 141 |
w->writeText(fmt::sprintf(" - FMS2: %d\n",ins->fm.fms2)); |
| 142 |
w->writeText(fmt::sprintf(" - AMS2: %d\n",ins->fm.ams2)); |
| 143 |
w->writeText(fmt::sprintf(" - operators: %d\n",opCount)); |
| 144 |
w->writeText(fmt::sprintf(" - OPLL patch: %d\n",ins->fm.opllPreset)); |
| 145 |
w->writeText(fmt::sprintf(" - fixed drum freq: %s\n",trueFalse[ins->fm.fixedDrums?1:0])); |
| 146 |
w->writeText(fmt::sprintf(" - kick freq: %.4X\n",ins->fm.kickFreq)); |
| 147 |
w->writeText(fmt::sprintf(" - snare/hat freq: %.4X\n",ins->fm.snareHatFreq)); |
| 148 |
w->writeText(fmt::sprintf(" - tom/top freq: %.4X\n",ins->fm.tomTopFreq)); |
| 149 |
|
| 150 |
for (int j=0; j<opCount; j++) { |
| 151 |
DivInstrumentFM::Operator& op=ins->fm.op[j]; |
| 152 |
|
| 153 |
w->writeText(fmt::sprintf(" - operator %d:\n",j)); |
| 154 |
w->writeText(fmt::sprintf(" - enabled: %s\n",trueFalse[op.enable?1:0])); |
| 155 |
w->writeText(fmt::sprintf(" - AM: %d\n",op.am)); |
| 156 |
w->writeText(fmt::sprintf(" - AR: %d\n",op.ar)); |
| 157 |
w->writeText(fmt::sprintf(" - DR: %d\n",op.dr)); |
| 158 |
w->writeText(fmt::sprintf(" - MULT: %d\n",op.mult)); |
| 159 |
w->writeText(fmt::sprintf(" - RR: %d\n",op.rr)); |
| 160 |
w->writeText(fmt::sprintf(" - SL: %d\n",op.sl)); |
| 161 |
w->writeText(fmt::sprintf(" - TL: %d\n",op.tl)); |
| 162 |
w->writeText(fmt::sprintf(" - DT2: %d\n",op.dt2)); |
| 163 |
w->writeText(fmt::sprintf(" - RS: %d\n",op.rs)); |
| 164 |
w->writeText(fmt::sprintf(" - DT: %d\n",op.dt)); |
| 165 |
w->writeText(fmt::sprintf(" - D2R: %d\n",op.d2r)); |
| 166 |
w->writeText(fmt::sprintf(" - SSG-EG: %d\n",op.ssgEnv)); |
| 167 |
w->writeText(fmt::sprintf(" - DAM: %d\n",op.dam)); |
| 168 |
w->writeText(fmt::sprintf(" - DVB: %d\n",op.dvb)); |
| 169 |
w->writeText(fmt::sprintf(" - EGT: %d\n",op.egt)); |
| 170 |
w->writeText(fmt::sprintf(" - KSL: %d\n",op.ksl)); |
| 171 |
w->writeText(fmt::sprintf(" - SUS: %d\n",op.sus)); |
| 172 |
w->writeText(fmt::sprintf(" - VIB: %d\n",op.vib)); |
| 173 |
w->writeText(fmt::sprintf(" - WS: %d\n",op.ws)); |
| 174 |
w->writeText(fmt::sprintf(" - KSR: %d\n",op.ksr)); |
| 175 |
w->writeText(fmt::sprintf(" - TL volume scale: %d\n",op.kvs)); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if (ins->type==DIV_INS_ESFM) { |
| 180 |
w->writeText("- ESFM parameters:\n"); |
| 181 |
w->writeText(fmt::sprintf(" - noise mode: %d\n",ins->esfm.noise)); |
| 182 |
|
| 183 |
for (int j=0; j<4; j++) { |
| 184 |
DivInstrumentESFM::Operator& opE=ins->esfm.op[j]; |
| 185 |
|
| 186 |
w->writeText(fmt::sprintf(" - operator %d:\n",j)); |
| 187 |
w->writeText(fmt::sprintf(" - DL: %d\n",opE.delay)); |
| 188 |
w->writeText(fmt::sprintf(" - OL: %d\n",opE.outLvl)); |
| 189 |
w->writeText(fmt::sprintf(" - MI: %d\n",opE.modIn)); |
| 190 |
w->writeText(fmt::sprintf(" - output left: %s\n",trueFalse[opE.left?1:0])); |
| 191 |
w->writeText(fmt::sprintf(" - output right: %s\n",trueFalse[opE.right?1:0])); |
| 192 |
w->writeText(fmt::sprintf(" - CT: %d\n",opE.ct)); |
| 193 |
w->writeText(fmt::sprintf(" - DT: %d\n",opE.dt)); |
| 194 |
w->writeText(fmt::sprintf(" - fixed frequency: %s\n",trueFalse[opE.fixed?1:0])); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (ins->type==DIV_INS_GB) { |
| 199 |
w->writeText("- Game Boy parameters:\n"); |
| 200 |
w->writeText(fmt::sprintf(" - volume: %d\n",ins->gb.envVol)); |
| 201 |
w->writeText(fmt::sprintf(" - direction: %s\n",gbEnvDir[ins->gb.envDir?1:0])); |
| 202 |
w->writeText(fmt::sprintf(" - length: %d\n",ins->gb.envLen)); |
| 203 |
w->writeText(fmt::sprintf(" - sound length: %d\n",ins->gb.soundLen)); |
| 204 |
w->writeText(fmt::sprintf(" - use software envelope: %s\n",trueFalse[ins->gb.softEnv?1:0])); |
| 205 |
w->writeText(fmt::sprintf(" - always initialize: %s\n",trueFalse[ins->gb.softEnv?1:0])); |
| 206 |
if (ins->gb.hwSeqLen>0) { |
| 207 |
w->writeText(" - hardware sequence:\n"); |
| 208 |
for (int j=0; j<ins->gb.hwSeqLen; j++) { |
| 209 |
w->writeText(fmt::sprintf(" - %d: %.2X %.4X\n",j,ins->gb.hwSeq[j].cmd,ins->gb.hwSeq[j].data)); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
bool header=false; |
| 215 |
writeTextMacro(w,ins->std.volMacro,"vol",header); |
| 216 |
writeTextMacro(w,ins->std.arpMacro,"arp",header); |
| 217 |
writeTextMacro(w,ins->std.dutyMacro,"duty",header); |
| 218 |
writeTextMacro(w,ins->std.waveMacro,"wave",header); |
| 219 |
writeTextMacro(w,ins->std.pitchMacro,"pitch",header); |
| 220 |
writeTextMacro(w,ins->std.panLMacro,"panL",header); |
| 221 |
writeTextMacro(w,ins->std.panRMacro,"panR",header); |
| 222 |
writeTextMacro(w,ins->std.phaseResetMacro,"phaseReset",header); |
| 223 |
writeTextMacro(w,ins->std.ex1Macro,"ex1",header); |
| 224 |
writeTextMacro(w,ins->std.ex2Macro,"ex2",header); |
| 225 |
writeTextMacro(w,ins->std.ex3Macro,"ex3",header); |
| 226 |
writeTextMacro(w,ins->std.ex4Macro,"ex4",header); |
| 227 |
writeTextMacro(w,ins->std.ex5Macro,"ex5",header); |
| 228 |
writeTextMacro(w,ins->std.ex6Macro,"ex6",header); |
| 229 |
writeTextMacro(w,ins->std.ex7Macro,"ex7",header); |
| 230 |
writeTextMacro(w,ins->std.ex8Macro,"ex8",header); |
| 231 |
writeTextMacro(w,ins->std.ex9Macro,"ex9",header); |
| 232 |
writeTextMacro(w,ins->std.ex10Macro,"ex10",header); |
| 233 |
writeTextMacro(w,ins->std.algMacro,"alg",header); |
| 234 |
writeTextMacro(w,ins->std.fbMacro,"fb",header); |
| 235 |
writeTextMacro(w,ins->std.fmsMacro,"fms",header); |
| 236 |
writeTextMacro(w,ins->std.amsMacro,"ams",header); |
| 237 |
|
| 238 |
// TODO: the rest |
| 239 |
w->writeText("\n"); |
| 240 |
} |
| 241 |
|
| 242 |
w->writeText("\n# Wavetables\n\n"); |
| 243 |
|
| 244 |
for (int i=0; i<song.waveLen; i++) { |
| 245 |
DivWavetable* wave=song.wave[i]; |
| 246 |
|
| 247 |
w->writeText(fmt::sprintf("- %d (%dx%d):",i,wave->len,wave->max+1)); |
| 248 |
for (int j=0; j<=wave->len; j++) { |
| 249 |
w->writeText(fmt::sprintf(" %d",wave->data[j])); |
| 250 |
} |
| 251 |
w->writeText("\n"); |
| 252 |
} |
| 253 |
|
| 254 |
w->writeText("\n# Samples\n\n"); |
| 255 |
|
| 256 |
for (int i=0; i<song.sampleLen; i++) { |
| 257 |
DivSample* sample=song.sample[i]; |
| 258 |
|
| 259 |
w->writeText(fmt::sprintf("## %.2X: %s\n\n",i,sample->name)); |
| 260 |
|
| 261 |
w->writeText(fmt::sprintf("- format: %d\n",(int)sample->depth)); |
| 262 |
w->writeText(fmt::sprintf("- data length: %d\n",sample->getCurBufLen())); |
| 263 |
w->writeText(fmt::sprintf("- samples: %d\n",sample->samples)); |
| 264 |
w->writeText(fmt::sprintf("- rate: %d\n",sample->centerRate)); |
| 265 |
w->writeText(fmt::sprintf("- compat rate: %d\n",sample->rate)); |
| 266 |
w->writeText(fmt::sprintf("- loop: %s\n",trueFalse[sample->loop?1:0])); |
| 267 |
if (sample->loop) { |
| 268 |
w->writeText(fmt::sprintf(" - start: %d\n",sample->loopStart)); |
| 269 |
w->writeText(fmt::sprintf(" - end: %d\n",sample->loopEnd)); |
| 270 |
w->writeText(fmt::sprintf(" - mode: %s\n",sampleLoopModes[sample->loopMode&3])); |
| 271 |
} |
| 272 |
w->writeText(fmt::sprintf("- BRR emphasis: %s\n",trueFalse[sample->brrEmphasis?1:0])); |
| 273 |
w->writeText(fmt::sprintf("- no BRR filters: %s\n",trueFalse[sample->brrNoFilter?1:0])); |
| 274 |
w->writeText(fmt::sprintf("- dither: %s\n",trueFalse[sample->dither?1:0])); |
| 275 |
|
| 276 |
// TODO' render matrix |
| 277 |
unsigned char* buf=(unsigned char*)sample->getCurBuf(); |
| 278 |
unsigned int bufLen=sample->getCurBufLen(); |
| 279 |
w->writeText("\n```"); |
| 280 |
for (unsigned int i=0; i<bufLen; i++) { |
| 281 |
if ((i&15)==0) w->writeText(fmt::sprintf("\n%.8X:",i)); |
| 282 |
w->writeText(fmt::sprintf(" %.2X",buf[i])); |
| 283 |
} |
| 284 |
w->writeText("\n```\n\n"); |
| 285 |
} |
| 286 |
|
| 287 |
w->writeText("\n# Subsongs\n\n"); |
| 288 |
|
| 289 |
for (size_t i=0; i<song.subsong.size(); i++) { |
| 290 |
DivSubSong* s=song.subsong[i]; |
| 291 |
w->writeText(fmt::sprintf("## %d: %s\n\n",(int)i,s->name)); |
| 292 |
|
| 293 |
w->writeText(fmt::sprintf("- tick rate: %g\n",s->hz)); |
| 294 |
w->writeText(fmt::sprintf("- speeds:")); |
| 295 |
for (int j=0; j<s->speeds.len; j++) { |
| 296 |
w->writeText(fmt::sprintf(" %d",s->speeds.val[j])); |
| 297 |
} |
| 298 |
w->writeText("\n"); |
| 299 |
w->writeText(fmt::sprintf("- virtual tempo: %d/%d\n",s->virtualTempoN,s->virtualTempoD)); |
| 300 |
w->writeText(fmt::sprintf("- time base: %d\n",s->timeBase)); |
| 301 |
w->writeText(fmt::sprintf("- pattern length: %d\n",s->patLen)); |
| 302 |
w->writeText(fmt::sprintf("\norders:\n```\n")); |
| 303 |
|
| 304 |
for (int j=0; j<s->ordersLen; j++) { |
| 305 |
w->writeText(fmt::sprintf("%.2X |",j)); |
| 306 |
for (int k=0; k<chans; k++) { |
| 307 |
w->writeText(fmt::sprintf(" %.2X",s->orders.ord[k][j])); |
| 308 |
} |
| 309 |
w->writeText("\n"); |
| 310 |
} |
| 311 |
w->writeText("```\n\n## Patterns\n\n"); |
| 312 |
|
| 313 |
if (separatePatterns) { |
| 314 |
w->writeText("TODO: separate patterns\n\n"); |
| 315 |
} else { |
| 316 |
for (int j=0; j<s->ordersLen; j++) { |
| 317 |
w->writeText(fmt::sprintf("----- ORDER %.2X\n",j)); |
| 318 |
|
| 319 |
for (int k=0; k<s->patLen; k++) { |
| 320 |
w->writeText(fmt::sprintf("%.2X ",k)); |
| 321 |
|
| 322 |
for (int l=0; l<chans; l++) { |
| 323 |
DivPattern* p=s->pat[l].getPattern(s->orders.ord[l][j],false); |
| 324 |
|
| 325 |
int note=p->data[k][0]; |
| 326 |
int octave=p->data[k][1]; |
| 327 |
|
| 328 |
if (note==0 && octave==0) { |
| 329 |
w->writeText("|... "); |
| 330 |
} else if (note==100) { |
| 331 |
w->writeText("|OFF "); |
| 332 |
} else if (note==101) { |
| 333 |
w->writeText("|=== "); |
| 334 |
} else if (note==102) { |
| 335 |
w->writeText("|REL "); |
| 336 |
} else if ((octave>9 && octave<250) || note>12) { |
| 337 |
w->writeText("|??? "); |
| 338 |
} else { |
| 339 |
if (octave>=128) octave-=256; |
| 340 |
if (note>11) { |
| 341 |
note-=12; |
| 342 |
octave++; |
| 343 |
} |
| 344 |
w->writeText(fmt::sprintf("|%s%d ",(octave<0)?notesNegative[note]:notes[note],(octave<0)?(-octave):octave)); |
| 345 |
} |
| 346 |
|
| 347 |
if (p->data[k][2]==-1) { |
| 348 |
w->writeText(".. "); |
| 349 |
} else { |
| 350 |
w->writeText(fmt::sprintf("%.2X ",p->data[k][2]&0xff)); |
| 351 |
} |
| 352 |
|
| 353 |
if (p->data[k][3]==-1) { |
| 354 |
w->writeText(".."); |
| 355 |
} else { |
| 356 |
w->writeText(fmt::sprintf("%.2X",p->data[k][3]&0xff)); |
| 357 |
} |
| 358 |
|
| 359 |
for (int m=0; m<s->pat[l].effectCols; m++) { |
| 360 |
if (p->data[k][4+(m<<1)]==-1) { |
| 361 |
w->writeText(" .."); |
| 362 |
} else { |
| 363 |
w->writeText(fmt::sprintf(" %.2X",p->data[k][4+(m<<1)]&0xff)); |
| 364 |
} |
| 365 |
if (p->data[k][5+(m<<1)]==-1) { |
| 366 |
w->writeText(".."); |
| 367 |
} else { |
| 368 |
w->writeText(fmt::sprintf("%.2X",p->data[k][5+(m<<1)]&0xff)); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
w->writeText("\n"); |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
saveLock.unlock(); |
| 381 |
return w; |
| 382 |
} |