All files / src/shared/lib/uge ugeHelper.ts

95.22% Statements 498/523
78.07% Branches 178/228
97.5% Functions 39/40
95.82% Lines 458/478

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958                  4x 4x 4x               4x                                                         4x 6x         6x   6x 45701x     6x 17900x     6x 6x 288x 288x 288x     203x   288x 288x     6x       6x 6x   6x       6x 6x 6x   6x   6x 6x 270x 270x   270x 270x 270x 270x     270x 270x 270x 88x   270x 110x     270x 270x 270x 270x 23x     270x   270x 270x   270x 270x   270x 270x 225x   225x   225x 14400x 14400x 14400x 14400x 14400x   14400x                   270x 270x 45x 45x 45x 45x 45x 270x 270x 270x         270x                                       6x 96x 96x 96x     6x   6x 5x 5x     6x 6x     6x 6x 6x 30x 30x 30x 30x       30x 1920x 256x     256x 256x   256x 1664x 1664x     1664x 1664x   1664x               30x     30x       6x 6x 24x 24x         24x         6x 6x 6x 6x                                   270x   270x 90x   90x 24x   66x     90x 90x 90x 90x   90x 90x   90x 75x 75x   15x 960x     90x 90x 180x 90x   90x 24x   66x     90x 90x 90x   90x 75x 75x   15x 960x     90x 90x 90x 90x   90x 16x   74x     90x 90x 90x 90x   90x 75x 75x         15x             15x 15x             90x 90x           6x 34x 34x 4x     30x   30x 1920x 1920x 1920x 4x   1920x     1920x           30x     6x 7x           7x           6x             4x 2x 2x 2x 2x     7656x 7656x     25970x 25970x     92x 92x     96x 96x 96x 96x 96x       90x 90x 5760x 5760x 5760x 5760x 5760x 5760x       30x   30x 30x 30x 30x 30x 30x   30x 30x 30x   30x   30x 30x   30x   30x       30x   30x 30x 30x 30x 30x 30x   30x 30x 30x   30x   30x 30x   30x   30x       30x   30x 30x 30x 30x 30x 30x   30x 30x 30x   30x   30x 30x   30x   30x     2x 2x 2x 2x   2x 30x   2x 30x   2x 30x   2x 32x 1024x     2x   2x   2x   2x 2x 2x 8x 8x 512x 512x 512x 512x 512x 512x     2x 8x 8x 8x   8x   2x 32x     2x           4x 10x   10x 13x 52x       10x 10x   10x   10x 17x 3x     14x 14x   14x 14x       55x 55x   55x 1x     54x 54x 3456x             10x     13x     52x   52x       52x                 4x 19x 19x 523x 515x 512x 512x   8x                     4x     4x 1002x     4x 16x 19x   8x     4x 16x 16x       4x 512x 512x 512x 512x 512x 512x       512x           4x       64x 64x 64x 64x 64x       64x           4x 4x           4x             4x 4x 16x   16x       16x   16x 1024x 3x           4x     4x   20x       20x     20x 20x 7x   20x 20x   20x         4x 15x 15x 15x 15x 15x   15x         4x   15x 15x 14x 15x 15x 15x 15x   15x         4x 16x 256x         4x 4x   4x 16x 16x 16x 1024x   16x 16x 8x   8x 8x       4x   4x 4x 4x 4x 4x   4x       50x       47x     3x 3x   3x 2x 2x   2x 2x 64x         2x 2x     3x 3x               4x 20x   4x 15x   4x 15x     4x                 4x 8x 8x 512x   8x   4x 2x   4x 16x     4x 4x 20x   4x 4x 4x 15x   4x 4x 4x 15x   4x       4x 4x 16x   4x   4x                     4x     4x       960x 15x 90x   15x 15x 15x    
import type {
  Song,
  PatternCell,
  SubPatternCell,
  DutyInstrument,
  NoiseInstrument,
  WaveInstrument,
  Pattern,
} from "shared/lib/uge/types";
import { TRACKER_NUM_CHANNELS } from "consts";
import { noteGBDKDefines } from "shared/lib/music/constants";
import {
  addDutyInstrument,
  addNoiseInstrument,
  addWaveInstrument,
  createPattern,
  createSong,
  createSubPatternCell,
} from "./song";
import { isSplitPatternSequence } from "shared/lib/uge/editor/helpers";
 
