<?php
/*
Public domain.
Kyrre/Gnute
http://folk.uio.no/kyrreg
SampleToSplit is thought to save me some clicking on virtual keys while making samplebased *.xrnis.
I cannot take responsibility for it, but it hasn't hurt anything in my tests. :D
*/
// ----------------------------------------------------------------------------
// Variables
// ----------------------------------------------------------------------------
$tmp_dir = '/tmp';
// ----------------------------------------------------------------------------
// Requires
// ----------------------------------------------------------------------------
require_once('xrns_functions.php');
// ----------------------------------------------------------------------------
// 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 < 3) {
echo "Error: $argv[0] expects at least 2 parameters.\n";
echo "Usage: `php $argv[0] /path/to/instrument1.xrni instrument2.xrni `\n";
echo "$argv[0] will output new instrument (instrument2.xrni) 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$|\.xrni$)/i', $argv[2]))) {
die("Error: The filename $argv[2] is invalid, use .xrni (or .zip)\n");
}
$instrument1 = $argv[1];
$instrument2 = $argv[2];
// ----------------------------------------------------------------------------
// 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 . '/Sample_To_Split_' . md5(uniqid(mt_rand(), true)) . '_Instrument01/';
// Unzip song1
$result = UnzipAllFiles($instrument1, $unzip1);
if($result === FALSE) {
echo "Error: There was a problem unzipping the first file.\n";
die();
}
// ----------------------------------------------------------------------------
// Procedure
// ----------------------------------------------------------------------------
$instrument = simplexml_load_file($unzip1 . 'Instrument.xml');
$basenotes = array();
foreach($instrument->Samples->children() as $sample) {
$basenotes[] = $sample->BaseNote;
}
//check for duplicate basenotes
if (count($basenotes) > count(array_unique($basenotes))) {
echo "Make sure each sample has unique basenotes.";
obliterate_directory($unzip1);
die();
} //end if
//check for rising basenote values
$b=$basenotes;
asort($b, SORT_NUMERIC);
if ($b !== $basenotes) {
echo "\nPlease make sure the basenotes are ascending.\n";
obliterate_directory($unzip1);
die();
}
//sets new splits according to basenotes
$i = 0;
$k = 0;
while ($k < count($basenotes)-1) {
while ($i < $basenotes[$k+1]) {
$instrument->SplitMap->Split[$i] = $k;
$i++;
} //end while
$k++;
} //end while
$k = count($basenotes)-1;
while ($i < 120) {
$instrument->SplitMap->Split[$i] = $k;
$i++;
} //end while
// detune?
if(isset($argv[3]) && $argv[3] == 0 ) {
$detune = $instrument->Samples->Sample[0]->Finetune;
$i = 1;
while ($i < count($instrument->Samples->children() )) {
$instrument->Samples->Sample[$i]->Finetune = $detune;
$i++;
}
}
/*
// ----------------------------------------------------------------------------
// Validate
// ----------------------------------------------------------------------------
if (!xrns_xsd_check($Instrument1, (int)$Instrument1['doc_version'])) {
echo "Error: XML is invalid!\n";
obliterate_directory($unzip1);
die();
}
*/
// ----------------------------------------------------------------------------
// Replace Instrument.xml
// ----------------------------------------------------------------------------
unlink($unzip1 . 'Instrument.xml') or die("Error: There was a problem deleting a file.\n");
file_put_contents($unzip1 . 'Instrument.xml', $instrument->asXML());
// Zip song
$result = ZipAllFiles($instrument2, $unzip1);
if($result === FALSE) {
echo "Error: There was a problem zipping the final file.\n";
obliterate_directory($unzip1);
die();
}
// ----------------------------------------------------------------------------
// 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";
?>