#!perl use Getopt::Long; use File::Copy; my $recursive = 1; my $preserve = 0; my $ulimit = 0; my $llimit = 0; $result = GetOptions ("preserve!" => \$preserve, "recursive!" => \$recursive, "upper=i" => \$ulimit, "lower=i" => \$llimit, "extensions=s" => \@extensions); $numargs = $#ARGV + 1; if ($numargs>=3){ $numsamp = shift(@ARGV); $outpath = shift(@ARGV); @inpath = @ARGV; foreach (@inpath) { getFileList($_); } if (@File_List != "") { if ($ulimit==0) {$ulimit = 1000000000}; for ($count = 1; $count <= $numsamp; $count++) { $rndnum = int(rand(scalar(@File_List)-1))+1; $oldfile = $File_List[$rndnum]; if ((-s $oldfile)<=$ulimit && (-s $oldfile)>=$llimit){ $fn = $oldfile; $fn =~ s/^.*[\/](.*)$/\1/; $ext = $oldfile; $ext =~ s/^.*\.(.*)$/.\1/; if ($ext eq ".".$oldfile) {$ext="";}; $newfile= "$outpath/".int(rand(100000000)).$ext; if ($preserve==1) { $newfile = "$outpath/$fn";}; $err=0; copy($oldfile, $newfile) or $err=1; if ($err) {print "File cannot be copied.";$count--;} }else{$count--;} } }else{ print "Error: No files found in source paths (".join(",",@inpath).")"; } }else{ printRules(); } sub printRules { print 'Random Sample Pack Generator Code: bYtE-sMaShEr (aka Shaun Winters) 2008 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'; } sub getFileList { $exthash = ",".join(",",@extensions).","; $exthash =~ s/[.]//; $internal_path = $_[0]; @Directory_Parent = $internal_path; while (@Directory_Parent) { $directory = shift (@Directory_Parent); opendir(DIR, $directory) || next; while (defined($child = readdir(DIR))) { if ((-d "$directory/$child") && ($child ne ".") && ($child ne "..")) { if ($recursive==1) {push(@Directory_Parent, "$directory/$child")}; } if ((-f "$directory/$child")) { $ext=$child; $ext =~ s/^.*\.(.*)$/\1/; if (($exthash =~ m/,($ext),/) || ($exthash=",,")) { push (@File_List, "$directory/$child"); } } } closedir(DIR); } }