Skip to main content
Topic: Insert accumulated time after each bar line (Read 1829 times) previous topic - next topic

Insert accumulated time after each bar line

Save the following with the name TimeText.js .  When installing a new user tool and doing the Browse to get this, add "wscript " (note trailing space) to the beginning of the line and the prompts on line 6 to the end of it.  Accept the defaults.

I wanted to give a warning when a Tempo Varience was found, but didn't find a way to do it.
Code: [Select · Download]
/* Time Through Current Bar by Warren Porter "TimeText.js"

   This script will insert the cumulative time at the beginning of each bar line.
   After downloading this file, when setting it up in NWC User Tools create this command line:

   wscript "'Browse can insert the path for you' \TimeText.js" <PROMPT:Run?:=|Yes|Undo> <PROMPT:BeginTime:=*00:00.0>

   The first parm will run the whole script ("Yes") or simply remove timings from a previous run ("Undo").
   Use the second parameter to specify a starting time for the song.
 */

var rc=0, errMsg="";
function getNoteLength(notelet) {  // Returns duration of note , whole = 768
var NoteLengths = { "16th":48, "32nd":24, "4th":192, "64th":12, "8th":96, "Half":384, "Whole":768 } ;

parts = notelet.split(",");
var  noteLength= -1;
noteLength = NoteLengths[parts[0]];
if (typeof(noteLength) == "undefined")
  return -1;

  for (var i = 1; i < parts.length; i++) {
    if (parts[i].substr(0,9) == "DblDotted") {
  noteLength = noteLength * 7 / 4; }
else
      if (parts[i].substr(0,6) == "Dotted") {
    noteLength = noteLength * 3 / 2; }
  else
    if (parts[i].substr(0,7) == "Triplet") {
      noteLength = noteLength * 2 / 3; } }
  return noteLength;
}
function tempoLength(notelet) {  // Returns duration of tempo
var NoteLengths = { "Eighth Dotted":144, "Eighth":96, "Quarter Dotted":288, "Quarter":192, "Half Dotted":576, "Half":384}
var noteLength = -1;
noteLength =NoteLengths[notelet];
if (typeof(noteLength) == "undefined")
  return -1;
return noteLength;  }

function calculate(clip) {
 
  var i, a, cumDur=0, durVal=0, durLen, min, sec, cumTime, wholeMeas=768, preTime=0, unDo=0, doWarn=1;
  var base=192;  //Default for tempo
  var beatletLength=0;  //Duration of "768th" note
  var result = new Array();
  var lines = new Array();
   var shell = new ActiveXObject("WScript.Shell");
    if (WScript.Arguments.length != 2) {
  errMsg = "I need two parms: Yes or Undo and initial time offset";
  rc=1;
return; }

  if (WScript.Arguments.Item(0) == "Undo")
  unDo=1;

  result = WScript.Arguments.Item(1).match(/(\d+):(\d+\.*\d*)/)
  if (result != null) {  // Read starting time
    min=parseInt(result[1]);
sec=parseFloat(result[2]);
preTime=min*60 + sec;}

  lines = clip.split("\r\n");
 
  for (i=0; i < lines.length; i++) {    //Main processing loop
 
result=lines[i].match(/\|Font:PageSmallText\|Pos:10\|Color:[36]/)
if (result != null)    // Remove earlier comments
  lines[i] = "#";
 
    if (unDo)
  continue;
 
if (lines[i].slice(0,15) == "|TempoVariance|") {  //Found tempo varience, give a warning
  if (doWarn) {
    shell.Popup("A Tempo Varience has been found, results are unpredictable");
    doWarn=0; } // Don't say it more than once.
      continue;   }
  
    if (lines[i].slice(0,5) == "|Flow") {
    errMsg="Repeats or flow changes not allowed";
rc=1;
return;}
 
if (lines[i].slice(0,7) == "|Tempo|") {  //Found tempo, look for base
  if (lines[i].substr(6,5) == "|Base") {
    result=lines[i].match(/Base:(.+)\|Tempo/)
base= tempoLength(result[1]); }
  else
    base = 192;
  result=lines[i].match(/(\|Tempo.*Tempo:)(\d+)/)
  a=parseInt(result[2]);
  beatletLength= 60 / (a * base);
      continue;   }  // End of Tempo code
 
    if (lines[i].slice(0,15) == "|Rest|Dur:Whole" && lines[i].indexOf(",") == -1) {
  cumDur += wholeMeas;   //Found whole rest but use duration of last time signature.
      continue;}
 
    result=lines[i].match(/Dur:([^\|]*)/);  // Looking for anything with duration not grace
if ((result != null) && (!/Grace/.test(lines[i]))) { // Has duration but is not grace.
  var durVal = result[1];
  var durLen = getNoteLength(durVal);
  cumDur += durLen;
      continue;   }
 
 
    result=lines[i].match(/Signature:(\d+)\/(\d+)/)
if (result != null)
  wholeMeas = 768 * result[1] / result[2];
 
result=lines[i].match(/\|RestMultiBar\|NumBars:(\d+)\|/)
if (result != null)
  cumDur += wholeMeas * result[1];
 
if (lines[i].slice(0,4) == "|Bar") { // Found a bar line
  if (beatletLength == 0) { // Shouldn't happen
    errMsg="Bar before Tempo indication, aborting";
rc=1;
return;}

  if (lines[i].search("Repeat") != -1) {
    errMsg="Repeats or flow changes not allowed";
rc=1;
return;}

  cumTime = preTime + cumDur * beatletLength;
      min = Math.floor(cumTime/60);
  sec = cumTime;
  sec -= min * 60;
  smin=min.toString()
  if (min < 10)
    smin = "0" + smin;
  sec =  Math.round(sec*1000)/1000;
  ssec = sec.toString();
  if (sec < 10)
    ssec = "0" + ssec;

  lines[i] += "\r\n|Text|Text:\"" + smin + ":" + ssec + "\"|Font:PageSmallText|Pos:10|Color:3" }

if (lines[i].slice(0,27) == "!NoteWorthyComposerClip-End") {
  cumTime = cumDur * beatletLength;
      min = Math.floor(cumTime/60);
  sec = cumTime;
  sec -= min * 60;
  smin=min.toString()
  if (min < 10)
    smin = "0" + smin;
  sec =  Math.round(sec*1000)/1000;
  ssec = sec.toString();
  if (sec < 10)
    ssec = "0" + ssec;

  lines[i] = "|Text|Text:\"" + smin + ":" + ssec + " " + cumDur + "\"\|Font:PageSmallText|Pos:10|Color:6\n" + lines[i];
  break;  }  // Don't do the end clip again
  } 
   rc = 0;
  return lines;
  }   // End main processing loop

var myLines=calculate(WScript.StdIn.ReadAll()).join("\r\n");
if (rc == 0)
  WScript.StdOut.Write(myLines);
else
  WScript.StdErr.Write(errMsg);
WScript.quit(rc);

Edited change:  Got a popup to warn of tempo variences.
Since 1998