@@ -54,6 +54,7 @@ const processABCNotes = function(logo, turtle) {
5454 * @param {string|number } note - The musical note to convert. It can be a string note (e.g., 'C#') or a frequency (number).
5555 * @returns {string } The note converted to ABC notation.
5656 */
57+
5758 const __toABCnote = ( note ) => {
5859 // beams -- no space between notes
5960 // ties use ()
@@ -64,41 +65,47 @@ const processABCNotes = function(logo, turtle) {
6465 // Also, notes must be lowercase.
6566 // And the octave bounday is at C, not A.
6667
68+ const OCTAVE_NOTATION_MAP = {
69+ 10 : "'''''" ,
70+ 9 : "''''" ,
71+ 8 : "'''" ,
72+ 7 : "''" ,
73+ 6 : "'" ,
74+ 5 : "" ,
75+ 4 : "" ,
76+ 3 : "," ,
77+ 2 : ",," ,
78+ 1 : ",,,"
79+ } ;
80+
81+ const ACCIDENTAL_MAP = {
82+ "♯" : "^" ,
83+ "♭" : "_"
84+ } ;
85+
86+ // Handle frequency conversion
6787 if ( typeof note === "number" ) {
6888 const pitchObj = frequencyToPitch ( note ) ;
6989 note = pitchObj [ 0 ] + pitchObj [ 1 ] ;
7090 }
7191
72- const replacements = {
73- "♯" : "^" ,
74- "♭" : "_" ,
75- "10" : "'''''" ,
76- "9" : "''''" ,
77- "8" : "'''" ,
78- "7" : "''" ,
79- "6" : "'" ,
80- "5" : "" ,
81- "4" : "" ,
82- "3" : "," ,
83- "2" : ",," ,
84- "1" : ",,,"
85- } ;
92+ // Handle accidentals first
93+ for ( const [ symbol , replacement ] of Object . entries ( ACCIDENTAL_MAP ) ) {
94+ note = note . replace ( new RegExp ( symbol , "g" ) , replacement ) ;
95+ }
8696
87- for ( const [ key , value ] of Object . entries ( replacements ) ) {
88- if ( note . includes ( key ) ) {
89- note = note . replace ( new RegExp ( key , "g" ) , value ) ;
90- if ( key . length === 1 ) break ;
97+ // Handle octave notation
98+ for ( const [ octave , notation ] of Object . entries ( OCTAVE_NOTATION_MAP ) ) {
99+ if ( note . includes ( octave ) ) {
100+ note = note . replace ( new RegExp ( octave , "g" ) , notation ) ;
101+ break ; // Only one octave notation should apply
91102 }
92103 }
93104
94- // Convert to uppercase or lowercase based on the octave
95- if ( note . includes ( "'''" ) || note . includes ( "''" ) || note . includes ( "'" ) || note . includes ( "" ) ) {
96- return note . toLowerCase ( ) ;
97- } else {
98- return note . toUpperCase ( ) ;
99- }
105+ // Convert case based on octave
106+ return note . includes ( "'" ) || note === "" ? note . toLowerCase ( ) : note . toUpperCase ( ) ;
100107 } ;
101-
108+
102109 let counter = 0 ;
103110 let queueSlur = false ;
104111 let articulation = false ;
0 commit comments