Skip to content

Commit

Permalink
split notation mappings into seperate Octaves
Browse files Browse the repository at this point in the history
  • Loading branch information
retrogtx committed Dec 2, 2024
1 parent fdbef2f commit 2344dd2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
1 change: 1 addition & 0 deletions abc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

57 changes: 32 additions & 25 deletions js/abc.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const processABCNotes = function(logo, turtle) {
* @param {string|number} note - The musical note to convert. It can be a string note (e.g., 'C#') or a frequency (number).
* @returns {string} The note converted to ABC notation.
*/

const __toABCnote = (note) => {
// beams -- no space between notes
// ties use ()
Expand All @@ -64,41 +65,47 @@ const processABCNotes = function(logo, turtle) {
// Also, notes must be lowercase.
// And the octave bounday is at C, not A.

const OCTAVE_NOTATION_MAP = {
10: "'''''",
9: "''''",
8: "'''",
7: "''",
6: "'",
5: "",
4: "",
3: ",",
2: ",,",
1: ",,,"
};

const ACCIDENTAL_MAP = {
"♯": "^",
"♭": "_"
};

// Handle frequency conversion
if (typeof note === "number") {
const pitchObj = frequencyToPitch(note);
note = pitchObj[0] + pitchObj[1];
}

const replacements = {
"♯": "^",
"♭": "_",
"10": "'''''",
"9": "''''",
"8": "'''",
"7": "''",
"6": "'",
"5": "",
"4": "",
"3": ",",
"2": ",,",
"1": ",,,"
};
// Handle accidentals first
for (const [symbol, replacement] of Object.entries(ACCIDENTAL_MAP)) {
note = note.replace(new RegExp(symbol, "g"), replacement);
}

for (const [key, value] of Object.entries(replacements)) {
if (note.includes(key)) {
note = note.replace(new RegExp(key, "g"), value);
if (key.length === 1) break;
// Handle octave notation
for (const [octave, notation] of Object.entries(OCTAVE_NOTATION_MAP)) {
if (note.includes(octave)) {
note = note.replace(new RegExp(octave, "g"), notation);
break; // Only one octave notation should apply
}
}

// Convert to uppercase or lowercase based on the octave
if (note.includes("'''") || note.includes("''") || note.includes("'") || note.includes("")) {
return note.toLowerCase();
} else {
return note.toUpperCase();
}
// Convert case based on octave
return note.includes("'") || note === "" ? note.toLowerCase() : note.toUpperCase();
};

let counter = 0;
let queueSlur = false;
let articulation = false;
Expand Down

0 comments on commit 2344dd2

Please sign in to comment.