Changeset 26 in flacrip for trunk/lib/Tracks.pm
- Timestamp:
- 10/31/14 21:22:35 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Tracks.pm
r24 r26 2 2 3 3 use Moose; 4 use Audio::FLAC::Header; 5 use IO::Lines; 6 use IO::File; 4 7 use Digest::SHA1; 5 8 9 # conversion factors 10 use constant FRAMES_PER_SECOND => 75; 11 use constant SECONDS_PER_MINUTE => 60; 12 13 # see also http://flac.sourceforge.net/format.html#cuesheet_track 14 # 588 samples/frame = 44100 samples/sec / 75 frames/sec 15 use constant SAMPLES_PER_FRAME => 588; 16 17 # XXX: does this every vary? 18 # or is the lead-in always 88200 samples (88200 / 588 = 150) i.e. 2 seconds? 6 19 use constant SECTOR_OFFSET => 150; 7 20 … … 68 81 } 69 82 83 sub parse_cuesheet { 84 my ($handle) = @_; 85 86 my @tracks; 87 my $track; 88 while (<$handle>) { 89 if (/TRACK (\d\d)/) { 90 $track = int($1); 91 } elsif (/INDEX 01/) { 92 my ($m,$s,$f) = /INDEX 01 (\d\d):(\d\d):(\d\d)/; 93 my $sector = ($m * SECONDS_PER_MINUTE * FRAMES_PER_SECOND) + ($s * FRAMES_PER_SECOND) + $f; 94 $tracks[$track] = { 95 number => $track, 96 sector => $sector, 97 msf => "$m:$s:$f", 98 }; 99 } elsif (/lead-out/) { 100 my ($total_samples) = /lead-out \d+ (\d+)/; 101 $tracks[0] = { 102 number => 170, 103 sector => $total_samples / SAMPLES_PER_FRAME, 104 }; 105 } 106 } 107 return @tracks; 108 } 109 110 sub read_flac { 111 my ($self, $file) = @_; 112 113 my $flac = Audio::FLAC::Header->new($file); 114 my $cuesheet_lines = $flac->cuesheet; 115 my $CUE = IO::Lines->new($cuesheet_lines); 116 117 $self->tracks([ parse_cuesheet($CUE) ]); 118 } 119 120 sub read_cue { 121 my ($self, $file) = @_; 122 123 my $CUE = $file eq '-' ? \*STDIN : IO::File->new($file, '<'); 124 $self->tracks([ parse_cuesheet($CUE) ]); 125 } 126 70 127 sub has_tracks { 71 128 my $self = shift; … … 73 130 } 74 131 75 sub get_mbz_discid { 132 # https://musicbrainz.org/doc/Disc_ID_Calculation 133 sub get_musicbrainz_discid { 76 134 my ($self) = @_; 77 135 … … 96 154 } 97 155 156 sub get_musicbrainz_tocdata { 157 my ($self) = @_; 158 my @tracks = @{ $self->tracks }; 159 # this is a CD TOC suitable for submitting to MusicBrainz as a CD Stub 160 # http://musicbrainz.org/doc/XML_Web_Service#Submitting_a_CDStub 161 return ( 162 # first track number 163 $tracks[1]{number}, 164 # last track number 165 $tracks[-1]{number}, 166 # last frame (sector?) 167 $tracks[0]{sector} + SECTOR_OFFSET, 168 # start frame for each track 169 map { $_->{sector} + SECTOR_OFFSET } @tracks[1 .. @tracks - 1], 170 ); 171 } 98 172 99 173 sub get_cuesheet { … … 113 187 } 114 188 115 sub get_cdparanoia_span {116 my ($self) = @_;117 # use a msf start unless track 1 begins at sector118 return $self->tracks->[1]{sector} == 0 ? '1-' : '00:00.00-';119 }120 121 189 # module return 122 190 1;
Note: See TracChangeset
for help on using the changeset viewer.