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 print "$file\n" if $opt_v;
172 if(length($file) > 255) {
173 print STDERR
"Skipping $file, filename >255 chars.\n" unless $opt_q;
177 # if(++$num % 10 == 0) { print STDERR "."; }
179 # get file encoding; skip if unknown
182 getinfo_mp3
($file, %info) ||
183 getinfo_ogg
($file, %info) ||
184 getinfo_mid
($file, %info) ||
185 getinfo_wav
($file, %info) ||
186 getinfo_flac
($file, %info) ||
187 getinfo_shorten
($file, %info) ||
188 getinfo_musepack
($file, %info) ||
189 getinfo_mplayer
($file, %info) ||
190 getinfo_ac3
($file, %info) ||
191 getinfo_raw
($file, %info);
193 getinfo_soepkip
($file, %info);
197 $info{len
} = 0 + $info{len
};
198 $info{encoding
} or do {
199 print STDERR
"Filetype of $file unknown\n" unless $opt_q;
202 if($info{freq
} && ($info{freq
} < 44090 || $info{freq
} > 44110)) {
203 my $s = sprintf "%f", $info{freq
} / 1000;
205 push @
{$info{encoding_extra
}}, "${s}kHz";
207 if($info{bps
} && ($info{bps
} != 16)) {
208 push @
{$info{encoding_extra
}}, sprintf "%dbit", $info{bps
};
210 if($info{chan
} == 1) {
211 push @
{$info{encoding_extra
}}, "mono";
213 if(exists $info{encoding_extra
} && @
{$info{encoding_extra
}}) {
214 $info{encoding
} .= sprintf " (%s)", join(", ", @
{$info{encoding_extra
}});
217 # check "nolocal" setting (only files on NFS are allowed)
218 if($conf{nolocal
} && !is_nfs
($file)) {
219 die "Error: file on local filesystem: $file\n";
222 # skip if already in database (unless "force" was given)
223 if($filename{$file}) {
225 print "Updating $file:\n";
229 delete $filename{$file};
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;
580 $info->{encoding} = "WAV
";
584 sub getinfo_mid($\%) {
585 my ($file, $info) = @_;
587 $file =~ /\.(mid|rcp|r36|g18|g36|mod)$/i
590 $info->{encoding} = "Midi
";
594 sub getinfo_flac($\%) {
595 my ($file, $info) = @_;
600 my $filesize = -s $file or return undef;
603 system "flac
", "-t
", $file;
605 warn "$file: integrity test failed
, ignoring
\n";
610 if(open(F, "-|") == 0) {
611 open STDERR, ">/dev/null
";
619 --show-tag=tracknumber
624 my ($totsamp, @rest, @a);
625 ($info->{freq}, $info->{chan}, $totsamp, $info->{bps}, @rest) = <F>;
627 if(s/^title=(.*\S)//i) { $info->{title} = $1; }
628 elsif(s/^tracknumber=(.*\S)//i) { $info->{track} = $1; }
629 elsif(s/^album=(.*\S)//i) { $info->{album} = $1; }
630 elsif(s/^artist=(.*\S)//i) { push @a, $1; }
632 $info->{artist} = join " & ", @a;
634 my $totbytes = $totsamp * $info->{chan} * $info->{bps} / 8;
635 $info->{encoding} = "flac
" . ($totbytes? sprintf(" (%d%%)", 100 * $filesize / $totbytes) : "");
636 $info->{len} = $info->{freq}? $totsamp / $info->{freq} : 0;
637 $info->{info_origin} = "flac
" if $info->{artist} && $info->{title};
641 sub getinfo_shorten($\%) {
642 my ($file, $info) = @_;
647 $info->{encoding} = "Shorten
";
649 $info->{freq} = 44100;
655 sub getinfo_musepack($\%) {
656 my ($file, $info) = @_;
660 $file =~ /\.mp[cp+]$/i
662 open F, $file or do {
670 $buf =~ /^MP\+/ or return undef;
671 my ($version, $frames, $flags) = unpack "VVV
", $buf;
674 warn "$_: unknown version
: SV
$version\n";
677 $info->{encoding} = "Musepack
";
678 $info->{freq} = (44100, 48000, 37800, 32000)[($flags >> 16) & 3];
679 $info->{len} = $frames * 1152 / $info->{freq};
680 push @{$info->{encoding_extra}}, sprintf "%dkb/s", (8 * (-s $file) / $info->{len
} + 500) / 1000;
683 get_ape
($file, %$info, 0) || # APE tag at beginning
684 get_ape
($file, %$info, -32) || # APE tag at end
685 get_ape
($file, %$info, -160) || # APE tag at end followed by ID3 tag
686 get_id3
($file, %$info);
690 sub getinfo_raw
($\
%) {
691 my ($file, $info) = @_;
696 $info->{encoding
} = "raw";
697 $info->{len
} = (-s
$file) / 176400;
698 $info->{freq
} = 44100;
703 sub getinfo_mplayer
($\
%) {
704 my ($file, $info) = @_;
708 $file =~ /\.(mpe?g|avi|asx|asf|vob|wmv|ra?m|ra|mov|m2v|wma|aac|m4a|m4b|mp4|flv|ape)$/i
712 if(open(F
, "-|") == 0) {
713 open STDIN
, "/dev/null";
714 open STDERR
, ">&STDOUT";
715 delete $ENV{DISPLAY
};
716 exec qw
"mplayer -identify -vo null -ao null -frames 0", $file;
721 /^\s+author:\s*(.*)/i and $info->{artist
} = $1;
722 /^\s+(name|title):\s*(.*)/i and $info->{title
} = $2;
723 /^\s+album:\s*(.*)/i and $info->{album
} = $1;
724 /^\s+track:\s*(\d+)/i and $info->{track
} = $1;
725 /^ID_(\w+)=(.*)/ and $prop{lc($1)} = $2;
728 if($file =~ /\.(ra?m|ra)$/i) {
729 $info->{encoding
} = $prop{video_format
}?
"RealVideo":"RealAudio";
731 $info->{encoding
} = ($prop{video_format
}?
"Video: ":"") . $1;
733 $info->{info_origin
} = "Clip Info" if $info->{artist
} || $info->{title
};
734 if($prop{video_width
} && $prop{video_height
}) {
735 $info->{encoding
} .= " ($prop{video_width}x$prop{video_height})";
737 if($prop{video_format
}) {
738 push @
{$info->{encoding_extra
}}, sprintf("%s+%skb/s",
739 $prop{video_bitrate
}?
int(($prop{video_bitrate
} + 500) / 1000) : "?",
740 $prop{audio_bitrate
}?
int(($prop{audio_bitrate
} + 500) / 1000) : "?");
745 if($prop{audio_format
} >= 0x160 && $prop{audio_format
} <= 0x163) {
746 my $wma = $prop{audio_format
} - 0x160 + 1;
747 $wma = 3 if $wma == 4;
748 $info->{encoding
} = "WMA$wma";
749 } elsif($prop{audio_format
} eq "mp4a") {
750 $info->{encoding
} = "AAC";
751 } elsif($prop{audio_format
} eq "alac" || $prop{audio_format
} eq "APE") {
752 $info->{encoding
} = uc $prop{audio_format
};
753 my $uncompbytes = $prop{length} * $prop{audio_rate
} * $prop{audio_nch
} * 2;
755 $info->{encoding
} .= sprintf " (%d%%)", 100 * (-s
$file) / $uncompbytes;
758 push @
{$info->{encoding_extra
}}, sprintf("%dkb/s", ($prop{audio_bitrate
} + 500) / 1000)
759 if $prop{audio_bitrate
};
760 $info->{freq
} = $prop{audio_freq
};
761 $info->{chan
} = $prop{audio_nch
};
763 if(exists $info->{encoding_extra
} && @
{$info->{encoding_extra
}} == 1 && ${$info->{encoding_extra
}}[0] =~ m
|115\d\
+224kb
/s
|) {
764 if($info->{encoding
} =~ s/mpg \(352x288\)/VCD (PAL)/ ||
765 $info->{encoding
} =~ s/mpg \(352x240\)/VCD (NTSC)/) {
766 delete $info->{encoding_extra
};
769 $info->{len
} = $prop{length};
773 sub getinfo_ac3
($\
%) {
774 my ($file, $info) = @_;
779 $info->{encoding
} = "AC3";
786 sub getinfo_soepkip
($\
%) {
787 my ($file, $info) = @_;
793 open F
, "$dir/.soepkiptng_info" or return undef;
796 if(/^\[(.+)\]/ && $1 eq $file) {
797 $info->{info_origin
} = ".soepkiptng_info";
801 /^(artist|album|track|title)\s*=\s*(.*)/
802 and $info->{$1} = $2;
812 # returns samplefreq, channels, seconds
813 sub get_wav_params
($\
%;$) {
814 my ($file, $info, $offset) = @_;
815 $offset = 0 + $offset;
819 open F
, $file or return undef;
821 if($offset) { read(F
, $buf, $offset) or last; }
822 read(F
, $buf, 12) or last;
823 my ($riff, $len, $wave) = unpack("a4Va4", $buf);
824 last if $riff ne 'RIFF' || $wave ne 'WAVE';
829 read(F
, $buf, 8) or last FILE
;
830 ($type, $len) = unpack("a4V", $buf);
831 last if $type eq 'fmt ';
834 my $r = $len < 4096?
$len : 4096;
835 $r = read F
, $buf, $r or last FILE
;
839 read(F
, $buf, $len) or last;
840 my ($fmt, $chan, $freq, $bytespersec, $align, $bitspersample) =
841 unpack("vvVVvv", $buf);
842 #print STDERR " my ($fmt, $chan, $freq, $bytespersec, $align, $bitspersample) =\n";
847 read(F
, $buf, 8) or last FILE
;
848 ($type, $len) = unpack("a4V", $buf);
849 last if $type eq 'data';
851 my $r = $len < 4096?
$len : 4096;
852 $r = read F
, $buf, $r or last FILE
;
857 $info->{len
} = int($len / $bytespersec + 0.5);
858 $info->{freq
} = $freq;
859 $info->{chan
} = $chan;
866 sub read_eric_files
() {
867 foreach my $file (split /\s+/, $conf{'description_files'}) {
868 my ($artist, $album, $dirname);
869 open ALB
, $file or die "$file: $!\n";
871 /\S/ or do { $artist = $album = $dirname = ""; next; };
872 /^artist\s*(.*?)\s*$/ and do { $artist = $1; next; };
873 /^title\s*(.*?)\s*$/ and do { $album = $1; next; };
874 if(/^dirname\s+(.*\S)/) {
878 (my $artist_s = $dirname) =~ s/-.*//;
880 /^##\s*(.*\S)/ and do {
884 if(s/^track\s+(\S+)\.\w+\s*//) {
887 $a =~ s/^(\d+)-([^-]+)-.*/\2/;
888 $tracknr{"$dirname/$filename"} = $1;
889 #print STDERR "a=$a artist_s=$artist_s artist=$artist realartist=$realartist\n";
891 $artist{"$dirname/$filename"} = $realartist;
893 } elsif($a eq $artist_s) {
894 $artist{"$dirname/$filename"} = $artist;
897 $a =~ s/\b(\w)/\U\1/g;
898 $artist{"$dirname/$filename"} = $a;
907 $track{"$dirname/$filename"} = $_;
908 #print STDERR "{$dirname/$filename} = $_\n";
909 #print qq~\$track{"$dirname/$filename"} = $_;\n~ if /magic/i;
910 $album{"$dirname/$filename"} = $album;
911 #print STDERR "$dirname/$filename $album\n";
921 sub extract_artist_title
($\
%) {
922 my ($file, $info) = @_;
923 my ($a, $t, $tr, $alb);
926 $info->{info_origin
} = "filename";
933 $file =~ s/(.*)\..*/\1/;
935 if(-e
"$p/.andre" || ($file !~ /-/ && $file =~ /\
..*\
./)) {
937 $file =~ s/_ddd\.hq//;
940 $info->{info_origin
} = "filename[andre]";
944 $pp = '' if -e
"$p/.noalbum";
945 if(open F
, "$p/.album") {
951 if($file =~ s/^\s*(\d\d)(\D)/\2/i || # nummer weg, 2 digits
952 $file =~ s/^\s*(\d{0,3})[^a-z0-9]+//i) # nummer weg, 1-3 digits met separator
956 $file =~ s/^[^a-z]+//i;
961 if($file =~ /[\x80-\xff]{2,}/) {
963 eval { $dfile = decode_utf8
($file, Encode
::FB_CROAK
); };
965 $file = encode
("iso-8859-1", $dfile);
966 $info->{info_origin
} .= "(utf8)";
970 if($file =~ /[_\s]*-[_\s]*/) {
972 if($pp) { ($alb = $pp) =~ s/.*-[_\s]*//; }
974 if(!$alb && $pp =~ s/-[_\s]*(.*)//) { $alb = $1; }
975 ($a, $t) = ($pp, $file);
977 if(-e "$p/.reverse") { ($a, $t) = ($t, $a); }
979 $info->{artist} = cleanup_name($a);
980 $info->{title} = cleanup_name($t);
981 $info->{album} = cleanup_name($alb);
982 $info->{track} = $tr;
990 # major device number must be 0
991 my $maj = (stat $file)[0] >> 8;