| [23] | 1 | package Ripper; | 
|---|
| [1] | 2 |  | 
|---|
| [23] | 3 | use Moose; | 
|---|
| [1] | 4 |  | 
|---|
| [22] | 5 | use Tracks; | 
|---|
| [1] | 6 |  | 
|---|
 | 7 | use File::Temp qw{tempdir}; | 
|---|
| [23] | 8 | use File::Spec::Functions qw{catfile}; | 
|---|
| [1] | 9 | use File::Copy; | 
|---|
 | 10 |  | 
|---|
| [29] | 11 | has device => ( | 
|---|
 | 12 |     is  => 'rw', | 
|---|
 | 13 |     isa => 'Str', | 
|---|
 | 14 | ); | 
|---|
| [23] | 15 | has tracks => (  | 
|---|
| [29] | 16 |     is      => 'rw', | 
|---|
 | 17 |     isa     => 'Tracks', | 
|---|
 | 18 |     handles => [qw{read_disc has_tracks}], | 
|---|
| [1] | 19 | ); | 
|---|
 | 20 |  | 
|---|
| [23] | 21 | sub rip_to_flac { | 
|---|
 | 22 |     my ($self, $archive_flac) = @_; | 
|---|
| [22] | 23 |  | 
|---|
| [23] | 24 |     $self->tracks(Tracks->new); | 
|---|
 | 25 |     $self->read_disc($self->device); | 
|---|
| [1] | 26 |  | 
|---|
| [24] | 27 |     die "No tracks found; is there a CD in the drive?\n" unless $self->has_tracks; | 
|---|
| [1] | 28 |  | 
|---|
| [23] | 29 |     my $tempdir = tempdir(CLEANUP => 1); | 
|---|
| [1] | 30 |  | 
|---|
| [23] | 31 |     my $wav_file  = catfile($tempdir, 'cdda.wav'); | 
|---|
 | 32 |     my $flac_file = catfile($tempdir, 'cdda.flac'); | 
|---|
 | 33 |     my $cue_file  = catfile($tempdir, 'cdda.cue'); | 
|---|
| [1] | 34 |  | 
|---|
| [23] | 35 |     # rip | 
|---|
| [26] | 36 |     my $span = $self->_get_cdparanoia_span; | 
|---|
| [23] | 37 |     system 'cdparanoia', '-d', $self->device, $span, $wav_file; | 
|---|
 | 38 |     die "\nRipping canceled\n" if ($? & 127); | 
|---|
| [1] | 39 |  | 
|---|
| [23] | 40 |     # encode + cuesheet | 
|---|
 | 41 |     open my $CUE, "> $cue_file"; | 
|---|
| [29] | 42 |     print $CUE $self->tracks->get_cuesheet; | 
|---|
| [23] | 43 |     close $CUE; | 
|---|
 | 44 |     system 'flac', '-o', $flac_file, '--cuesheet', $cue_file, $wav_file; | 
|---|
 | 45 |     die "\nFLAC encoding canceled\n" if ($? & 127); | 
|---|
| [1] | 46 |  | 
|---|
| [23] | 47 |     # MusicBrainz discid metadata | 
|---|
| [28] | 48 |     my $discid = $self->tracks->discid; | 
|---|
| [1] | 49 |  | 
|---|
| [23] | 50 |     # copy to permanent location | 
|---|
 | 51 |     copy($flac_file, $archive_flac); | 
|---|
 | 52 |     system 'metaflac', '--set-tag', "MUSICBRAINZ_DISCID=$discid", $archive_flac; | 
|---|
 | 53 | } | 
|---|
| [1] | 54 |  | 
|---|
| [26] | 55 | sub _get_cdparanoia_span { | 
|---|
 | 56 |     my ($self) = @_; | 
|---|
 | 57 |     # use a msf start unless track 1 begins at sector | 
|---|
 | 58 |     return $self->tracks->tracks->[1]{sector} == 0 ? '1-' : '00:00.00-'; | 
|---|
 | 59 | } | 
|---|
 | 60 |  | 
|---|
| [23] | 61 | # module return | 
|---|
 | 62 | 1; | 
|---|