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

/*

Clip2Code converts Renoise Pattern data from the clipboard to a ASCII string representation.
Usage: `php clip2code.php /path/to/clipboard.txt`
clip2code.php will output ASCII to buffer.

Author: Mick Rippon
Website: www.mickrippon.com

*/


// ----------------------------------------------------------------------------
// Class
// ----------------------------------------------------------------------------

class renoiseUtil{
    function PatternClipToArray($clipdata){
        $maxLines=0;
        $doc = new DOMDocument();
        @$doc->loadXML(stripslashes($clipdata));
        $items=$doc->getElementsByTagName("TrackColumns")->item(0);
        if(!$items)$this->death("No Track Columns Found, XML struct appears to be fucked.");
        $TrackColumns=$this->getChildrenOf($items);
        if(!$TrackColumns)$this->death("No Track Columns Found");
        foreach($TrackColumns as $track_id=>$track_obj){
            $Track_subcols=$this->getChildrenOf($track_obj);
            foreach($Track_subcols as $subcol_id=>$subcol_obj){
                $subcol_info=$this->getChildrenOf($subcol_obj);
                foreach($subcol_info as $trackEntity_obj){
                    $tagName=$trackEntity_obj->tagName;
                    if($tagName=="Lines"){
                        $lineData=$this->getChildrenOf($trackEntity_obj);
                        foreach($lineData as $rowdata){
                            $rowIndex=$rowdata->attributes->getNamedItem('index')->value;
                            if($rowIndex>$maxLines)$maxLines=$rowIndex;
                            $cellData=$this->getChildrenOf($rowdata);
                            if($cellData){
                                foreach($cellData as $cellinfo){
                                    if($cellinfo->tagName=="NoteColumns"){
                                        $noteData=$this->getChildrenOf($cellinfo); $noteData=$noteData[0];
                                        $noteData=$this->getChildrenOf($noteData);
                                        foreach($noteData as $noteAttribs){
                                            $build[$track_id][NoteColumns][$subcol_id][$rowIndex][$noteAttribs->tagName]=$noteAttribs->nodeValue;
                                            if($noteAttribs->tagName=="Panning")$panmode=TRUE;
                                        }
                                    }
                                    if($cellinfo->tagName=="EffectColumns"){
                                        $noteData=$this->getChildrenOf($cellinfo); $noteData=$noteData[0];
                                        $noteData=$this->getChildrenOf($noteData);
                                        foreach($noteData as $noteAttribs){
                                            $build[$track_id][EffectColumns][$subcol_id][$rowIndex][$noteAttribs->tagName]=$noteAttribs->nodeValue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return array("data"=>$build, "maxlines"=>$maxLines, "panmode"=>$panmode);
    }

    function getChildrenOf($obj){
        $itemList=$obj->childNodes;
        if(!$itemList)return FALSE;
        foreach($itemList as $k=>$thiselementObj){
            if($thiselementObj->nodeType==XML_ELEMENT_NODE){
                $ret[]=$thiselementObj;
            }

        }
        return $ret;
    }

    function death($err){
        die("Error: $err\n");
    }
}

// ----------------------------------------------------------------------------
// Check Variables
// ----------------------------------------------------------------------------

// get filename component of path
$argv[0] = basename($argv[0]);

if ($argc != 2) {
    echo "Error: $argv[0] expects 1 parameter.\n";
    echo "Usage: `php $argv[0] /path/to/clipboard.txt`\n";
    echo "$argv[0] will output ASCII to buffer.\n";
    die();
}

// get the clipboard from the temp file
if (!file_exists($argv[1])) die("Error: The file $argv[1] was not found.\n");
else $clipboard = file_get_contents($argv[1]);

// ----------------------------------------------------------------------------
// Output ASCII
// ----------------------------------------------------------------------------

$renoise = new renoiseUtil();
$cliparray = $renoise->PatternClipToArray($clipboard);

ob_start(); // Turn on output buffering

foreach($cliparray[data] as $track_id=>$track_data){
    if($track_data[NoteColumns]){
        foreach($track_data[NoteColumns] as $col_id=>$col_data){
            for($x=0; $x<=$cliparray[maxlines]; $x++){
                if(!$col_data[$x][Note])$col_data[$x][Note]="---";
                if(!$col_data[$x][Instrument])$col_data[$x][Instrument]='  '; // Two spaces
                if(!$col_data[$x][Volume])$col_data[$x][Volume]="..";
                if($cliparray[panmode]){
                    if(!$col_data[$x][Panning])$col_data[$x][Panning]=" .."; else $col_data[$x][Panning]=" ".$col_data[$x][Panning];
                }else{
                    $col_data[$x][Panning]="";
                }
                $output[$x].=$col_data[$x][Note].$col_data[$x][Instrument].$col_data[$x][Volume].$col_data[$x][Panning]." ";
            }
        }
    }
    if($track_data[EffectColumns]){
        foreach($track_data[EffectColumns] as $col_id=>$col_data){
            for($x=0; $x<=$cliparray[maxlines]; $x++){
                if(!$col_data[$x][Number])$col_data[$x][Number]="--";
                if(!$col_data[$x][Value])$col_data[$x][Value]="--";
                $output[$x].=$col_data[$x][Number].$col_data[$x][Value]." ";
            }
        }
    }else{
        for($x=0; $x<=$cliparray[maxlines]; $x++){
            if(!$col_data[$x][Number])$col_data[$x][Number]="--";
            if(!$col_data[$x][Value])$col_data[$x][Value]="--";
            $output[$x].=$col_data[$x][Number].$col_data[$x][Value]." ";
        }
    }

    for($x=0; $x<=$cliparray[maxlines]; $x++){
        $output[$x].=" | ";
    }
}
if(!$output) {
    echo "we have a problem. :(";
}
else{
    foreach($output as $row_id=>$row_data){
        $row_id=str_pad($row_id,3, "0", STR_PAD_LEFT);
        echo "$row_id | $row_data\r\n";
    }
}

$output = ob_get_contents(); // Return the contents of the output buffer
ob_end_clean();

// write the converted data back to the temp file
file_put_contents($argv[1], $output);

echo "---------------------------------------\n";
echo "$argv[0] is done! ASCII below\n";
echo date("D M j G:i:s T Y\n");
echo "---------------------------------------\n";
echo $output;

?>
Return current item: XRNS-PHP