// JavaScript Document /* OGNoteNames.js by YourName Installation instructions: Command Line: "Wscript YourScriptsFolder\OGNoteNames.js " Input Type: File Text Under 'Options: Check 'Returns File text' */ // Global variables var HelpTxt = "With this tool you can insert note names into your song file. \r\n\r\n" + "Options:\r\n" + " 1. Act upon all staffs, visible staffs or active staff.\r\n" + " 2. Note names: 'A, B, C...' or 'do, re, mi...'.\r\n" + " 3. Insert as text or lyrics.\r\n"; // Parameters var Testing = false; // enables/disables the function ShowVar. var NPrompts = 3; var InputType = "File"; var ReturnFormat = "File"; // end Parameters var RC=0; // Returncode var FileInput = ""; // Inputstream from STDIN var F=0; //Loopvariable var Lines = new Array(); // Lines from inputstream var L=0; // Index of Lines var CurLine = ""; // Current occurence of array Lines var PrevItem = ""; var CurIndex = 0; // Current item number in the current staff; var Parts = new Array(); // Parts of CurLine after 'splitting' it using delimiter "|". var NLines=new Array(); // OutputLines for STDOUT var LL=0; // Index of NLines var Retain=true; // enables/disables copying a line from STDIN to STDOUT var EmptyMeasure = true; var MeasureNumber = 0; var TSDur = 0; // Duration of a whole measure (calculated via function DuRTimeSig var ActiveStaff = -1; var CurrentStaff = 0; var CaretIndex = 0; var CurrentIndex = 0 var CaretPos = 0; var SelectIndex = -1; var SelectStart = -1; var SelectEnd = -1; //-------------------------------------------- var NoteNames = new Array(); var NoteChars = new Array("C", "D", "E", "F", "G", "A", "B"); var NoteWords = new Array("do", "re", "mi", "fa", "sol", "la", "si"); var InsertAs = ""; var InsertAt = 0; var NoteLyrics = ""; var NoteText = ""; var Clef = ""; var Signature = ""; var KeySigs = new Array(); var Positions = new Array(); var Accidentals = new Array(); var Tie = false; var StaffAction = ""; // "Y" or "N" var LyricsLine = false; var LyricFontSize = 0; // |Text|Text:"g"|Font:StaffItalic|Pos:14 // |Text|Text:"g"|Font:StaffSymbols|Pos:14|Wide:Y|Placement:AtNextNote // End Global Variables // Function Declarations function QuitError(ErrMsg) { WScript.StdErr.Write(ErrMsg); WScript.quit(1); } // end function QuitError function ShowHelp(){ WScript.StdErr.Write(HelpTxt); WScript.quit(1); } // end function ShowHelpFile function ShowVar(Txt, Val) { if (Testing) { WScript.StdErr.Write(Txt + "==> " + Val+ "\r\n"); } } // end function ShowVar function ProcessPrompts(p) { var userprompt = WScript.Arguments.Item(p); switch (p) { case 0: if (userprompt == "Help") ShowHelp(); ActUpon = userprompt.substr(0,3); if ((ActUpon == "Act") || (ActUpon == "Vis") || (ActUpon == "All")) break; QuitError("Invalid 'Act upon: " + userprompt + "'"); case 1: if (userprompt == "A_B_C_..." ){ NoteNames = NoteChars; break;} if (userprompt == "do_re_mi..." ){ NoteNames = NoteWords; break;} QuitError("Invalid NotesFormat: '" + userprompt + "'"); case 2: if (userprompt == "Lyrics" || userprompt == "Text") {InsertAs = userprompt;break;} QuitError("Invalid \"Insert_As\" '" + userprompt + "'"); } } // End function ProcessPrompts function GetProp(prop){; //ShowVar("GetProp", prop); prop += ":"; var pl = prop.length; for (var p = 2; p < Parts.length; p++) { var head = Parts[p].substr(0,pl); if (head == prop) return(Parts[p].slice(pl)); // prop found } // end for return("*"); // prop not found } // end GetProp function SetProp(prop, op, val){; // prop: property to set // op: operator // val: value //ShowVar("SetProp", prop + op + val); var oldval = GetProp(prop); if (oldval == "*" && op != "=") oldval = 0; var newval; if (op != "=") { oldval = parseInt(oldval); val = parseInt(val); } // end if val = parseInt(oldval); switch (op){ case "=" : newval = val; break; case "+" : newval = oldval + val; break; case "-" : newval = oldval - val; break; case "*" : newval = oldval * val; break; case "/" : newval = oldval / val; break; default : QuitError("Invalid operator " + op + " in function SetProp"); } // end switch prop += ":"; var pl = prop.length; for (var p = 2; p < Parts.length; p++) { var head = Parts[p].substr(0,pl); if (head == prop) { // prop found, set val var oldval = Parts[p].slice(pl); Parts[p] = prop + newval; CurLine = Parts.join("|"); return; } // end if } // end for // prop not found, add prop Parts[Parts.length] = prop + newval; CurLine = Parts.join("|"); return; } // end SetProp function InsertItem(array, it){ for(var p = 0; p < array.length; p++) { if (array[p] == it) return; } // end for // pos not found, insert pos array[array.length] = it; } // end InsertItem function InsertItemAt(array, it, at){ for(var p = array.length; p > at; p--) array[p] = array[p-1]; array[at] = it return; } // end InsertItemAt function SearchItem(array, it){ for(var p = 0; p < array.length; p++) { if (array[p] == it) return(p); } // end for // pos not found, return -1 return(-1); } // end SearchItem function RemoveItem (array, it) { var al = array.length for(var p = 0; p < al; p++) { if (array[p] == it) { for (var ak = p; ak < al -1; ak++) { array[ak] = array[ak+1]; } // end for ak array.length -= 1; } // end if } // end for p } // end RemoveItem function CalculateNoteInd (clef, pos) { var val = 0; var note = new Array("C", "D", "E", "F", "G", "A", "B"); switch (clef) { // position of middle c case "Bass": val = 6; break; case "Treble": val = -6; break; case "Alto": val = 0; break; case "Tenor": val = 2; break; case "Percussion": val = 6; break; }// end switch (clef) var ind = (pos - val) % 7; if (ind < 0) ind += 7; return ind; } // end CalculateNoteName function GetPositions () { var mp = 0; Accidentals.length = 0; Positions.length = 0; var posprop = new Array("Pos","Pos2"); for (var pi in posprop){ var pospart = GetProp(posprop[pi]); if (pospart != "*") { var posarray = pospart.split(","); var pl = posarray.length for (var mj=0; mj < posarray.length; mj++) { var acc = ""; switch (posarray[mj].substr(0,1)){ case "n": case "b": case "#": case "x": case "v": var acc = posarray[mj].substr(0,1); posarray[mj] = posarray[mj].slice(1); default: var pos = parseInt(posarray[mj]); var pl = posarray[mj].length-1; if (posarray[mj].substr(pl,1) == "^") Tie = true; else Tie = false; if (acc == "v") acc = "bb"; Positions[mp]=pos; Accidentals[mp++]= acc; } // end switch } // end for } // end if } // end for } // end GetPositions function CreateNoteText() { GetPositions(); for (var p=0; p < Positions.length; p++) { var ind = CalculateNoteInd (Clef, Positions[p]) var notechar = NoteChars[ind]; NoteText += NoteNames[ind]; if (Accidentals[p] == ""){ var sigs = KeySigs; for (var s=0; s < sigs.length; s++){ if (notechar == sigs[s].substr(0,1)) Accidentals[p] = sigs[s].substr(1,1); } // end for s } // end if if (Accidentals[p] == "n") Accidentals[p] = ""; NoteText += Accidentals[p]; } // end for p if (InsertAs == "Text"){ if (NoteText != "") NLines[LL++] = "|Text|Text:" + NoteText + "|Font:PageSmallText|Pos:9"; } // end if else { var durarray = GetProp("Dur").split(","); if (SearchItem(durarray, "Grace") != -1) return; if (SearchItem(durarray, "Slur") != -1 || Tie) NoteLyrics += NoteText + "_"; else NoteLyrics += NoteText + " "; } // end else NoteText = ""; } // end CreateNoteText function InsertLyric1Line() { if (!LyricsLine){ InsertItemAt(NLines, "|Lyrics|Placement:Bottom|Align:Standard Rules|Offset:0", InsertAt); LL++; InsertAt++; }// end if InsertItemAt(NLines, "|Lyric1|Text:" + NoteLyrics + "\r\n", InsertAt); LL++; NoteLyrics = ""; } // end InsertLyric1Line function FileEnvelopeLine(){ if (CurLine.substr(0) == "#" || CurLine.substr(0) == "!") return (true); else return (false); } function FileHeaderItem() { var p = Parts[1]; if (p == "Locale" || p == "Editor" || p == "SongInfo" || p =="PgSetup" || p == "Font" || p == "PgMargins") return (true) else return false; } function StaffHeaderItem() { var p = Parts[1]; if (p == "AddStaff" || p == "StaffProperties" || p == "StaffInstrument" || p.substr(0,5) =="Lyric") return (true) else return (false); } function ProcessFileTextLine(){ ShowVar("Testing", Testing); return; } function ProcessFileEnd(){ if (StaffAction == "Y" && InsertAs == "Lyrics") InsertLyric1Line(); } function ProcessFont() { if (GetProp("Style") == "StaffLyric") LyricFontSize = GetProp("Size"); return; } function ProcessEditor() { var pj, part; for (pj=2; pj= SelectStart && CurrentIndex < SelectEnd) return(true) else return (false); } function AtCursor() { if (CurrentStaff == ActiveStaff && CurrentIndex != 0 && CurrentIndex == CaretIndex - 1 ) return(true) else return (false); } function ProcessAddStaff() { CurrentStaff++; if (StaffAction == "Y" && InsertAs == "Lyrics") InsertLyric1Line(); LyricsLine = false; InsertAt = -1; NoteLyrics = ""; StaffAction = ""; CurrentIndex = 0; Signature = ""; KeySigs.length = 0; return; } function ProcessStaffProperties() { // !! Er zijn 2 lijnen StaffProperties, de property Visible zit in de 1e, Volume in de 2e lijn !! var pj, part; if (ActUpon == "All" || (ActUpon == "Act" && (CurrentStaff == ActiveStaff))) StaffAction = "Y"; if (ActUpon == "Vis" && StaffAction == "") { StaffAction = GetProp("Visible"); } if (StaffAction == "Y"){ var boundarybottom = GetProp("BoundaryBottom"); if (boundarybottom !="*") { SetProp("BoundaryBottom", "+", Math.round(LyricFontSize / 2)); } //end if } //end if return; } function ProcessStaffInstrument() { InsertAt = LL + 1; return; } // function ProcessLyrics() { InsertAt = LL + 1; LyricsLine = true; return; } function ProcessLyricn(n) { if (InsertAs == "Text") return; if (n == 8) QuitError("Maximum lyric lines = 8, choose 'Text' as 'Insert_as'"); n++; Parts[1] = "Lyric" + n; CurLine = Parts.join("|"); return; } function ProcessClef() { Clef = GetProp("Type"); return; } function ProcessKey() { Signature = GetProp("Signature"); KeySigs = Signature.split(","); return; } function ProcessNote() { CreateNoteText(); return; } function ProcessChord() { CreateNoteText(); return; } function ProcessRestChord() { CreateNoteText(); return; } function ProcessBar() { if (NoteLyrics != "" && !Tie) NoteLyrics += "\\r\\n"; return; } function ProcessDynamic() { return; } function ProcessRestMultiBar() { return; } function ProcessSpacer() { return; } function ProcessBoundary() { return; } function ProcessDynamicVariance() { return; } function ProcessFlow() { return; } function ProcessPerformanceStyle() { return; } function ProcessEnding() { return; } function ProcessSustainPedal() { return; } function ProcessTempoVariance() { return; } function ProcessText() { return; } function ProcessInstrument() { return; } function ProcessMPC() { return; } function ProcessLine () { Retain = true; if (CurLine.substr(0,7) == "#/File:") { ProcessFileTextLine(); return;} if (CurLine.substr(0,23) == "!NoteWorthyComposer-End") { ProcessFileEnd(); return;} if (CurLine.substr(0,1) == "|") { Parts = CurLine.split("|"); if (!StaffHeaderItem() && !FileHeaderItem()) CurrentIndex++; switch (Parts[1]) { case "Editor": ProcessEditor(); break; case "Font": ProcessFont(); break; case "AddStaff": ProcessAddStaff(); break; case "StaffProperties": ProcessStaffProperties();break; } // end switch if (StaffAction == "Y"){ switch (Parts[1]) { case "StaffInstrument": ProcessStaffInstrument();break; case "Lyrics": ProcessLyrics(); break; case "Lyric1": ProcessLyricn(1); break; case "Lyric2": ProcessLyricn(2); break; case "Lyric3": ProcessLyricn(3); break; case "Lyric4": ProcessLyricn(4); break; case "Lyric5": ProcessLyricn(5); break; case "Lyric6": ProcessLyricn(6); break; case "Lyric7": ProcessLyricn(7); break; case "Lyric8": ProcessLyricn(8); break; case "Clef": ProcessClef(); break; case "Key": ProcessKey(); break; case "Note": ProcessNote(); break; case "Chord": ProcessChord(); break; case "RestChord": ProcessRestChord(); break; case "Bar": ProcessBar(); break; case "Dynamic": ProcessDynamic(); break; case "RestMultiBar": ProcessRestMultiBar(); break; case "Spacer": ProcessSpacer(); break; case "Boundary": ProcessBoundary(); break; case "DynamicVariance": ProcessDynamicVariance(); break; case "Flow": ProcessFlow(); break; case "PerformanceStyle": ProcessPerformanceStyle(); break; case "Ending": ProcessEnding(); break; case "SustainPedal": ProcessSustainPedal(); break; case "TempoVariance": ProcessTempoVariance(); break; case "Text": ProcessText(); break; case "Instrument": ProcessInstrument(); break; case "MPC": ProcessMPC(); break; } // end switch } // end if PrevItem = Parts[1]; } return; } // End function ProcessLine function CheckInstallation() { if (WScript.Arguments.length != NPrompts) QuitError("Number of prompts needed: " + NPrompts); if (Lines[0].substr(0,7) != "#/File:") QuitError("Input type must be 'File Text'"); if (Lines[2].substr(16,4) != ReturnFormat){ switch (ReturnFormat) { case "File" : QuitError("Under 'Options', check 'Returns File Text'"); break; case "Clip" : QuitError("Under 'Options', uncheck 'Returns File Text'"); break; } } } // End function Declarations // Main Processing block FileInput = WScript.StdIn.ReadAll(); Lines = FileInput.split("\r\n"); CheckInstallation(); for (F=0; F < NPrompts; F++){ ProcessPrompts(F); } // End for F for (L=0; L < Lines.length; L++){ CurLine = Lines[L] ProcessLine(); if (Retain) NLines[LL++] = CurLine; } // End for L WScript.StdOut.Write(NLines.join("\r\n")); WScript.quit(RC);