2 # Copyright (c) 2012 Steve McIntyre <93sam@debian.org>
3 # This code is hereby licensed for public consumption under either the
4 # GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice.
6 # You should have received a copy of the GNU General Public License along
7 # with this program; if not, write to the Free Software Foundation, Inc.,
8 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
10 # abcde-musicbrainz-tool
12 # Helper script for abcde to work with the MusicBrainz WS API (v2)
18 use MusicBrainz::DiscID;
19 use WebService::MusicBrainz::Release;
20 use WebService::MusicBrainz::Artist;
21 use WebService::MusicBrainz::Response::Track;
22 use WebService::MusicBrainz::Response::TrackList;
26 my $FRAMES_PER_S = 75;
28 my ($device, $command, $discid, @discinfo, $workdir, $help, $man);
29 Getopt::Long::Configure ('no_ignore_case');
30 Getopt::Long::Configure ('no_auto_abbrev');
31 GetOptions ("device=s" => \$device,
32 "command=s" => \$command,
33 "discid=s" => \$discid,
34 "discinfo=i{5,}" => \@discinfo,
35 "workdir=s" => \$workdir,
37 "man" => \$man) or pod2usage(-verbose => 0, -exitcode => 2);
39 print STDERR "Extraneous arguments given.\n";
40 pod2usage(-verbose => 0, -exitcode => 2);
42 pod2usage(-verbose => 1, -exitcode => 0) if $help;
43 pod2usage(-verbose => 2, -exitcode => 0) if $man;
46 if (!defined($device)) {
47 $device = "/dev/cdrom";
49 if (!defined($command)) {
52 if (!defined($workdir)) {
58 my $s = Digest::SHA->new(1);
59 $s->addfile($filename);
63 if ($command =~ m/^id/) {
64 my $disc = new MusicBrainz::DiscID($device);
66 # read the disc in the default disc drive */
67 if ( $disc->read() == 0 ) {
68 printf STDERR "Error: %s\n", $disc->error_msg();
72 printf("%s ", $disc->id());
73 printf("%d ", $disc->last_track_num() + 1 - $disc->first_track_num());
75 for ( my $i = $disc->first_track_num;
76 $i <= $disc->last_track_num; $i++ ) {
77 printf("%d ", $disc->track_offset($i));
79 printf("%d\n", $disc->sectors() / $FRAMES_PER_S);
81 } elsif ($command =~ m/data/) {
82 if (!defined $discid or !$discid) {
83 print STDERR "Discid undefined.\n";
86 my $ws = WebService::MusicBrainz::Release->new();
87 my $response = $ws->search({ DISCID => $discid });
88 my @releases = $response->release_list();
92 foreach my $release (@releases) {
93 my $a_artist = $release->artist()->name();
95 if ($a_artist =~ /Various Artists/) {
99 open (OUT, "> $workdir/cddbread.$releasenum");
100 binmode OUT, ":utf8";
101 print OUT "# xmcd style database file\n";
103 print OUT "# Track frame offsets:\n";
104 # Assume standard pregap
105 my $total_len = 2000;
106 my @tracks = @{$release->track_list()->tracks()};
107 for (my $i = 0; $i < scalar(@tracks); $i++) {
108 printf OUT "# %d\n", ceil($total_len * $FRAMES_PER_S / 1000.0);
109 $total_len += $tracks[$i]->duration();
112 printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0;
114 print OUT "# Submitted via: XXXXXX\n";
116 print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n";
117 print OUT "#CATEGORY=none\n";
118 print OUT "DISCID=" . $discid . "\n";
119 print OUT "DTITLE=" . $a_artist. " / " . $release->title() . "\n";
120 print OUT "DYEAR=\n";
121 print OUT "DGENRE=\n";
123 my @tracks = @{$release->track_list()->tracks()};
124 for (my $i = 0; $i < scalar(@tracks); $i++) {
125 my $track = $tracks[$i];
126 my $t_name = $track->title;
128 my $t_artist = $track->artist->name;
129 printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name;
131 printf OUT "TTITLE%d=%s\n", $i, $t_name;
136 for (my $i = 0; $i < scalar(@tracks); $i++) {
137 printf OUT "EXTT%d=\n", $i;
139 print OUT "PLAYORDER=\n";
144 open (OUT, "> $workdir/mbid.$releasenum");
145 print OUT $release->id;
149 open (OUT, "> $workdir/asin.$releasenum");
150 print OUT $release->asin;
153 # Check to see that this entry is unique; generate a checksum
154 # and compare to any previous checksums
155 my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
156 foreach my $sum (@sums) {
157 if ($checksum eq $sum) {
158 unlink("$workdir/cddbread.$releasenum");
163 push (@sums, $checksum);
165 } elsif ($command =~ m/calcid/) {
166 # Calculate MusicBrainz ID from disc offsets; see
167 # http://musicbrainz.org/doc/DiscIDCalculation
170 if ($#discinfo < 5) {
171 print STDERR "Insufficient or missing discinfo data.\n";
174 my ($first, $last, $leadin, $leadout, @offsets) = @discinfo;
176 my $s = Digest::SHA->new(1);
177 $s->add(sprintf "%02X", int($first));
178 $s->add(sprintf "%02X", int($last));
181 for (my $i = 0; $i < 100; $i++) {
185 foreach my $o ($leadout, @offsets) {
186 $a[$i++] = int($o) + int($leadin);
188 for (my $i = 0; $i < 100; $i++) {
189 $s->add(sprintf "%08X", $a[$i]);
192 my $id = $s->b64digest;
193 # CPAN Digest modules do not pad their Base64 output, so we have to do it.
194 while (length($id) % 4) {
203 if (-t STDOUT) { print "\n"; }
205 print STDERR "Unknown commmand given.\n";
213 abcde-musicbrainz-tool - Musicbrainz query tool
217 abcde-musicbrainz-tool [options]
220 --command {id|data|calcid} mode of operation (default: id)
221 --device <DEV> read from CD-ROM device DEV (default: /dev/cdrom)
222 --discid <ID> Disc ID to query with --command data.
223 --discinfo <F> <L> <LI> <LO> <TRK1OFF> [<TRK2OFF> [...]]
224 Disc information for --command calcid.
225 --workdir <DIR> working directory (default: /tmp)
226 --help print option summary
227 --man full documentation
233 =item B<--command> I<{id|data|calcid}>
235 Select mode of operation:
241 Read the disc-ID from the disc in the given device, and print it, the number of tracks, their start sectors, and the duration of the disc in seconds, to stdout. Format:
243 ID TRACKCOUNT OFFSET1 [OFFSET2 [...]] LENGTH_S
247 Query MusicBrainz web service and store data into the workdir into cddbread.1, cddbread.2, ... files in the workdir.
251 Calculate MusicBrainz ID from given B<--discinfo> data.
257 Specify CD-ROM drive's device name, to read ID from with B<--command id>.
261 Supply disc ID for B<--command data>.
263 =item B<--discinfo> I<<first track> <last track> <lead-in sector> <lead-out sector> <track1 offset> [<track2 offset> [...]]>
265 Supply disc information for B<--command calcid>.
267 =item B<--workdir> I<directory>
269 The cddbread.* output files from B<--command data> go into this directory.
273 Print a brief help message and exit.
277 Display full manual and exit.