interface InstrumentMap {
  [index: number]: number;
}
 
interface InstrumentData {
  idx: number;
  type: number;
  name: string;
  length: number;
  lengthEnabled: number;
  initialVolume: number;
  volumeSweepAmount: number;
  freqSweepTime: number;
  freqSweepShift: number;
  duty: number;
  waveOutputLevel: number;
  waveWaveformIndex: number;
  subpatternEnabled: number;
  subpattern: SubPatternCell[];
  noiseCounterStep: number;
  noiseMacro: number[];
}
 
/**
 * Parses a `.uge` binary buffer and returns a fully populated Song object.
 * Supports all versions of the hUGETracker format up to version 6.
 */
export const loadUGESong = (buffer: Buffer): Song => {
  const data = buffer.buffer.slice(
    buffer.byteOffset,
    buffer.byteOffset + buffer.byteLength,
  ) as ArrayBuffer;
 
  const uint8data = new Uint8Array(data);
 
  const readUint32 = () => {
    return new Uint32Array(data.slice(offset, (offset += 4)))[0];
  };
 
  const readUint8 = () => {
    return uint8data[offset++];
  };
 
  const td = new TextDecoder();
  const readText = () => {
    const len = uint8data[offset];
    let text = "";
    if (len > 0) {
      // Need to check string length > 0 here to prevent
      // ERR_ENCODING_INVALID_ENCODED_DATA when this is run from Electron main process
      text = td.decode(data.slice(offset + 1, offset + 1 + len));
    }
    offset += 256;
    return text;
  };
 
  const song = createSong();
 
  // TODO: Sanity checks on data.
  // TODO: Use `DataView` object instead of loads of Uint32Arrays
  let offset = 0;
  const version = readUint32();
 
  Iif (version < 0 || version > 6) {
    throw new Error(`UGE version ${version} is not supported by GB Studio`);
  }
 
  song.name = readText();
  song.artist = readText();
  song.comment = readText();
 
  const instrumentCount = version < 3 ? 15 : 45;
 
  const instrumentData: Array<InstrumentData> = [];
  for (let n = 0; n < instrumentCount; n++) {
    const type = readUint32();
    const name = readText();
 
    const length = readUint32();
    const lengthEnabled = readUint8();
    let initialVolume = readUint8();
    Iif (initialVolume > 15) {
      initialVolume = 15; // ??? bug in the song files?
    }
    const volumeDirection = readUint32();
    let volumeSweepAmount = readUint8();
    if (volumeSweepAmount !== 0) {
      volumeSweepAmount = 8 - volumeSweepAmount;
    }
    if (volumeDirection) {
      volumeSweepAmount = -volumeSweepAmount;
    }
 
    const freqSweepTime = readUint32();
    const freqSweepDirection = readUint32();
    let freqSweepShift = readUint32();
    if (freqSweepDirection) {
      freqSweepShift = -freqSweepShift;
    }
 
    const duty = readUint8();
 
    const waveOutputLevel = readUint32();
    const waveWaveformIndex = readUint32();
 
    let subpatternEnabled = 0;
    let noiseCounterStep = 0;
 
    const subpattern: SubPatternCell[] = [];
    if (version >= 6) {
      noiseCounterStep = readUint32();
 
      subpatternEnabled = readUint8();
 
      for (let m = 0; m < 64; m++) {
        const note = readUint32();
        offset += 4; // unused uint32 field. increase offset by 4.
        const jump = readUint32();
        const effectCode = readUint32();
        const effectParam = readUint8();
 
        subpattern.push({
          note: note === 90 ? null : note,
          jump,
          effectCode: effectCode === 0 && effectParam === 0 ? null : effectCode,
          effectParam:
            effectCode === 0 && effectParam === 0 ? null : effectParam,
        });
      }
    }
 
    const noiseMacro = [];
    if (version < 6) {
      offset += 4; // unused uint32 field. increase offset by 4.
      noiseCounterStep = readUint32();
      offset += 4; // unused uint32 field. increase offset by 4.
      if (version >= 4) {
        for (let n = 0; n < 6; n++) {
          const uint8ref = readUint8();
          const int8ref = uint8ref > 0x7f ? uint8ref - 0x100 : uint8ref;
          noiseMacro.push(int8ref);
        }
      }
    }
 
    instrumentData.push({
      idx: n,
      type,
      name,
      length,
      lengthEnabled,
      initialVolume,
      volumeSweepAmount,
      freqSweepTime,
      freqSweepShift,
      duty,
      waveOutputLevel,
      waveWaveformIndex,
      subpatternEnabled,
      subpattern,
      noiseCounterStep,
      noiseMacro,
    });
  }
 
  for (let n = 0; n < 16; n++) {
    song.waves.push(Uint8Array.from(uint8data.slice(offset, offset + 32)));
    offset += 32;
    Iif (version < 3) offset += 1; // older versions have an off-by-one error
  }
 
  song.ticksPerRow = readUint32();
 
  if (version >= 6) {
    song.timerEnabled = readUint8() !== 0;
    song.timerDivider = readUint32();
  }
 
  const patternCount = new Uint32Array(data.slice(offset, offset + 4))[0];
  Iif (offset + patternCount * 13 * 64 > data.byteLength) {
    throw new Error(`Song has too many patterns (${patternCount})`);
  }
  offset += 4;
  const patterns = [];
  for (let n = 0; n < patternCount; n++) {
    let patternId = 0;
    const pattern = [];
    if (version >= 5) {
      patternId = readUint32();
    } else E{
      patternId = n;
    }
    for (let m = 0; m < 64; m++) {
      if (version < 6) {
        const [note, instrument, effectCode] = new Int32Array(
          data.slice(offset, offset + 3 * 4),
        );
        offset += 3 * 4;
        const effectParam = readUint8();
 
        pattern.push([note, instrument, effectCode, effectParam]);
      } else if (version >= 6) {
        const [note, instrument, _unused, effectCode] = new Int32Array(
          data.slice(offset, offset + 4 * 4),
        );
        offset += 4 * 4;
        const effectParam = readUint8();
 
        pattern.push([note, instrument, effectCode, effectParam]);
      }
    }
    /*
     If there's a repeated pattern it probably means the song was saved
     with an old version of GB Studio (3.0.2 or earlier) that didn't save the
     unique pattern ids and instead expected them to always be consecutive.
    */
    Iif (version === 5 && patterns[patternId]) {
      patterns[n] = pattern;
    } else {
      patterns[patternId] = pattern;
    }
  }
 
  const orders: number[][] = [];
  for (let n = 0; n < 4; n++) {
    const orderCount = readUint32(); // The amount of pattern orders stored in the file has an off-by-one.
    orders.push(
      Array.from(
        new Uint32Array(data.slice(offset, offset + 4 * (orderCount - 1))),
      ),
    );
    offset += 4 * orderCount;
  }
  // TODO: If version > 1 then custom routines follow.
 
  // Add instruments
  const dutyInstrumentMapping: InstrumentMap = {};
  const waveInstrumentMapping: InstrumentMap = {};
  const noiseInstrumentMapping: InstrumentMap = {};
  instrumentData.forEach((instrument: InstrumentData) => {
    const {
      idx,
      type,
      name,
      length,
      lengthEnabled,
      initialVolume,
      volumeSweepAmount,
      freqSweepTime,
      freqSweepShift,
      duty,
      waveOutputLevel,
      waveWaveformIndex,
      subpatternEnabled,
      subpattern,
      noiseCounterStep,
      noiseMacro,
    } = instrument;
 
    if (type === 0) {
      const instr = {} as DutyInstrument;
 
      if (lengthEnabled) {
        instr.length = 64 - length;
      } else {
        instr.length = null;
      }
 
      instr.name = name;
      instr.dutyCycle = duty;
      instr.initialVolume = initialVolume;
      instr.volumeSweepChange = volumeSweepAmount;
 
      instr.frequencySweepTime = freqSweepTime;
      instr.frequencySweepShift = freqSweepShift;
 
      if (version >= 6) {
        instr.subpatternEnabled = subpatternEnabled !== 0;
        instr.subpattern = subpattern;
      } else {
        instr.subpatternEnabled = false;
        instr.subpattern = [...Array(64)].map(() => createSubPatternCell());
      }
 
      dutyInstrumentMapping[(idx % 15) + 1] = song.dutyInstruments.length;
      addDutyInstrument(song, instr);
    } else if (type === 1) {
      const instr = {} as WaveInstrument;
 
      if (lengthEnabled) {
        instr.length = 256 - length;
      } else {
        instr.length = null;
      }
 
      instr.name = name;
      instr.volume = waveOutputLevel;
      instr.waveIndex = waveWaveformIndex;
 
      if (version >= 6) {
        instr.subpatternEnabled = subpatternEnabled !== 0;
        instr.subpattern = subpattern;
      } else {
        instr.subpatternEnabled = false;
        instr.subpattern = [...Array(64)].map(() => createSubPatternCell());
      }
 
      waveInstrumentMapping[(idx % 15) + 1] = song.waveInstruments.length;
      addWaveInstrument(song, instr);
    } else if (type === 2) {
      const instr = {} as NoiseInstrument;
 
      if (lengthEnabled) {
        instr.length = 64 - length;
      } else {
        instr.length = null;
      }
 
      instr.name = name;
      instr.initialVolume = initialVolume;
      instr.volumeSweepChange = volumeSweepAmount;
      instr.bitCount = noiseCounterStep ? 7 : 15;
 
      if (version >= 6) {
        instr.subpatternEnabled = subpatternEnabled !== 0;
        instr.subpattern = subpattern;
      } else {
        /* 
          Older versions of the uge format had a noise macro field for the noise instrument that needs to be migrated to the subpattern.
        */
        Iif (noiseMacro.length === 0) {
          // if noise macro is empty create an empty subpattern and disable
          // subpattern for this instrument
          instr.subpatternEnabled = false;
          instr.subpattern = [...Array(64)].map(() => createSubPatternCell());
        } else {
          // if noise macro is not empty migrate to the subpattern
          instr.subpatternEnabled = true;
          instr.subpattern = subpatternFromNoiseMacro(
            noiseMacro ?? [],
            song.ticksPerRow,
          );
        }
      }
 
      noiseInstrumentMapping[(idx % 15) + 1] = song.noiseInstruments.length;
      addNoiseInstrument(song, instr);
    } else E{
      throw Error(`Invalid instrument type ${type} [${idx}, "${name}"]`);
    }
  });
 
  song.patterns = Array.from(patterns).map((patternData) => {
    const pattern = createPattern();
    if (!patternData) {
      return pattern;
    }
 
    const rowCount = Math.min(patternData.length, pattern.length);
 
    for (let i = 0; i < rowCount; i++) {
      const cell = pattern[i];
      const [note, instrument, effectCode, effectParam] = patternData[i];
      if (note !== 90) {
        cell.note = note;
      }
      Iif (instrument !== 0) {
        cell.instrument = instrument - 1;
      }
      Iif (effectCode !== 0 || effectParam !== 0) {
        cell.effectCode = effectCode;
        cell.effectParam = effectParam;
      }
    }
 
    return pattern;
  });
 
  song.sequence = Array.from(orders[0]).map((value, index) => {
    const channels: [number, number, number, number] = [
      orders[0][index],
      orders[1][index],
      orders[2][index],
      orders[3][index],
    ];
    return {
      splitPattern: isSplitPatternSequence(channels),
      channels,
    };
  });
 
  return compactUGESong(song);
};
 
