| 1 |
/* |
| 2 |
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL. |
| 3 |
*/ |
| 4 |
|
| 5 |
/* |
| 6 |
TODO: make all purge funcs public so user can optimize selected tables. |
| 7 |
requires writing proper initialization for each func so they can be run |
| 8 |
independently. |
| 9 |
*/ |
| 10 |
|
| 11 |
module ct.purge; |
| 12 |
import ct.base; |
| 13 |
import com.util; |
| 14 |
import std.string; |
| 15 |
import std.stdio; |
| 16 |
|
| 17 |
final class Purge { |
| 18 |
Song song; |
| 19 |
private { |
| 20 |
// song.seqIterator does not care if seq is in use or not |
| 21 |
// therefore purgeSeqs must be done always 1st on purgeAll - unused seqs get cleared |
| 22 |
bool[0x80] seqUsed; |
| 23 |
bool[0x30] instrUsed; |
| 24 |
bool[0x40] super_used, pulse_used, filter_used; |
| 25 |
bool verbose, initialized; |
| 26 |
} |
| 27 |
|
| 28 |
this(Song song) { |
| 29 |
this.song = song; |
| 30 |
} |
| 31 |
|
| 32 |
this(Song song, bool v) { |
| 33 |
verbose = v; |
| 34 |
this(song); |
| 35 |
} |
| 36 |
|
| 37 |
void purgeAll() { |
| 38 |
initialize(true); |
| 39 |
purgeSeqs(); |
| 40 |
purgeInstruments(); |
| 41 |
purgeWavetable(); |
| 42 |
purgeChordtable(); |
| 43 |
purgeCmdtable(); |
| 44 |
purgePulseFilter(); |
| 45 |
initialized = false; |
| 46 |
} |
| 47 |
|
| 48 |
void deleteInstrument(int insno) { |
| 49 |
initialize(false); |
| 50 |
int waveptr = song.wavetablePointer(insno); |
| 51 |
int pulseptr = song.pulsetablePointer(insno); |
| 52 |
int filtptr = song.filtertablePointer(insno); |
| 53 |
|
| 54 |
// ugly hack to not clear defined but unused instruments |
| 55 |
for(int i = 47; i >= 0; i--) { |
| 56 |
if(instrUsed[i] == false) |
| 57 |
instrUsed[i] = !instrIsEmpty(i); |
| 58 |
} |
| 59 |
instrUsed[insno] = false; |
| 60 |
|
| 61 |
purgeWavetable(); |
| 62 |
purgePulseFilter(); |
| 63 |
|
| 64 |
for(int i = 0; i < 8; i++) { |
| 65 |
song.instrumentTable[i * 48 + insno] = 0; |
| 66 |
} |
| 67 |
song.insLabels[insno][] = ' '; |
| 68 |
initialized = false; |
| 69 |
} |
| 70 |
|
| 71 |
private: |
| 72 |
bool instrIsEmpty(int insno) { |
| 73 |
for(int i = 0; i < 8; i++) { |
| 74 |
if(song.instrumentTable[i * 48 + insno] != 0) |
| 75 |
return false; |
| 76 |
} |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
// marking unused seqs optional in case you only want to optimize some tables |
| 81 |
void initialize(bool markUnusedSeqs) { |
| 82 |
if(initialized) return; |
| 83 |
if(markUnusedSeqs) { |
| 84 |
seqUsed[] = false; |
| 85 |
song.trackIterator((Track t) { |
| 86 |
seqUsed[t.number] = true; |
| 87 |
}); |
| 88 |
} |
| 89 |
else seqUsed[] = true; |
| 90 |
|
| 91 |
instrUsed[] = false; |
| 92 |
// find out which instruments are in use |
| 93 |
foreach(i, n; seqUsed) { |
| 94 |
if(!n) continue; |
| 95 |
Sequence s = song.seqs[i]; |
| 96 |
for(int j = 0; j < s.rows; j++) { |
| 97 |
Element e = s.data[j]; |
| 98 |
if(!e.instr.hasValue()) continue; |
| 99 |
instrUsed[e.instr.value] = true; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
initialized = true; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
private void purgeSeqs() { |
| 108 |
int counter; |
| 109 |
|
| 110 |
for(int i = 0x7f; i >= 1; i--) { |
| 111 |
if(!seqUsed[i]) continue; |
| 112 |
for(int j = 1; j < i; j++) { |
| 113 |
Sequence s1 = song.seqs[i]; |
| 114 |
Sequence s2 = song.seqs[j]; |
| 115 |
|
| 116 |
if (s1 == s2) { |
| 117 |
seqUsed[j] = false; |
| 118 |
replaceTrackvalue(j, i); |
| 119 |
counter++; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
explain(format("%d identical seqs found.",counter)); |
| 124 |
|
| 125 |
counter = 0; |
| 126 |
for(int i = 0x7f; i >= 1; i--) { |
| 127 |
if(!seqUsed[i]) { |
| 128 |
song.seqs[i].clear(); |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
for(int j = 1; j < i; j++) { |
| 133 |
if(!seqUsed[j]) { |
| 134 |
song.seqs[j].copyFrom(song.seqs[i]); |
| 135 |
song.seqs[i].clear(); |
| 136 |
seqUsed[j] = true; |
| 137 |
seqUsed[i] = false; |
| 138 |
replaceTrackvalue(i, j); |
| 139 |
counter++; |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
explain(format("%d unused sequences removed.",counter)); |
| 145 |
} |
| 146 |
|
| 147 |
private void purgeInstruments() { |
| 148 |
int counter; |
| 149 |
|
| 150 |
// clear unused |
| 151 |
for(int i = 0; i < 48; i++) { |
| 152 |
if(instrUsed[i]) { |
| 153 |
counter++; continue; |
| 154 |
} |
| 155 |
//instrtab[i*8 .. i*8 + 8] = 0; |
| 156 |
for(int j = 0; j < 8; j++) { |
| 157 |
song.instrumentTable[i + j * 48] = 0; |
| 158 |
} |
| 159 |
song.insLabels[i][] = ' '; |
| 160 |
} |
| 161 |
|
| 162 |
// compact |
| 163 |
for(int i = 0; i < 48; i++) { |
| 164 |
if(instrUsed[i]) continue; |
| 165 |
for(int j = i + 1; j < 48; j++) { |
| 166 |
if(instrUsed[j]) { |
| 167 |
for(int cidx = 0; cidx < 8; cidx++) { |
| 168 |
int o = cidx * 48; |
| 169 |
song.instrumentTable[i + o] = |
| 170 |
song.instrumentTable[j + o]; |
| 171 |
} |
| 172 |
song.insLabels[i][] = song.insLabels[j][]; |
| 173 |
replaceInsvalue(j, i); |
| 174 |
|
| 175 |
instrUsed[i] = true; |
| 176 |
instrUsed[j] = false; |
| 177 |
|
| 178 |
for(int k = 0; k < 8; k++) { |
| 179 |
song.instrumentTable[j + k * 48] = 0; |
| 180 |
} |
| 181 |
song.insLabels[j][] = ' '; |
| 182 |
break; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
explain(format("%d instruments used.", counter)); |
| 187 |
} |
| 188 |
|
| 189 |
private void purgeWavetable() { |
| 190 |
Song.Chunk[] chunks = song.tWave.getChunks(); |
| 191 |
|
| 192 |
for(int i = 0; i < 48; i++) { |
| 193 |
if(!instrUsed[i]) continue; |
| 194 |
int ptr = song.wavetablePointer(i); |
| 195 |
int cell = song.tWave.whichCell(chunks, ptr); |
| 196 |
if(cell < 0) continue; |
| 197 |
song.tWave.markCells(chunks, cell); |
| 198 |
} |
| 199 |
|
| 200 |
int numcleared; |
| 201 |
// compact |
| 202 |
for(int i = cast(int)(chunks.length - 1); i >= 0; i--) { |
| 203 |
Song.Chunk chunk = chunks[i]; |
| 204 |
if(chunk.used) continue; |
| 205 |
song.tWave.deleteRow(song, chunk.offset, cast(int)chunk.wave1.length); |
| 206 |
numcleared++; |
| 207 |
} |
| 208 |
explain(format("%d wave programs removed.", numcleared)); |
| 209 |
} |
| 210 |
|
| 211 |
private void purgePulseFilter() { |
| 212 |
int i; |
| 213 |
pulse_used[] = false; |
| 214 |
filter_used[] = false; |
| 215 |
filter_used[0] = true; |
| 216 |
pulse_used[0] = true; |
| 217 |
void seekNMark(ref ubyte[] table, bool* usedflags, int pointer) { |
| 218 |
int pp = pointer; |
| 219 |
for(;;) { |
| 220 |
if(usedflags[pp]) break; |
| 221 |
usedflags[pp] = true; |
| 222 |
int newptr = table[pp*4 + 3]; |
| 223 |
if(newptr == 0x7f) break; |
| 224 |
if(newptr == 0) pp++; |
| 225 |
else pp = newptr; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
for(i = 0; i < 48; i++) { |
| 230 |
if(!instrUsed[i]) continue; |
| 231 |
try { |
| 232 |
seekNMark(song.pulseTable, &pulse_used[0], song.pulsetablePointer(i)); |
| 233 |
seekNMark(song.filterTable, &filter_used[0], song.filtertablePointer(i)); |
| 234 |
} |
| 235 |
catch(Exception e) { |
| 236 |
explain(format("Could not purge pulse / filter table: %s", e.toString())); |
| 237 |
return; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
song.seqIterator((Sequence s, Element e) { |
| 242 |
if(e.cmd.value >= 0x40 && e.cmd.value < 0x60) |
| 243 |
seekNMark(song.pulseTable, &pulse_used[0], e.cmd.value - 0x40); |
| 244 |
if(e.cmd.value >= 0x60 && e.cmd.value < 0x80) |
| 245 |
seekNMark(song.filterTable, &filter_used[0], e.cmd.value - 0x60); |
| 246 |
|
| 247 |
}); |
| 248 |
|
| 249 |
for(i = 0; i < 64; i++) { |
| 250 |
if(!pulse_used[i]) |
| 251 |
song.pulseTable[i * 4 .. i * 4 + 4] = 0; |
| 252 |
if(!filter_used[i]) |
| 253 |
song.filterTable[i * 4 .. i * 4 + 4] = 0; |
| 254 |
} |
| 255 |
|
| 256 |
// compact filter & pulse table. this is EXTREMELY slow. |
| 257 |
for(i = 0; i < 0x3e; i++) { |
| 258 |
int seek = i + 1; |
| 259 |
while(!filter_used[i] && seek < 64) { |
| 260 |
{ |
| 261 |
filterDeleteRow(song, i); |
| 262 |
filter_used[i .. $-1] = filter_used[i+1 .. $].dup; |
| 263 |
} |
| 264 |
seek++; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
for(i = 0; i < 0x3e; i++) { |
| 269 |
int seek = i + 1; |
| 270 |
while(!pulse_used[i] && seek < 64) { |
| 271 |
{ |
| 272 |
pulseDeleteRow(song, i); |
| 273 |
pulse_used[i .. $-1] = pulse_used[i+1 .. $].dup; |
| 274 |
} |
| 275 |
seek++; |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
private void purgeChordtable() { |
| 281 |
bool[0x20] chordsUsed; |
| 282 |
song.seqIterator((Sequence s, Element e) { |
| 283 |
if(e.cmd.value >= 0x80 && e.cmd.value <= 0x9f) { |
| 284 |
chordsUsed[e.cmd.value & 0x1f] = true; |
| 285 |
} |
| 286 |
}); |
| 287 |
|
| 288 |
struct Chunk { |
| 289 |
ubyte[] data; |
| 290 |
int oldOffset; |
| 291 |
} |
| 292 |
|
| 293 |
int getidx2(int cmdidx, int idx) { |
| 294 |
for(int i = idx; i < 128; i++) { |
| 295 |
if(song.chordTable[i] >= 0x80) return i; |
| 296 |
} |
| 297 |
throw new PurgeException(format("Could not find valid chord for value %x", cmdidx)); |
| 298 |
} |
| 299 |
|
| 300 |
song.generateChordIndex(); |
| 301 |
Chunk[] chunks; |
| 302 |
chunks ~= Chunk(song.chordTable[0 .. song.chordIndexTable[1]].dup, 0); |
| 303 |
int tablestart = 1; |
| 304 |
// TODO: find out if swingtepo is used and purge first chunk too if needed |
| 305 |
for(int i = tablestart; i < 0x20; i++) { |
| 306 |
if(chordsUsed[i]) { |
| 307 |
int idx = song.chordIndexTable[i]; |
| 308 |
int idx2 = getidx2(i,idx)+1; // FIX: check... might cause problems |
| 309 |
//int idx2 = song.chordIndexTable[i+1]; |
| 310 |
assert(idx != 0); |
| 311 |
if(idx2 == 0) break; |
| 312 |
ubyte[] chord = song.chordTable[idx .. idx2].dup; |
| 313 |
chunks ~= Chunk(chord, idx); |
| 314 |
} |
| 315 |
else { |
| 316 |
for(int j = i + 1; j < 0x20; j++) { |
| 317 |
if(chordsUsed[j]) { |
| 318 |
int idx = song.chordIndexTable[j]; |
| 319 |
int idx2 = getidx2(j, idx)+1; |
| 320 |
|
| 321 |
replaceCmdColumnvalue(song, 0x80 + j, 0x80 + i); |
| 322 |
ubyte[] chord = song.chordTable[idx .. idx2].dup; |
| 323 |
|
| 324 |
chunks ~= Chunk(chord, idx); |
| 325 |
|
| 326 |
chordsUsed[i] = true; |
| 327 |
chordsUsed[j] = false; |
| 328 |
break; |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
int counter; |
| 335 |
int idx; |
| 336 |
int np; |
| 337 |
|
| 338 |
foreach(chunk; chunks) { |
| 339 |
if(chunk.data.length == 0) continue; |
| 340 |
int oldwrap = (chunk.data[$-1] - 0x80) - chunk.oldOffset; |
| 341 |
assert(chunk.data[$-1] >= 0x80); |
| 342 |
chunk.data[$-1] = cast(ubyte)(idx + oldwrap + 0x80); |
| 343 |
idx += chunk.data.length; |
| 344 |
counter++; |
| 345 |
} |
| 346 |
|
| 347 |
song.chordTable[] = 0; |
| 348 |
foreach(chunk; chunks) { |
| 349 |
song.chordTable[np .. np + chunk.data.length] = chunk.data; |
| 350 |
np += chunk.data.length; |
| 351 |
} |
| 352 |
song.generateChordIndex(); |
| 353 |
explain(format("%d chords used.",counter)); |
| 354 |
} |
| 355 |
|
| 356 |
private void purgeCmdtable() { |
| 357 |
song.seqIterator((Sequence s, Element e) { |
| 358 |
if(e.cmd.value == 0) return; |
| 359 |
if(e.cmd.value < 0x40) |
| 360 |
super_used[e.cmd.value] = true; |
| 361 |
}); |
| 362 |
|
| 363 |
int counter; |
| 364 |
|
| 365 |
for(int i = 1; i < 64; i++) { |
| 366 |
if(super_used[i]) { |
| 367 |
counter++; continue; |
| 368 |
} |
| 369 |
song.superTable[i] = 0; |
| 370 |
song.superTable[i+64] = 0; |
| 371 |
song.superTable[i+128] = 0; |
| 372 |
|
| 373 |
for(int j = i + 1; j < 64; j++) { |
| 374 |
if(super_used[j]) { |
| 375 |
song.superTable[i] = song.superTable[j]; |
| 376 |
song.superTable[i+64] = song.superTable[j+64]; |
| 377 |
song.superTable[i+128] = song.superTable[j+128]; |
| 378 |
replaceCmdColumnvalue(song, j, i); |
| 379 |
super_used[i] = true; |
| 380 |
super_used[j] = false; |
| 381 |
song.superTable[j] = 0; |
| 382 |
song.superTable[j+64] = 0; |
| 383 |
song.superTable[j+128] = 0; |
| 384 |
break; |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
void replaceInsvalue(int seek, int repl) { |
| 391 |
int skipped; |
| 392 |
song.seqIterator((Sequence s, Element e) { |
| 393 |
if(!e.instr.hasValue()) return; |
| 394 |
if(e.instr.value() == seek) |
| 395 |
e.instr = cast(ubyte)repl; |
| 396 |
}); |
| 397 |
} |
| 398 |
|
| 399 |
void replaceTrackvalue(int find, int rep) { |
| 400 |
song.trackIterator((Track t) { |
| 401 |
if(t.number == find) |
| 402 |
t.number = cast(ubyte)rep; |
| 403 |
}); |
| 404 |
} |
| 405 |
|
| 406 |
void explain(string s) { |
| 407 |
if(verbose) writefln(s); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
// TODO: move to tFilter |
| 412 |
void filterDeleteRow(Song song, int row) { |
| 413 |
genericDeleteRow(song, song.filterTable, row); |
| 414 |
|
| 415 |
song.seqIterator((Sequence s, Element e) { |
| 416 |
if(row > 0x1f) return; |
| 417 |
if(e.cmd.value == 0) return; |
| 418 |
if(e.cmd.value() >= (0x60 + (row & 0x1f) + 1) && e.cmd.value() < 0x80) |
| 419 |
e.cmd = cast(ubyte)(e.cmd.value - 1); |
| 420 |
}); |
| 421 |
|
| 422 |
for(int j = 0; j < 48; j++) { |
| 423 |
int fptr = song.instrumentTable[4 * 48 + j]; |
| 424 |
if(fptr >= row && fptr < 0x40 && fptr > 0) |
| 425 |
song.instrumentTable[4 * 48 + j]--; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
// TODO: move to tPulse |
| 430 |
void pulseDeleteRow(Song song, int row) { |
| 431 |
genericDeleteRow(song, song.pulseTable, row); |
| 432 |
|
| 433 |
song.seqIterator((Sequence s, Element e) { |
| 434 |
if(row > 0x1f) return; |
| 435 |
if(e.cmd.value == 0) return; |
| 436 |
if(e.cmd.value() >= (0x40 + (row & 0x1f) + 1) && e.cmd.value() < 0x60) |
| 437 |
e.cmd = cast(ubyte)(e.cmd.value - 1); |
| 438 |
}); |
| 439 |
|
| 440 |
for(int j = 0; j < 48; j++) { |
| 441 |
int pptr = song.instrumentTable[5 * 48 + j]; |
| 442 |
if(pptr >= row && pptr < 0x40 && pptr > 0) |
| 443 |
song.instrumentTable[5 * 48 + j]--; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
// TODO: move to tFilter |
| 448 |
void filterInsertRow(Song song, int row) { |
| 449 |
genericInsertRow(song, song.filterTable, row); |
| 450 |
|
| 451 |
song.seqIterator((Sequence s, Element e) { |
| 452 |
if(row > 0x1f) return; |
| 453 |
if(e.cmd.value == 0) return; |
| 454 |
if(e.cmd.value() >= (0x60 + (row & 0x1f)) && e.cmd.value() < 0x80) |
| 455 |
e.cmd = cast(ubyte)(e.cmd.value + 1); |
| 456 |
}); |
| 457 |
|
| 458 |
for(int j = 0; j < 48; j++) { |
| 459 |
int fptr = song.instrumentTable[4 * 48 + j]; |
| 460 |
if(fptr >= row && fptr < 0x40) |
| 461 |
song.instrumentTable[4 * 48 + j]++; |
| 462 |
} |
| 463 |
|
| 464 |
} |
| 465 |
|
| 466 |
// TODO: move to tPulse |
| 467 |
void pulseInsertRow(Song song, int row) { |
| 468 |
genericInsertRow(song, song.pulseTable, row); |
| 469 |
|
| 470 |
song.seqIterator((Sequence s, Element e) { |
| 471 |
if(row > 0x1f) return; |
| 472 |
if(e.cmd.value == 0) return; |
| 473 |
if(e.cmd.value() >= (0x40 + (row & 0x1f)) && e.cmd.value() < 0x60) |
| 474 |
e.cmd = cast(ubyte)(e.cmd.value + 1); |
| 475 |
}); |
| 476 |
|
| 477 |
for(int j = 0; j < 48; j++) { |
| 478 |
int pptr = song.instrumentTable[5 * 48 + j]; |
| 479 |
if(pptr >= row && pptr < 0x40 && pptr > 0) |
| 480 |
song.instrumentTable[5 * 48 + j]++; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
private void replaceCmdColumnvalue(Song song, int seek, int repl) { |
| 485 |
int skipped; |
| 486 |
|
| 487 |
song.seqIterator((Sequence s, Element e) { |
| 488 |
if(e.cmd.value == 0) return; |
| 489 |
if(e.cmd.value() == seek) |
| 490 |
e.cmd = cast(ubyte)repl; |
| 491 |
}); |
| 492 |
} |
| 493 |
|
| 494 |
// TODO: move to Table |
| 495 |
private void genericDeleteRow(Song song, ubyte[] table, int row) { |
| 496 |
assert(row < 64 && row >= 0); |
| 497 |
|
| 498 |
int row4 = row * 4; |
| 499 |
table[row4 .. $ - 4] = |
| 500 |
table[row4 + 4 .. $].dup; |
| 501 |
|
| 502 |
for(int j = 0; j < 64; j++) { |
| 503 |
int fptr = table[j * 4 + 3]; |
| 504 |
if(fptr > 0 && fptr < 0x40) { |
| 505 |
if(fptr >= row) table[j * 4 + 3]--; |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
// TODO: move to Table |
| 511 |
private void genericInsertRow(Song song, ubyte[] table, int row) { |
| 512 |
assert(row < 64 && row >= 0); |
| 513 |
|
| 514 |
int row4 = row * 4; |
| 515 |
table[row4 + 4 .. $] = |
| 516 |
table[row4 .. $ - 4].dup; |
| 517 |
|
| 518 |
table[row4 .. row4+4] = 0; |
| 519 |
|
| 520 |
for(int j = 0; j < 64; j++) { |
| 521 |
int fptr = table[j * 4 + 3]; |
| 522 |
if(fptr > 0 && fptr < 0x40) { |
| 523 |
if(fptr >= row) table[j * 4 + 3]++; |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
class PurgeException : Exception { |
| 529 |
this(string msg) { |
| 530 |
super(msg); |
| 531 |
} |
| 532 |
|
| 533 |
override string toString() { |
| 534 |
return "Purge error: " ~ msg; |
| 535 |
} |
| 536 |
} |