Audio-ConvTools

 view release on metacpan or  search on metacpan

bin/audiocdmaker  view on Meta::CPAN

use Pod::Usage;
use File::NCopy;
use Audio::ConvTools qw/:DEFAULT :Log :Tmp/;

my $VERSION = Audio::ConvTools::getVersion();

my @listFiles;
my $device;
my $speed;

sub parseCmdline()
{
	my $helpOpt;
	my $inFmt;
	my $outFmt;

	#reading the options
	GetOptions(
		"help"     => \$helpOpt,
		"device:s" => \$device,
		"speed:i"  => \$speed
	) or pod2usage(1);
	$helpOpt and pod2usage(0);
	$device or pod2usage(1);
	$speed  or pod2usage(1);

	#the @ARGV was shifted for each option
	$#ARGV>=0 or pod2usage(1);
	@listFiles = map { {file=>$_,} } @ARGV;
}

sub copyWav($$)
{
	my ($src, $dst) = @_;
	my $copyer = new File::NCopy('preserve'=>1);
	$copyer->copy($src, $dst) or do {
		errMsg("Cannot copy $src to $dst: $!");
		return 0;
	};
	return 1;
}

sub analyseFilesExtensions()
{
	logMsg("Analysing files types");
	foreach my $f (@listFiles) {
		if ($f->{file} =~ /^(.*)\.mp3$/i) {
			$f->{toWav} = \&mp32wav;
			next;
		}
		if ($f->{file} =~ /^(.*)\.ogg$/i) {
			$f->{toWav} = \&ogg2wav;
			next;
		}
		if ($f->{file} =~ /^(.*)\.wav$/i) {
			$f->{toWav} = \&copyWav;
			next;
		}
		errMsg("Not managed extension for $f");
		pod2usage(1);
	};
}

sub showCopyrights()
{
	print "audiocdmaker $VERSION - Make audio CD from audio files$/";
	print $/;
	print "Copyright (C) 2006 Michael Hooreman <michael_AT_mijoweb_DOT_net>$/";
	print "Licensed under the terms of the GNU General Public Licence$/";
	print $/;
}

sub prepareAndBurn()
{
	logMsg("Processing");
	my @normalized = ();
	push @normalized, makeSampledWav($_) foreach (@listFiles);
	my $files = join " ", map {"$_"} @normalized;
	my $status;
	$status = normalizeFiles($files);
	burnCd($files) if $status;
	destroyTmpFile(\$_) foreach @normalized;
	return $status;
}

sub burnCd($)
{
	my $files = shift;
	logMsg("Burning CD");
	if (system("cdrecord speed=$speed dev=$device -pad -audio $files")) {
		errMsg("Cannot burn");
		return 0;
	}
	return 1;
}

sub normalizeFiles($)
{
	my $files = shift;
	logMsg("Normalizing tracks");
	if (system("normalize -m $files")) {
		errMsg("Cannot normalize files");
		return 0;
	}
	return 1;
}

sub resample($)
#resample a wav file to 44100 Hz
{
	my $file = shift;
	my $tmp = getTmpFile('.wav');
	my $tmpName = $tmp."";

	logMsg("Resampling $file to 44100 Hz");

	#we make a temp file who is a copy of the input,
	#and then we remove the input

bin/audiocdmaker  view on Meta::CPAN

	if (system("sox $tmpName -r 44100 -c 2 $file")) {
		errMsg("Cannot resample $file");
		return 0;
	}

	destroyTmpFile(\$tmp);

	return 1;
}

sub makeSampledWav($)
{
	my $in = shift;
	my $out = getTmpFile('.wav');
	my $toWav = $in->{toWav};
	my $inFile = $in->{file};
	my $outFile = $out.""; #this is a File::Tmp=>textual context=file name
	logMsg("Making samled file from $inFile");
	&$toWav($inFile, $outFile) or exit(1);
	resample($outFile) or exit(1);
	return $out;

bin/audioconv  view on Meta::CPAN

use Getopt::Long;
use Pod::Usage;
use File::Temp;
use Audio::ConvTools qw/:DEFAULT :Log :Tmp/;

my $VERSION = Audio::ConvTools::getVersion();

my @listFiles;
my $converter;

sub parseCmdline()
{
	my $helpOpt;
	my $inFmt;
	my $outFmt;

	#reading the options
	GetOptions(
		"help"    => \$helpOpt,
		"in:s"    => \$inFmt,
		"out:s"   => \$outFmt

bin/audioconv  view on Meta::CPAN

			last SWITCH;
		};
		($inFmt eq "wav" and $outFmt eq "mp3") and do{
			$converter = \&wav2mp3;
			last SWITCH;
		};
		die("Assertion error: we don't have to come here!");
	};
}

sub allowedFormat($)
{
	my $fmt = shift;
	return 0 unless defined $fmt;
	return 1 if $fmt eq "ogg";
	return 1 if $fmt eq "mp3";
	return 1 if $fmt eq "wav";
	return 0;
}

