Random Sample Pack Generator

Just coded this. Looking for feedback. It’s commandline.

Windows: http://codeitude.com/robosecks/rndpack.exe
Linux/Mac: http://codeitude.com/robosecks/rndpack.txt <-- rename .txt to .pl

Let me know what you think, and have fun! :drummer:

how does it work ?? :)

I can’t download the perl script, it’s trying to execute on the server. Can you zip it or something?

oic… lol… I’ll rename it :P

k… fixed it… stupid host… try again! :D

Tested the perl version.

It ran through my sample directory and randomly picked parts from samples, saving them as new samples.

Pretty cool! Except that in a lot cases I got silence.

At first I thought it was taking random chunks of files and getting the end of them (reverb) but then I also ran it on a documents directory and it copied full files, not chunks.

So, not too sure what your script is doing?

I looked at the code and looks like it’s copying files and renaming them a random number?

So, I can’t really explain the silence part… Maybe I had silent wav files in my directory?

Do you write a lot of perl? Do you know CPAN? I did a quick search and found:

http://search.cpan.org/~ni-s/Audio-1.029/Data/Data.pm

which might be fun for you?

Again, I’m not too sure what you are doing here. It’s been a couple of years since I did Perl, and I was never really good at it.

Just copying files… no advanced sample mangling going on. I figure, my biggest issue with sample collections is this: I tend to default to certain sounds for certain tasks. I figure this tool will help me inject fresh sounds into my mixes :D … it should also be useful for people running compos :)

… and you’ve obviously either got silent samples in your collection… or you didn’t bother restricting the file extensions, and picked up some text files or something :P

hey bytesmasher, how does this work on mac? do i use terminal or something? i don’t know much about commandline.

Yeah.

  
 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  
  

So you do something like: ./rndpack -E wav,aif 10 randomSamples Drums/ Leads/ Bass/

Where:

-E wav,aif are file formats to gather
10 is number of samples to gather
randomSamples is a directory where to copy samples
Drums, Leads, Bass are directories where to look samples from.

You have to be in right directory at first though…

Command “cd” will help you navigate. Command “ls” will list the directory contents. Command “pwd” will tell you in which directory you are.

So you just copy the script to your samples folder and do:

cd /your/samples/folder
./rndpack -E wav,aif 10 randomSamples Drums/ Leads/ Bass/

Sorry, I haven’t used much Mac but, I know it behaves quite similar to linux. AFAIK it uses csh as default shell.

If anyone feels like writing a small gui for this in windows or mac, be my guest… I know command line stuff can be a pain in the ass for some people. I personally prefer it for something like this because then you can wrap it in a batch file or shell script for instant gratification :D

Can I rewrite it in PHP5? :) Better yet, i’ll give you until sunday to do it and contribute it to the XRNS-PHP project so you can get your name in there. Here’s a few tips, oh and this one which hasn’t been used much in the project, yet.

-=-=-

PS: In OS X land it’s

#!/usr/bin/perl

at the top, and

chmod +x rndpack.pl

before it works.

One question: what’s the point in having such a tool written in PHP? Most people don’t run PHP scripts at the commandline/shell level :P

Most programs used to be written in Cobol and/or Fortran, too.

Things change…

That still doesn’t give me any indication of why it would be worth my time to rewrite it in PHP :rolleyes:

Three hours of my life down the toilet, enjoy?

<?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 conner_bw (aka Dac Chartrand)  
  
 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"  
  
?>  

Edit: Re-written at 2:32 PM EST to remove some redundancy in directory scanning…

Haha… well at least you have what you wanted now :P … [too_lazy_to_look]which redundancies did you remove in the directory scanning?[/too_lazy_to_look]

It was redundancy in my own code, not yours. :) I had two procedures when all i needed was just one.

In fact, my code is twice the size of yours because I didn’t end up using getopts so I had to manually check and adjust myself. Also, I use a lot of white spaces and comments.

Do you mind if I commit it to XRNS-PHP?

May I rewrite it in:

  • TCL/TK
  • Python
  • C++ (JUCE, QT)
  • Postscript :D
  • Java

Pick the language that suits the best. :)

Go right ahead :) … it’s open source as far as I’m concerned. Anything else would be silly :P

Ok, I added it! w00t.

As I don’t have windows someone else will have to check if I did the .cfg file for the GUI correctly.

get_cvs_dev_version_scripts.cmd in xrns-sf for anyone who wants to try.