Audio-ConvTools

 view release on metacpan or  search on metacpan

bin/audiocdmaker  view on Meta::CPAN

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
        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

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                        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

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
                destroyTmpFile
/;
 
 
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

383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
                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

409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
                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 )