2 # Copyright (c) 2012-2018 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, $start);
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,
38 "man" => \$man) or pod2usage(-verbose => 0, -exitcode => 2);
40 print STDERR "Extraneous arguments given.\n";
41 pod2usage(-verbose => 0, -exitcode => 2);
43 pod2usage(-verbose => 1, -exitcode => 0) if $help;
44 pod2usage(-verbose => 2, -exitcode => 0) if $man;
47 if (!defined($device)) {
48 $device = "/dev/cdrom";
50 if (!defined($command)) {
53 if (!defined($workdir)) {
56 if (!defined($start)) {
62 my $s = Digest::SHA->new(1);
63 $s->addfile($filename);
67 if ($command =~ m/^id/) {
68 my $disc = new MusicBrainz::DiscID($device);
70 # read the disc in the default disc drive */
71 if ( $disc->read() == 0 ) {
72 printf STDERR "Error: %s\n", $disc->error_msg();
76 printf("%s ", $disc->id());
77 printf("%d ", $disc->last_track_num() + 1 - $disc->first_track_num());
79 for ( my $i = $disc->first_track_num;
80 $i <= $disc->last_track_num; $i++ ) {
81 printf("%d ", $disc->track_offset($i));
83 printf("%d\n", $disc->sectors() / $FRAMES_PER_S);
85 } elsif ($command =~ m/data/) {
86 if (!defined $discid or !$discid) {
87 print STDERR "Discid undefined.\n";
90 my $ws = WebService::MusicBrainz::Release->new();
91 my $response = $ws->search({ DISCID => $discid });
92 my @releases = $response->release_list();
93 my $releasenum = $start;
96 foreach my $release (@releases) {
97 my $a_artist = $release->artist()->name();
100 if ($a_artist =~ /Various Artists/) {
103 my $release_event_list = $release->release_event_list();
104 if ($release_event_list) {
105 my @events = @{$release->release_event_list()->events()};
106 $rel_year = substr($events[0]->date(),0,4);
110 open (OUT, "> $workdir/cddbread.$releasenum");
111 binmode OUT, ":utf8";
112 print OUT "# xmcd style database file\n";
114 print OUT "# Track frame offsets:\n";
115 # Assume standard pregap
116 my $total_len = 2000;
117 my @tracks = @{$release->track_list()->tracks()};
118 for (my $i = 0; $i < scalar(@tracks); $i++) {
119 printf OUT "# %d\n", ceil($total_len * $FRAMES_PER_S / 1000.0);
120 $total_len += $tracks[$i]->duration();
123 printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0;
125 print OUT "# Submitted via: XXXXXX\n";
127 print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n";
128 print OUT "#CATEGORY=none\n";
129 print OUT "DISCID=" . $discid . "\n";
130 print OUT "DTITLE=" . $a_artist. " / " . $release->title() . "\n";
131 print OUT "DYEAR=" . $rel_year . "\n";
132 print OUT "DGENRE=\n";
134 my @tracks = @{$release->track_list()->tracks()};
135 for (my $i = 0; $i < scalar(@tracks); $i++) {
136 my $track = $tracks[$i];
137 my $t_name = $track->title;
139 my $t_artist = $track->artist->name;
140 printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name;
142 printf OUT "TTITLE%d=%s\n", $i, $t_name;
147 for (my $i = 0; $i < scalar(@tracks); $i++) {
148 printf OUT "EXTT%d=\n", $i;
150 print OUT "PLAYORDER=\n";
155 open (OUT, "> $workdir/mbid.$releasenum");
156 print OUT $release->id;
160 open (OUT, "> $workdir/asin.$releasenum");
161 print OUT $release->asin;
164 # Check to see that this entry is unique; generate a checksum
165 # and compare to any previous checksums
166 my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
167 foreach my $sum (@sums) {
168 if ($checksum eq $sum) {
169 unlink("$workdir/cddbread.$releasenum");
174 push (@sums, $checksum);
176 } elsif ($command =~ m/calcid/) {
177 # Calculate MusicBrainz ID from disc offsets; see
178 # https://musicbrainz.org/doc/DiscIDCalculation
181 if ($#discinfo < 5) {
182 print STDERR "Insufficient or missing discinfo data.\n";
185 my ($first, $last, $leadin, $leadout, @offsets) = @discinfo;
187 my $s = Digest::SHA->new(1);
188 $s->add(sprintf "%02X", int($first));
189 $s->add(sprintf "%02X", int($last));
192 for (my $i = 0; $i < 100; $i++) {
196 foreach my $o ($leadout, @offsets) {
197 $a[$i++] = int($o) + int($leadin);
199 for (my $i = 0; $i < 100; $i++) {
200 $s->add(sprintf "%08X", $a[$i]);
203 my $id = $s->b64digest;
204 # CPAN Digest modules do not pad their Base64 output, so we have to do it.
205 while (length($id) % 4) {
214 if (-t STDOUT) { print "\n"; }
216 print STDERR "Unknown command given.\n";
224 abcde-musicbrainz-tool - Musicbrainz query tool
228 abcde-musicbrainz-tool [options]
231 --command {id|data|calcid} mode of operation (default: id)
232 --device <DEV> read from CD-ROM device DEV (default: /dev/cdrom)
233 --discid <ID> Disc ID to query with --command data.
234 --discinfo <F> <L> <LI> <LO> <TRK1OFF> [<TRK2OFF> [...]]
235 Disc information for --command calcid.
236 --workdir <DIR> working directory (default: /tmp)
237 --help print option summary
238 --man full documentation
244 =item B<--command> I<{id|data|calcid}>
246 Select mode of operation:
252 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:
254 ID TRACKCOUNT OFFSET1 [OFFSET2 [...]] LENGTH_S
258 Query MusicBrainz web service and store data into the workdir into cddbread.1, cddbread.2, ... files in the workdir.
262 Calculate MusicBrainz ID from given B<--discinfo> data.
268 Specify CD-ROM drive's device name, to read ID from with B<--command id>.
272 Supply disc ID for B<--command data>.
274 =item B<--discinfo> I<<first track> <last track> <lead-in sector> <lead-out sector> <track1 offset> [<track2 offset> [...]]>
276 Supply disc information for B<--command calcid>.
278 =item B<--workdir> I<directory>
280 The cddbread.* output files from B<--command data> go into this directory.
284 Print a brief help message and exit.
288 Display full manual and exit.