All files / src/shared/lib/uge/song Song.ts

40.32% Statements 25/62
0% Branches 0/10
66.67% Functions 4/6
41.67% Lines 25/60

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                30x   30x                                 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x   4x 4x   4x 4x       60x 60x 60x       60x 60x 60x       60x 60x 60x                                                                                                                              
/* eslint-disable camelcase */
import type {
  DutyInstrument,
  NoiseInstrument,
  WaveInstrument,
} from "store/features/trackerDocument/trackerDocumentTypes";
import { PatternCell } from "./PatternCell";
 
const LAST_VERSION = 6;
 
export class Song {
  version: number;
  name: string;
  artist: string;
  comment: string;
  filename: string;
  duty_instruments: DutyInstrument[];
  wave_instruments: WaveInstrument[];
  noise_instruments: NoiseInstrument[];
  waves: Uint8Array[];
  ticks_per_row: number;
  timer_enabled: boolean;
  timer_divider: number;
  patterns: PatternCell[][][];
  sequence: number[];
 
  constructor() {
    this.version = LAST_VERSION;
    this.name = "";
    this.artist = "";
    this.comment = "";
    this.filename = "song";
 
    this.duty_instruments = [];
    this.wave_instruments = [];
    this.noise_instruments = [];
    this.waves = [];
    this.ticks_per_row = 6;
 
    this.timer_enabled = false;
    this.timer_divider = 0;
 
    this.patterns = [];
    this.sequence = [];
  }
 
  addDutyInstrument(instrument: DutyInstrument) {
    const list = this.duty_instruments;
    instrument.index = list.length;
    list.push(instrument);
  }
 
  addWaveInstrument(instrument: WaveInstrument) {
    const list = this.wave_instruments;
    instrument.index = list.length;
    list.push(instrument);
  }
 
  addNoiseInstrument(instrument: NoiseInstrument) {
    const list = this.noise_instruments;
    instrument.index = list.length;
    list.push(instrument);
  }
 
  usesInstrument(type: string, index: number) {
    let cols: number[] = [];
    Iif (type === "duty") {
      cols = [0, 1];
    }
    Iif (type === "wave") {
      cols = [2];
    }
    Iif (type === "noise") {
      cols = [3];
    }
 
    for (const pattern of this.patterns) {
      for (const row of pattern) {
        for (const col of cols) {
          Iif (row[col].instrument === index) return true;
        }
      }
    }
    return false;
  }
 
  removeInstrument(type: string, index: number) {
    let list: (DutyInstrument | WaveInstrument | NoiseInstrument)[] = [];
    let cols: number[] = [];
    Iif (type === "duty") {
      list = this.duty_instruments;
      cols = [0, 1];
    }
    Iif (type === "wave") {
      list = this.wave_instruments;
      cols = [2];
    }
    Iif (type === "noise") {
      list = this.noise_instruments;
      cols = [3];
    }
 
    for (const pattern of this.patterns) {
      for (const row of pattern) {
        for (const col of cols) {
          const instrumentIndex = row[col].instrument;
          Iif (instrumentIndex) {
            Iif (instrumentIndex === index) {
              row[col].instrument = null;
            }
            Iif (instrumentIndex > index) {
              row[col].instrument = instrumentIndex - 1;
            }
          }
        }
      }
    }
 
    list.splice(index, 1);
    for (let idx = 0; idx < list.length; idx++) {
      list[idx].index = idx;
    }
  }
}