/**
 * Serialises a Song object into a `.uge` binary buffer that is compatible
 * with hUGETracker version 6.
 */
export const saveUGESong = (rawSong: Song): Buffer => {
  const song = compactUGESong(rawSong);
  const buffer = new ArrayBuffer(1024 * 1024);
  const view = new DataView(buffer);
  let idx = 0;
 
  function addUint8(value: number) {
    view.setUint8(idx, value);
    idx += 1;
  }
  function addUint32(value: number) {
    view.setUint32(idx, value, true);
    idx += 4;
  }
  function addInt8(value: number) {
    view.setInt8(idx, value);
    idx += 1;
  }
  function addShortString(s: string) {
    view.setUint8(idx, s.length);
    idx += 1;
    const te = new TextEncoder();
    te.encodeInto(s, new Uint8Array(buffer, idx, idx + 255));
    idx += 255;
  }
 
  function addSubpattern(i: DutyInstrument | WaveInstrument | NoiseInstrument) {
    addInt8(i.subpatternEnabled ? 1 : 0);
    for (let n = 0; n < 64; n++) {
      const subpattern = i.subpattern[n];
      addUint32(subpattern?.note ?? 90);
      addUint32(0);
      addUint32(subpattern?.jump ?? 0);
      addUint32(subpattern?.effectCode ?? 0);
      addUint8(subpattern?.effectParam ?? 0);
    }
  }
  function addDutyInstrument(type: number, i: DutyInstrument) {
    addUint32(type);
 
    addShortString(i.name || "");
    addUint32(i.length !== null ? 64 - i.length : 0);
    addUint8(i.length === null ? 0 : 1);
    addUint8(i.initialVolume);
    addUint32(i.volumeSweepChange < 0 ? 1 : 0);
    addUint8(i.volumeSweepChange !== 0 ? 8 - Math.abs(i.volumeSweepChange) : 0);
 
    addUint32(i.frequencySweepTime);
    addUint32(i.frequencySweepShift < 0 ? 1 : 0);
    addUint32(Math.abs(i.frequencySweepShift));
 
    addUint8(i.dutyCycle);
 
    addUint32(0);
    addUint32(0);
 
    addUint32(0);
 
    addSubpattern(i);
  }
 
  function addWaveInstrument(type: number, i: WaveInstrument) {
    addUint32(type);
 
    addShortString(i.name || "");
    addUint32(i.length !== null ? 256 - i.length : 0);
    addUint8(i.length === null ? 0 : 1);
    addUint8(0);
    addUint32(0);
    addUint8(0);
 
    addUint32(0);
    addUint32(0);
    addUint32(0);
 
    addUint8(0);
 
    addUint32(i.volume);
    addUint32(i.waveIndex);
 
    addUint32(0);
 
    addSubpattern(i);
  }
 
  function addNoiseInstrument(type: number, i: NoiseInstrument) {
    addUint32(type);
 
    addShortString(i.name || "");
    addUint32(i.length !== null ? 64 - i.length : 0);
    addUint8(i.length === null ? 0 : 1);
    addUint8(i.initialVolume);
    addUint32(i.volumeSweepChange < 0 ? 1 : 0);
    addUint8(i.volumeSweepChange !== 0 ? 8 - Math.abs(i.volumeSweepChange) : 0);
 
    addUint32(0);
    addUint32(0);
    addUint32(0);
 
    addUint8(0);
 
    addUint32(0);
    addUint32(0);
 
    addUint32(i.bitCount === 7 ? 1 : 0);
 
    addSubpattern(i);
  }
 
  addUint32(6); // version
  addShortString(song.name);
  addShortString(song.artist);
  addShortString(song.comment);
 
  for (let n = 0; n < 15; n++) {
    addDutyInstrument(0, song.dutyInstruments[n] || {});
  }
  for (let n = 0; n < 15; n++) {
    addWaveInstrument(1, song.waveInstruments[n] || {});
  }
  for (let n = 0; n < 15; n++) {
    addNoiseInstrument(2, song.noiseInstruments[n] || {});
  }
  for (let n = 0; n < 16; n++) {
    for (let m = 0; m < 32; m++) {
      addUint8(song.waves[n] ? song.waves[n][m] : 0);
    }
  }
  addUint32(song.ticksPerRow);
 
  addInt8(song.timerEnabled ? 1 : 0);
 
  addUint32(song.timerDivider);
 
  addUint32(song.patterns.length);
  let patternKey = 0;
  for (const pattern of song.patterns) {
    addUint32(patternKey++);
    for (let m = 0; m < 64; m++) {
      const t = pattern[m];
      addUint32(t.note === null ? 90 : t.note);
      addUint32(t.instrument === null ? 0 : t.instrument + 1);
      addUint32(0);
      addUint32(t.effectCode === null ? 0 : t.effectCode);
      addUint8(t.effectParam === null ? 0 : t.effectParam);
    }
  }
  for (let track = 0; track < 4; track++) {
    addUint32(song.sequence.length + 1); //amount of "orders" in a uge file has an off-by-one
    for (const i of song.sequence) {
      addUint32(i.channels[track]);
    }
    addUint32(0); // add the off-by-one error
  }
  for (let n = 0; n < 16; n++) {
    addUint32(0); //Add empty routines
  }
 
  return Buffer.from(buffer.slice(0, idx));
};
 
