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}";
43 our ($opt_c, $opt_d, $opt_h, $opt_n, $opt_t);
48 usage: soepkiptng_detect_hidden_track [-h] [-c configfile] [-t threshold]
52 read_configfile
(\
%conf, $opt_c);
54 my $dbh = connect_to_db
(\
%conf);
56 FILE
: foreach my $file (@ARGV) {
57 my $path = abs_path
($file);
58 my $song = $dbh->selectrow_hashref("SELECT * FROM song WHERE filename = ?", undef, $path);
60 if(open(F
, "-|") == 0) {
61 open STDERR
, ">&STDOUT";
62 open STDOUT
, ">/dev/null";
63 exec "sox", "-V3", $path, "-";
68 /^([^:]+[^:\s])\s+:\s*(.*\S)/ and $prop{$1} = $2;
71 print Dumper
(\
%prop) if $opt_d;
72 my $ch = $prop{Channels
}
73 or die "$file: number of channels not found\n";
74 my $srate = $prop{"Sample Rate"}
75 or die "$file: sample rate not found\n";
77 open F
, "-|", "sox", $path, "-c2", "-traw", "-b16", "-esigned-integer", "-";
78 my ($buf, $off, $start, $len, %sil);
79 while(read F
, $buf, 4096) {
80 foreach(unpack("s*", $buf)) {
81 if(abs($_) <= $opt_t) {
89 $sil{$start} = $len if defined($start) && $len > 44100;
97 if(defined($start)) { $sil{$start} = $len; }
99 print "$file: no gaps detected\n";
102 printf "%d %s", $off, Dumper
(\
%sil) if $opt_d;
104 my ($max, $max2) = sort { $sil{$b} <=> $sil{$a} } keys %sil;
105 if($sil{$max} < $sil{max2
} * 2) {
106 print "no clear gap\n" . Dumper
(\
%sil);
109 my $sillen = $sil{$max};
110 my $trailing = $siloff + $sillen == $off;
112 $siloff = int($siloff / $ch);
116 $sillen -= ($ch - $p) % $ch;
118 $sillen = int($sillen / $ch);
123 Gap from %d length %d (%d:%02d.%03d - %d:%02d.%03d), trailing %ss
127 $siloff / $srate / 60, ($siloff / $srate) % 60, ($siloff / $srate * 1000) % 1000,
128 ($siloff + $sillen) / $srate / 60, (($siloff + $sillen) / $srate) % 60, (($siloff + $sillen) / $srate * 1000) % 1000,
129 ($trailing?
"n/a " : int(($off / $ch - ($siloff + $sillen)) / $srate));
134 print STDERR
"y) split b) play last 10 sec before a) play after : ";
137 system "play", "-V3", $path, "trim", sprintf("%ds", $siloff - 10 * $srate), 10;
139 system "play", "-V3", $path, "trim", sprintf("%ds", $siloff + $sillen);
147 $dbh->do("UPDATE song SET length=?,trimstart=NULL,trimlength=? WHERE id=?", undef,
148 int($siloff / $srate), $siloff, $song->{id
})
149 or die "can't do sql command: " . $dbh->errstr . "\n";
153 delete $song->{last_played
};
154 delete $song->{mtime
};
155 delete $song->{time_added
};
156 delete $song->{trimlength
};
157 delete $song->{uuid
};
158 $song->{title
} = "(Bonus Track)";
160 $song->{trimstart
} = $siloff + $sillen;
161 $song->{length} = int((($off / $ch) - ($siloff + $sillen)) / $srate);
163 my @k = sort keys %$song;
164 $dbh->do(sprintf("INSERT INTO song (%s,time_added) VALUES (%s,now())",
165 join(",", @k), join(",", map { "?" } @k)), undef,
166 map { $song->{$_} } @k)
167 or die "can't do sql command: " . $dbh->errstr . "\n";