Skip to main content
Topic: Bug: ObjTypeHasPitchPos() in obj_NWC2NotePitchPos.inc (Read 16904 times) previous topic - next topic

Bug: ObjTypeHasPitchPos() in obj_NWC2NotePitchPos.inc

If you run this:
Code: [Select · Download]
<?php
require_once("lib/nwc2clips.inc");
$clip = new NWC2Clip();
foreach ($clip->Items as $item) {
$o = new NWC2ClipItemWithPitchPos($item);
$type = $o->ObjType;
echo $type.'->'.$o->ObjTypeHasPitchPos($type).PHP_EOL;
}
exit(NWC2RC_REPORT);
?>
true is reported for Rest objects. Rest objects do not have PitchPos.
Registered user since 1996

Re: Bug: ObjTypeHasPitchPos() in obj_NWC2NotePitchPos.inc

Reply #1
Agreed.  Within the function, the strpos needs to look up "|$ObjType|" instead of just $ObjType, to keep "Rest" from being spotted within "RestChord".

IMHO, NWC2ClipItemWithPitchPos should initialize $PitchPos to only array(), instead of array("Pos" => array(), "Pos2" => array()).  Then one need only code if ($o->PitchPos) to determine if an object has any pitches.

Note that to process pitches, one need not first determine if an object has pitches.  The following code works just fine in all cases (any excess processing time for non-pitched objects will not be noticed):
Code: [Select · Download]
foreach ($clip-Items as $item) {
   $o = new NWC2ClipItemWithPitchPos($item);

   foreach ($o->PitchPos as $npps) {
      foreach ($npps as $npp) {
         <operate on $npp>
      }
   }
}
Of course, to effect any changes to pitches, one currently must not call ReconstructPosOpts without first verifying the object has pitches.

IMHO, ReconstructPosOpts should be replaced by its own ReconstructClipText, which overrides the one in its parent NWC2ClipItem class.  The implementation would do the "ReconstructPosOpts" stuff (as needed), and then call its parent ReconstructClipText.  Then all objects could be "reconstructed", without regard to whether they are pitched or not, or a pitched item class or not.

Static functions are traditionally called with a "class::function" syntax, instead of the regular "object->function" syntax.  It was interesting to me to see that PHP allows the $o->ObjTypeHasPitchPos(...) shorthand, instead of insisting on NWC2ClipItemWithPitchPos::ObjTypeHasPitchPos(...)!  But sure enough, from the manual:
Quote
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

Re: Bug: ObjTypeHasPitchPos() in obj_NWC2NotePitchPos.inc

Reply #2
A little experiment into how NWC2ClipItemWithPitchPos might be improved.
Code: [Select · Download]
<?php
// https://forum.noteworthycomposer.com/?topic=7504.0
// ver 0.1
require_once("lib/nwc2clips.inc");
// -----------------------------------------------------------------------
class myClipItemWithPitchPos extends NWC2ClipItem {
// -----------------------------------------------------------------------
public $PitchPos = array("Pos" => array(), "Pos2" => array());

function __construct($itemtext,$remapFakes=false) {
parent::__construct($itemtext,$remapFakes);
foreach ($this->PitchTags() as $tag) {
foreach ($this->GetTaggedOptAsArray($tag, array()) as $np)
$this->PitchPos[$tag][] = new NWC2NotePitchPos($np);
}
}
function PitchTags() {
switch ($this->ObjType) {
case 'Note': return array('Pos');
case 'Chord': return array('Pos', 'Pos2');
case 'RestChord': return array('Pos2');
default: return array();
}
}
function __toString() {
foreach ($this->PitchTags() as $tag) {
unset($this->Opts[$tag]); // clear
foreach ($this->PitchPos[$tag] as $npp)
$this->Opts[$tag][] = $npp->ReconstructClipText();
}
return $this->ReconstructClipText();
}
}
//
array_shift($argv); // remove argv[0]
preg_match('/^([\#bnxv]*)(-?\d*)(\w*)(\^?)(!?\d*)/', implode($argv), $m);
$tieAll = $m[0]==''; // empty commandline, tie everything
$acc = str_split($m[1]); // remove any of #bnxv
$shift = (int) $m[2]; // move notes if not zero
$head = str_split($m[3]); // change these to default notehead
$zapTie = $m[4]=='^'; // remove ties
$color = str_split($m[5]); // change these to default color
if ($argc > 2) fputs(STDERR, print_r($m, true));

$clip = new NWC2Clip();
echo $clip->GetClipHeader().PHP_EOL;
foreach ($clip->Items as $item) {
$o = new myClipItemWithPitchPos($item);
foreach ($o->PitchPos as $np) {
foreach ($np as $npp) {
if (in_array($npp->Accidental, $acc)) $npp->Accidental = '';
$npp->Position+= $shift;
if(in_array($npp->Notehead, $head)) $npp->Notehead = '';
if($tieAll) $npp->Tied = '^'; else if($zapTie) $npp->Tied = '';
if(in_array($npp->Color, $color)) $npp->Color = '';
}
}
echo $o.PHP_EOL; // o$->__toString()
}
echo $clip->GetClipFooter().PHP_EOL;
?>
Registered user since 1996

Re: Bug: ObjTypeHasPitchPos() in obj_NWC2NotePitchPos.inc

Reply #3
The ObjTypeHasPitchPos function will be fixed in the next Starter Kit. The fix will appear on github shortly.