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/) {
98 my @events = @{$release->release_event_list()->events()};
99 my $rel_year = substr($events[0]->date(),0,4);
102 open (OUT, "> $workdir/cddbread.$releasenum");
103 binmode OUT, ":utf8";
104 print OUT "# xmcd style database file\n";
106 print OUT "# Track frame offsets:\n";
107 # Assume standard pregap
108 my $total_len = 2000;
109 my @tracks = @{$release->track_list()->tracks()};
110 for (my $i = 0; $i < scalar(@tracks); $i++) {
111 printf OUT "# %d\n", ceil($total_len * $FRAMES_PER_S / 1000.0);
112 $total_len += $tracks[$i]->duration();
115 printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0;
117 print OUT "# Submitted via: XXXXXX\n";
119 print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n";
120 print OUT "#CATEGORY=none\n";
121 print OUT "DISCID=" . $discid . "\n";
122 print OUT "DTITLE=" . $a_artist. " / " . $release->title() . "\n";
123 print OUT "DYEAR=" . $rel_year . "\n";
124 print OUT "DGENRE=\n";
126 my @tracks = @{$release->track_list()->tracks()};
127 for (my $i = 0; $i < scalar(@tracks); $i++) {
128 my $track = $tracks[$i];
129 my $t_name = $track->title;
131 my $t_artist = $track->artist->name;
132 printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name;
134 printf OUT "TTITLE%d=%s\n", $i, $t_name;
139 for (my $i = 0; $i < scalar(@tracks); $i++) {
140 printf OUT "EXTT%d=\n", $i;
142 print OUT "PLAYORDER=\n";
147 open (OUT, "> $workdir/mbid.$releasenum");
148 print OUT $release->id;
152 open (OUT, "> $workdir/asin.$releasenum");
153 print OUT $release->asin;
156 # Check to see that this entry is unique; generate a checksum
157 # and compare to any previous checksums
158 my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
159 foreach my $sum (@sums) {
160 if ($checksum eq $sum) {
161 unlink("$workdir/cddbread.$releasenum");
166 push (@sums, $checksum);
168 } elsif ($command =~ m/calcid/) {
169 # Calculate MusicBrainz ID from disc offsets; see
170 # http://musicbrainz.org/doc/DiscIDCalculation
173 if ($#discinfo < 5) {
174 print STDERR "Insufficient or missing discinfo data.\n";
177 my ($first, $last, $leadin, $leadout, @offsets) = @discinfo;
179 my $s = Digest::SHA->new(1);
180 $s->add(sprintf "%02X", int($first));
181 $s->add(sprintf "%02X", int($last));
184 for (my $i = 0; $i < 100; $i++) {
188 foreach my $o ($leadout, @offsets) {
189 $a[$i++] = int($o) + int($leadin);
191 for (my $i = 0; $i < 100; $i++) {
192 $s->add(sprintf "%08X", $a[$i]);
195 my $id = $s->b64digest;
196 # CPAN Digest modules do not pad their Base64 output, so we have to do it.
197 while (length($id) % 4) {
206 if (-t STDOUT) { print "\n"; }
208 print STDERR "Unknown commmand given.\n";
216 abcde-musicbrainz-tool - Musicbrainz query tool
220 abcde-musicbrainz-tool [options]
223 --command {id|data|calcid} mode of operation (default: id)
224 --device <DEV> read from CD-ROM device DEV (default: /dev/cdrom)
225 --discid <ID> Disc ID to query with --command data.
226 --discinfo <F> <L> <LI> <LO> <TRK1OFF> [<TRK2OFF> [...]]
227 Disc information for --command calcid.
228 --workdir <DIR> working directory (default: /tmp)
229 --help print option summary
230 --man full documentation
236 =item B<--command> I<{id|data|calcid}>
238 Select mode of operation:
244 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:
246 ID TRACKCOUNT OFFSET1 [OFFSET2 [...]] LENGTH_S
250 Query MusicBrainz web service and store data into the workdir into cddbread.1, cddbread.2, ... files in the workdir.
254 Calculate MusicBrainz ID from given B<--discinfo> data.
260 Specify CD-ROM drive's device name, to read ID from with B<--command id>.
264 Supply disc ID for B<--command data>.
266 =item B<--discinfo> I<<first track> <last track> <lead-in sector> <lead-out sector> <track1 offset> [<track2 offset> [...]]>
268 Supply disc information for B<--command calcid>.
270 =item B<--workdir> I<directory>
272 The cddbread.* output files from B<--command data> go into this directory.
276 Print a brief help message and exit.
280 Display full manual and exit.