<?php
/*
Requires: PHP5 and Info-Zip (http://www.info-zip.org/)
Usage: `php xrns_searchandreplace.php /path/to/file1.xrns file2.xrns oldnote oldinstrument newnote newinstrument Yes|No`
Example: `php xrns_searchandreplace.php file1.xrns file2.xrns C-4 01 C-4 02 Yes`
the above example will replace C-4 01 with C-4 02 ignoring muted tracks ("Yes")
Will output the new file to current working directory
Coded by Fabio Napodano http://napodano.com
v. 0.03 2007/09/01
searches and replaces a specified note with another one in a song. can optionally ignore non active tracks (mute/off)
*/
// ----------------------------------------------------------------------------
// Variables
// ----------------------------------------------------------------------------
$tmp_dir = '/tmp';
// ----------------------------------------------------------------------------
// Requires
// ----------------------------------------------------------------------------
require_once('xrns_functions.php');
// ----------------------------------------------------------------------------
// Functions
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Check Variables
// ----------------------------------------------------------------------------
// get filename component of path
$argv[0] = basename($argv[0]);
if (!is_dir($tmp_dir)) {
$tmp_dir = get_temp_dir();
if (!$tmp_dir) die("Error: Please set \$tmp_dir in $argv[0] to an existing directory.\n");
}
// ----------------------------------------------------------------------------
// Check User Input
// ----------------------------------------------------------------------------
if ($argc < 7) {
echo "Error: $argv[0] expects at least 6 parameters.\n";
echo "you have input $argc parameters only!\n";
echo "Usage: `php $argv[0] /path/to/file1.xrns file2.xrns oldnote oldoctave newnote newoctave`\n";
echo "$argv[0] will output new file (file2.xrns) to current working directory.\n";
die();
}
if (!file_exists($argv[1])) die("Error: The file $argv[1] was not found.\n");
if (!(preg_match('/(\.zip$|\.xrns$)/i', $argv[2]))) {
die("Error: The filename $argv[2] is invalid, use .xrns (or .zip)\n");
}
$song1 = $argv[1];
$song2 = $argv[2];
$oldnote = $argv[3];
$newnote = $argv[5];
$oldinstrument = $argv[4];
$newinstrument = $argv[6];
if(!$newinstrument)
$newinstrument = $oldinstrument;
$ignoremutedtracks = (isset($argv[7]) && $argv[7] == 1 || strtolower($argv[7]) == 'yes') ? true : false;
// ----------------------------------------------------------------------------
// Unpack
// ----------------------------------------------------------------------------
echo "---------------------------------------\n";
echo "$argv[0] is working...\n";
echo date("D M j G:i:s T Y\n");
echo "---------------------------------------\n";
echo "Using temporary directory: $tmp_dir\n";
// Create a unique directory
$unzip1 = $tmp_dir . '/xrns_searchandreplace_' . md5(uniqid(mt_rand(), true)) . '_Track01/';
// Unzip song1
$result = UnzipAllFiles($song1, $unzip1);
if($result === FALSE) {
echo "Error: There was a problem unzipping the first file.\n";
die();
}
// Load XML
$sx1 = simplexml_load_file($unzip1 . 'Song.xml');
echo "Replacing $oldnote $oldinstrument with $newnote $newinstrument ..\n";
// ----------------------------------------------------------------------------
// Replace the notes
// ----------------------------------------------------------------------------
$tracksstatearray = array();
$ntrack = -1;
foreach ($sx1->Tracks->SequencerTrack as $track) {
$ntrack++;
if($track->State=="Active"||$ignoremutedtracks=="No")
$tracksstatearray[$ntrack] = 1;
else {
$tracksstatearray[$ntrack] = 0;
echo "will skip track \"$track->Name\" because is not active.\n";
}
}
foreach ($sx1->PatternPool->Patterns->Pattern as $p) {
$ntrack = -1;
foreach ($p->Tracks->PatternTrack as $x) {
$ntrack++;
if($tracksstatearray[$ntrack]!=1) {
continue;
}
if ($x->Lines->Line) {
foreach ($x->Lines->Line as $y) {
if ($y->NoteColumns->NoteColumn) {
foreach ($y->NoteColumns->NoteColumn as $z) {
if ($z->Note) {
if($z->Note == $oldnote) {
if($oldinstrument) {
if($z->Instrument == $oldinstrument) {
if($newinstrument)
$z->Instrument = $newinstrument;
if($newnote)
$z->Note = $newnote;
}
} else {
$z->Note = $newnote;
}
// echo "Replaced $oldnote $oldinstrument\n";
}
}
}
}
}
}
}
}
// ----------------------------------------------------------------------------
// Validate
// ----------------------------------------------------------------------------
if (!xrns_xsd_check($sx1, (string) $sx1['doc_version'])) {
echo "Error: XML is invalid!\n";
obliterate_directory($unzip1);
die();
}
// ----------------------------------------------------------------------------
// Replace Song.xml
// ----------------------------------------------------------------------------
unlink($unzip1 . 'Song.xml') or die("Error: There was a problem deleting a file.\n");
file_put_contents($unzip1 . 'Song.xml', $sx1->asXML());
// Zip song
$result = ZipAllFiles($song2, $unzip1);
if($result === FALSE) {
echo "Error: There was a problem zipping the final file.\n";
die();
} else
echo "New song saved succesfully at $song2\n";
// ----------------------------------------------------------------------------
// Remove temp directories
// ----------------------------------------------------------------------------
obliterate_directory($unzip1);
echo "---------------------------------------\n";
echo "$argv[0] is done!\n";
echo date("D M j G:i:s T Y\n");
echo "---------------------------------------\n";
?>
<html><body></body></html>