Here's the new version
<?php
/*******************************************************************************
kbsc_Clef_Change.php Version 1.00
This script changes the Clef and adjusts the positions of the notes on the stave to keep the same note values
Copyright © 2009 by K.B.S. Creer.
All Rights Reserved
HISTORY:
================================================================================
[2009-01-21] Version 1.01: Improvements to code as suggested by Rick G and Lawrie Pardy
[2009-01-19] Version 1.00: Initial release
*******************************************************************************/
require_once("lib/nwc2clips.inc");
function help_msg_and_exit()
{
echo 'Called as:
php\php.exe scripts\kbsc_Clef_Change.php "/clef=<PROMPT: Select Clef:=|help|Treble|Bass|Alto|Tenor|Percussion|>" "/octave=<PROMPT:Select Octave Shift:=|None|Octave Up|Octave Down|>"
Notes are defined in Noteworthy by their position on the stave.
If the Clef is changed, the notes remain in the same position and hence represent different note values.
This User Tool changes the Clef and adjusts the positions of the notes to keep the same note values.
The first clef symbol in the selection (or the whole tune if no selection is made) is changed and all subsequent
notes are adjusted up to the next clef symbol or the end of the tune, whichever comes first.' ;
exit(NWC2RC_REPORT) ;
}
$noteTypes = array ("Note", "Chord", "RestChord"); //Items to process
$clefShift = array ("Treble" => -6, "Alto" => 0, "Tenor" => 2, "Bass" => 6, "Percussion" => 6); //Position of middle C
$octaveShift = array ("None" => 0 , "Octave Up" => -7 , "Octave Down" => 7); //Position of octaves
$clip = new NWC2Clip('php://stdin');
$arg = array('clef' => 'Treble','octave' => 'None');
foreach ($argv as $k => $v) {
if (!$k) continue;
if (preg_match('/^\/([a-z]+)\=(.*)$/',$v,$m)) {
$argname = $m[1];
$argvalue = $m[2];
$arg[strtolower($argname)] = $argvalue;
}
}
if ($arg["clef"] == "help") help_msg_and_exit() ;
echo $clip->GetClipHeader()."\n";
//
$firstClefFound=FALSE;
$secondClefFound=FALSE;
$shiftPos = 0;
foreach ($clip->Items as $item) {
$o = new NWC2ClipItem($item);
$oType = $o->GetObjType();
if ($secondClefFound) { //After the second clef write everything back unchanged
echo $item;
continue;
}
if ($oType=="Clef") {
if ($firstClefFound) { //You've found a second clef. Stop adjusting
echo $item;
$secondClefFound=TRUE;
} else { //Calculate the amount to shift notes by
$startPos = $clefShift[$o->Opts['Type']]; //
$endPos = $clefShift[$arg['clef']]; //For change of clef
if (isset($o->Opts['OctaveShift'])) {
$startOctave = $octaveShift[$o->Opts['OctaveShift']]; //
} else { //
$startOctave = 0; //For change of octave
} //
$endOctave = $octaveShift[$arg['octave']]; //
$shiftPos = $endPos - $startPos + $endOctave - $startOctave;
$o->Opts['Type'] = $arg['clef']; //Set new clef symbol
$o->Opts['OctaveShift'] = $arg['octave']; // and octave shift
echo $o->ReconstructClipText()."\n";
$firstClefFound=TRUE;
}
} else {
if ($shiftPos) { //Only if there is a non zero position shift
if (in_array($oType, $noteTypes)) {
if ($oType=="Note") settype($o->Opts["Pos"], "array"); //Note "Pos" is a string, others are array
foreach (array("Pos", "Pos2") as $p) {
if (isset($o->Opts[$p])) {
for ($i = 0; $i < count($o->Opts[$p]); $i++) {
$notepitchObj = new NWC2NotePitchPos($o->Opts[$p][$i]);
$notepitchObj->Position += $shiftPos;
$o->Opts[$p][$i] = $notepitchObj->ReconstructClipText();
}
}
}
echo $o->ReconstructClipText()."\n";
} else {
echo $item;
}
} else {
echo $item;
}
}
}
echo NWC2_ENDCLIP."\n";
exit(NWC2RC_SUCCESS);
?>
And this is the command line courtesy of Lawrie -
php\php.exe scripts\kbsc_Clef_Change.php "/clef=<PROMPT: Select Clef:=|help|Treble|Bass|Alto|Tenor|Percussion|>" "/octave=<PROMPT:Select Octave Shift:=|None|Octave Up|Octave Down|>"
Bryan