Wednesday, August 22, 2007

Convert MusicXML to Power Tabs

Here's a source snippet that will allow you to convert "P1" of a MusicXML score to a power tab file. This is just an example of what you can do with the php power tab classes. Keep in mind, you must have the latest version of the PHP ptparser for this to work.

The score will look funny when you open it. Run the score polisher. This will make it readable.

Hopefully this will inspire someone to make a complete converter.




<?

require_once "powertabdocument.php";


$ptb = new PowerTabDocument ();


$durArr = array('64th'=>64, '32nd'=>32, '16th'=>16,'eighth'=>8, 'quarter'=>4, 'half'=>2, 'whole'=>1);

$ptb->Load ("BlankPowerTabFile.ptb");
$guitarScore = &$ptb->GetGuitarScore();

$sxe = simplexml_load_file("music.xml");

$s = -1;
$measureCt = 3;

foreach ($sxe->part->measure as $measure) {

if ($measureCt > 1) {
$guitarScore->m_systemArray[++$s] = new System;
$measureCt = 1;
$p=0;
}

$firstStaff = &$guitarScore->GetSystem($s)->GetStaff(0);


foreach ($measure->note as $note) {

$string = $note->notations->technical->string;
$fret = $note->notations->technical->fret;
$tied = $note->notations->tied["type"];

$dur = $durArr[(string) $note->type];

if ( isset($string,$fret) ) {

if (!$note->chord) {
$thePosition = &$firstStaff->m_positionArray[0][$p];
$thePosition = new Position ();

$thePosition->SetDurationType($dur);

//$thePosition->SetPreviousBeamDurationType($prevDur);

$prevDur = $dur;

++$p;
}

array_push($thePosition->m_noteArray,new Note($string-1, $fret) );

if ($tied=="stop")
$thePosition->m_noteArray[count($thePosition->m_noteArray) - 1]->SetTied();
}


}

$barLines = &$guitarScore->GetSystem($s)->m_barlineArray;

array_push($barLines,new Barline($p) );


++$measureCt;


}

$ptb->Save ("ConvertedFileName.ptb");

?>

0 comments:

Music Headlines

Loading...