Location: PHPKode > projects > XRNS-PHP > xrns_delete_unused_tracks.php
<?php

/*

Requires: PHP5 and Info-Zip (http://www.info-zip.org/)
Usage: `php xrns_delete_unused_tracks.php /path/to/file1.xrns file2.xrns`
Will output resulting file to current working directory

Public Domain, last modified November 6th, 2007
Coded by Dac Chartrand of http://www.trotch.com/

*/

// ----------------------------------------------------------------------------
// 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 2 parameters.\n";
    echo "Usage: `php $argv[0] /path/to/file1.xrns file2.xrns`\n";
    echo "$argv[0] will output resulting 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];

// ----------------------------------------------------------------------------
// 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_delete_unused_tracks_' . 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');


// ----------------------------------------------------------------------------
// Search & Destroy
// ----------------------------------------------------------------------------

// Find stuff
$in_use = array();
foreach ($sx1->PatternPool->Patterns->Pattern as $p) {
    $i = 0;
    foreach ($p->Tracks->PatternTrack as $x) {
        if ($x->Lines) {
            $in_use[$i] = true;
        }
        ++$i;
    }
}

// Delete stuff
$total = count($sx1->Tracks->SequencerTrack);
foreach ($sx1->PatternPool->Patterns->Pattern as $p) {
    for($i = $total - 1; $i >= 0; --$i) {
        if (!isset($in_use[$i])) {
            unset($p->Tracks->PatternTrack[$i]);
        }
    }
}

for($i = $total - 1; $i >= 0; --$i) {
    if (!isset($in_use[$i])) {
        unset($sx1->Tracks->SequencerTrack[$i]);
    }
}

// Prevent Renoise from crashing ...
$sx1->SelectedTrackIndex = 0;

// ----------------------------------------------------------------------------
// Validate
// ----------------------------------------------------------------------------

if (!xrns_xsd_check($sx1, (int)$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();
}

// ----------------------------------------------------------------------------
// 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";

?>
Return current item: XRNS-PHP