All files / src/store/features/trackerDocument trackerDocumentHelpers.ts

73.87% Statements 82/111
40% Branches 14/35
66.67% Functions 10/15
70.97% Lines 66/93

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 28537x             37x   37x 37x               37x 5x     37x                           37x         2x 2x 2x 1x   1x     37x     37x           37x                                     37x 448x             37x                                                               37x     2x 2x   2x 5x 5x     5x   5x 2x   3x   3x           2x     37x                                                                 37x       4x 1x     3x 1x     2x             37x       4x 4x                         37x       5x 5x   5x 14x 14x     14x   14x 6x   8x   8x             5x                             37x       2x 2x   2x 4x   4x 4x 4x   4x 1x   3x   3x               2x    
import {
  OCTAVE_SIZE,
  TRACKER_CHANNEL_FIELDS,
  TRACKER_NUM_CHANNELS,
  TRACKER_PATTERN_LENGTH,
  TRACKER_ROW_SIZE,
} from "consts";
import { createPatternCell } from "shared/lib/uge/song";
import { Pattern, PatternCell, SequenceItem, Song } from "shared/lib/uge/types";
import { toValidChannelId } from "shared/lib/uge/editor/helpers";
import { transposeNoteValue } from "shared/lib/uge/display";
 
interface AbsRowPosition {
  sequenceId: number;
  rowId: number;
}
 
/** Converts a (sequenceId, rowId) pair to a single absolute row index. */
export const toAbsRow = (sequenceId: number, rowId: number) =>
  sequenceId * TRACKER_PATTERN_LENGTH + rowId;
 
/** Splits an absolute row index back into its (sequenceId, rowId) components. */
export const fromAbsRow = (absRow: number): AbsRowPosition => ({
  sequenceId: Math.floor(absRow / TRACKER_PATTERN_LENGTH),
  rowId: absRow % TRACKER_PATTERN_LENGTH,
});
 
interface ResolvedAbsRow extends AbsRowPosition {
  patternId: number;
}
 
/**
 * Resolves an absolute row index against the song sequence, returning the
 * sequenceId, rowId, and patternId. Returns null when the sequence slot is
 * out of bounds.
 */
export const resolveAbsRow = (
  sequence: SequenceItem[],
  absRow: number,
  channelId: number,
): ResolvedAbsRow | null => {
  const { sequenceId, rowId } = fromAbsRow(absRow);
  const patternId = sequence[sequenceId]?.channels[channelId];
  if (patternId === undefined) {
    return null;
  }
  return { sequenceId, rowId, patternId };
};
 
export const getPatternBlockCount = (patterns: Pattern[] | undefined): number =>
  Math.ceil((patterns?.length ?? 0) / TRACKER_NUM_CHANNELS);
 
export const getSequenceChannelPatternId = (
  song: Song,
  sequenceId: number,
  channelId: number,
): number | undefined => song.sequence[sequenceId]?.channels[channelId];
 
export const getSequenceChannelCell = (
  song: Song,
  sequenceId: number,
  channelId: number,
  rowId: number,
): { patternId: number; cell: PatternCell } | null => {
  const patternId = getSequenceChannelPatternId(song, sequenceId, channelId);
  Iif (patternId === undefined) {
    return null;
  }
 
  const cell = song.patterns[patternId]?.[rowId];
  Iif (!cell) {
    return null;
  }
 
  return { patternId, cell };
};
 
export const createPatternMatrix = (): PatternCell[][] =>
  Array.from({ length: TRACKER_PATTERN_LENGTH }, () => [
    createPatternCell(),
    createPatternCell(),
    createPatternCell(),
    createPatternCell(),
  ]);
 
export const buildSequencePattern = (
  song: Song,
  sequenceId: number,
): PatternCell[][] => {
  const pattern = createPatternMatrix();
 
  for (let rowId = 0; rowId < TRACKER_PATTERN_LENGTH; rowId++) {
    for (let channelId = 0; channelId < TRACKER_NUM_CHANNELS; channelId++) {
      const resolved = getSequenceChannelCell(
        song,
        sequenceId,
        channelId,
        rowId,
      );
      pattern[rowId][channelId] = resolved
        ? { ...resolved.cell }
        : createPatternCell();
    }
  }
 
  return pattern;
};
 
interface ResolvedTrackerPosition {
  rowIndex: number;
  channelIndex: 0 | 1 | 2 | 3;
}
 
interface ResolvedTrackerPositionField extends ResolvedTrackerPosition {
  fieldIndex: number;
}
 
