Changeset 26 in flacrip for trunk


Ignore:
Timestamp:
10/31/14 21:22:35 (10 years ago)
Author:
peter
Message:
  • added parse_cuesheet() function to Tracks
  • Tracks can now read tracks in from the CUESHEET block of a FLAC file (read_flac) or from a cue file directly (read_cue)
  • Tracks can caluclate the MusicBrainz TOC lookup string (get_musicbrainz_tocdata)
  • renamed the get_mbz_discid method to get_musicbrainz_discid
  • moved the get_cdparanoia_span method into the Ripper class and made it private
Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Ripper.pm

    r24 r26  
    3030 
    3131    # rip 
    32     my $span = $self->tracks->get_cdparanoia_span; 
     32    my $span = $self->_get_cdparanoia_span; 
    3333    system 'cdparanoia', '-d', $self->device, $span, $wav_file; 
    3434    die "\nRipping canceled\n" if ($? & 127); 
     
    4242 
    4343    # MusicBrainz discid metadata 
    44     my $discid = $self->tracks->get_mbz_discid; 
     44    my $discid = $self->tracks->get_musicbrainz_discid; 
    4545 
    4646    # copy to permanent location 
     
    4949} 
    5050 
     51sub _get_cdparanoia_span { 
     52    my ($self) = @_; 
     53    # use a msf start unless track 1 begins at sector 
     54    return $self->tracks->tracks->[1]{sector} == 0 ? '1-' : '00:00.00-'; 
     55} 
     56 
    5157# module return 
    52581; 
  • trunk/lib/Tracks.pm

    r24 r26  
    22 
    33use Moose; 
     4use Audio::FLAC::Header; 
     5use IO::Lines; 
     6use IO::File; 
    47use Digest::SHA1; 
    58 
     9# conversion factors 
     10use constant FRAMES_PER_SECOND => 75; 
     11use 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 
     15use 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? 
    619use constant SECTOR_OFFSET => 150; 
    720 
     
    6881} 
    6982 
     83sub 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 
     110sub 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 
     120sub 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 
    70127sub has_tracks { 
    71128    my $self = shift; 
     
    73130} 
    74131 
    75 sub get_mbz_discid { 
     132# https://musicbrainz.org/doc/Disc_ID_Calculation 
     133sub get_musicbrainz_discid { 
    76134    my ($self) = @_; 
    77135 
     
    96154} 
    97155 
     156sub 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} 
    98172 
    99173sub get_cuesheet { 
     
    113187} 
    114188 
    115 sub get_cdparanoia_span { 
    116     my ($self) = @_; 
    117     # use a msf start unless track 1 begins at sector 
    118     return $self->tracks->[1]{sector} == 0 ? '1-' : '00:00.00-'; 
    119 } 
    120  
    121189# module return 
    1221901; 
Note: See TracChangeset for help on using the changeset viewer.