Search code examples
javascriptnotationpitch

Convert string to pitch notation in javascript


I have for ex. string t0 means in treble clef on 0 position. In pitch notation is B4

So t1 = C5 , t-1 = A4 , t-2 = G4...

Should I create every single string in array to map all notes, or could be done easily? Thx.


Solution

  • Is this what you're looking for?

    let curT = -22;
    const letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
    let result = {};
    
    for (let i=0; i < 52; i++) {
      const letNum = `${letters[i % 7]}${(parseInt(i / 7) + 1)}`;
      result[`t${curT}`] = letNum;
      curT += 1;
    }
    console.log(result);