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);
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()};
100 if ($release->release_event_list()->count() > 0) {
101 $rel_year = substr($events[0]->date(),0,4);
105 open (OUT, "> $workdir/cddbread.$releasenum");
106 binmode OUT, ":utf8";
107 print OUT "# xmcd style database file\n";
109 print OUT "# Track frame offsets:\n";
110 # Assume standard pregap
111 my $total_len = 2000;
112 my @tracks = @{$release->track_list()->tracks()};
113 for (my $i = 0; $i < scalar(@tracks); $i++) {
114 printf OUT "# %d\n", ceil($total_len * $FRAMES_PER_S / 1000.0);
115 $total_len += $tracks[$i]->duration();
118 printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0;
120 print OUT "# Submitted via: XXXXXX\n";
122 print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n";
123 print OUT "#CATEGORY=none\n";
124 print OUT "DISCID=" . $discid . "\n";
125 print OUT "DTITLE=" . $a_artist. " / " . $release->title() . "\n";
126 print OUT "DYEAR=" . $rel_year . "\n";
127 print OUT "DGENRE=\n";
129 my @tracks = @{$release->track_list()->tracks()};
130 for (my $i = 0; $i < scalar(@tracks); $i++) {
131 my $track = $tracks[$i];
132 my $t_name = $track->title;
134 my $t_artist = $track->artist->name;
135 printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name;
137 printf OUT "TTITLE%d=%s\n", $i, $t_name;
142 for (my $i = 0; $i < scalar(@tracks); $i++) {
143 printf OUT "EXTT%d=\n", $i;
145 print OUT "PLAYORDER=\n";
150 open (OUT, "> $workdir/mbid.$releasenum");
151 print OUT $release->id;
155 open (OUT, "> $workdir/asin.$releasenum");
156 print OUT $release->asin;
159 # Check to see that this entry is unique; generate a checksum
160 # and compare to any previous checksums
161 my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
162 foreach my $sum (@sums) {
163 if ($checksum eq $sum) {
164 unlink("$workdir/cddbread.$releasenum");
169 push (@sums, $checksum);
171 } elsif ($command =~ m/calcid/) {
172 # Calculate MusicBrainz ID from disc offsets; see
173 # https://musicbrainz.org/doc/DiscIDCalculation
176 if ($#discinfo < 5) {
177 print STDERR "Insufficient or missing discinfo data.\n";
180 my ($first, $last, $leadin, $leadout, @offsets) = @discinfo;
182 my $s = Digest::SHA->new(1);
183 $s->add(sprintf "%02X", int($first));
184 $s->add(sprintf "%02X", int($last));
187 for (my $i = 0; $i < 100; $i++) {
191 foreach my $o ($leadout, @offsets) {
192 $a[$i++] = int($o) + int($leadin);
194 for (my $i = 0; $i < 100; $i++) {
195 $s->add(sprintf "%08X", $a[$i]);
198 my $id = $s->b64digest;
199 # CPAN Digest modules do not pad their Base64 output, so we have to do it.
200 while (length($id) % 4) {
209 if (-t STDOUT) { print "\n"; }
211 print STDERR "Unknown commmand given.\n";
219 abcde-musicbrainz-tool - Musicbrainz query tool
223 abcde-musicbrainz-tool [options]
226 --command {id|data|calcid} mode of operation (default: id)
227 --device <DEV> read from CD-ROM device DEV (default: /dev/cdrom)
228 --discid <ID> Disc ID to query with --command data.
229 --discinfo <F> <L> <LI> <LO> <TRK1OFF> [<TRK2OFF> [...]]
230 Disc information for --command calcid.
231 --workdir <DIR> working directory (default: /tmp)
232 --help print option summary
233 --man full documentation
239 =item B<--command> I<{id|data|calcid}>
241 Select mode of operation:
247 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:
249 ID TRACKCOUNT OFFSET1 [OFFSET2 [...]] LENGTH_S
253 Query MusicBrainz web service and store data into the workdir into cddbread.1, cddbread.2, ... files in the workdir.
257 Calculate MusicBrainz ID from given B<--discinfo> data.
263 Specify CD-ROM drive's device name, to read ID from with B<--command id>.
267 Supply disc ID for B<--command data>.
269 =item B<--discinfo> I<<first track> <last track> <lead-in sector> <lead-out sector> <track1 offset> [<track2 offset> [...]]>
271 Supply disc information for B<--command calcid>.
273 =item B<--workdir> I<directory>
275 The cddbread.* output files from B<--command data> go into this directory.
279 Print a brief help message and exit.
283 Display full manual and exit.