| 1 | #!/usr/bin/perl |
| 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. |
| 5 | # |
| 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 |
| 9 | # |
| 10 | # abcde-musicbrainz-tool |
| 11 | # |
| 12 | # Helper script for abcde to work with the MusicBrainz WS API (v2) |
| 13 | |
| 14 | use strict; |
| 15 | use encoding "utf8"; |
| 16 | use POSIX qw(ceil); |
| 17 | use Digest::SHA; |
| 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; |
| 23 | use Getopt::Long; |
| 24 | |
| 25 | my $FRAMES_PER_SEC = 75; |
| 26 | |
| 27 | my ($device, $command, $discid, @discinfo, $workdir); |
| 28 | Getopt::Long::Configure ('no_ignore_case'); |
| 29 | Getopt::Long::Configure ('no_auto_abbrev'); |
| 30 | GetOptions ("device=s" => \$device, |
| 31 | "command=s" => \$command, |
| 32 | "discid=s" => \$discid, |
| 33 | "discinfo=i{5,}" => \@discinfo, |
| 34 | "workdir=s" => \$workdir); |
| 35 | |
| 36 | if (!defined($device)) { |
| 37 | $device = "/dev/cdrom"; |
| 38 | } |
| 39 | if (!defined($command)) { |
| 40 | $command = "id"; |
| 41 | } |
| 42 | if (!defined($workdir)) { |
| 43 | $workdir = "/tmp"; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | if ($command =~ m/^id/) { |
| 48 | my $disc = new MusicBrainz::DiscID($device); |
| 49 | |
| 50 | # read the disc in the default disc drive */ |
| 51 | if ( $disc->read() == 0 ) { |
| 52 | printf STDERR "Error: %s\n", $disc->error_msg(); |
| 53 | exit(1); |
| 54 | } |
| 55 | |
| 56 | printf("%s ", $disc->id()); |
| 57 | printf("%d ", $disc->last_track_num() + 1 - $disc->first_track_num()); |
| 58 | |
| 59 | for ( my $i = $disc->first_track_num; |
| 60 | $i <= $disc->last_track_num; $i++ ) { |
| 61 | printf("%d ", $disc->track_offset($i)); |
| 62 | } |
| 63 | printf("%d\n", $disc->sectors() / $FRAMES_PER_SEC); |
| 64 | undef $disc; |
| 65 | |
| 66 | } elsif ($command =~ m/data/) { |
| 67 | my $ws = WebService::MusicBrainz::Release->new(); |
| 68 | my $response = $ws->search({ DISCID => $discid }); |
| 69 | my @releases = $response->release_list(); |
| 70 | my $releasenum = 0; |
| 71 | |
| 72 | foreach my $release (@releases) { |
| 73 | my $a_artist = $release->artist()->name(); |
| 74 | my $va = 0; |
| 75 | if ($a_artist =~ /Various Artists/) { |
| 76 | $va = 1; |
| 77 | } |
| 78 | $releasenum++; |
| 79 | open (OUT, "> $workdir/cddbread.$releasenum"); |
| 80 | binmode OUT, ":utf8"; |
| 81 | print OUT "# xmcd style database file\n"; |
| 82 | print OUT "#\n"; |
| 83 | print OUT "# Track frame offsets:\n"; |
| 84 | # Assume standard pregap |
| 85 | my $total_len = 2000; |
| 86 | my @tracks = @{$release->track_list()->tracks()}; |
| 87 | for (my $i = 0; $i < scalar(@tracks); $i++) { |
| 88 | printf OUT "# %d\n", ceil($total_len * $FRAMES_PER_SEC / 1000.0); |
| 89 | $total_len += $tracks[$i]->duration(); |
| 90 | } |
| 91 | print OUT "#\n"; |
| 92 | printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0; |
| 93 | print OUT "#\n"; |
| 94 | print OUT "# Submitted via: XXXXXX\n"; |
| 95 | print OUT "#\n"; |
| 96 | print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n"; |
| 97 | print OUT "#CATEGORY=none\n"; |
| 98 | print OUT "DISCID=" . $discid . "\n"; |
| 99 | print OUT "DTITLE=" . $a_artist. " / " . $release->title() . "\n"; |
| 100 | print OUT "DYEAR=\n"; |
| 101 | print OUT "DGENRE=\n"; |
| 102 | |
| 103 | my @tracks = @{$release->track_list()->tracks()}; |
| 104 | for (my $i = 0; $i < scalar(@tracks); $i++) { |
| 105 | my $track = $tracks[$i]; |
| 106 | my $t_name = $track->title; |
| 107 | if ($va) { |
| 108 | my $t_artist = $track->artist->name; |
| 109 | printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name; |
| 110 | } else { |
| 111 | printf OUT "TTITLE%d=%s\n", $i, $t_name; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | print OUT "EXTD=\n"; |
| 116 | for (my $i = 0; $i < scalar(@tracks); $i++) { |
| 117 | printf OUT "EXTT%d=\n", $i; |
| 118 | } |
| 119 | print OUT "PLAYORDER=\n"; |
| 120 | print OUT ".\n"; |
| 121 | close OUT; |
| 122 | } |
| 123 | } elsif ($command =~ m/calcid/) { |
| 124 | # Calculate MusicBrainz ID from disc offsets; see |
| 125 | # http://musicbrainz.org/doc/DiscIDCalculation |
| 126 | |
| 127 | my ($first, $last, $leadin, $leadout, @offsets) = @discinfo; |
| 128 | |
| 129 | my $s = Digest::SHA->new(1); |
| 130 | $s->add(sprintf "%02X", int($first)); |
| 131 | $s->add(sprintf "%02X", int($last)); |
| 132 | |
| 133 | my @a; |
| 134 | for (my $i = 0; $i < 100; $i++) { |
| 135 | $a[$i] = 0; |
| 136 | } |
| 137 | my $i = 0; |
| 138 | foreach my $o ($leadout, @offsets) { |
| 139 | $a[$i++] = int($o) + int($leadin); |
| 140 | } |
| 141 | for (my $i = 0; $i < 100; $i++) { |
| 142 | $s->add(sprintf "%08X", $a[$i]); |
| 143 | } |
| 144 | |
| 145 | my $id = $s->b64digest; |
| 146 | # CPAN Digest modules do not pad their Base64 output, so we have to do it. |
| 147 | while (length($id) % 4) { |
| 148 | $id .= '='; |
| 149 | } |
| 150 | |
| 151 | $id =~ tr#+#.#; |
| 152 | $id =~ tr#/#_#; |
| 153 | $id =~ tr#=#-#; |
| 154 | |
| 155 | print $id; |
| 156 | } |
| 157 | |