/**
 * Compact a Song removing all unreferenced patterns
 */
export const compactUGESong = (song: Song): Song => {
  const usedBlockIds = new Set<number>();
 
  for (const sequenceItem of song.sequence) {
    for (const patternId of sequenceItem.channels) {
      usedBlockIds.add(Math.floor(patternId / TRACKER_NUM_CHANNELS));
    }
  }
 
  const nextPatterns: Pattern[] = [];
  const patternIdMap = new Map<number, number>();
 
  const blockCount = Math.ceil(song.patterns.length / TRACKER_NUM_CHANNELS);
 
  for (let blockId = 0; blockId < blockCount; blockId++) {
    if (!usedBlockIds.has(blockId)) {
      continue;
    }
 
    const oldBasePatternId = blockId * TRACKER_NUM_CHANNELS;
    const newBasePatternId = nextPatterns.length;
 
    for (
      let channelOffset = 0;
      channelOffset < TRACKER_NUM_CHANNELS;
      channelOffset++
    ) {
      const oldPatternId = oldBasePatternId + channelOffset;
      const sourcePattern = song.patterns[oldPatternId];
 
      if (!sourcePattern) {
        break;
      }
 
      patternIdMap.set(oldPatternId, newBasePatternId + channelOffset);
      nextPatterns.push(
        sourcePattern.map((cell) => ({
          ...cell,
        })) as (typeof song.patterns)[number],
      );
    }
  }
 
  return {
    ...song,
    patterns: nextPatterns,
    sequence: song.sequence.map((sequenceItem) => ({
      ...sequenceItem,
      channels: sequenceItem.channels.map((patternId) => {
        const mappedPatternId = patternIdMap.get(patternId);
 
        Iif (mappedPatternId === undefined) {
          throw new Error(`Unable to remap UGE pattern ${patternId}`);
        }
 
        return mappedPatternId;
      }) as typeof sequenceItem.channels,
    })),
  };
};
 
