Audio-ConvTools
view release on metacpan or search on metacpan
bin/audiocdmaker view on Meta::CPAN
134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251use
Pod::Usage;
use
File::NCopy;
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} = \
©Wav
;
next
;
}
errMsg(
"Not managed extension for $f"
);
pod2usage(1);
};
}
sub
showCopyrights()
{
"audiocdmaker $VERSION - Make audio CD from audio files$/"
;
$/;
"Copyright (C) 2006 Michael Hooreman <michael_AT_mijoweb_DOT_net>$/"
;
"Licensed under the terms of the GNU General Public Licence$/"
;
$/;
}
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
261262263264265266267268269270271272273274275276277278279280281
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
108109110111112113114115116117118119120121122123124125126127128use
Getopt::Long;
use
Pod::Usage;
use
File::Temp;
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
161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
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()
{
"audioconv $VERSION - Conversion beteen misc audio file formats$/"
;
$/;
"Copyright (C) 2006 Michael Hooreman <michael_AT_mijoweb_DOT_net>$/"
;
"Licensed under the terms of the GNU General Public Licence$/"
;
$/;
}
showCopyrights();
parseCmdline();
lib/Audio/ConvTools.pm view on Meta::CPAN
314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
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
;
STDERR getNowTxt() .
": INFO: "
.
$txt
. $/;
}
sub
errMsg($)
{
my
$txt
=
shift
;
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
383384385386387388389390391392393394395396397398399400401402403
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
409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
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.289 second using v1.01-cache-2.11-cpan-94b05bcf43c )