/**
 * Description: This library is able to play soundfonts (sf2 files) with the Web Audio API..
 *
 * @package     midi-Synth
 * @category    module js
 * @author      felixroos <https://github.com/felixroos/sfumato>
 * @copyright   sfumato
 * @license     GPL-2.0-or-later
 */

const tokenizeNote = note => {
  if (typeof note !== "string") {
    return []
  }
  const [pc, acc = "", oct] =
    note.match(/^([a-gA-G])([#bs]*)([0-9])?$/)?.slice(1) || []
  if (!pc) {
    return []
  }
  return [pc, acc, oct ? Number(oct) : undefined]
}
const accs = { "#": 1, b: -1, s: 1 }

// turns the given note into its midi number representation
const toMidi = note => {
  if (typeof note === "number") {
    return note
  }
  const [pc, acc, oct] = tokenizeNote(note)
  if (!pc) {
    throw new Error('not a note: "' + note + '"')
  }
  const chroma = { c: 0, d: 2, e: 4, f: 5, g: 7, a: 9, b: 11 }[pc.toLowerCase()]
  const offset = acc?.split("").reduce((o, char) => o + accs[char], 0) || 0
  return (Number(oct) + 1) * 12 + chroma + offset
}

// timecents to seconds
const tc2s = timecents => Math.pow(2, timecents / 1200)
// seconds to timecents
const s2tc = seconds => Math.round(Math.log2(seconds) * 1200)
const normalizePermille = permille => permille / 1000

const precision = (n, digits) => {
  const factor = Math.pow(10, digits)
  return Math.round(n * factor) / factor
}