export const resolveUniqueTrackerPositions = (
  selectedTrackerFields: number[],
): ResolvedTrackerPosition[] => {
  const seen = new Set<string>();
  const resolvedCells: ResolvedTrackerPosition[] = [];
 
  for (const field of selectedTrackerFields) {
    const rowIndex = Math.floor(field / TRACKER_ROW_SIZE);
    const channelIndex = toValidChannelId(
      Math.floor(field / TRACKER_CHANNEL_FIELDS) % TRACKER_NUM_CHANNELS,
    );
    const key = `${rowIndex}:${channelIndex}`;
 
    if (seen.has(key)) {
      continue;
    }
    seen.add(key);
 
    resolvedCells.push({
      rowIndex,
      channelIndex,
    });
  }
 
  return resolvedCells;
};
 
export const resolveTrackerFieldPositions = (
  selectedTrackerFields: number[],
): ResolvedTrackerPositionField[] => {
  const seen = new Set<string>();
  const resolvedCells: ResolvedTrackerPositionField[] = [];
 
  for (const field of selectedTrackerFields) {
    const rowIndex = Math.floor(field / TRACKER_ROW_SIZE);
    const channelIndex = toValidChannelId(
      Math.floor(field / TRACKER_CHANNEL_FIELDS) % TRACKER_NUM_CHANNELS,
    );
    const fieldIndex = field % TRACKER_CHANNEL_FIELDS;
    const key = `${rowIndex}:${channelIndex}:${fieldIndex}`;
 
    Iif (seen.has(key)) {
      continue;
    }
    seen.add(key);
 
    resolvedCells.push({
      rowIndex,
      channelIndex,
      fieldIndex,
    });
  }
 
  return resolvedCells;
};
 
/**
 * Mutates a PatternCell's note in-place by transposing it by `noteDelta`
 * semitones. No-ops when the cell is undefined or its note is null.
 */
export const transposePatternCellNote = (
  cell: PatternCell | undefined,
  noteDelta: number,
) => {
  if (!cell) {
    return;
  }
 
  if (cell.note === null) {
    return;
  }
 
  cell.note = transposeNoteValue(cell.note, noteDelta);
};
 
/**
 * Returns the semitone delta for a transpose operation.
 * "octave" uses ±12 semitones; "note" uses ±1.
 */
export const getTransposeNoteDelta = (
  direction: "up" | "down",
  size: "note" | "octave",
) => {
  const deltaBase = direction === "up" ? 1 : -1;
  return size === "octave" ? deltaBase * OCTAVE_SIZE : deltaBase;
};
 
interface ResolvedTrackerCell {
  patternId: number;
  rowIndex: number;
  channelIndex: 0 | 1 | 2 | 3;
}
 
/**
 * Converts a list of tracker field indices into unique (patternId, rowIndex,
 * channelIndex) tuples, deduplicating fields that map to the same cell.
 */
export const resolveUniqueTrackerCells = (
  patternId: number,
  selectedTrackerFields: number[],
): ResolvedTrackerCell[] => {
  const seen = new Set<string>();
  const resolvedCells: ResolvedTrackerCell[] = [];
 
  for (const field of selectedTrackerFields) {
    const rowIndex = Math.floor(field / TRACKER_ROW_SIZE);
    const channelIndex = toValidChannelId(
      Math.floor(field / TRACKER_CHANNEL_FIELDS) % TRACKER_NUM_CHANNELS,
    );
    const key = `${patternId}:${rowIndex}:${channelIndex}`;
 
    if (seen.has(key)) {
      continue;
    }
    seen.add(key);
 
    resolvedCells.push({
      patternId,
      rowIndex,
      channelIndex,
    });
  }
 
  return resolvedCells;
};
 
interface ResolvedTrackerCellField {
  patternId: number;
  rowIndex: number;
  channelIndex: number;
  fieldIndex: number;
}
 
/**
 * Converts a list of tracker field indices into unique (patternId, rowIndex,
 * channelIndex, fieldIndex) tuples, preserving individual cell field granularity
 * and deduplicating exact field positions.
 */
export const resolveTrackerCellFields = (
  patternId: number,
  selectedTrackerFields: number[],
): ResolvedTrackerCellField[] => {
  const seen = new Set<string>();
  const resolvedCells: ResolvedTrackerCellField[] = [];
 
  for (const field of selectedTrackerFields) {
    const rowIndex = Math.floor(field / TRACKER_ROW_SIZE);
    const channelIndex =
      Math.floor(field / TRACKER_CHANNEL_FIELDS) % TRACKER_NUM_CHANNELS;
    const fieldIndex = field % TRACKER_CHANNEL_FIELDS;
    const key = `${patternId}:${rowIndex}:${channelIndex}:${fieldIndex}`;
 
    if (seen.has(key)) {
      continue;
    }
    seen.add(key);
 
    resolvedCells.push({
      patternId,
      rowIndex,
      channelIndex,
      fieldIndex,
    });
  }
 
  return resolvedCells;
};