Update of /cvsroot/xrns-php/xrns-php/scripts
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv5430/scripts
Added Files:
rndpack.cfg rndpack.php
Log Message:
Generate a random sample pack by scanning directories
--- NEW FILE: rndpack.php ---
<?php
// ----------------------------------------------------------------------------
// Functions
// ----------------------------------------------------------------------------
function rules($string = null) {
if ($string) echo "
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Error: $string
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
";
echo'
Random Sample Pack Generator
Original code: bYtE-sMaShEr (aka Shaun Winters) 2008
Rewritten in PHP, the most hated of all loafs, by XXXXXXXXX (aka XXXXXXXXXXXXX)
Usage: rndpack [options] numSamples outputPath inputPath[...inputPath]
Options:
-R Recursive
-E [ext[,ext]] Restrict extension
-P Preserve filenames
-U [bytes] Upper filesize limit
-L [bytes] Lower filesize limit
Example:
Generate 100 sample pack, recursively searching through input paths for mp3s and wavs:
rndpack -r -e wav,mp3 100 ./samples/random "./samples/drum kits" ./samples/noises
Warning: This is a homebrew music community tool designed to simplify the
creative process. It is unforgiving, and may glitch out if used incorrectly.
Use at your own risk!
' . "\n";
exit;
}
function _reindex() {
// Re-Index
$GLOBALS['argc'] = count($GLOBALS['argv']);
$i = 0;
$tmp = array();
foreach($GLOBALS['argv'] as $val) {
$tmp[$i] = $val;
++$i;
}
$GLOBALS['argv'] = $tmp;
}
// Something is f*cked with array_search(), workaround function
function _getKey($needle) {
$haystack = $GLOBALS['argv'];
foreach($haystack as $key => $val) {
if (mb_strtolower($val) == mb_strtolower($needle)) return $key;
}
return false;
}
/**
* @param int $num
* @param array $inpath
* @param bool $r Recusrive
* @param int $u Upper filesize limit
* @param int $l Lower filesize limit
* @param string $e Restrict extension
* @return array
*/
function getFileList($num, array $inpath, $r, $l, $u, $e) {
if ($e) $e = mb_split(',', $e); // Restrict extension(s)
$files = array();
foreach ($inpath as $path) {
if ($r) {
$dir = new RecursiveDirectoryIterator($path);
$d = new RecursiveIteratorIterator($dir);
}
else $d = new DirectoryIterator($path);
foreach($d as $file) {
if ($file->isFile()) {
if ($e) {
$ext = mb_strtolower(end(explode('.', $file)));
if (!in_array($ext, $e)) continue; // No match, skip
}
$f = realpath($file->getPathname());
if (filesize($f) >= $l && filesize($f) <= $u) $files[] = $f;
}
}
}
$count = count($files);
if ($count > $num) {
// We have more files than we want
// randomly reduce this to $num
for ($i = $count; $i > $num; --$i) {
$tmp = rand(0, $i);
if (isset($files[$tmp])) unset($files[$tmp]);
else array_shift($files);
shuffle($files); // Extra random
}
}
return $files;
}
// ----------------------------------------------------------------------------
// Options
// ----------------------------------------------------------------------------
// Recursive
$r = false;
if ($key = _getKey('-r')) {
$r = true;
unset($argv[$key]);
_reindex();
}
// Restrict extension(s)
$e = null;
if ($key = _getKey('-e')) {
$e = @$argv[$key + 1];
unset($argv[$key + 1], $argv[$key]);
_reindex();
}
// Preserve filenames
$p = false;
if ($key = _getKey('-p')) {
$p = true;
unset($argv[$key]);
_reindex();
}
// Upper filesize limit
$u = 1000000000;
if ($key = _getKey('-u')) {
$u = @$argv[$key + 1];
unset($argv[$key + 1], $argv[$key]);
_reindex();
}
// Lower filesize limit
$l = 0;
if ($key = _getKey('-l')) {
$l = @$argv[$key + 1];
unset($argv[$key + 1], $argv[$key]);
_reindex();
}
// ----------------------------------------------------------------------------
// Check User Input
// ----------------------------------------------------------------------------
// Check number of parameters
if ($argc < 4) rules("$argv[0] expects at least 3 parameters.");
$outpath = $argv[2];
if (!is_dir($outpath )) rules("{$outpath} is not a directory.");
else $outpath = realpath($outpath);
$inpath = array_slice($argv, 3, $argc);
foreach ($inpath as $tmp) {
if (!is_dir($tmp)) rules("{$tmp} is not a directory.");
}
$num = $argv[1];
if (!filter_var($num, FILTER_VALIDATE_INT) || $num < 1) rules("{$num} is not an integer.");
// Sanity check the options, too
if (!filter_var($u, FILTER_VALIDATE_INT) || $u < 1) rules("-u {$u} is invalid.");
if ($l < 0) rules("-l {$l} is invalid.");
// ----------------------------------------------------------------------------
// Procedure
// ----------------------------------------------------------------------------
$files = getFileList($num, $inpath, $r, $l, $u, $e);
echo count($files) . " files found, copying...\n";
foreach ($files as $file) {
// Extract the filename
if (isset($_ENV['OS']) && strripos($_ENV['OS'], "windows", 0) !== FALSE) $filename = end(explode("\\", $file));
else $filename = end(explode('/', $file));
if (!$p) {
// Randomize the name of the file
$ext = end(explode('.', $filename));
$filename = uniqid() . '.' . $ext;
}
$copy = "{$outpath}/$filename";
if (!is_file($copy)) copy($file, $copy); // Prevent overwritting
}
echo "Done!\n"
?>
--- NEW FILE: rndpack.cfg ---
[config]
caption=rndpack
author=Original code: bYtE-sMaShEr (aka Shaun Winters), Rewritten in PHP5 by XXXXXXXXX (aka XXXXXXXXXXXXX)
url=http://www.trotch.com/
description=Generate a random sample pack by scanning directories
gui=label('Options:'); text(-r -e ogg,wav,mp3 -p); label('Number of Samples:'); text(100); label('Output Path:'); folder; label('Input Path:'); folder;
|