sub runConversions()
{
	logMsg("Beginnning of the conversions");
	foreach my $f (@listFiles) {
		my $st;
		logMsg("Converting $f");
		if (&$converter($f)) {
			logMsg("Conversion of $f done");
		} else {
			errMsg("Problem while converting $f");
		}
	}
	logMsg("End of the conversions");
}

sub showCopyrights()
{
	print "audioconv $VERSION - Conversion beteen misc audio file formats$/";
	print $/;
	print "Copyright (C) 2006 Michael Hooreman <michael_AT_mijoweb_DOT_net>$/";
	print "Licensed under the terms of the GNU General Public Licence$/";
	print $/;
}

showCopyrights();
parseCmdline();

lib/Audio/ConvTools.pm  view on Meta::CPAN

		destroyTmpFile
/;

use File::Temp;
use String::ShellQuote;

BEGIN {
	#$Exporter::Verbose = 1
};

sub getVersion()
{
	return $VERSION;
}

sub getNowTxt()
{
	my ($s, $m, $h, $D, $M, $Y) = localtime(time);
	return sprintf(
		"%04d-%02d-%02d %02d:%02d:%02d",
		$Y+1900, $M+1, $D, $h, $m, $s
	);
}

sub logMsg($)
{
	my $txt = shift;
	print STDERR getNowTxt() . ": INFO: " . $txt . $/;
}

sub errMsg($)
{
	my $txt = shift;
	print STDERR getNowTxt() . ": ERROR: " . $txt . $/;
}

sub getTmpFile($)
{
	my $extension = shift;
	my $tmp = new File::Temp(
		SUFFIX=>$extension,
		UNLINK=>1         , #to automatically remove when out of scope
	);
	return $tmp;
}

sub destroyTmpFile($)
{
	my $pTmp = shift;
	$$pTmp->cleanup(); #to be sure
	$$pTmp = undef; #old tmp object is out of scope => automatically cleaned
}

sub mp32ogg($)
{
	my $inFile = shift;
	my $outFile;
	my $tmpFile;
	my $status;

	($inFile =~ /^(.*)\.[Mm][Pp]3$/) or do {
		errMsg("$inFile is not a mp3 file");
		return 0;
	};

lib/Audio/ConvTools.pm  view on Meta::CPAN

		errMsg("Cannot create temp wav file");
		return $status;
	}

	$status = wav2ogg($tmpFile, $outFile);
	destroyTmpFile(\$tmpFile);

	return $status;
}

sub ogg2mp3($)
{
	my $inFile = shift;
	my $outFile;
	my $tmpFile;
	my $status;

	($inFile =~ /^(.*)\.[Oo][Gg][Gg]$/) or do {
		errMsg("$inFile is not a ogg file");
		return 0;
	};

lib/Audio/ConvTools.pm  view on Meta::CPAN

		errMsg("Cannot create temp wav file");
		return $status;
	}

	$status = wav2mp3($tmpFile, $outFile);
	destroyTmpFile(\$tmpFile);

	return $status;
}

sub mp32wav($;$)
{
	my $inFile = shift;
	my $outFile = shift;
	my $status;
	($inFile =~ /^(.*)\.[Mm][Pp]3$/) or do {
		errMsg("$inFile is not an mp3 file");
		return 0;
	};
	$outFile = "$1.wav" unless defined $outFile;
	$status = system(
		"mpg321 -w " . shell_quote($outFile) . " " . shell_quote($inFile)
	);
	return ($status==0);
}

sub ogg2wav($;$)
{
	my $inFile = shift;
	my $outFile = shift;
	my $status;
	($inFile =~ /^(.*)\.[Oo][Gg][Gg]$/) or do {
		errMsg("$inFile is not an ogg vorbis file");
		return 0;
	};
	$outFile = "$1.wav" unless defined $outFile;
	$status = system(
		"oggdec " . shell_quote($inFile) . " -o " . shell_quote($outFile)
	);
	return ($status==0);
}

sub wav2ogg($;$)
{
	my $inFile = shift;
	my $outFile = shift;
	my $status;
	($inFile =~ /^(.*)\.[Ww][Aa][Vv]$/) or do {
		errMsg("$inFile is not an wav file");
		return 0;
	};
	$outFile = "$1.ogg" unless defined $outFile;
	$status = system(
		"oggenc -q 10 -o " . shell_quote($outFile) . " " . shell_quote($inFile)
	);
	return ($status==0);
}

sub wav2mp3($;$)
{
	my $inFile = shift;
	my $outFile = shift;
	my $status;
	($inFile =~ /^(.*)\.[Ww][Aa][Vv]$/) or do {
		errMsg("$inFile is not an wav file");
		return 0;
	};
	$outFile = "$1.mp3" unless defined $outFile;
	$status = system(



( run in 0.373 second using v1.01-cache-2.11-cpan-94b05bcf43c )