2 # Copyright (c) 2012-2016 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,
36 "workdir=s" => \$workdir,
39 "man" => \$man) or pod2usage(-verbose => 0, -exitcode => 2);
41 print STDERR "Extraneous arguments given.\n";
42 pod2usage(-verbose => 0, -exitcode => 2);
44 pod2usage(-verbose => 1, -exitcode => 0) if $help;
45 pod2usage(-verbose => 2, -exitcode => 0) if $man;
48 if (!defined($device)) {
49 $device = "/dev/cdrom";
51 if (!defined($command)) {
54 if (!defined($workdir)) {
57 if (!defined($start)) {
63 my $s = Digest::SHA->new(1);
64 $s->addfile($filename);
68 if ($command =~ m/^id/) {
69 my $disc = new MusicBrainz::DiscID($device);
71 # read the disc in the default disc drive */
72 if ( $disc->read() == 0 ) {
73 printf STDERR "Error: %s\n", $disc->error_msg();
77 printf("%s ", $disc->id());
78 printf("%d ", $disc->last_track_num() + 1 - $disc->first_track_num());
80 for ( my $i = $disc->first_track_num;
81 $i <= $disc->last_track_num; $i++ ) {
82 printf("%d ", $disc->track_offset($i));
84 printf("%d\n", $disc->sectors() / $FRAMES_PER_S);
86 } elsif ($command =~ m/data/) {
87 if (!defined $discid or !$discid) {
88 print STDERR "Discid undefined.\n";
91 my $ws = WebService::MusicBrainz::Release->new();
92 my $response = $ws->search({ DISCID => $discid });
93 my @releases = $response->release_list();
94 my $releasenum = $start;
97 foreach my $release (@releases) {
98 my $a_artist = $release->artist()->name();
101 if ($a_artist =~ /Various Artists/) {
104 my $release_event_list = $release->release_event_list();
105 if ($release_event_list) {
106 my @events = @{$release->release_event_list()->events()};
107 $rel_year = substr($events[0]->date(),0,4);
111 open (OUT, "> $workdir/cddbread.$releasenum");
112 binmode OUT, ":utf8";
113 print OUT "# xmcd style database file\n";
115 print OUT "# Track frame offsets:\n";
116 # Assume standard pregap
117 my $total_len = 2000;
118 my @tracks = @{$release->track_list()->tracks()};
119 for (my $i = 0; $i < scalar(@tracks); $i++) {
120 printf OUT "# %d\n", ceil($total_len * $FRAMES_PER_S / 1000.0);
121 $total_len += $tracks[$i]->duration();
124 printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0;
126 print OUT "# Submitted via: XXXXXX\n";
128 print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n";
129 print OUT "#CATEGORY=none\n";
130 print OUT "DISCID=" . $discid . "\n";
131 print OUT "DTITLE=" . $a_artist. " / " . $release->title() . "\n";
132 print OUT "DYEAR=" . $rel_year . "\n";
133 print OUT "DGENRE=\n";
135 my @tracks = @{$release->track_list()->tracks()};
136 for (my $i = 0; $i < scalar(@tracks); $i++) {
137 my $track = $tracks[$i];
138 my $t_name = $track->title;
140 my $t_artist = $track->artist->name;
141 printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name;
143 printf OUT "TTITLE%d=%s\n", $i, $t_name;
148 for (my $i = 0; $i < scalar(@tracks); $i++) {
149 printf OUT "EXTT%d=\n", $i;
151 print OUT "PLAYORDER=\n";
156 open (OUT, "> $workdir/mbid.$releasenum");
157 print OUT $release->id;
161 open (OUT, "> $workdir/asin.$releasenum");
162 print OUT $release->asin;
165 # Check to see that this entry is unique; generate a checksum
166 # and compare to any previous checksums
167 my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
168 foreach my $sum (@sums) {
169 if ($checksum eq $sum) {
170 unlink("$workdir/cddbread.$releasenum");
175 push (@sums, $checksum);
177 } elsif ($command =~ m/calcid/) {
178 # Calculate MusicBrainz ID from disc offsets; see
179 # https://musicbrainz.org/doc/DiscIDCalculation
182 if ($#discinfo < 5) {
183 print STDERR "Insufficient or missing discinfo data.\n";
186 my ($first, $last, $leadin, $leadout, @offsets) = @discinfo;
188 my $s = Digest::SHA->new(1);
189 $s->add(sprintf "%02X", int($first));
190 $s->add(sprintf "%02X", int($last));
193 for (my $i = 0; $i < 100; $i++) {
197 foreach my $o ($leadout, @offsets) {
198 $a[$i++] = int($o) + int($leadin);
200 for (my $i = 0; $i < 100; $i++) {
201 $s->add(sprintf "%08X", $a[$i]);
204 my $id = $s->b64digest;
205 # CPAN Digest modules do not pad their Base64 output, so we have to do it.
206 while (length($id) % 4) {
215 if (-t STDOUT) { print "\n"; }
217 print STDERR "Unknown command given.\n";
225 abcde-musicbrainz-tool - Musicbrainz query tool
229 abcde-musicbrainz-tool [options]
232 --command {id|data|calcid} mode of operation (default: id)
233 --device <DEV> read from CD-ROM device DEV (default: /dev/cdrom)
234 --discid <ID> Disc ID to query with --command data.
235 --discinfo <F> <L> <LI> <LO> <TRK1OFF> [<TRK2OFF> [...]]
236 Disc information for --command calcid.
237 --workdir <DIR> working directory (default: /tmp)
238 --help print option summary
239 --man full documentation
245 =item B<--command> I<{id|data|calcid}>
247 Select mode of operation:
253 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:
255 ID TRACKCOUNT OFFSET1 [OFFSET2 [...]] LENGTH_S
259 Query MusicBrainz web service and store data into the workdir into cddbread.1, cddbread.2, ... files in the workdir.
263 Calculate MusicBrainz ID from given B<--discinfo> data.
269 Specify CD-ROM drive's device name, to read ID from with B<--command id>.
273 Supply disc ID for B<--command data>.
275 =item B<--discinfo> I<<first track> <last track> <lead-in sector> <lead-out sector> <track1 offset> [<track2 offset> [...]]>
277 Supply disc information for B<--command calcid>.
279 =item B<--workdir> I<directory>
281 The cddbread.* output files from B<--command data> go into this directory.
285 Print a brief help message and exit.
289 Display full manual and exit.