/**
 * Determine if two patterns contain identical note data
 */
const patternEqual = function (a: PatternCell[], b: PatternCell[]) {
  Iif (a.length !== b.length) return false;
  for (let idx = 0; idx < a.length; idx++) {
    if (a[idx].note !== b[idx].note) return false;
    if (a[idx].instrument !== b[idx].instrument) return false;
    Iif (a[idx].effectCode !== b[idx].effectCode) return false;
    Iif (a[idx].effectParam !== b[idx].effectParam) return false;
  }
  return true;
};
 
/**
 * Exports a Song to a GBDK-compatible C source file. The output contains
 * instrument definitions, wave data, patterns, and sequence data ready for
 * inclusion in a GB Studio game ROM.
 *
 * @param song - The song to export.
 * @param trackName - C identifier used as the base name for exported symbols.
 */
export const exportToC = (song: Song, trackName: string): string => {
  type InstrumentType = "duty" | "wave" | "noise";
 
  const decHex = (n: number, maxLength = 2) => {
    return "0x" + n.toString(16).toUpperCase().padStart(maxLength, "0");
  };
 
  const findPattern = function (pattern: PatternCell[]) {
    for (let idx = 0; idx < patterns.length; idx++) {
      if (patternEqual(pattern, patterns[idx])) return idx;
    }
    return null;
  };
 
  const getSequenceMappingFor = function (track: number) {
    return song.sequence
      .map((sequence) => `song_pattern_${patternMap[sequence.channels[track]]}`)
      .join(", ");
  };
 
  const formatPatternCell = function (cell: PatternCell) {
    const note = cell.note !== null ? noteGBDKDefines[cell.note] : "___";
    let instrument = 0;
    let effectCode = 0;
    let effectParam = 0;
    if (cell.instrument !== null) instrument = cell.instrument + 1;
    Iif (cell.effectCode !== null) {
      effectCode = cell.effectCode;
      effectParam = cell.effectParam || 0;
    }
    return `DN(${note}, ${instrument}, ${decHex(
      (effectCode << 8) | effectParam,
      3,
    )})`;
  };
 
  const formatSubPatternCell = function (
    cell: SubPatternCell,
    isLast: boolean,
  ) {
    const note = cell.note ?? "___";
    const jump = cell.jump !== null && isLast ? 1 : (cell.jump ?? 0);
    let effectCode = 0;
    let effectParam = 0;
    Iif (cell.effectCode !== null) {
      effectCode = cell.effectCode;
      effectParam = cell.effectParam || 0;
    }
    return `DN(${note}, ${jump}, ${decHex(
      (effectCode << 8) | effectParam,
      3,
    )})`;
  };
 
  const getUsedInstrumentIndexes = function () {
    const usedInstruments: Record<InstrumentType, Set<number>> = {
      duty: new Set(),
      wave: new Set(),
      noise: new Set(),
    };
 
    const instrumentTypeByTrack: InstrumentType[] = [
      "duty",
      "duty",
      "wave",
      "noise",
    ];
 
    for (const sequenceItem of song.sequence) {
      for (let track = 0; track < TRACKER_NUM_CHANNELS; track++) {
        const pattern = song.patterns[sequenceItem.channels[track]];
 
        Iif (!pattern) {
          continue;
        }
 
        const instrumentType = instrumentTypeByTrack[track];
 
        for (const cell of pattern) {
          if (cell.note !== null && cell.instrument !== null) {
            usedInstruments[instrumentType].add(cell.instrument);
          }
        }
      }
    }
 
    return usedInstruments;
  };
 
  const formatDutyInstrument = function (instr: DutyInstrument) {
    const sweep =
      (instr.frequencySweepTime << 4) |
      (instr.frequencySweepShift < 0 ? 0x08 : 0x00) |
      Math.abs(instr.frequencySweepShift);
    const lenDuty =
      (instr.dutyCycle << 6) |
      ((instr.length !== null ? 64 - instr.length : 0) & 0x3f);
    let envelope =
      (instr.initialVolume << 4) | (instr.volumeSweepChange > 0 ? 0x08 : 0x00);
    if (instr.volumeSweepChange !== 0) {
      envelope |= 8 - Math.abs(instr.volumeSweepChange);
    }
    const subpatternRef = dutySubpatternRefs.get(instr.index) ?? 0;
    const highmask = 0x80 | (instr.length !== null ? 0x40 : 0);
 
    return `{ ${decHex(sweep)}, ${decHex(lenDuty)}, ${decHex(
      envelope,
    )}, ${subpatternRef}, ${decHex(highmask)} }`;
  };
 
  const formatWaveInstrument = function (instr: WaveInstrument) {
    const length = (instr.length !== null ? 256 - instr.length : 0) & 0xff;
    const volume = instr.volume << 5;
    const waveform = instr.waveIndex;
    const subpatternRef = waveSubpatternRefs.get(instr.index) ?? 0;
    const highmask = 0x80 | (instr.length !== null ? 0x40 : 0);
 
    return `{ ${decHex(length)}, ${decHex(volume)}, ${decHex(
      waveform,
    )}, ${subpatternRef}, ${decHex(highmask)} }`;
  };
 
  const formatNoiseInstrument = function (instr: NoiseInstrument) {
    let envelope =
      (instr.initialVolume << 4) | (instr.volumeSweepChange > 0 ? 0x08 : 0x00);
    if (instr.volumeSweepChange !== 0)
      envelope |= 8 - Math.abs(instr.volumeSweepChange);
    const subpatternRef = noiseSubpatternRefs.get(instr.index) ?? 0;
    let highmask = (instr.length !== null ? 64 - instr.length : 0) & 0x3f;
    if (instr.length !== null) highmask |= 0x40;
    if (instr.bitCount === 7) highmask |= 0x80;
 
    return `{ ${decHex(envelope)}, ${subpatternRef}, ${decHex(
      highmask,
    )}, 0, 0 }`;
  };
 
  const formatWave = function (wave: Uint8Array) {
    return Array.from(Array(16).keys(), (n) =>
      decHex((wave[n * 2] << 4) | wave[n * 2 + 1]),
    ).join(", ");
  };
 
  // Load patterns
  const patterns: PatternCell[][] = [];
  const patternMap: { [key: string]: number } = {};
 
  for (let n = 0; n < song.patterns.length; n++) {
    const sourcePattern = song.patterns[n];
    const targetPattern = [];
    for (let m = 0; m < sourcePattern.length; m++) {
      targetPattern.push(sourcePattern[m]);
    }
    const idx = findPattern(targetPattern);
    if (idx !== null) {
      patternMap[n] = idx;
    } else {
      patternMap[n] = patterns.length;
      patterns.push(targetPattern);
    }
  }
 
  const usedInstrumentIndexes = getUsedInstrumentIndexes();
 
  const emittedSubpatterns = new Map<string, string>();
  const dutySubpatternRefs = new Map<number, string>();
  const waveSubpatternRefs = new Map<number, string>();
  const noiseSubpatternRefs = new Map<number, string>();
  const subpatternDefinitions: string[] = [];
 
  const registerSubpattern = (
    instr: DutyInstrument | WaveInstrument | NoiseInstrument,
    type: InstrumentType,
  ) => {
    if (
      !instr.subpatternEnabled ||
      !usedInstrumentIndexes[type].has(instr.index)
    ) {
      return;
    }
 
    const subpatternKey = JSON.stringify(instr.subpattern);
    let subpatternSymbol = emittedSubpatterns.get(subpatternKey);
 
    if (!subpatternSymbol) {
      subpatternSymbol = `subpattern_${emittedSubpatterns.size}`;
      emittedSubpatterns.set(subpatternKey, subpatternSymbol);
 
      let definition = `static const unsigned char ${subpatternSymbol}[] = {\n`;
      for (let idx = 0; idx < 32; idx++) {
        definition += `    ${formatSubPatternCell(
          instr.subpattern[idx],
          idx === 32 - 1,
        )},\n`;
      }
      definition += "};\n";
      subpatternDefinitions.push(definition);
    }
 
    if (type === "duty") {
      dutySubpatternRefs.set(instr.index, subpatternSymbol);
    } else Eif (type === "wave") {
      waveSubpatternRefs.set(instr.index, subpatternSymbol);
    } else {
      noiseSubpatternRefs.set(instr.index, subpatternSymbol);
    }
  };
 
  for (const instr of song.dutyInstruments) {
    registerSubpattern(instr, "duty");
  }
  for (const instr of song.waveInstruments) {
    registerSubpattern(instr, "wave");
  }
  for (const instr of song.noiseInstruments) {
    registerSubpattern(instr, "noise");
  }
 
  let data = `#pragma bank 255
 
#include "hUGEDriver.h"
#include <stddef.h>
#include "hUGEDriverRoutines.h"
 
static const unsigned char order_cnt = ${song.sequence.length * 2};
`;
 
  for (let idx = 0; idx < patterns.length; idx++) {
    data += `static const unsigned char song_pattern_${idx}[] = {\n`;
    for (const cell of patterns[idx]) {
      data += `    ${formatPatternCell(cell)},\n`;
    }
    data += "};\n";
  }
  for (const definition of subpatternDefinitions) {
    data += definition;
  }
  for (let track = 0; track < 4; track++)
    data += `static const unsigned char* const order${
      track + 1
    }[] = {${getSequenceMappingFor(track)}};\n`;
  data += "static const hUGEDutyInstr_t duty_instruments[] = {\n";
  for (const instr of song.dutyInstruments) {
    data += `    ${formatDutyInstrument(instr)},\n`;
  }
  data += "};\n";
  data += "static const hUGEWaveInstr_t wave_instruments[] = {\n";
  for (const instr of song.waveInstruments) {
    data += `    ${formatWaveInstrument(instr)},\n`;
  }
  data += "};\n";
  data += "static const hUGENoiseInstr_t noise_instruments[] = {\n";
  for (const instr of song.noiseInstruments) {
    data += `    ${formatNoiseInstrument(instr)},\n`;
  }
  data += "};\n";
  //data += "static const unsigned char routines[] = {\n";
  //TODO
  //data += "};\n";
  data += "static const unsigned char waves[] = {\n";
  for (const wave of song.waves) {
    data += `    ${formatWave(wave)},\n`;
  }
  data += "};\n";
 
  data += `
const void __at(255) __bank_${trackName}_Data;
const hUGESong_t ${trackName}_Data = {
    ${song.ticksPerRow},
    &order_cnt,
    order1, order2, order3, order4,
    duty_instruments, wave_instruments, noise_instruments,
    routines,
    waves
};
`;
  return data;
};
 
const subpatternFromNoiseMacro = function (
  noiseMacro: number[],
  ticksPerRow: number,
) {
  const subpattern = [...Array(64)].map(() => createSubPatternCell());
  for (let n = 0; n < 6; n++) {
    subpattern[n + 1].note = noiseMacro[n] + 36;
  }
  const wrapPoint = Math.min(ticksPerRow, 7);
  subpattern[wrapPoint - 1].jump = wrapPoint;
  return subpattern;
};