| 1 |
/* |
| 2 |
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL. |
| 3 |
*/ |
| 4 |
module ct.base; |
| 5 |
import com.cpu; |
| 6 |
import com.util; |
| 7 |
import ct.purge; |
| 8 |
import std.string; |
| 9 |
import std.file; |
| 10 |
import std.zlib; |
| 11 |
|
| 12 |
enum Offsets |
| 13 |
{ |
| 14 |
Features, Volume, Editorflag, |
| 15 |
Songsets, PlaySpeed, Subnoteplay, Submplayplay, InstrumentDescriptionsHeader, |
| 16 |
PulseDescriptionsHeader, FilterDescriptionsHeader, WaveDescriptionsHeader, |
| 17 |
CmdDescriptionsHeader, FREQTABLE, FINETUNE, Arp1, Arp2, |
| 18 |
FILTTAB, PULSTAB, Inst, Track1, Track2, Track3, SeqLO, SeqHI, |
| 19 |
CMD1, S00, SPEED, TRACKLO, VOICE, GATE, ChordTable, TRANS, ChordIndexTable, |
| 20 |
SHTRANS, FOO3, NEXT, CURINST, GEED, NEWSEQ |
| 21 |
} |
| 22 |
|
| 23 |
immutable string[] NOTES = |
| 24 |
[ "C-0", "C#0", "D-0", "D#0", "E-0", "F-0", |
| 25 |
"F#0", "G-0", "G#0", "A-0", "A#0", "B-0", |
| 26 |
"C-1", "C#1", "D-1", "D#1", "E-1", "F-1", |
| 27 |
"F#1", "G-1", "G#1", "A-1", "A#1", "B-1", |
| 28 |
"C-2", "C#2", "D-2", "D#2", "E-2", "F-2", |
| 29 |
"F#2", "G-2", "G#2", "A-2", "A#2", "B-2", |
| 30 |
"C-3", "C#3", "D-3", "D#3", "E-3", "F-3", |
| 31 |
"F#3", "G-3", "G#3", "A-3", "A#3", "B-3", |
| 32 |
"C-4", "C#4", "D-4", "D#4", "E-4", "F-4", |
| 33 |
"F#4", "G-4", "G#4", "A-4", "A#4", "B-4", |
| 34 |
"C-5", "C#5", "D-5", "D#5", "E-5", "F-5", |
| 35 |
"F#5", "G-5", "G#5", "A-5", "A#5", "B-5", |
| 36 |
"C-6", "C#6", "D-6", "D#6", "E-6", "F-6", |
| 37 |
"F#6", "G-6", "G#6", "A-6", "A#6", "B-6", |
| 38 |
"C-7", "C#7", "D-7", "D#7", "E-7", "F-7", |
| 39 |
"F#7", "G-7", "G#7", "A-7", "A#7", "B-7" ]; |
| 40 |
|
| 41 |
enum { |
| 42 |
MAX_SEQ_ROWS = 0x40, |
| 43 |
MAX_SEQ_NUM = 0x80, |
| 44 |
TRACK_LIST_LENGTH = 0x200, |
| 45 |
OFFSETTAB_LENGTH = 16 * 6, |
| 46 |
SEQ_END_MARK = 0xbf, |
| 47 |
SONG_REVISION = 12, |
| 48 |
NOTE_KEYOFF = 1, |
| 49 |
NOTE_KEYON = 2, |
| 50 |
SUBTUNE_MAX = 32 |
| 51 |
} |
| 52 |
|
| 53 |
immutable ubyte[] CLEAR = [0xf0, 0xf0, 0x60, 0x00]; |
| 54 |
immutable ubyte[] INITIAL_SEQ = [0xf0, 0xf0, 0x60, 0x00, 0xbf]; |
| 55 |
|
| 56 |
alias char*[] ByteDescription; |
| 57 |
|
| 58 |
struct Cmd { |
| 59 |
private ubyte[] data; |
| 60 |
static Cmd opCall() { |
| 61 |
static Cmd cmd; |
| 62 |
return cmd; |
| 63 |
} |
| 64 |
|
| 65 |
static Cmd opCall(ubyte[] d) { |
| 66 |
static Cmd cmd; |
| 67 |
cmd.data = d; |
| 68 |
return cmd; |
| 69 |
} |
| 70 |
|
| 71 |
void opAssign(ubyte cmd) { |
| 72 |
data[3] = cmd; |
| 73 |
if(cmd >= 0 && data[2] < 0x60) data[2] += 0x60; |
| 74 |
} |
| 75 |
|
| 76 |
void opAssign(Cmd cmd) { |
| 77 |
data = cmd.data; |
| 78 |
} |
| 79 |
|
| 80 |
@property ubyte value() { return data[3]; } |
| 81 |
alias value rawValue; |
| 82 |
|
| 83 |
string toString() { |
| 84 |
return toString(true); |
| 85 |
} |
| 86 |
|
| 87 |
string toString(bool colors) { |
| 88 |
ubyte v = data[3]; |
| 89 |
if(v > 0) |
| 90 |
return format("`+f%02X", v); |
| 91 |
else return "`+b--"; |
| 92 |
} |
| 93 |
|
| 94 |
string toPlainString() { |
| 95 |
ubyte v = data[3]; |
| 96 |
if(v > 0) |
| 97 |
return format("%02X", v); |
| 98 |
else return "--"; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
struct Ins { |
| 103 |
private ubyte[] data; |
| 104 |
static Ins opCall() { |
| 105 |
static Ins ins; |
| 106 |
return ins; |
| 107 |
} |
| 108 |
static Ins opCall(ubyte[] d) { |
| 109 |
static Ins ins; |
| 110 |
ins.data = d; |
| 111 |
return ins; |
| 112 |
} |
| 113 |
|
| 114 |
void opAssign(ubyte newins) { |
| 115 |
if(newins < 0x30) |
| 116 |
data[0] = cast(ubyte) (newins + 0xc0); |
| 117 |
else data[0] = cast(ubyte)0xf0; |
| 118 |
} |
| 119 |
|
| 120 |
void opAssign(Ins ins) { |
| 121 |
data = ins.data; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
@property ubyte rawValue() { return data[0]; } |
| 126 |
@property ubyte value() { return cast(ubyte)(data[0] - 0xc0); } |
| 127 |
private alias value v; |
| 128 |
|
| 129 |
@property bool hasValue() { return value() < 0x30; } |
| 130 |
|
| 131 |
string toString() { |
| 132 |
if(v >= 0 && v < 0x30) |
| 133 |
return format("`+f%02X", v); |
| 134 |
else return "`+b--"; |
| 135 |
} |
| 136 |
|
| 137 |
string toPlainString() { |
| 138 |
alias value v; |
| 139 |
if(v >= 0 && v < 0x30) |
| 140 |
return format("%02X", v); |
| 141 |
else return "--"; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
struct Note { |
| 146 |
private ubyte[] data; |
| 147 |
static Note opCall(ubyte[] d) { |
| 148 |
static Note no; |
| 149 |
no.data = d; |
| 150 |
return no; |
| 151 |
} |
| 152 |
|
| 153 |
void opAssign(ubyte newnote) { |
| 154 |
if(newnote > 0x5e) newnote = 0x5e; |
| 155 |
data[2] = cast(ubyte)(newnote + 0x60); |
| 156 |
} |
| 157 |
|
| 158 |
void opAssign(Note note) { |
| 159 |
data = note.data; |
| 160 |
} |
| 161 |
|
| 162 |
@property ubyte rawValue() { |
| 163 |
return data[2]; |
| 164 |
} |
| 165 |
|
| 166 |
@property ubyte value() { |
| 167 |
return data[2] % 0x60; |
| 168 |
} |
| 169 |
private alias value v; |
| 170 |
|
| 171 |
@property bool isTied() { return data[1] == 0x5f; } |
| 172 |
|
| 173 |
void setTied(bool t) { |
| 174 |
data[1] = t ? 0x5f : 0xf0; |
| 175 |
} |
| 176 |
|
| 177 |
string toString(int trns) { |
| 178 |
string col, colh; |
| 179 |
if(isTied()) { |
| 180 |
col = "`4f"; |
| 181 |
colh = "`4b"; |
| 182 |
} |
| 183 |
else { |
| 184 |
col = "`0f"; |
| 185 |
colh = "`0b"; |
| 186 |
} |
| 187 |
switch(v) { |
| 188 |
case 0: |
| 189 |
return format("%s---", colh ); |
| 190 |
case 1: |
| 191 |
return format("%s===", col ); |
| 192 |
case 2: |
| 193 |
return format("%s+++", col ); |
| 194 |
default: |
| 195 |
if((v + trns) > 0x5e || (v + trns) < 0) |
| 196 |
return format("%s???", col); |
| 197 |
else return format("%s%s", col, |
| 198 |
NOTES[v + trns]); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
string toPlainString(int trns) { |
| 203 |
switch(v) { |
| 204 |
case 0: |
| 205 |
return "---"; |
| 206 |
case 1: |
| 207 |
return "==="; |
| 208 |
case 2: |
| 209 |
return "+++"; |
| 210 |
default: |
| 211 |
if((v + trns) > 0x5e || (v + trns) < 0) |
| 212 |
return "???"; |
| 213 |
else return NOTES[v + trns]; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
struct Element { |
| 219 |
Ins instr; |
| 220 |
alias instr instrument; |
| 221 |
Cmd cmd; |
| 222 |
Note note; |
| 223 |
int transpose; |
| 224 |
ubyte[] data; |
| 225 |
|
| 226 |
static Element opCall(ubyte[] chunk) { |
| 227 |
static Element e; |
| 228 |
e.cmd = Cmd.opCall(chunk); |
| 229 |
e.instr = Ins(chunk); |
| 230 |
e.note = Note(chunk); |
| 231 |
e.data = chunk; |
| 232 |
return e; |
| 233 |
} |
| 234 |
|
| 235 |
string toString() { |
| 236 |
return toString(transpose); |
| 237 |
} |
| 238 |
|
| 239 |
string toString(int trans) { |
| 240 |
return format("%s`+0 %s %s", note.toString(trans), |
| 241 |
instr.toString(), cmd.toString()); |
| 242 |
} |
| 243 |
|
| 244 |
string toPlainString() { |
| 245 |
return format("%s %s %s", note.toPlainString(transpose), |
| 246 |
instr.toPlainString(), cmd.toPlainString()); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
struct Tracklist { |
| 251 |
private Track[] list; |
| 252 |
|
| 253 |
Track opIndex(int i) { |
| 254 |
if(i >= 0x400) i = 0; |
| 255 |
assert(i >= 0 && i < length); |
| 256 |
return list[i]; |
| 257 |
} |
| 258 |
|
| 259 |
static Tracklist opCall(Tracklist tl) { |
| 260 |
Tracklist t; |
| 261 |
t = tl; |
| 262 |
return t; |
| 263 |
} |
| 264 |
|
| 265 |
static Tracklist opCall(Track[] t) { |
| 266 |
Tracklist tl; |
| 267 |
tl.list = t; |
| 268 |
return tl; |
| 269 |
} |
| 270 |
|
| 271 |
Tracklist deepcopy() { |
| 272 |
auto copy = new Track[](list.length); |
| 273 |
foreach(idx, t; list) { |
| 274 |
auto tr = cast(ubyte)t.trans; |
| 275 |
auto number = cast(ubyte)t.number; |
| 276 |
|
| 277 |
copy[idx] = Track([tr, number]); |
| 278 |
} |
| 279 |
return Tracklist(copy); |
| 280 |
} |
| 281 |
|
| 282 |
void overwriteFrom(Tracklist tl) { |
| 283 |
for(int idx = 0; idx < tl.length; idx++) { |
| 284 |
auto tr = cast(ubyte)tl[idx].trans; |
| 285 |
auto number = cast(ubyte)tl[idx].number; |
| 286 |
list[idx].trans = tr; |
| 287 |
list[idx].number = number; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
void opIndexAssign(Track t, size_t il) { |
| 292 |
list[il] = t; |
| 293 |
} |
| 294 |
|
| 295 |
Track[] opSlice() { return list; } |
| 296 |
|
| 297 |
Tracklist opSlice(size_t x, size_t y) { |
| 298 |
return Tracklist(list[x .. y]); |
| 299 |
} |
| 300 |
|
| 301 |
int opApply(int delegate(ref Track) dg) { |
| 302 |
int result; |
| 303 |
for(int i = 0; i < trackLength; i++) { |
| 304 |
result = dg(list[i]); |
| 305 |
if(result) break; |
| 306 |
} |
| 307 |
return result; |
| 308 |
} |
| 309 |
|
| 310 |
int opApplyReverse(int delegate(ref Track) dg) { |
| 311 |
int result; |
| 312 |
for(int i = cast(int)(length - 1); i >= 0; i--) { |
| 313 |
result = dg(list[i]); |
| 314 |
if(result) break; |
| 315 |
} |
| 316 |
return result; |
| 317 |
} |
| 318 |
|
| 319 |
@property int length() { |
| 320 |
return cast(int) list.length; |
| 321 |
} |
| 322 |
|
| 323 |
@property void length(size_t il) { |
| 324 |
list.length = il; |
| 325 |
} |
| 326 |
|
| 327 |
@property Track lastTrack() { |
| 328 |
return list[trackLength]; |
| 329 |
} |
| 330 |
|
| 331 |
@property int trackLength() { |
| 332 |
int i; |
| 333 |
for(i = 0; i < length; i++) { |
| 334 |
Track t = list[i]; |
| 335 |
if(t.trans >= 0xf0) return i; |
| 336 |
} |
| 337 |
assert(0); |
| 338 |
} |
| 339 |
|
| 340 |
@property address wrapOffset() { |
| 341 |
return (lastTrack.smashedValue() / 2) & 0x7ff; |
| 342 |
} |
| 343 |
|
| 344 |
@property void wrapOffset(address offset) { |
| 345 |
if((offset & 0xff00) >= 0xf000) return; |
| 346 |
assert(offset >= 0 && offset < 0x400); |
| 347 |
if(offset >= trackLength) |
| 348 |
offset = cast(ushort)(trackLength - 1); |
| 349 |
offset *= 2; |
| 350 |
offset |= 0xf000; |
| 351 |
lastTrack() = [(offset & 0xff00) >> 8, offset & 0x00ff]; |
| 352 |
} |
| 353 |
|
| 354 |
void expand() { |
| 355 |
insertAt(trackLength); |
| 356 |
} |
| 357 |
|
| 358 |
void shrink() { |
| 359 |
deleteAt(trackLength-1); |
| 360 |
} |
| 361 |
|
| 362 |
// returns transpose value other than 0x80 above idx OR below(if idx == 0) |
| 363 |
ubyte getTransAt(int idx) { |
| 364 |
if(idx > 0) { |
| 365 |
do { |
| 366 |
if(list[idx].trans > 0x80 && |
| 367 |
list[idx].trans < 0xc0) |
| 368 |
return list[idx].trans; |
| 369 |
} while(idx-- > 0); |
| 370 |
} |
| 371 |
else if(idx == 0) { |
| 372 |
do { |
| 373 |
if(list[idx].trans > 0x80 && |
| 374 |
list[idx].trans < 0xc0) |
| 375 |
return list[idx].trans; |
| 376 |
} while(idx++ < trackLength); |
| 377 |
} |
| 378 |
return 0xa0; |
| 379 |
} |
| 380 |
|
| 381 |
void insertAt(int offset) { |
| 382 |
if(offset > list.length - 2) return; |
| 383 |
assert(offset >= 0 && offset < list.length); |
| 384 |
for(int i = cast(int)(list.length - 2); i >= offset; i--) { |
| 385 |
list[i+1] = list[i].dup; |
| 386 |
} |
| 387 |
list[offset].trans = getTransAt(offset); |
| 388 |
list[offset].number = 0; |
| 389 |
if(wrapOffset() >= offset) { |
| 390 |
wrapOffset = cast(address)(wrapOffset + 1); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
void deleteAt(int offset) { |
| 395 |
if(list[1].trans >= 0xf0) return; |
| 396 |
for(int i = offset; i < list.length - 2; i++) { |
| 397 |
list[i] = list[i+1].dup; |
| 398 |
} |
| 399 |
if(wrapOffset() >= offset) { |
| 400 |
wrapOffset = cast(address)(wrapOffset - 1); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
void transposeAt(int s, int e, int t) { |
| 405 |
foreach(trk; list[s .. e]) |
| 406 |
trk.transpose(t); |
| 407 |
} |
| 408 |
|
| 409 |
auto compact() { |
| 410 |
ubyte[] arr = new ubyte[1024]; |
| 411 |
int p, trans = -1, wrapptr = wrapOffset * 2; |
| 412 |
|
| 413 |
foreach(idx, track; list) { |
| 414 |
if(track.trans >= 0xf0) { |
| 415 |
wrapptr |= 0xf000; |
| 416 |
arr[p .. p + 2] = [(wrapptr & 0xff00) >> 8, wrapptr & 0x00ff]; |
| 417 |
p += 2; |
| 418 |
break; |
| 419 |
} |
| 420 |
if((track.trans != trans && track.trans != 0x80) || idx == wrapOffset) { |
| 421 |
trans = track.trans; |
| 422 |
arr[p++] = cast(ubyte)trans; |
| 423 |
} |
| 424 |
else if(idx < wrapOffset) { |
| 425 |
wrapptr--; |
| 426 |
} |
| 427 |
arr[p++] = track.number; |
| 428 |
} |
| 429 |
return arr[0..p]; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
struct Track { |
| 434 |
private ubyte[] data; |
| 435 |
|
| 436 |
static Track opCall(ubyte[] tr) { |
| 437 |
Track t; |
| 438 |
t.data = tr; |
| 439 |
assert(t.data.length == 2); |
| 440 |
return t; |
| 441 |
} |
| 442 |
|
| 443 |
void opAssign(ushort s) { |
| 444 |
data[0] = s & 255; |
| 445 |
data[1] = s >> 8; |
| 446 |
} |
| 447 |
|
| 448 |
void opAssign(ubyte[] d) { |
| 449 |
data[] = d[]; |
| 450 |
} |
| 451 |
|
| 452 |
void opAssign(Track tr) { |
| 453 |
data = tr.data; |
| 454 |
} |
| 455 |
|
| 456 |
@property void trans(ubyte t) { |
| 457 |
data[0] = t; |
| 458 |
} |
| 459 |
|
| 460 |
@property deprecated ushort dup() { |
| 461 |
return trans | (number << 8); |
| 462 |
} |
| 463 |
|
| 464 |
@property ushort smashedValue() { // "real" int value, trans = highbyte |
| 465 |
return number | (trans << 8); |
| 466 |
} |
| 467 |
|
| 468 |
@property ubyte trans() { |
| 469 |
return data[0]; |
| 470 |
} |
| 471 |
|
| 472 |
@property ubyte number() { |
| 473 |
return data[1]; |
| 474 |
} |
| 475 |
@property void number(ubyte no) { |
| 476 |
data[1] = no; |
| 477 |
} |
| 478 |
alias number trackNumber; |
| 479 |
|
| 480 |
void setValue(int tr, int no) { |
| 481 |
tr = clamp(tr, 0x80, 0xf3); |
| 482 |
if(no < 0) no = 0; |
| 483 |
if(no >= MAX_SEQ_NUM) no = MAX_SEQ_NUM-1; |
| 484 |
data[0] = tr & 255; |
| 485 |
data[1] = no & 255; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
string toString() { |
| 490 |
string s = format("%02X%02X", trans, number); |
| 491 |
return s; |
| 492 |
} |
| 493 |
|
| 494 |
void transpose(int val) { |
| 495 |
if(trans == 0x80 || trans >= 0xf0) return; |
| 496 |
int t = trans + val; |
| 497 |
trans = (cast(ubyte)clamp(t, 0x80, 0xbf)); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
class Sequence { |
| 502 |
ElementArray data; |
| 503 |
int rows; |
| 504 |
|
| 505 |
static struct ElementArray { |
| 506 |
ubyte[] raw; |
| 507 |
|
| 508 |
Element opIndex(int i) { |
| 509 |
assert(i < MAX_SEQ_ROWS * 4); |
| 510 |
assert(i < (raw.length * 4)); |
| 511 |
ubyte[] chunk = raw[i * 4 .. i * 4 + 4]; |
| 512 |
return Element(chunk); |
| 513 |
} |
| 514 |
|
| 515 |
void opIndexAssign(int value, size_t il) { |
| 516 |
raw[il] = cast(ubyte)value; |
| 517 |
} |
| 518 |
|
| 519 |
int opApply(int delegate(Element) dg) { |
| 520 |
int result; |
| 521 |
for(int i = 0; i < length()/4; i++) { |
| 522 |
result = dg(opIndex(i)); |
| 523 |
if(result) break; |
| 524 |
} |
| 525 |
return result; |
| 526 |
} |
| 527 |
|
| 528 |
@property int length() { return cast(int)raw.length; } |
| 529 |
} |
| 530 |
|
| 531 |
this(ubyte[] d) { |
| 532 |
data = ElementArray(d); |
| 533 |
refresh(); |
| 534 |
if(rows*4+4 < 254) |
| 535 |
data.raw[rows*4 + 4 .. 254] = 0; |
| 536 |
} |
| 537 |
|
| 538 |
this(ubyte[] rd, int r) { |
| 539 |
data = ElementArray(rd); |
| 540 |
rows = r; |
| 541 |
} |
| 542 |
|
| 543 |
void refresh() { |
| 544 |
int p, r; |
| 545 |
// find seq length |
| 546 |
while(p < data.length) { |
| 547 |
ubyte b; |
| 548 |
b = data.raw[p+0]; |
| 549 |
if(b == SEQ_END_MARK) |
| 550 |
break; |
| 551 |
p += 4; r++; |
| 552 |
} |
| 553 |
rows = r; |
| 554 |
} |
| 555 |
|
| 556 |
override bool opEquals(Object o) const { |
| 557 |
auto rhs = cast(const Sequence)o; |
| 558 |
return (rhs && (data.raw[] == rhs.data.raw[])); |
| 559 |
} |
| 560 |
|
| 561 |
void clear() { |
| 562 |
data.raw[] = 0; |
| 563 |
data.raw[0..5] = [0xf0,0xf0,0x60,0x00,0xbf]; |
| 564 |
refresh(); |
| 565 |
} |
| 566 |
|
| 567 |
void expand(int pos, int r) { |
| 568 |
expand(pos, r, true); |
| 569 |
} |
| 570 |
|
| 571 |
void expand(int pos, int r, bool doInsert) { |
| 572 |
int i, len; |
| 573 |
int j; |
| 574 |
|
| 575 |
if(rows >= MAX_SEQ_ROWS) return; |
| 576 |
for(j=0;j<r;j++) { |
| 577 |
if(rows >= MAX_SEQ_ROWS) break; |
| 578 |
rows++; |
| 579 |
if(doInsert) |
| 580 |
insert(pos); |
| 581 |
else data.raw[(rows-1) * 4..(rows-1) * 4 + 4] = cast(ubyte[])CLEAR; |
| 582 |
} |
| 583 |
if(rows < 64) |
| 584 |
data.raw[rows*4] = SEQ_END_MARK; |
| 585 |
} |
| 586 |
|
| 587 |
void shrink(int pos, int r, bool doRemove) { |
| 588 |
if(rows <= 1 || pos >= rows - 1) return; |
| 589 |
for(int j = 0; j < r; j++) { |
| 590 |
if(doRemove) |
| 591 |
remove(pos); |
| 592 |
// clear endmark |
| 593 |
data.raw[rows * 4 .. $] = 0; |
| 594 |
rows--; |
| 595 |
data.raw[rows * 4 .. rows * 4 + 4] = cast(ubyte[])[ SEQ_END_MARK, 0, 0, 0 ]; |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
void transpose(int r, int n) { |
| 600 |
for(int i = r; i < rows;i++) { |
| 601 |
Note note = data[i].note; |
| 602 |
int v = note.value; |
| 603 |
if(v < 3) continue; |
| 604 |
if(n >= 0 && (v+n) < 0x60) |
| 605 |
v += n; |
| 606 |
if(n < 0 && (v+n) >= 3) v += n; |
| 607 |
note = cast(ubyte) v; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
void insert(int pos) { |
| 612 |
int p1 = pos * 4; |
| 613 |
int p2 = rows * 4; |
| 614 |
if(p2 > 256) return; |
| 615 |
ubyte[] c = data.raw[p1 .. p2]; |
| 616 |
ubyte[] n = c.dup; |
| 617 |
c[4..$] = n[0..$-4].dup; |
| 618 |
// clear cursor pos |
| 619 |
c[0..4] = cast(ubyte[])CLEAR; |
| 620 |
} |
| 621 |
|
| 622 |
void remove(int pos) { |
| 623 |
ubyte[] tmp; |
| 624 |
int start = pos * 4; |
| 625 |
int end = rows * 4; |
| 626 |
|
| 627 |
tmp = data.raw[start + 4 .. end].dup; |
| 628 |
data.raw[start .. end - 4] = tmp; |
| 629 |
data.raw[end - 4 .. end] = cast(ubyte[])CLEAR; |
| 630 |
} |
| 631 |
|
| 632 |
void copyFrom(Sequence f) { |
| 633 |
rows = f.rows; |
| 634 |
data.raw[] = f.data.raw[].dup; |
| 635 |
} |
| 636 |
|
| 637 |
// insert seq f to offset ofs |
| 638 |
void insertFrom(Sequence f, int ofs) { |
| 639 |
// make temporary copy so that seq can be appended over itself |
| 640 |
Sequence copy = new Sequence(f.data.raw.dup); |
| 641 |
expand(ofs, f.rows); |
| 642 |
|
| 643 |
int max = MAX_SEQ_ROWS*4; |
| 644 |
int st = ofs * 4; |
| 645 |
int len = copy.rows * 4; |
| 646 |
int end = st + len; |
| 647 |
if(end >= max) { |
| 648 |
end = max; |
| 649 |
len = end - st; |
| 650 |
} |
| 651 |
data.raw[st .. end] = copy.data.raw[0..len]; |
| 652 |
} |
| 653 |
|
| 654 |
ubyte[] compact() { |
| 655 |
ubyte[] outarr = new ubyte[257]; |
| 656 |
int i, outp, olddel, oldins = -1, |
| 657 |
olddelay = -1, delay; |
| 658 |
for(i = 0; i < rows;) { |
| 659 |
Element e = data[i]; |
| 660 |
bool cmd = false; |
| 661 |
int note = e.note.rawValue; |
| 662 |
|
| 663 |
if(note >= 0x60 && e.cmd.rawValue > 0) |
| 664 |
cmd = true; |
| 665 |
else { |
| 666 |
if(note >= 0x60) note -= 0x60; |
| 667 |
} |
| 668 |
|
| 669 |
if(e.instr.value < 0x30 && oldins != e.instr.value) { |
| 670 |
oldins = e.instr.value; |
| 671 |
outarr[outp++] = cast(ubyte)(e.instr.value + 0xc0); |
| 672 |
} |
| 673 |
|
| 674 |
// calc delay |
| 675 |
delay = 0; |
| 676 |
for(int j = i + 1; j < rows; j++) { |
| 677 |
Element ee = data[j]; |
| 678 |
if((ee.note.rawValue % 0x60) == 0 && |
| 679 |
ee.cmd.rawValue == 0 && |
| 680 |
!ee.instr.hasValue()) { |
| 681 |
delay++; i++; |
| 682 |
} |
| 683 |
else break; |
| 684 |
} |
| 685 |
|
| 686 |
if(olddelay != delay) { |
| 687 |
olddelay = delay & 15; |
| 688 |
outarr[outp++] = cast(ubyte)(delay | 0xf0); |
| 689 |
olddelay = delay & 15; |
| 690 |
delay -= delay & 15; |
| 691 |
} |
| 692 |
|
| 693 |
if(e.note.isTied()) outarr[outp++] = 0x5f; |
| 694 |
outarr[outp++] = cast(ubyte)note; |
| 695 |
if(cmd) |
| 696 |
outarr[outp++] = cast(ubyte)(e.cmd.rawValue); |
| 697 |
|
| 698 |
while(delay > 15) { |
| 699 |
int d = delay; |
| 700 |
if(d > 15) d = 15; |
| 701 |
if(olddelay != d) { |
| 702 |
outarr[outp++] = cast(ubyte)(d | 0xf0); |
| 703 |
olddelay = d; |
| 704 |
} |
| 705 |
outarr[outp++] = cast(ubyte)0; |
| 706 |
delay -= 16; |
| 707 |
} |
| 708 |
|
| 709 |
i++; |
| 710 |
} |
| 711 |
outarr[outp++] = cast(ubyte)SEQ_END_MARK; |
| 712 |
return outarr[0..outp]; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
class Song { |
| 717 |
enum DatafileOffset { |
| 718 |
Binary, |
| 719 |
Header = 65536, |
| 720 |
Title = Header + 256 + 5, |
| 721 |
Author = Title + 32, |
| 722 |
Release = Author + 32, |
| 723 |
Insnames = Title + 40 * 4, |
| 724 |
Subtunes = Insnames + 1024 * 2 |
| 725 |
} |
| 726 |
|
| 727 |
private struct Features { |
| 728 |
ubyte requestedTables; |
| 729 |
ubyte[8] instrumentFlags; |
| 730 |
ubyte[16] cmdFlags; |
| 731 |
} |
| 732 |
|
| 733 |
private struct Patch { |
| 734 |
string name; |
| 735 |
ubyte[] def, wave1, wave2, filt, pulse; |
| 736 |
} |
| 737 |
|
| 738 |
static struct Chunk { |
| 739 |
int offset; |
| 740 |
ubyte[] wave1, wave2; |
| 741 |
bool used; |
| 742 |
int tokill; |
| 743 |
string toString() { return format("%x", offset); } |
| 744 |
// TODO implement .dup which copies arrasys as well |
| 745 |
Chunk dup() { |
| 746 |
assert(0); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
static class Table { |
| 751 |
ubyte[] data; |
| 752 |
this(ubyte[] data) { |
| 753 |
this.data = data; |
| 754 |
} |
| 755 |
int size() { |
| 756 |
foreach_reverse(idx, val; data) { |
| 757 |
if(val != 0) return cast(int)(idx + 1); |
| 758 |
} |
| 759 |
return 0; |
| 760 |
} |
| 761 |
int length() { return cast(int)data.length; } |
| 762 |
ubyte[] opSlice() { return data; } |
| 763 |
ubyte[] opSlice(size_t x, size_t y) { |
| 764 |
return data[x..y]; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
class WaveTable : Table { |
| 769 |
struct WaveProgram { |
| 770 |
ubyte[] wave1, wave2; |
| 771 |
int offset; |
| 772 |
} |
| 773 |
// info for each waveprogram |
| 774 |
|
| 775 |
ubyte[] wave1, wave2; |
| 776 |
|
| 777 |
this(ubyte[] data) { |
| 778 |
super(data); |
| 779 |
wave1 = data[0 .. 256]; |
| 780 |
wave2 = data[256 .. 512]; |
| 781 |
} |
| 782 |
|
| 783 |
int seekTableEnd() { |
| 784 |
for(int i = 255; i >= 1; i--) { |
| 785 |
if(wave1[i-1] != 0) { |
| 786 |
return i; |
| 787 |
} |
| 788 |
} |
| 789 |
return 0; |
| 790 |
} |
| 791 |
|
| 792 |
Chunk[] getChunks() { |
| 793 |
return getChunks(data); |
| 794 |
} |
| 795 |
|
| 796 |
Chunk[] getChunks(ubyte[] wavetab) { |
| 797 |
/+ static +/ Chunk[] chunks = new Chunk[256]; |
| 798 |
int counter; |
| 799 |
for(int i = 0, b; i < 256; i++) { |
| 800 |
if(wavetab[i] == 0x7f || wavetab[i] == 0x7e) { |
| 801 |
chunks[counter] = Chunk(b, wavetab[b .. i + 1], wavetab[b + 256 .. i + 256 + 1]); |
| 802 |
b = i + 1; |
| 803 |
counter++; |
| 804 |
} |
| 805 |
} |
| 806 |
return chunks[0 .. counter]; |
| 807 |
} |
| 808 |
|
| 809 |
bool isValid(int waveOffset) { |
| 810 |
auto chunks = getChunks(data); |
| 811 |
return whichCell(chunks, waveOffset) >= 0; |
| 812 |
} |
| 813 |
|
| 814 |
// get program starting at waveOffset |
| 815 |
// mostly copied from purgeWave |
| 816 |
WaveProgram getProgram(int waveOffset) { |
| 817 |
auto chunks = getChunks(data.dup); |
| 818 |
int topRow = 255; |
| 819 |
|
| 820 |
int cell = whichCell(chunks, waveOffset); |
| 821 |
if(cell < 0) |
| 822 |
throw new Error("Illegal waveprogram offset"); |
| 823 |
markCells(chunks, cell); |
| 824 |
|
| 825 |
WaveProgram wp; |
| 826 |
|
| 827 |
/+ get top row in used chunks, needed to recalc arpofs NB might be necessary to calc last +/ |
| 828 |
foreach(ref chunk; chunks) { |
| 829 |
if(chunk.used && chunk.offset < topRow) |
| 830 |
topRow = chunk.offset; |
| 831 |
} |
| 832 |
/+ generate arrays +/ |
| 833 |
foreach(ref chunk; chunks) { |
| 834 |
if(!chunk.used) { |
| 835 |
if(waveOffset >= chunk.offset) |
| 836 |
waveOffset -= cast(int)chunk.wave1.length; |
| 837 |
|
| 838 |
foreach(ref chunk2; chunks) { |
| 839 |
if(chunk2.wave1[$ - 1] == 0x7f && |
| 840 |
chunk2.wave2[$ - 1] >= chunk.offset) { |
| 841 |
// check that 7f-xx is not pointing WITHIN this chunk |
| 842 |
//assert(chunk2.wave2[$ - 1] > chunk.offset + chunk.wave1.length); |
| 843 |
// calc wrap |
| 844 |
int t = chunk2.wave2[$ - 1] - cast(int)chunk.wave1.length; |
| 845 |
t = com.util.clamp(t, 0, 256); |
| 846 |
chunk2.wave2[$ - 1] = cast(ubyte)t; |
| 847 |
} |
| 848 |
if(chunk2.offset >=chunk.offset) |
| 849 |
chunk2.offset -= chunk.wave1.length; |
| 850 |
|
| 851 |
} |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
foreach(ref chunk; chunks) { |
| 856 |
if(!chunk.used) continue; |
| 857 |
wp.wave1 ~= chunk.wave1; |
| 858 |
wp.wave2 ~= chunk.wave2; |
| 859 |
assert(wp.wave1[$-1] == 0x7f || |
| 860 |
wp.wave1[$-1] == 0x7e); |
| 861 |
} |
| 862 |
wp.offset = waveOffset; |
| 863 |
return wp; |
| 864 |
} |
| 865 |
|
| 866 |
static int whichCell(Chunk[] chunks, int ptr) { |
| 867 |
foreach(idx, chunk; chunks) { |
| 868 |
int b = chunk.offset, |
| 869 |
e = cast(int)(chunk.offset + chunk.wave1.length); |
| 870 |
if(ptr >= b && ptr < e) { |
| 871 |
return cast(int)idx; |
| 872 |
} |
| 873 |
} |
| 874 |
return -1; |
| 875 |
} |
| 876 |
|
| 877 |
static void markCells(Chunk[] chunks, int cell) { |
| 878 |
assert(cell >= 0 && cell < chunks.length); |
| 879 |
for(;;) { |
| 880 |
if(chunks[cell].used) break; |
| 881 |
chunks[cell].used = true; |
| 882 |
Chunk c = chunks[cell]; |
| 883 |
assert(c.wave1[$-1] == 0x7e || |
| 884 |
c.wave1[$-1] == 0x7f); |
| 885 |
if(c.wave1[$-1] == 0x7e) |
| 886 |
break; |
| 887 |
cell = whichCell(chunks, c.wave2[$-1]); |
| 888 |
if(cell < 0) break; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
void deleteRow(Song song, int pos) { |
| 893 |
deleteRow(song, pos, 1); |
| 894 |
} |
| 895 |
|
| 896 |
void deleteRow(Song song, int pos, int num) { |
| 897 |
for(int n = 0; n < num; n++) { |
| 898 |
int i; |
| 899 |
assert(pos < 255 && pos >= 0); |
| 900 |
for(i = pos; i < 255; i++) { |
| 901 |
wave1[i] = wave1[i + 1]; |
| 902 |
wave2[i] = wave2[i + 1]; |
| 903 |
} |
| 904 |
for(i=0;i < 256;i++) { |
| 905 |
if((wave1[i] == 0x7f || wave1[i] == 0x7e) && |
| 906 |
wave2[i] >= pos) { |
| 907 |
if(wave2[i] > 0) --wave2[i]; |
| 908 |
} |
| 909 |
} |
| 910 |
arpPointerUpdate(song, pos, -1); |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
void insertRow(Song song, int pos) { |
| 915 |
int i; |
| 916 |
for(i = 254; i >= pos; i--) { |
| 917 |
wave1[i + 1] = wave1[i]; |
| 918 |
wave2[i + 1] = wave2[i]; |
| 919 |
} |
| 920 |
for(i=0;i<256;i++) { |
| 921 |
if(wave1[i] == 0x7f && |
| 922 |
wave2[i] >= pos) |
| 923 |
wave2[i]++; |
| 924 |
} |
| 925 |
wave1[pos] = 0; |
| 926 |
wave2[pos] = 0; |
| 927 |
arpPointerUpdate(song, pos, 1); |
| 928 |
} |
| 929 |
|
| 930 |
private void arpPointerUpdate(Song song, int pos, int val) { |
| 931 |
for(int j = 0; j < 48; j++) { |
| 932 |
ubyte b7 = instrumentTable[j + 7 * 48]; |
| 933 |
if(b7 > pos) { |
| 934 |
int v = b7 + val; |
| 935 |
if(v < 0) v = 0; |
| 936 |
// TODO rewrite not to access global.. |
| 937 |
instrumentTable[j + 7 * 48] = cast(ubyte)v; |
| 938 |
} |
| 939 |
} |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
class InstrumentTable : Table { |
| 944 |
this(ubyte[] data) { |
| 945 |
super(data); |
| 946 |
} |
| 947 |
|
| 948 |
ubyte[] getInstrument(int no) { |
| 949 |
ubyte[] arr = new ubyte[8]; |
| 950 |
for(int i = 0; i < 8 ; i++) { |
| 951 |
arr[i] = data[no + i * 48]; |
| 952 |
} |
| 953 |
return arr; |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
class SweepTable : Table { |
| 958 |
this(ubyte[] data) { |
| 959 |
super(data); |
| 960 |
} |
| 961 |
|
| 962 |
struct SweepProgram { |
| 963 |
int offset; |
| 964 |
ubyte[] data; |
| 965 |
} |
| 966 |
|
| 967 |
bool isValid(int currentRow) { |
| 968 |
if(currentRow >= 0x80 && currentRow < 0x90) |
| 969 |
return true; |
| 970 |
|
| 971 |
bool[0x40] visited; |
| 972 |
for(int row = currentRow; row < 0x40;) { |
| 973 |
if(visited[row]) return true; |
| 974 |
visited[row] = true; |
| 975 |
int jumpValue = data[row * 4 + 3]; |
| 976 |
if(jumpValue > 0x3f && jumpValue != 0x7f) // if illegal, break |
| 977 |
return false; |
| 978 |
if(jumpValue == 0x7f) |
| 979 |
return true; |
| 980 |
else if(jumpValue == 0) |
| 981 |
row++; |
| 982 |
else row = jumpValue; |
| 983 |
} |
| 984 |
return false; |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
SweepProgram getProgram(int currentRow) { |
| 989 |
bool[0x40] visited; |
| 990 |
int topRow = currentRow; |
| 991 |
for(int row = currentRow; row < 0x40;) { |
| 992 |
if(row < topRow) topRow = row; |
| 993 |
if(visited[row]) break; |
| 994 |
visited[row] = true; |
| 995 |
int jumpValue = data[row * 4 + 3]; |
| 996 |
if(jumpValue > 0x3f && jumpValue != 0x7f) // if illegal, break |
| 997 |
break; |
| 998 |
if(jumpValue == 0x7f) |
| 999 |
break; // if loops or ends, break |
| 1000 |
else if(jumpValue == 0) |
| 1001 |
row++; |
| 1002 |
else row = jumpValue; |
| 1003 |
} |
| 1004 |
|
| 1005 |
int toremove; |
| 1006 |
ubyte[] copy = data.dup; |
| 1007 |
foreach(idx, vis; visited) { |
| 1008 |
if(!vis && idx > 0) { |
| 1009 |
for(int i = 1; i < 64; i++) { |
| 1010 |
int jumpval = |
| 1011 |
data[i * 4 + 3]; |
| 1012 |
if(jumpval < 0x40 && |
| 1013 |
jumpval >= idx) { |
| 1014 |
copy[i * 4 + 3]--; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
if(currentRow >= idx) |
| 1018 |
++toremove; |
| 1019 |
//--currentRow; |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
ubyte[] arr; |
| 1024 |
|
| 1025 |
foreach(idx, vis; visited) { |
| 1026 |
if(vis) { |
| 1027 |
arr ~= copy[idx * 4 .. idx * 4 + 4]; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
assert(arr[$-1] > 0); |
| 1032 |
|
| 1033 |
return SweepProgram(currentRow - toremove, arr); |
| 1034 |
} |
| 1035 |
|
| 1036 |
int seekTableEnd() { |
| 1037 |
for(int i = 0x3c; i >= 0; i -= 4) { |
| 1038 |
if(data[i + 3] > 0) { |
| 1039 |
return i / 4 + 1; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
return 0; |
| 1043 |
} |
| 1044 |
|
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
private class Subtunes { |
| 1049 |
ubyte[1024][3][32] subtunes; |
| 1050 |
private int active; |
| 1051 |
|
| 1052 |
this() { |
| 1053 |
initArray(); |
| 1054 |
} |
| 1055 |
|
| 1056 |
this(ubyte[] arr) { |
| 1057 |
ubyte[] subts; |
| 1058 |
this(); |
| 1059 |
subts = cast(ubyte[])(&subtunes)[0..1]; |
| 1060 |
subts[] = arr; |
| 1061 |
} |
| 1062 |
|
| 1063 |
@property int numOf() { |
| 1064 |
foreach_reverse(idx, ref tune; subtunes) { |
| 1065 |
foreach(ref voice; tune) { |
| 1066 |
if(voice[1 .. 4] != cast(ubyte[])[0x00, 0xf0, 0x00]) { |
| 1067 |
return cast(int)(idx + 1); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
} |
| 1071 |
return 0; |
| 1072 |
} |
| 1073 |
|
| 1074 |
private void initArray() { |
| 1075 |
foreach(ref tune; subtunes) { |
| 1076 |
foreach(ref voice; tune) { |
| 1077 |
voice[0 .. 2] = cast(ubyte[])[0xa0, 0x00]; |
| 1078 |
for(int i = 2; i < voice.length; i += 2) { |
| 1079 |
voice[i .. i+2] = cast(ubyte[])[0xf0, 0x00]; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
void clearAll() { |
| 1086 |
initArray(); |
| 1087 |
syncFromBuffer(); |
| 1088 |
} |
| 1089 |
|
| 1090 |
void clear(int no) { |
| 1091 |
foreach(ref voice; subtunes[no]) { |
| 1092 |
voice[0 .. 2] = cast(ubyte[])[0xa0, 0x00]; |
| 1093 |
for(int i = 2; i < voice.length; i += 2) { |
| 1094 |
voice[i .. i+2] = cast(ubyte[])[0xf0, 0x00]; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
if(no == active) |
| 1098 |
syncFromBuffer(); |
| 1099 |
} |
| 1100 |
|
| 1101 |
void swap(int targetNo, int sourceNo) { |
| 1102 |
if(targetNo == sourceNo) return; |
| 1103 |
if(targetNo == active || sourceNo == active) |
| 1104 |
sync(); |
| 1105 |
ubyte[1024][3] sourcebuf, targetbuf; |
| 1106 |
for(int i = 0; i < 3; i++) { |
| 1107 |
targetbuf[i][] = subtunes[targetNo][i][]; |
| 1108 |
subtunes[targetNo][i][] = subtunes[sourceNo][i][]; |
| 1109 |
subtunes[sourceNo][i][] = targetbuf[i][]; |
| 1110 |
} |
| 1111 |
ubyte spdtemp = songspeeds[targetNo]; |
| 1112 |
songspeeds[targetNo] = songspeeds[sourceNo]; |
| 1113 |
songspeeds[sourceNo] = spdtemp; |
| 1114 |
if(active == targetNo || active == sourceNo) |
| 1115 |
syncFromBuffer(); |
| 1116 |
} |
| 1117 |
|
| 1118 |
private void syncFromBuffer() { |
| 1119 |
for(int i = 0; i < 3; i++) { |
| 1120 |
data[offsets[Offsets.Track1 + i] .. offsets[Offsets.Track1 + i] + 0x400] = |
| 1121 |
subtunes[active][i][0..0x400]; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
Tracklist[] opIndex(int n) { |
| 1126 |
static Tracklist[3] tr; |
| 1127 |
|
| 1128 |
for(int i=0;i<3;i++) { |
| 1129 |
tr[i].length = TRACK_LIST_LENGTH; |
| 1130 |
} |
| 1131 |
for(int i = 0; i < 3 ; i++) { |
| 1132 |
|
| 1133 |
ubyte[] b; |
| 1134 |
// use array from c64 memory if getting current subtune |
| 1135 |
if(n == active) |
| 1136 |
b = buffer[offsets[Offsets.Track1 + i] .. offsets[Offsets.Track1 + i] + 0x400]; |
| 1137 |
else b = subtunes[n][i][0..0x400]; |
| 1138 |
for(int j = 0; j < b.length / 2; j++) { |
| 1139 |
tr[i][j] = Track(b[j * 2 .. j * 2 + 2]); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
return tr; |
| 1143 |
} |
| 1144 |
|
| 1145 |
void activate(int n) { activate(n, true); } |
| 1146 |
void activate(int n, bool dosync) { |
| 1147 |
if(n > 0x1f || n < 0) return; |
| 1148 |
if(dosync) |
| 1149 |
sync(); |
| 1150 |
active = n; |
| 1151 |
for(int i = 0; i < 3; i++) { |
| 1152 |
buffer[offsets[Offsets.Track1 + i] .. offsets[Offsets.Track1 + i] + 0x400] = |
| 1153 |
subtunes[active][i][0..0x400]; |
| 1154 |
} |
| 1155 |
if(ver >= 6) |
| 1156 |
speed = songspeeds[active]; |
| 1157 |
} |
| 1158 |
|
| 1159 |
/* sync "external" subtune array to active one (stored in c64s mem) |
| 1160 |
* the correct way would be to let trackinput also update the external array */ |
| 1161 |
private void sync() { |
| 1162 |
for(int i = 0; i < 3; i++) { |
| 1163 |
subtunes[active][i][0..0x400] = |
| 1164 |
buffer[offsets[Offsets.Track1 + i] .. offsets[Offsets.Track1 + i] + 0x400]; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
// highly dubious coding here (actually, like in most of this class..) |
| 1169 |
ubyte[][][] compact() { |
| 1170 |
ubyte[][][] arr; |
| 1171 |
arr.length = numOf(); |
| 1172 |
foreach(ref subarr; arr) { |
| 1173 |
subarr.length = 3; |
| 1174 |
} |
| 1175 |
|
| 1176 |
for(int i = 0; i < numOf(); i++) { |
| 1177 |
ubyte[][] subarr = arr[i]; |
| 1178 |
for(int j = 0; j < 3; j++) { |
| 1179 |
buffer[offsets[Offsets.Track1 + j] .. offsets[Offsets.Track1 + j] + 0x400] = |
| 1180 |
subtunes[i][j][0..0x400]; |
| 1181 |
} |
| 1182 |
|
| 1183 |
foreach(idx, ref voice; subarr) { |
| 1184 |
voice = tracks[idx].compact().dup; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
return arr; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
int ver = SONG_REVISION, clock, multiplier = 1, sidModel, fppres; |
| 1193 |
char[32] title = ' ', author = ' ', release = ' ', message = ' '; |
| 1194 |
char[32][48] insLabels; |
| 1195 |
private Features features; |
| 1196 |
CPU cpu; |
| 1197 |
ubyte[] sidbuf; |
| 1198 |
ubyte[65536] data; |
| 1199 |
alias data buffer; |
| 1200 |
alias data memspace; |
| 1201 |
Tracklist[] tracks; |
| 1202 |
Sequence[] seqs; |
| 1203 |
alias seqs sequences; |
| 1204 |
address[] offsets; |
| 1205 |
ubyte[32] songspeeds; |
| 1206 |
ubyte[] songsets, |
| 1207 |
wave1Table, |
| 1208 |
wave2Table, |
| 1209 |
waveTable, |
| 1210 |
instrumentTable, |
| 1211 |
pulseTable, |
| 1212 |
filterTable, |
| 1213 |
superTable, |
| 1214 |
chordTable, |
| 1215 |
chordIndexTable, |
| 1216 |
seqlo, |
| 1217 |
seqhi; |
| 1218 |
ByteDescription instrumentByteDescriptions, |
| 1219 |
pulseDescriptions, |
| 1220 |
filterDescriptions, |
| 1221 |
waveDescriptions, |
| 1222 |
cmdDescriptions; |
| 1223 |
|
| 1224 |
// dupes of raw tables above, will eventually update all code to use these |
| 1225 |
Table tSongsets, |
| 1226 |
tSuper, |
| 1227 |
tChord, |
| 1228 |
tChordIndex, |
| 1229 |
tSeqlo, |
| 1230 |
tSeqhi; |
| 1231 |
InstrumentTable tInstr; |
| 1232 |
WaveTable tWave; |
| 1233 |
SweepTable tPulse, tFilter; |
| 1234 |
Table tTrack1, tTrack2, tTrack3; |
| 1235 |
Table[string] tables; |
| 1236 |
char[] playerID; |
| 1237 |
int subtune; |
| 1238 |
Subtunes subtunes; |
| 1239 |
// these used to be sequencer vars but they're here now since they get saved with the tune |
| 1240 |
int highlight = 4, |
| 1241 |
highlightOffset = 0; |
| 1242 |
|
| 1243 |
|
| 1244 |
private auto playerBinary = cast(ubyte[])import("player.bin"); |
| 1245 |
|
| 1246 |
this() { |
| 1247 |
this(playerBinary); |
| 1248 |
} |
| 1249 |
|
| 1250 |
this(ubyte[] player) { |
| 1251 |
cpu = new CPU(buffer); |
| 1252 |
subtunes = new Subtunes(); |
| 1253 |
foreach(ref desc; insLabels) { |
| 1254 |
desc[] = 0x20; |
| 1255 |
} |
| 1256 |
ver = SONG_REVISION; |
| 1257 |
ubyte[] bin; |
| 1258 |
bin.length = 65536; |
| 1259 |
bin[0xdfe .. 0xdfe + player.length] = player; |
| 1260 |
if(bin[0xdfe .. 0xe00] != cast(ubyte[])[ 0x00, 0x0e ]) |
| 1261 |
throw new UserException("Illegal loading address."); |
| 1262 |
songspeeds[] = 5; |
| 1263 |
initialize(bin); |
| 1264 |
sidbuf = memspace[0xd400 .. 0xd419]; |
| 1265 |
} |
| 1266 |
|
| 1267 |
|
| 1268 |
@property int numOfSeqs() { |
| 1269 |
int upto; |
| 1270 |
foreach(i, s; seqs) { |
| 1271 |
if(s.data.raw[0 .. 5] != INITIAL_SEQ) upto = cast(int)i; |
| 1272 |
} |
| 1273 |
return upto + 1; |
| 1274 |
} |
| 1275 |
|
| 1276 |
@property int speed() { |
| 1277 |
return memspace[offsets[Offsets.SPEED]]; |
| 1278 |
} |
| 1279 |
|
| 1280 |
@property void speed(int spd) { |
| 1281 |
memspace[offsets[Offsets.Songsets] + 6] = cast(ubyte)spd; |
| 1282 |
memspace[offsets[Offsets.SPEED]] = cast(ubyte)spd; |
| 1283 |
songspeeds[subtune] = cast(ubyte)spd; |
| 1284 |
if(ver >= 5 && spd >= 2) |
| 1285 |
memspace[offsets[Offsets.PlaySpeed]] = cast(ubyte)spd; |
| 1286 |
} |
| 1287 |
|
| 1288 |
@property int playSpeed() nothrow { |
| 1289 |
return memspace[offsets[Offsets.PlaySpeed]]; |
| 1290 |
} |
| 1291 |
|
| 1292 |
@property int numInstr() { |
| 1293 |
int maxInsno; |
| 1294 |
seqIterator((Sequence s, Element e) { |
| 1295 |
int insval = e.instr.value; |
| 1296 |
if(insval > 0x2f) return; |
| 1297 |
if(insval > maxInsno) maxInsno = insval; }); |
| 1298 |
return maxInsno; |
| 1299 |
} |
| 1300 |
|
| 1301 |
void open(string fn) { |
| 1302 |
ubyte[] inbuf = cast(ubyte[])read(fn); |
| 1303 |
if(inbuf[0..3] != cast(ubyte[])"CC2"[0..3]) { |
| 1304 |
throw new UserException(format("%s: Incorrect filetype.", fn)); |
| 1305 |
} |
| 1306 |
|
| 1307 |
ubyte[] debuf = cast(ubyte[])std.zlib.uncompress(inbuf[3..$],167832); |
| 1308 |
int offset = 65536; |
| 1309 |
ver = debuf[offset++]; |
| 1310 |
if(ver < 6) |
| 1311 |
throw new UserException("The song is incompatible (too old) for this version of the editor."); |
| 1312 |
if(ver >= 128) |
| 1313 |
throw new UserException("The song appears to be a stereo SID file and doesn't work with this editor."); |
| 1314 |
|
| 1315 |
clock = debuf[offset++]; |
| 1316 |
multiplier = debuf[offset++]; |
| 1317 |
sidModel = debuf[offset++]; |
| 1318 |
fppres = debuf[offset++]; |
| 1319 |
if(ver >= 6) { |
| 1320 |
songspeeds[0..32] = debuf[offset .. offset+32]; |
| 1321 |
offset += 32; |
| 1322 |
} |
| 1323 |
if(ver > 10) { |
| 1324 |
highlight = debuf[offset++]; |
| 1325 |
highlightOffset = debuf[offset++]; |
| 1326 |
} |
| 1327 |
offset = DatafileOffset.Title; |
| 1328 |
title[0..32] = cast(char[])debuf[offset .. offset + 32]; |
| 1329 |
author[0..32] = cast(char[])debuf[offset + 32 .. offset + 64]; |
| 1330 |
release[0..32] = cast(char[])debuf[offset + 64 .. offset + 96]; |
| 1331 |
offset += 40 * 4; |
| 1332 |
assert(DatafileOffset.Insnames == offset); |
| 1333 |
offset = DatafileOffset.Insnames; |
| 1334 |
|
| 1335 |
ubyte[] insnames = |
| 1336 |
cast(ubyte[])(&insLabels)[0..1]; |
| 1337 |
insnames[] = debuf[offset .. offset + 48*32]; |
| 1338 |
|
| 1339 |
assert(DatafileOffset.Subtunes == offset + 1024 * 2); |
| 1340 |
offset += 1024 * 2; |
| 1341 |
int len = 1024*3*32; |
| 1342 |
subtunes = new Subtunes(debuf[offset .. offset + len]); |
| 1343 |
offset += len; |
| 1344 |
initialize(debuf[0..65536]); |
| 1345 |
} |
| 1346 |
|
| 1347 |
void setMultiplier(int m) { |
| 1348 |
assert(m > 0 && m < 16); |
| 1349 |
multiplier = m; |
| 1350 |
memspace[Offsets.Volume + 1] = cast(ubyte)m; |
| 1351 |
} |
| 1352 |
|
| 1353 |
protected void initialize(ubyte[] buf) { |
| 1354 |
int voi, ofs; |
| 1355 |
data[] = buf; |
| 1356 |
|
| 1357 |
offsets.length = 0x60; |
| 1358 |
seqs.length = MAX_SEQ_NUM; |
| 1359 |
tracks.length = 3; |
| 1360 |
for(int i = 0; i < 3; i++) { |
| 1361 |
tracks[i].length = TRACK_LIST_LENGTH; |
| 1362 |
} |
| 1363 |
|
| 1364 |
for(int i = 0; i < OFFSETTAB_LENGTH; i++) { |
| 1365 |
offsets[i] = data[0xfa0+i*2] | (data[0xfa1+i*2] << 8); |
| 1366 |
} |
| 1367 |
|
| 1368 |
for(int no = 0; no < MAX_SEQ_NUM; no++) { |
| 1369 |
int p, lo, hi; |
| 1370 |
int lobyt = offsets[Offsets.SeqLO] + no, hibyt = offsets[Offsets.SeqHI] + no; |
| 1371 |
p = data[lobyt] + (data[hibyt] << 8); |
| 1372 |
|
| 1373 |
ubyte[] raw_seq_data = data[p .. p+256]; |
| 1374 |
seqs[no] = new Sequence(raw_seq_data); |
| 1375 |
} |
| 1376 |
|
| 1377 |
for(voi = 0; voi < 3; voi++) { |
| 1378 |
ubyte[] b; |
| 1379 |
int t; |
| 1380 |
int offset = offsets[Offsets.Track1 + voi]; |
| 1381 |
b = data[offset .. offset + 0x400]; |
| 1382 |
for(int i = 0; i < b.length/2; i++) { |
| 1383 |
tracks[voi][i] = Track(memspace[offset + i * 2 .. offset + i * 2 + 2]); |
| 1384 |
} |
| 1385 |
} |
| 1386 |
|
| 1387 |
ofs = offsets[Offsets.Songsets]; |
| 1388 |
songsets = data[ofs .. ofs + 256]; |
| 1389 |
tSongsets = new Table(songsets); |
| 1390 |
|
| 1391 |
ofs = offsets[Offsets.Arp1]; |
| 1392 |
wave1Table = data[ofs .. ofs + 256]; |
| 1393 |
wave2Table = data[ofs + 256 .. ofs + 512]; |
| 1394 |
waveTable = data[ofs .. ofs + 512]; |
| 1395 |
// tWave1 = new Table(wave1Table); |
| 1396 |
// tWave2 = new Table(wave2Table, ofs + 256); |
| 1397 |
tWave = new WaveTable(waveTable); |
| 1398 |
|
| 1399 |
ofs = offsets[Offsets.Inst]; |
| 1400 |
instrumentTable = data[ofs .. ofs + 512]; |
| 1401 |
tInstr = new InstrumentTable(instrumentTable); |
| 1402 |
|
| 1403 |
ofs = offsets[Offsets.CMD1]; |
| 1404 |
superTable = data[ofs .. ofs + 256]; |
| 1405 |
tSuper = new Table(superTable); |
| 1406 |
|
| 1407 |
ofs = offsets[Offsets.PULSTAB]; |
| 1408 |
pulseTable = data[ofs .. ofs + 256]; |
| 1409 |
tPulse = new SweepTable(pulseTable); |
| 1410 |
|
| 1411 |
ofs = offsets[Offsets.FILTTAB]; |
| 1412 |
filterTable = data[ofs .. ofs + 256]; |
| 1413 |
tFilter = new SweepTable(filterTable); |
| 1414 |
|
| 1415 |
ofs = offsets[Offsets.SeqLO]; |
| 1416 |
seqlo = data[ofs .. ofs + 256]; |
| 1417 |
tSeqlo = new Table(seqlo); |
| 1418 |
|
| 1419 |
ofs = offsets[Offsets.SeqHI]; |
| 1420 |
seqhi = data[ofs ..ofs + 256]; |
| 1421 |
tSeqhi = new Table(seqhi); |
| 1422 |
|
| 1423 |
ofs = offsets[Offsets.ChordTable]; |
| 1424 |
chordTable = data[ofs .. ofs + 128]; |
| 1425 |
tChord = new Table(chordTable); |
| 1426 |
|
| 1427 |
ofs = offsets[Offsets.ChordIndexTable]; |
| 1428 |
chordIndexTable = data[ofs .. ofs + 32]; |
| 1429 |
tChordIndex = new Table(chordIndexTable); |
| 1430 |
|
| 1431 |
generateChordIndex(); |
| 1432 |
|
| 1433 |
ofs = offsets[Offsets.Track1]; |
| 1434 |
tTrack1 = new Table(data[ofs .. ofs + 0x400]); |
| 1435 |
ofs = offsets[Offsets.Track2]; |
| 1436 |
tTrack2 = new Table(data[ofs .. ofs + 0x400]); |
| 1437 |
ofs = offsets[Offsets.Track3]; |
| 1438 |
tTrack3 = new Table(data[ofs .. ofs + 0x400]); |
| 1439 |
|
| 1440 |
playerID = cast(char[])data[0xfee .. 0xff5]; |
| 1441 |
subtune = 0; |
| 1442 |
|
| 1443 |
ofs = offsets[Offsets.Features]; |
| 1444 |
ubyte[] b = memspace[ofs .. ofs + 64]; |
| 1445 |
features.requestedTables = b[0]; |
| 1446 |
features.instrumentFlags[] = b[1..9]; |
| 1447 |
|
| 1448 |
/* |
| 1449 |
ubyte* b = cast(ubyte*)&features; |
| 1450 |
b[0..features.sizeof] = memspace[ofs .. ofs + features.sizeof]; |
| 1451 |
*/ |
| 1452 |
|
| 1453 |
if(ver > 7) { |
| 1454 |
instrumentByteDescriptions.length = 8; |
| 1455 |
ofs = offsets[Offsets.InstrumentDescriptionsHeader]; |
| 1456 |
for(int j = 0; j < 8; j++) { |
| 1457 |
int iofs = memspace[ofs] | (memspace[ofs+1] << 8); |
| 1458 |
instrumentByteDescriptions[j] = cast(char*)&memspace[iofs]; |
| 1459 |
ofs += 2; |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
if(ver > 8) { |
| 1464 |
int offset; |
| 1465 |
filterDescriptions.length = 4; |
| 1466 |
offset = offsets[Offsets.FilterDescriptionsHeader]; |
| 1467 |
foreach(idx, ref descr; filterDescriptions) { |
| 1468 |
int soffset = memspace[offset + idx * 2] | (memspace[offset + idx * 2 + 1] << 8); |
| 1469 |
descr = cast(char*)&memspace[soffset]; |
| 1470 |
} |
| 1471 |
pulseDescriptions.length = 4; |
| 1472 |
offset = offsets[Offsets.PulseDescriptionsHeader]; |
| 1473 |
foreach(idx, ref descr; pulseDescriptions) { |
| 1474 |
int soffset = memspace[offset + idx * 2] | (memspace[offset + idx * 2 + 1] << 8); |
| 1475 |
descr = cast(char*)&memspace[soffset]; |
| 1476 |
} |
| 1477 |
waveDescriptions.length = 2; |
| 1478 |
offset = offsets[Offsets.WaveDescriptionsHeader]; |
| 1479 |
foreach(idx, ref descr; waveDescriptions) { |
| 1480 |
int soffset = memspace[offset + idx * 2] | (memspace[offset + idx * 2 + 1] << 8); |
| 1481 |
descr = cast(char*)&memspace[soffset]; |
| 1482 |
} |
| 1483 |
|
| 1484 |
cmdDescriptions.length = 2; |
| 1485 |
offset = offsets[Offsets.CmdDescriptionsHeader]; |
| 1486 |
foreach(idx, ref descr; cmdDescriptions) { |
| 1487 |
int soffset = memspace[offset + idx * 2] | (memspace[offset + idx * 2 + 1] << 8); |
| 1488 |
descr = cast(char*)&memspace[soffset]; |
| 1489 |
} |
| 1490 |
|
| 1491 |
} |
| 1492 |
|
| 1493 |
subtunes.syncFromBuffer(); |
| 1494 |
speed = songspeeds[0]; |
| 1495 |
cpu.reset(); |
| 1496 |
cpu.execute(0x1000); |
| 1497 |
tables = [ cast(string)"songsets":tSongsets, "wave":tWave, |
| 1498 |
"instr":tInstr, "pulse":tPulse, "filter":tFilter, "cmd":tSuper, |
| 1499 |
"chord":tChord, "chordidx":tChordIndex, "seqlo":tSeqlo, "seqhi":tSeqhi ]; |
| 1500 |
} |
| 1501 |
|
| 1502 |
void save(string fn) { |
| 1503 |
// get tracks from the c64 memory to subtunes-array |
| 1504 |
subtunes.sync(); |
| 1505 |
ubyte[] b; |
| 1506 |
int offset; |
| 1507 |
b.length = 300000; |
| 1508 |
|
| 1509 |
b[0..65536] = memspace; |
| 1510 |
offset += 65536; |
| 1511 |
|
| 1512 |
foreach(val; [ver, clock, multiplier, sidModel, fppres]) { |
| 1513 |
b[offset++] = val & 255; |
| 1514 |
} |
| 1515 |
|
| 1516 |
b[offset .. offset+32] = songspeeds[]; |
| 1517 |
offset += 32; |
| 1518 |
b[offset++] = cast(ubyte)highlight; |
| 1519 |
b[offset++] = cast(ubyte)highlightOffset; |
| 1520 |
|
| 1521 |
offset = DatafileOffset.Title; |
| 1522 |
|
| 1523 |
foreach(str; [title, author, release, message]) { |
| 1524 |
b[offset .. offset + 32] = cast(ubyte[])str[]; |
| 1525 |
offset += 32; |
| 1526 |
} |
| 1527 |
|
| 1528 |
offset = DatafileOffset.Insnames; |
| 1529 |
ubyte[] arr; |
| 1530 |
arr = cast(ubyte[])(&insLabels)[0..1]; |
| 1531 |
b[offset .. offset + arr.length] = arr[]; |
| 1532 |
offset += arr.length; |
| 1533 |
|
| 1534 |
offset += 32 * 32 - 0x200; |
| 1535 |
arr = cast(ubyte[])(&subtunes.subtunes)[0..1]; |
| 1536 |
b[offset .. offset + arr.length] = arr[]; |
| 1537 |
offset += arr.length; |
| 1538 |
std.file.write(fn, "CC2"); |
| 1539 |
append(fn, std.zlib.compress(b)); |
| 1540 |
} |
| 1541 |
|
| 1542 |
void splitSequence(int seqnumber, int seqofs) { |
| 1543 |
if(seqnumber == 0 || seqofs == 0) return; |
| 1544 |
int suborig = subtune; |
| 1545 |
int newseqnumber = getFreeSequence(0); |
| 1546 |
Sequence s = seqs[seqnumber]; |
| 1547 |
if(seqofs == s.rows - 1) return; |
| 1548 |
Sequence copy = new Sequence(s.data.raw.dup); |
| 1549 |
Sequence ns = seqs[newseqnumber]; |
| 1550 |
ns.copyFrom(s); |
| 1551 |
ns.shrink(0, seqofs, true); |
| 1552 |
s.shrink(seqofs, s.rows - seqofs, true); |
| 1553 |
subtunes.sync(); |
| 1554 |
foreach(sIdx, st; subtunes.subtunes) { |
| 1555 |
subtunes.activate(cast(int)sIdx); |
| 1556 |
foreach(vIdx, voice; tracks) { |
| 1557 |
for(int tIdx = voice.length - 1; tIdx >= 0; tIdx--) { |
| 1558 |
Track t = voice[tIdx]; |
| 1559 |
if(t.number == seqnumber) { |
| 1560 |
tracks[vIdx].insertAt(tIdx+1); |
| 1561 |
Track t2 = tracks[vIdx][tIdx+1]; |
| 1562 |
t2.trans = 0x80; |
| 1563 |
t2.number = (cast(ubyte)newseqnumber); |
| 1564 |
} |
| 1565 |
|
| 1566 |
} |
| 1567 |
} |
| 1568 |
} |
| 1569 |
subtunes.activate(suborig); |
| 1570 |
} |
| 1571 |
|
| 1572 |
private int getTablepointer(ubyte[] table, ubyte[] flags, int requestedFlag, int insno) { |
| 1573 |
foreach(i, flag; flags) { |
| 1574 |
if(flag != requestedFlag) continue; |
| 1575 |
return table[insno + i * 48]; |
| 1576 |
} |
| 1577 |
throw new UserException(std.string.format("Missing tablepointer %d", requestedFlag)); |
| 1578 |
} |
| 1579 |
|
| 1580 |
int wavetablePointer(int insno) { |
| 1581 |
return getTablepointer(instrumentTable, features.instrumentFlags, 1, insno); |
| 1582 |
} |
| 1583 |
|
| 1584 |
int pulsetablePointer(int insno) { |
| 1585 |
int ptr = getTablepointer(instrumentTable, features.instrumentFlags, 3, insno); |
| 1586 |
if(ptr >= 0x80) return 0; |
| 1587 |
return ptr; |
| 1588 |
} |
| 1589 |
|
| 1590 |
int filtertablePointer(int insno) { |
| 1591 |
return getTablepointer(instrumentTable, features.instrumentFlags, 4, insno); |
| 1592 |
} |
| 1593 |
|
| 1594 |
void seqIterator(void delegate(Sequence s, Element e) dg) { |
| 1595 |
foreach(i, s; seqs) { |
| 1596 |
for(int j = 0; j < s.rows; j++) { |
| 1597 |
Element e = s.data[j]; |
| 1598 |
dg(s, e); |
| 1599 |
} |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
void seqIterator(void delegate(int seqno, Sequence s, Element e) dg) { |
| 1604 |
foreach(i, s; seqs) { |
| 1605 |
for(int j = 0; j < s.rows; j++) { |
| 1606 |
Element e = s.data[j]; |
| 1607 |
dg(cast(int)i, s, e); |
| 1608 |
} |
| 1609 |
} |
| 1610 |
} |
| 1611 |
|
| 1612 |
void trackIterator(void delegate(Track t) dg) { |
| 1613 |
for(int sidx = 0; sidx < 32; sidx++) { |
| 1614 |
Tracklist[] subtune = subtunes[sidx]; |
| 1615 |
foreach(voice; subtune) { |
| 1616 |
foreach(track; voice) { |
| 1617 |
dg(track); |
| 1618 |
} |
| 1619 |
} |
| 1620 |
} |
| 1621 |
} |
| 1622 |
|
| 1623 |
void tableIterator(void delegate(Table t) dg) { |
| 1624 |
foreach(idx, table; [ "wave", "cmd", "instr", "chord", |
| 1625 |
"pulse", "filter"]) { |
| 1626 |
dg(tables[table]); |
| 1627 |
} |
| 1628 |
|
| 1629 |
} |
| 1630 |
|
| 1631 |
void setVoicon(shared int[] m) { |
| 1632 |
//setVoicon(m[0], m[1], m[2]); |
| 1633 |
buffer[offsets[Offsets.VOICE]+0] = m[0] ? 0x19 : 0x00; |
| 1634 |
buffer[offsets[Offsets.VOICE]+1] = m[1] ? 0x19 : 0x07; |
| 1635 |
buffer[offsets[Offsets.VOICE]+2] = m[2] ? 0x19 : 0x0e; |
| 1636 |
} |
| 1637 |
|
| 1638 |
int getFreeSequence(int start) { |
| 1639 |
bool seqUsed; |
| 1640 |
subtunes.sync(); |
| 1641 |
for(int seqnum = start; seqnum < MAX_SEQ_NUM; seqnum++) { |
| 1642 |
seqUsed = false; |
| 1643 |
foreach(ist, st; subtunes.subtunes) { |
| 1644 |
foreach(voice; st) { |
| 1645 |
foreach(t; voice) { |
| 1646 |
if(t == seqnum) |
| 1647 |
seqUsed = true; |
| 1648 |
} |
| 1649 |
} |
| 1650 |
} |
| 1651 |
if(!seqUsed) return seqnum; |
| 1652 |
} |
| 1653 |
return -1; |
| 1654 |
} |
| 1655 |
|
| 1656 |
void clearSeqs() { |
| 1657 |
for(int i = 1; i < MAX_SEQ_NUM; i++) { |
| 1658 |
seqs[i].clear(); |
| 1659 |
} |
| 1660 |
subtunes.clearAll(); |
| 1661 |
} |
| 1662 |
|
| 1663 |
void incSubtune() { |
| 1664 |
if(subtune < 31) |
| 1665 |
subtunes.activate(++subtune); |
| 1666 |
} |
| 1667 |
|
| 1668 |
void decSubtune() { |
| 1669 |
if(subtune > 0) |
| 1670 |
subtunes.activate(--subtune); |
| 1671 |
} |
| 1672 |
|
| 1673 |
void generateChordIndex() { |
| 1674 |
int crd, p; |
| 1675 |
for(int i = 0; i < 128; i++) { |
| 1676 |
if(chordTable[i] >= 0x80) { |
| 1677 |
chordIndexTable[crd] = cast(ubyte)p; |
| 1678 |
if(++crd >= 31) throw new Error("Chord index overflow"); |
| 1679 |
p = i + 1; |
| 1680 |
} |
| 1681 |
} |
| 1682 |
chordIndexTable[crd++] = cast(ubyte)(p); |
| 1683 |
} |
| 1684 |
|
| 1685 |
void importData(Song insong) { |
| 1686 |
buffer[0xdfe .. 0xdfe + playerBinary.length] = playerBinary[]; |
| 1687 |
ver = SONG_REVISION; |
| 1688 |
initialize(buffer.dup); |
| 1689 |
// copy tables |
| 1690 |
foreach(idx, table; [ "wave", "cmd", "instr", "chord", |
| 1691 |
"pulse", "filter"]) { |
| 1692 |
tables[table].data[] = insong.tables[table].data; |
| 1693 |
} |
| 1694 |
// sequences |
| 1695 |
foreach(idx, ref seq; insong.seqs) { |
| 1696 |
seqs[idx].data.raw[] = seq.data.raw; |
| 1697 |
seqs[idx].refresh(); |
| 1698 |
|
| 1699 |
} |
| 1700 |
// subtunes |
| 1701 |
subtunes.subtunes[][][] = insong.subtunes.subtunes[][][]; |
| 1702 |
subtunes.syncFromBuffer(); |
| 1703 |
// labels |
| 1704 |
insLabels[] = insong.insLabels[]; |
| 1705 |
title[] = insong.title[]; |
| 1706 |
author[] = insong.author[]; |
| 1707 |
release[] = insong.release[]; |
| 1708 |
// vars |
| 1709 |
clock = insong.clock; |
| 1710 |
multiplier = insong.multiplier; |
| 1711 |
sidModel = insong.sidModel; |
| 1712 |
fppres = insong.fppres; |
| 1713 |
songspeeds = insong.songspeeds[]; |
| 1714 |
speed = songspeeds[0]; |
| 1715 |
// TODO highlight, highlightoffset |
| 1716 |
generateChordIndex(); |
| 1717 |
subtunes.activate(0); |
| 1718 |
} |
| 1719 |
|
| 1720 |
// hack to help sequencer rowcounting |
| 1721 |
Sequence sequence(Track t) { |
| 1722 |
return seqs[t.number]; |
| 1723 |
} |
| 1724 |
|
| 1725 |
void savePatch(string filename, int no) { |
| 1726 |
import std.conv; |
| 1727 |
string insname = std.string.stripRight |
| 1728 |
(to!string(insLabels[no])); |
| 1729 |
int waveptr = wavetablePointer(no); |
| 1730 |
int pulseptr = pulsetablePointer(no); |
| 1731 |
int filtptr = filtertablePointer(no); |
| 1732 |
|
| 1733 |
if(!tWave.isValid(waveptr)) { |
| 1734 |
throw new UserException("Cannot save; instrument is not valid (wavetable does not wrap)."); |
| 1735 |
} |
| 1736 |
|
| 1737 |
if(!tPulse.isValid(pulseptr)) { |
| 1738 |
throw new UserException("Cannot save; pulse is not valid."); |
| 1739 |
} |
| 1740 |
|
| 1741 |
if(!tFilter.isValid(filtptr)) { |
| 1742 |
throw new UserException("Cannot save; filter is not valid."); |
| 1743 |
} |
| 1744 |
|
| 1745 |
auto wp = tWave.getProgram(waveptr); |
| 1746 |
auto pp = tPulse.getProgram(pulseptr); |
| 1747 |
auto fp = tFilter.getProgram(filtptr); |
| 1748 |
// rewrite pointers. TODO: move elsewhere |
| 1749 |
|
| 1750 |
auto instr = tInstr.getInstrument(no); |
| 1751 |
instr[7] = cast(ubyte)wp.offset; |
| 1752 |
// skipping 0-rows since it implies inactive sweep |
| 1753 |
if(pp.offset > 0) |
| 1754 |
instr[5] = cast(ubyte)pp.offset; |
| 1755 |
if(fp.offset > 0) |
| 1756 |
instr[4] = cast(ubyte)fp.offset; |
| 1757 |
|
| 1758 |
string csv = |
| 1759 |
to!string(playerID[0..6]) ~ "`" ~ |
| 1760 |
insname ~ |
| 1761 |
"`" ~ com.util.arr2str(instr) ~ |
| 1762 |
"`" ~ com.util.arr2str(wp.wave1) ~ |
| 1763 |
"`" ~ com.util.arr2str(wp.wave2) ~ |
| 1764 |
"`" ~ (pulseptr > 0 ? |
| 1765 |
com.util.arr2str(pp.data) : "") ~ |
| 1766 |
"`" ~ (filtptr > 0 ? |
| 1767 |
com.util.arr2str(fp.data) : ""); |
| 1768 |
|
| 1769 |
if(filename is null) { |
| 1770 |
filename = com.util.fnClean(insname ~ ".cti"); |
| 1771 |
} |
| 1772 |
std.file.write(filename, csv); |
| 1773 |
} |
| 1774 |
|
| 1775 |
void insertPatch(string filename, int insno) { |
| 1776 |
import std.csv : csvReader; |
| 1777 |
auto conv(string s) { |
| 1778 |
ubyte[] arr = new ubyte[s.length / 2]; |
| 1779 |
for(int i = 0, j = 0; i < s.length; i += 2, j++) { |
| 1780 |
arr[j] = cast(ubyte)com.util.convertHex(s[i .. i + 2]); |
| 1781 |
} |
| 1782 |
return arr; |
| 1783 |
} |
| 1784 |
struct Rec { |
| 1785 |
string playerid, name, def, wave1, wave2, pulse, filt; |
| 1786 |
} |
| 1787 |
string patch = cast(string)std.file.read(filename); |
| 1788 |
auto recs = csvReader!Rec(patch,'`'); |
| 1789 |
foreach(rec; recs) { |
| 1790 |
auto p = new Purge(this); |
| 1791 |
p.deleteInstrument(insno); |
| 1792 |
insertInstrument(Patch(rec.name, conv(rec.def), conv(rec.wave1), |
| 1793 |
conv(rec.wave2), conv(rec.filt), |
| 1794 |
conv(rec.pulse)), insno); |
| 1795 |
|
| 1796 |
if(rec.name.length > 31) rec.name.length = 31; |
| 1797 |
insLabels[insno][] = ' '; |
| 1798 |
insLabels[insno][0..rec.name.length] = rec.name; |
| 1799 |
|
| 1800 |
// break because only 1st row needed (though there should never be more) |
| 1801 |
break; |
| 1802 |
} |
| 1803 |
} |
| 1804 |
|
| 1805 |
private void insertInstrument(Patch patch, int insno) { |
| 1806 |
assert(patch.wave1.length == patch.wave2.length); |
| 1807 |
// check for free space in tables |
| 1808 |
int waveptr = tWave.seekTableEnd(); |
| 1809 |
int pulseptr = tPulse.seekTableEnd() * 4; |
| 1810 |
int filterptr = tFilter.seekTableEnd() * 4; |
| 1811 |
if(patch.wave1.length + waveptr > tWave.wave1.length - 1) |
| 1812 |
throw new UserException("Not enough free rows in wavetable"); |
| 1813 |
if(patch.pulse.length + pulseptr > tPulse.data.length - 4) |
| 1814 |
throw new UserException("Not enough free rows in pulse table"); |
| 1815 |
if(patch.filt.length + filterptr > tFilter.data.length - 4) |
| 1816 |
throw new UserException("Not enough free rows in filter table"); |
| 1817 |
|
| 1818 |
// cram loaded data into tables |
| 1819 |
ubyte[] wave1 = tWave.wave1[waveptr .. waveptr + patch.wave1.length]; |
| 1820 |
ubyte[] wave2 = tWave.wave2[waveptr .. waveptr + patch.wave2.length]; |
| 1821 |
for(int i = 0; i < wave1.length; i++) { |
| 1822 |
if(wave1[i] == 0x7f) |
| 1823 |
wave2[i] += waveptr; |
| 1824 |
} |
| 1825 |
wave1[0..$] = patch.wave1[]; |
| 1826 |
wave2[0..$] = patch.wave2[]; |
| 1827 |
|
| 1828 |
// ---------------------------------------- |
| 1829 |
void fixSweepOffsets(ubyte[] table, int offset) { |
| 1830 |
for(int i = 0; i < table.length; i += 4) { |
| 1831 |
if(table[i + 3] > 0 && table[i + 3] < 0x40) { |
| 1832 |
table[i + 3] += offset; |
| 1833 |
assert(table[i + 3] < 0x40); |
| 1834 |
} |
| 1835 |
} |
| 1836 |
} |
| 1837 |
|
| 1838 |
// insert pulse program if defined |
| 1839 |
if(patch.def[5] > 0) { |
| 1840 |
ubyte[] pulse = tPulse.data[pulseptr .. $]; |
| 1841 |
fixSweepOffsets(pulse, pulseptr + patch.def[5]); |
| 1842 |
|
| 1843 |
tPulse.data[pulseptr .. pulseptr + patch.pulse.length] = |
| 1844 |
patch.pulse[]; |
| 1845 |
|
| 1846 |
for(int i = 0; i < pulse.length - 4; i += 4) { |
| 1847 |
if(pulse[i + 3] > 0 && pulse[i + 3] < 0x40) { |
| 1848 |
pulse[i + 3] += pulseptr / 4 - 1; |
| 1849 |
} |
| 1850 |
} |
| 1851 |
patch.def[5] += pulseptr / 4 - 1; |
| 1852 |
} |
| 1853 |
|
| 1854 |
// insert filter if defined |
| 1855 |
if(patch.def[4] > 0) { |
| 1856 |
ubyte[] filt = tFilter.data[filterptr .. $]; |
| 1857 |
fixSweepOffsets(filt, filterptr + patch.def[4]); |
| 1858 |
|
| 1859 |
tFilter.data[filterptr .. filterptr + patch.filt.length] = |
| 1860 |
patch.filt[]; |
| 1861 |
|
| 1862 |
for(int i = 0; i < filt.length; i += 4) { |
| 1863 |
if(filt[i + 3] > 0 && filt[i + 3] < 0x40) { |
| 1864 |
filt[i + 3] += filterptr / 4 - 1; |
| 1865 |
} |
| 1866 |
} |
| 1867 |
patch.def[4] += filterptr / 4 - 1; |
| 1868 |
} |
| 1869 |
|
| 1870 |
// recalc wrap points for inserted data |
| 1871 |
for(int i = 0; i < wave1.length; i++) { |
| 1872 |
if(wave1[i] == 0x7f) |
| 1873 |
wave2[i] += waveptr; |
| 1874 |
} |
| 1875 |
|
| 1876 |
patch.def[7] += waveptr; |
| 1877 |
|
| 1878 |
for(int i = 0; i < 8; i++) { |
| 1879 |
instrumentTable[i * 48 + insno] = patch.def[i]; |
| 1880 |
} |
| 1881 |
} |
| 1882 |
} |