export const keys: number[] = [];

for (let i = 0; i < 127; i++) {
  keys.push(i);
}

export const keyNames = [
  ['C'],
  ['C♯', 'D♭'],
  ['D'],
  ['D♯', 'E♭'],
  ['E'],
  ['F'],
  ['F♯', 'G♭'],
  ['G'],
  ['G♯', 'A♭'],
  ['A'],
  ['A♯', 'B♭'],
  ['B'],
];

export const getNoteName = (pitch: number) => {
  const octave = Math.floor(pitch / 12);
  const key = pitch % 12;
  return `${keyNames[key][0]}${octave}`;
};

export const getPitchFromAsciiNoteName = (name: string) => {
  const octave = parseInt(name[name.length - 1]);
  if (Number.isNaN(octave)) return null;
  const key = keyNames.findIndex((k) => k.includes(name.replace('#', '♯').replace('b', '♭').slice(0, -1)));
  if (key === -1) return null;
  return octave * 12 + key;
};

export const getAsciiNoteName = (pitch: number) => {
  return getNoteName(pitch).replace('♯', '#').replace('♭', 'b');
};

export const isBlack = (pitch: number) => [1, 3, 6, 8, 10].includes(pitch % 12);
