2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
4 ############################################################################
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License, version 2, as
7 # published by the Free Software Foundation.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # A copy of the GNU General Public License is available on the World Wide Web
15 # at `http://www.gnu.org/copyleft/gpl.html'. You can also obtain it by
16 # writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 # Boston, MA 02111-1307, USA.
18 ############################################################################
22 use Cwd qw
'abs_path cwd';
24 # find program directory
27 my $l = readlink or die "readlink $_: $!\n";
28 if($l =~ m
|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
31 $progdir = abs_path
($1);
33 unshift @INC, "$progdir/lib";
35 require "$progdir/soepkiptng.lib";
36 $ENV{PATH
} = "$progdir/bin:$ENV{PATH}";
46 our ($opt_C, $opt_L, $opt_M, $opt_R, $opt_T, $opt_c, $opt_d, $opt_e, $opt_f, $opt_h, $opt_l, $opt_m, $opt_n, $opt_p, $opt_q, $opt_r, $opt_v);
48 sub get_wav_params
($\
%;$);
53 sub getinfo_flac
($\
%);
54 sub getinfo_shorten
($\
%);
55 sub getinfo_musepack
($\
%);
57 sub getinfo_mplayer
($\
%);
59 sub getinfo_soepkip
($\
%);
60 sub read_eric_files
();
61 sub extract_artist_title
($\
%);
65 if(open F
, ".soepkiptng_random_pref") {
69 getopts
('Cfredqvhc:LM:R:Tpmln');
70 my $force = 1 if $opt_f;
72 my $do_delete = !$opt_d && !$opt_r;
73 if($opt_p && scalar @ARGV == 0) { push @ARGV, "."; }
74 $opt_n = " (not)" if $opt_n;
77 usage: soepkiptng_update [-defhqrvCL] [-c configfile] [-R pref] [dir...]
80 -C : don't fix artist/title/album upper/lowercase
81 -c configfile : override soepkiptng config file
82 -d : turn on debugging
83 -e : don't read "ericfiles"
84 -f : force updating of info even if song is already in database
86 -L : set last_played to current time
87 -m : skip recent files
88 -M n : add max N files
89 -n : don't actually do anything to the database
91 -r : don't recurse subdirectories
92 -R pref : set random_pref field
93 -T : test file integrity before adding
96 Directories containing the file ".nosoepkiptng" will be skipped.
100 read_configfile
(\
%conf, $opt_c);
103 # for the web interface (stupid browsers/users)
104 $SIG{'PIPE'} = 'IGNORE';
106 # disable warnings (to make MP3::Tag shut up)
108 $SIG{__WARN__
} = sub { };
111 my $dbh = DBI
->connect("DBI:$conf{'db_type'}:$conf{'db_name'}:$conf{'db_host'}", $conf{'db_user'}, $conf{'db_pass'})
112 or die "can't connect to database";
114 my (%track, %artist, %album, %tracknr);
115 read_eric_files
() unless $opt_e;
117 # get existing filenames in database
121 @paths = grep { length } map { abs_path
$_; } @ARGV;
122 @paths or exit 1; # abs_path complains for us
123 $sth = $dbh->prepare("SELECT id,filename,unix_timestamp(last_played)," .
124 "unix_timestamp(time_added),present FROM song WHERE (0" .
125 (" OR binary filename LIKE ?" x
scalar @paths) . ")");
126 $sth->execute(map { $_ . "%" } @paths);
128 $sth = $dbh->prepare("SELECT id,filename,unix_timestamp(last_played),unix_timestamp(time_added),present FROM song WHERE filename LIKE \"/%\"");
130 @paths = split /\s+/, $conf{'mp3dirs'};
132 my (%filename, %last_played, %time_added, %longname, %longname_id);
133 while(my ($id, $f, $l, $t, $pres) = $sth->fetchrow_array) {
134 $filename{$f} = $id if $pres;
135 $last_played{$f} = $l;
136 $time_added{$f} = $t;
139 $longname_id{$1} = $id;
142 # scan music directories
143 if(open(FILES
, "-|") == 0) {
145 $opt_r and push @findargs, '-maxdepth', 1;
146 exec 'find', @paths, @findargs, '-type', 'f', '-print0';
151 my ($num_added, $num_updated, $num_deleted, $num_moved, $num_skipped);
152 my (%artistid_cache, %albumid_cache);
153 while(defined(my $file = <FILES
>)) {
156 next if $file =~ m
|/\.[^/]+$|;
157 # skip zero-length files
158 next unless -s
$file;
159 $file =~ m
|(.*?
)[^/]*$|;
160 next if -e
"$1/.nosoepkiptng";
162 my ($mt, $ct) = (stat $file)[9,10];
163 if($ct > $mt) { $mt = $ct; }
164 if((time - $mt) < 30) {
165 warn "skipping $_ (too recent)\n";
170 # skip if already in database (unless "force" was given)
171 if($filename{$file} && !$force) {
173 delete $filename{$file};
177 print "$file\n" if $opt_v;
179 if(length($file) > 255) {
180 print STDERR
"Skipping $file, filename >255 chars.\n" unless $opt_q;
184 # if(++$num % 10 == 0) { print STDERR "."; }
186 # get file encoding; skip if unknown
189 getinfo_mp3
($file, %info) ||
190 getinfo_ogg
($file, %info) ||
191 getinfo_mid
($file, %info) ||
192 getinfo_wav
($file, %info) ||
193 getinfo_flac
($file, %info) ||
194 getinfo_shorten
($file, %info) ||
195 getinfo_musepack
($file, %info) ||
196 getinfo_mplayer
($file, %info) ||
197 getinfo_ac3
($file, %info) ||
198 getinfo_raw
($file, %info);
200 getinfo_soepkip
($file, %info);
204 $info{len
} = 0 + $info{len
};
205 $info{encoding
} or do {
206 print STDERR
"Filetype of $file unknown\n" unless $opt_q;
209 if($info{freq
} && ($info{freq
} < 44090 || $info{freq
} > 44110)) {
210 my $s = sprintf "%f", $info{freq
} / 1000;
212 push @
{$info{encoding_extra
}}, "${s}kHz";
214 if($info{bps
} && ($info{bps
} != 16)) {
215 push @
{$info{encoding_extra
}}, sprintf "%dbit", $info{bps
};
217 if($info{chan
} == 1) {
218 push @
{$info{encoding_extra
}}, "mono";
220 if(exists $info{encoding_extra
} && @
{$info{encoding_extra
}}) {
221 $info{encoding
} .= sprintf " (%s)", join(", ", @
{$info{encoding_extra
}});
224 # check "nolocal" setting (only files on NFS are allowed)
225 if($conf{nolocal
} && !is_nfs
($file)) {
226 die "Error: file on local filesystem: $file\n";
229 if($filename{$file}) {
230 print "Updating $file:\n";
233 # check whether an old entry (ie. the file does not exist anymore)
234 # exists with the same filename (without path)
235 $file =~ /([^\/]*)$/;
236 my $oldname = $longname{$1};
237 if($oldname && ($oldname eq $file || ! -e
$oldname)) {
239 $dbh->do("DELETE FROM song WHERE present=0 AND id!=? AND " .
240 "binary filename=?", undef, $longname_id{$1}, $file)
241 or die "can't do sql command: " . $dbh->errstr . "\n";
242 $dbh->do("UPDATE song SET present=1, filename=?, " .
243 "length=?, encoding=? WHERE id=?", undef,
244 $file, $info{len
}, $info{encoding
}, $longname_id{$1})
245 or die "can't do sql command: " . $dbh->errstr . "\n";
247 delete $filename{$oldname};
249 $oldname =~ s
|[^/]+$||;
250 print "Moving $file (from $oldname)\n";
253 if(defined($opt_M) && $num_added >= $opt_M) {
254 print "Skipping $file (max $opt_M adds)\n";
256 delete $filename{$file};
260 print "Adding $file:\n";
263 # get artist/title/track/album info
264 $file =~ m
|([^/]+/+[^/]+)\
.\w
+$|;
265 #print STDERR "1=$1\n";
267 $info{info_origin
} = "ericfile";
268 $info{artist
} = $artist{$1};
269 $info{title
} = $track{$1};
270 $info{album
} = $album{$1};
271 $info{track
} = $tracknr{$1};
272 } elsif(!$info{info_origin
}) {
273 extract_artist_title
($file, %info);
274 } elsif(!$info{title
}) {
276 extract_artist_title
($file, %i);
277 $info{title
} = $i{title
};
280 $info{artist
} =~ s/\s*\n\s*/ /g;
281 $info{album
} =~ s/\s*\n\s*/ /g;
282 $info{title
} =~ s/\s*\n\s*/ /g;
284 if(!$info{track
} && $file =~ m
~(^|/)(\d\d+)[^/]+$~) {
286 warn " (taking track ($2) from filename)\n";
290 if($lyrfile =~ s/\.[^.]+$/.txt/) {
291 if(open F
, $lyrfile) {
292 warn " (taking lyrics from .txt)\n";
295 $info{lyrics
} =~ s/\t/ /g;
296 $info{lyrics
} =~ s/ +($)/\1/mg;
297 while($info{lyrics
} =~ /^( *)\S/mg) {
298 if(length($1) < $indent || !defined($indent)) {
299 $indent = length($1);
302 $indent = " "x
$indent;
303 $info{lyrics
} =~ s/(^)$indent/\1/mg;
323 $info{len
} / 60, $info{len
} % 60,
327 # insert song into database
328 if(!$artistid_cache{$info{artist
}}) {
329 $artistid_cache{$info{artist
}} = get_id
($dbh, "artist", $info{artist
}) or die;
331 if(!$albumid_cache{$info{album
}}) {
332 $albumid_cache{$info{album
}} = get_id
($dbh, "album", $info{album
});
334 my $q = "REPLACE INTO song SET id=?, artist_id=?, title=?, album_id=?, " .
335 "track=?, present=1, filename=?, length=?, encoding=?, random_pref=?, " .
336 "last_played=from_unixtime(?), time_added=from_unixtime(?)";
338 $dbh->do($q, undef, $filename{$file}, $artistid_cache{$info{artist
}},
339 $info{title
}, $albumid_cache{$info{album
}}, $info{track
} || 0,
340 $file, $info{len
}, $info{encoding
}, $opt_R,
341 $last_played{$file} || ($opt_L?
time : 0),
342 $time_added{$file} || time)
343 or die "can't do sql command: " . $dbh->errstr . "\n";
346 $dbh->do("REPLACE INTO lyrics SET id=?, description=?, language=?, lyrics=?",
347 undef, $filename{$file} || $dbh->{'mysql_insertid'},
348 $info{description
}, $info{language
}, $info{lyrics
})
349 or die "can't do sql command: " . $dbh->errstr . "\n";
352 delete $filename{$file};
356 or die sprintf "find: %s signal %d\n",
357 (($?
& 0x7f)?
"killed by":"exit status"),
358 (($?
& 0x7f)?
($?
& 0x7f) : ($?
>> 8));
360 # delete all filenames in database what were not found on disk
361 if($do_delete && !$opt_n) {
362 $sth = $dbh->prepare("UPDATE song SET present=0 WHERE binary filename=?");
363 foreach(keys %filename) {
365 print "Deleting $_\n";
371 %4d songs added$opt_n.
372 %4d songs updated$opt_n.
373 %4d songs deleted$opt_n.
374 %4d songs moved$opt_n.
377 $num_added, $num_updated, $num_deleted, $num_moved, $num_skipped
378 unless $opt_q && $num_added == 0 && $num_updated == 0
379 && $num_deleted == 0 && $num_moved == 0;
381 print "Optimizing Tables.\n" unless $opt_q;
382 $dbh->do("optimize table song");
383 $dbh->do("optimize table album");
384 $dbh->do("optimize table artist");
391 my ($file, $info) = @_;
393 if($file =~ m
|(.*/)| and -e "$1/.noid3
") { return undef; }
395 my $id3 = MP3::Tag->new($file);
396 $id3->config("autoinfo
", "ID3v2
", "ID3v1
");
397 my ($t, $tr, $a, $alb) = $id3->autoinfo();
400 my $tag = $id3->{ID3v2};
402 my $uslt = $tag->getFrame('USLT');
404 $info->{language} = $uslt->{Language};
405 $info->{lyrics} = $uslt->{Text};
406 $info->{description} = $uslt->{Description};
411 $a =~ s/([^'\w\xc0-\xff]|^)(\w)(\S*)\b/\1\U\2\L\3/g unless $a =~ /[A-Z]/ || $opt_C;
413 $t =~ s/([^'\w\xc0-\xff]|^)(\w)(\S*)\b/\1\U\2\L\3/g unless $t =~ /[A-Z]/ || $opt_C;
415 $alb =~ s/([^'\w\xc0-\xff]|^)(\w)(\S*)\b/\1\U\2\L\3/g unless $alb =~ /[A-Z]/ || $opt_C;
417 $a && $t or return undef;
419 $info->{info_origin} = $id3->{ID3v2}? "ID3v2
" : "ID3
";
420 $info->{artist} = $a;
422 $info->{album} = $alb || "";
423 $info->{track} = $tr;
427 # used by musepack; see http://www.personal.uni-jena.de/~pfk/mpp/sv8/apetag.html
429 my ($file, $info, $offset) = @_;
434 open F, $file or return undef;
435 seek F, $offset, ($offset < 0? 2 : 0) or goto OUT;
436 read F, $apetag, 32 or goto OUT;
437 $apetag =~ /^APETAGEX/ or goto OUT;
438 my ($version, $length, $tagcount, $flags) = unpack "VVVV
", substr $apetag, 8;
439 $version == 1000 || $version == 2000 or goto OUT;
440 $length > 32 or goto OUT;
441 unless(($flags >> 29) & 1) {
442 seek F, -$length, 1 or goto OUT;
444 read F, $apetag, $length - 32 or goto OUT;
446 for($pos = 0, $tag = 0; $pos < $length && $tag < $tagcount; $tag++) {
447 my ($len, $fl, $name) = unpack "VVZ
*", substr $apetag, $pos;
448 $pos += 8 + length($name) + 1;
449 if($name =~ /^(Artist|Title|Album|Track)$/) {
450 $info->{lc $1} = substr $apetag, $pos, $len;
456 $info->{info_origin} = sprintf "APE
%d.0", $version / 1000;
462 # getinfo_* returns (description, length[sec], sfreq[khz], channels
463 # or undef if it isn't the correct type
465 sub getinfo_mp3($\%) {
466 my ($file, $info) = @_;
468 $file =~ /\.mp[1-3]$/i
471 # open file to check Xing VBR tag
472 open STDIN, $file or do {
473 print STDERR "$file: $!\n";
479 read STDIN, $buf, 10;
480 my ($tag, $ver1, $ver2, $flags, $s1, $s2, $s3, $s4) = unpack "a3CCCCCCC
", $buf;
482 my $size = (((((($s1 << 7) | $s2) << 7) | $s3) << 7) | $s4) + 10;
483 seek STDIN, $size, 1;
488 read STDIN, $buf, 12;
490 my ($xtag, $xflags, $xframes);
491 ($xtag, $xflags, $xframes) = unpack("a4NN
", $buf);
492 if($xtag ne "Xing
") { $xframes = 0; }
494 my $mp3info = get_mp3info($file);
495 $info->{len} = $mp3info->{MM} * 60 + $mp3info->{SS};
496 $info->{freq} = $mp3info->{FREQUENCY} * 1000;
497 $info->{chan} = $mp3info->{STEREO}? 2 : 1;
499 my $bitrate = $mp3info->{BITRATE};
501 $info->{len} = $xframes * ($mp3info->{VERSION}? 1152 : 576) /
503 $bitrate = int(8 * (-s $file) / $info->{len} / 1000);
507 open F, "mp3_check
-|";
509 if($l =~ /^SONG_LENGTH\s+(\d+):(\d+)/) {
510 $info->{len} = $1 * 60 + $2;
512 if($l =~ /^VBR_AVERAGE\s+(\d+)/) {
518 if($mp3info->{LAYER} == 3) {
519 $info->{encoding} = "MP3
";
521 $info->{encoding} = sprintf "MPEG
-%d Layer
%d",
522 $mp3info->{VERSION}, $mp3info->{LAYER};
524 push @{$info->{encoding_extra}}, sprintf("%dkb/s
", $bitrate);
525 if($xframes) { push @{$info->{encoding_extra}}, "VBR
"; }
527 get_id3($file, %$info);
533 sub getinfo_ogg($\%) {
534 my ($file, $info) = @_;
540 if(open(F, "-|") == 0) {
541 exec "ogginfo
", $file;
547 if(/^Rate:\s*(\d+)/i) {
549 } elsif(/^Channels:\s*(\d+)/) {
551 } elsif(/^\s+Playback length:\s*(\d+)m:(\d+)/i) {
552 $info->{len} = $1 * 60 + $2;
553 } elsif(/^\s+Average bitrate:\s*(\d+(\.\d+)?)/i) {
555 } elsif(/^\s+artist=(.*)/) {
556 $info->{artist} = $1;
557 } elsif(/^\s+title=(.*)/) {
559 } elsif(/^\s+album=(.*)/) {
561 } elsif(/^\s+tracknumber=(.*)/) {
566 $info->{freq} or return undef;
567 $info->{encoding} = "Ogg
-Vorbis
";
568 push @{$info->{encoding_extra}}, sprintf("%dkb/s
", $br);
569 $info->{info_origin} = "Ogg
-Vorbis
" if $info->{artist} && $info->{title};
573 sub getinfo_wav($\%) {
574 my ($file, $info) = @_;
579 get_wav_params($file, %$info) or return undef;
583 sub getinfo_mid($\%) {
584 my ($file, $info) = @_;
586 $file =~ /\.(mid|rcp|r36|g18|g36|mod)$/i
589 $info->{encoding} = "Midi
";
593 sub getinfo_flac($\%) {
594 my ($file, $info) = @_;
599 my $filesize = -s $file or return undef;
602 system "flac
", "-t
", $file;
604 warn "$file: integrity test failed
, ignoring
\n";
609 if(open(F, "-|") == 0) {
610 open STDERR, ">/dev/null
";
618 --show-tag=tracknumber
623 my ($totsamp, @rest, @a);
624 ($info->{freq}, $info->{chan}, $totsamp, $info->{bps}, @rest) = <F>;
626 if(s/^title=(.*\S)//i) { $info->{title} = $1; }
627 elsif(s/^tracknumber=(.*\S)//i) { $info->{track} = $1; }
628 elsif(s/^album=(.*\S)//i) { $info->{album} = $1; }
629 elsif(s/^artist=(.*\S)//i) { push @a, $1; }
631 $info->{artist} = join " & ", @a;
633 my $totbytes = $totsamp * $info->{chan} * $info->{bps} / 8;
634 $info->{encoding} = "flac
" . ($totbytes? sprintf(" (%d%%)", 100 * $filesize / $totbytes) : "");
635 $info->{len} = $info->{freq}? $totsamp / $info->{freq} : 0;
636 $info->{info_origin} = "flac
" if $info->{artist} && $info->{title};
640 sub getinfo_shorten($\%) {
641 my ($file, $info) = @_;
646 $info->{encoding} = "Shorten
";
648 $info->{freq} = 44100;
654 sub getinfo_musepack($\%) {
655 my ($file, $info) = @_;
659 $file =~ /\.mp[cp+]$/i
661 open F, $file or do {
669 $buf =~ /^MP\+/ or return undef;
670 my ($version, $frames, $flags) = unpack "VVV
", $buf;
673 warn "$_: unknown version
: SV
$version\n";
676 $info->{encoding} = "Musepack
";
677 $info->{freq} = (44100, 48000, 37800, 32000)[($flags >> 16) & 3];
678 $info->{len} = $frames * 1152 / $info->{freq};
679 push @{$info->{encoding_extra}}, sprintf "%dkb/s", (8 * (-s $file) / $info->{len
} + 500) / 1000;
682 get_ape
($file, %$info, 0) || # APE tag at beginning
683 get_ape
($file, %$info, -32) || # APE tag at end
684 get_ape
($file, %$info, -160) || # APE tag at end followed by ID3 tag
685 get_id3
($file, %$info);
689 sub getinfo_raw
($\
%) {
690 my ($file, $info) = @_;
695 $info->{encoding
} = "raw";
696 $info->{len
} = (-s
$file) / 176400;
697 $info->{freq
} = 44100;
702 sub getinfo_mplayer
($\
%) {
703 my ($file, $info) = @_;
707 $file =~ /\.(mpe?g|avi|asx|asf|vob|wmv|ra?m|ra|mov|m2v|wma|aac|m4a|m4b|mp4|flv|ape|aiff)$/i
711 if(open(F
, "-|") == 0) {
712 open STDIN
, "/dev/null";
713 open STDERR
, ">&STDOUT";
714 delete $ENV{DISPLAY
};
715 exec qw
"mplayer -identify -vo null -ao null -frames 0", $file;
720 /^\s+author:\s*(.*)/i and $info->{artist
} = $1;
721 /^\s+(name|title):\s*(.*)/i and $info->{title
} = $2;
722 /^\s+album:\s*(.*)/i and $info->{album
} = $1;
723 /^\s+track:\s*(\d+)/i and $info->{track
} = $1;
724 /^ID_(\w+)=(.*)/ and $prop{lc($1)} = $2;
727 if($file =~ /\.(ra?m|ra)$/i) {
728 $info->{encoding
} = $prop{video_format
}?
"RealVideo":"RealAudio";
730 $info->{encoding
} = ($prop{video_format
}?
"Video: ":"") . $1;
732 $info->{info_origin
} = "Clip Info" if $info->{artist
} || $info->{title
};
733 if($prop{video_width
} && $prop{video_height
}) {
734 $info->{encoding
} .= " ($prop{video_width}x$prop{video_height})";
736 if($prop{video_format
}) {
737 push @
{$info->{encoding_extra
}}, sprintf("%s+%skb/s",
738 $prop{video_bitrate
}?
int(($prop{video_bitrate
} + 500) / 1000) : "?",
739 $prop{audio_bitrate
}?
int(($prop{audio_bitrate
} + 500) / 1000) : "?");
744 if($prop{audio_format
} >= 0x160 && $prop{audio_format
} <= 0x163) {
745 my $wma = $prop{audio_format
} - 0x160 + 1;
746 $wma = 3 if $wma == 4;
747 $info->{encoding
} = "WMA$wma";
748 } elsif($prop{audio_format
} eq "mp4a") {
749 $info->{encoding
} = "AAC";
750 } elsif($prop{audio_format
} eq "twos" && $file =~ /\.aiff$/i) {
751 $info->{encoding
} = "AIFF";
752 delete $prop{audio_bitrate
};
753 } elsif($prop{audio_format
} eq "alac" || $prop{audio_format
} eq "APE") {
754 $info->{encoding
} = uc $prop{audio_format
};
755 my $uncompbytes = $prop{length} * $prop{audio_rate
} * $prop{audio_nch
} * 2;
757 $info->{encoding
} .= sprintf " (%d%%)", 100 * (-s
$file) / $uncompbytes;
760 push @
{$info->{encoding_extra
}}, sprintf("%dkb/s", ($prop{audio_bitrate
} + 500) / 1000)
761 if $prop{audio_bitrate
};
762 $info->{freq
} = $prop{audio_freq
};
763 $info->{chan
} = $prop{audio_nch
};
765 if(exists $info->{encoding_extra
} && @
{$info->{encoding_extra
}} == 1 && ${$info->{encoding_extra
}}[0] =~ m
|115\d\
+224kb
/s
|) {
766 if($info->{encoding
} =~ s/mpg \(352x288\)/VCD (PAL)/ ||
767 $info->{encoding
} =~ s/mpg \(352x240\)/VCD (NTSC)/) {
768 delete $info->{encoding_extra
};
771 $info->{len
} = $prop{length};
775 sub getinfo_ac3
($\
%) {
776 my ($file, $info) = @_;
781 $info->{encoding
} = "AC3";
788 sub getinfo_soepkip
($\
%) {
789 my ($file, $info) = @_;
795 open F
, "$dir/.soepkiptng_info" or return undef;
798 if(/^\[(.+)\]/ && $1 eq $file) {
799 $info->{info_origin
} = ".soepkiptng_info";
803 /^(artist|album|track|title)\s*=\s*(.*)/
804 and $info->{$1} = $2;
814 # returns samplefreq, channels, seconds
815 sub get_wav_params
($\
%;$) {
816 my ($file, $info, $offset) = @_;
817 $offset = 0 + $offset;
821 open F
, $file or return undef;
823 if($offset) { read(F
, $buf, $offset) or last; }
824 read(F
, $buf, 12) or last;
825 my ($riff, $len, $wave) = unpack("a4Va4", $buf);
826 last if $riff ne 'RIFF' || $wave ne 'WAVE';
831 read(F
, $buf, 8) or last FILE
;
832 ($type, $len) = unpack("a4V", $buf);
833 last if $type eq 'fmt ';
836 my $r = $len < 4096?
$len : 4096;
837 $r = read F
, $buf, $r or last FILE
;
841 read(F
, $buf, $len) or last;
842 my ($fmt, $chan, $freq, $bytespersec, $align, $bitspersample, $cbsize, $validbitspersample, undef, $subfmt, $subguid) =
843 unpack("vvVVvvvvVva14", $buf);
844 #print STDERR "fmt=$fmt ch=$chan f=$freq byps=$bytespersec al=$align bps=$bitspersample cb=$cbsize sfmt=$subfmt\n";
845 if($len >= 18 && $fmt == 0xfffe && $cbsize == 22 && $subguid eq "\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71") {
846 $bitspersample = $validbitspersample;
849 my $encoding = "WAV";
850 if($fmt == 3) { $encoding .= " (float)"; }
851 elsif($fmt == 6) { $encoding .= " (A-law)"; }
852 elsif($fmt == 7) { $encoding .= " (ยต-law)"; }
853 elsif($fmt != 1) { $encoding .= sprintf " (fmt %d)", $fmt; }
857 read(F
, $buf, 8) or last FILE
;
858 ($type, $len) = unpack("a4V", $buf);
859 last if $type eq 'data';
861 my $r = $len < 4096?
$len : 4096;
862 $r = read F
, $buf, $r or last FILE
;
867 $info->{len
} = int($len / $bytespersec + 0.5);
868 $info->{freq
} = $freq;
869 $info->{chan
} = $chan;
870 $info->{bps
} = $bitspersample;
871 $info->{encoding
} = $encoding;
878 sub read_eric_files
() {
879 foreach my $file (split /\s+/, $conf{'description_files'}) {
880 my ($artist, $album, $dirname);
881 open ALB
, $file or die "$file: $!\n";
883 /\S/ or do { $artist = $album = $dirname = ""; next; };
884 /^artist\s*(.*?)\s*$/ and do { $artist = $1; next; };
885 /^title\s*(.*?)\s*$/ and do { $album = $1; next; };
886 if(/^dirname\s+(.*\S)/) {
890 (my $artist_s = $dirname) =~ s/-.*//;
892 /^##\s*(.*\S)/ and do {
896 if(s/^track\s+(\S+)\.\w+\s*//) {
899 $a =~ s/^(\d+)-([^-]+)-.*/\2/;
900 $tracknr{"$dirname/$filename"} = $1;
901 #print STDERR "a=$a artist_s=$artist_s artist=$artist realartist=$realartist\n";
903 $artist{"$dirname/$filename"} = $realartist;
905 } elsif($a eq $artist_s) {
906 $artist{"$dirname/$filename"} = $artist;
909 $a =~ s/\b(\w)/\U\1/g;
910 $artist{"$dirname/$filename"} = $a;
919 $track{"$dirname/$filename"} = $_;
920 #print STDERR "{$dirname/$filename} = $_\n";
921 #print qq~\$track{"$dirname/$filename"} = $_;\n~ if /magic/i;
922 $album{"$dirname/$filename"} = $album;
923 #print STDERR "$dirname/$filename $album\n";
933 sub extract_artist_title
($\
%) {
934 my ($file, $info) = @_;
935 my ($a, $t, $tr, $alb);
938 $info->{info_origin
} = "filename";
945 $file =~ s/(.*)\..*/\1/;
947 if(-e
"$p/.andre" || ($file !~ /-/ && $file =~ /\
..*\
./)) {
949 $file =~ s/_ddd\.hq//;
952 $info->{info_origin
} = "filename[andre]";
956 $pp = '' if -e
"$p/.noalbum";
957 if(open F
, "$p/.album") {
963 if($file =~ s/^\s*(\d\d)(\D)/\2/i || # nummer weg, 2 digits
964 $file =~ s/^\s*(\d{0,3})[^a-z0-9]+//i) # nummer weg, 1-3 digits met separator
968 $file =~ s/^[^a-z]+//i;
973 if($file =~ /[\x80-\xff]{2,}/) {
975 eval { $dfile = decode_utf8
($file, Encode
::FB_CROAK
); };
977 $file = encode
("iso-8859-1", $dfile);
978 $info->{info_origin
} .= "(utf8)";
982 if($file =~ /[_\s]*-[_\s]*/) {
984 if($pp) { ($alb = $pp) =~ s/.*-[_\s]*//; }
986 if(!$alb && $pp =~ s/-[_\s]*(.*)//) { $alb = $1; }
987 ($a, $t) = ($pp, $file);
989 if(-e "$p/.reverse") { ($a, $t) = ($t, $a); }
991 $info->{artist} = cleanup_name($a);
992 $info->{title} = cleanup_name($t);
993 $info->{album} = cleanup_name($alb);
994 $info->{track} = $tr;
1002 # major device number must be 0
1003 my $maj = (stat $file)[0] >> 8;