1# $Id: Audio.pm 2187 2006-08-16 19:34:38Z joern $
2
3#-----------------------------------------------------------------------
4# Copyright (C) 2001-2006 J�rn Reder <joern AT zyn.de>.
5# All Rights Reserved. See file COPYRIGHT for details.
6#
7# This module is part of Video::DVDRip, which is free software; you can
8# redistribute it and/or modify it under the same terms as Perl itself.
9#-----------------------------------------------------------------------
10
11package Video::DVDRip::Audio;
12use Locale::TextDomain qw (video.dvdrip);
13
14use base Video::DVDRip::Base;
15
16use Carp;
17use strict;
18
19# Title this track belongs to
20sub title			{ shift->{title}			}
21sub set_title			{ shift->{title}		= $_[1]	}
22
23# Attributes of the audio channel on DVD
24
25sub type			{ shift->{type}				}
26sub lang			{ shift->{lang}				}
27sub channels			{ shift->{channels}			}
28sub bitrate			{ shift->{bitrate}			}
29sub sample_rate			{ shift->{sample_rate}			}
30sub volume_rescale		{ shift->{volume_rescale}		}
31sub scan_output			{ shift->{scan_output}			}
32
33sub set_type			{ shift->{type}			= $_[1]	}
34sub set_lang			{ shift->{lang}			= $_[1]	}
35sub set_channels		{ shift->{channels}		= $_[1]	}
36sub set_bitrate			{ shift->{bitrate}		= $_[1]	}
37sub set_sample_rate		{ shift->{sample_rate}		= $_[1]	}
38sub set_volume_rescale		{ shift->{volume_rescale}	= $_[1]	}
39sub set_scan_output		{ shift->{scan_output}		= $_[1] }
40
41# Options for transcoding the audio channel
42
43sub tc_nr			{ shift->{tc_nr}			}
44sub tc_target_track		{ shift->{tc_target_track}		}
45sub tc_audio_filter		{ shift->{tc_audio_filter}		}
46sub tc_option_n			{ shift->{tc_option_n}			}
47sub tc_volume_rescale		{ shift->{tc_volume_rescale}		}
48
49sub set_tc_nr			{ shift->{tc_nr}		= $_[1]	}
50sub set_tc_target_track		{ shift->{tc_target_track}	= $_[1]	}
51#sub set_tc_audio_filter	{ shift->{tc_audio_filter}	= $_[1]	}
52sub set_tc_option_n		{ shift->{tc_option_n}		= $_[1]	}
53sub set_tc_volume_rescale	{ shift->{tc_volume_rescale}	= $_[1]	}
54
55sub tc_audio_codec		{ shift->{tc_audio_codec}		}
56sub tc_mp3_bitrate		{ shift->{tc_mp3_bitrate}		}
57sub tc_mp3_samplerate		{ shift->{tc_mp3_samplerate}		}
58sub tc_mp3_quality		{ shift->{tc_mp3_quality}		}
59sub tc_ac3_bitrate		{ shift->{tc_ac3_bitrate}		}
60sub tc_vorbis_bitrate		{ shift->{tc_vorbis_bitrate}		}
61sub tc_vorbis_samplerate	{ shift->{tc_vorbis_samplerate}		}
62sub tc_vorbis_quality		{ shift->{tc_vorbis_quality}		}
63sub tc_vorbis_quality_enable	{ shift->{tc_vorbis_quality_enable}	}
64sub tc_mp2_bitrate		{ shift->{tc_mp2_bitrate}		}
65sub tc_mp2_samplerate		{ shift->{tc_mp2_samplerate}		}
66sub tc_pcm_bitrate		{ shift->{tc_pcm_bitrate}		}
67
68#sub set_tc_audio_codec		{ shift->{tc_audio_codec}	= $_[1]	}
69#sub set_tc_mp3_bitrate		{ shift->{tc_mp3_bitrate}	= $_[1]	}
70sub set_tc_mp3_samplerate	{ shift->{tc_mp3_samplerate}	= $_[1]	}
71sub set_tc_mp3_quality		{ shift->{tc_mp3_quality}	= $_[1]	}
72sub set_tc_ac3_bitrate		{ shift->{tc_ac3_bitrate}	= $_[1]	}
73#sub set_tc_vorbis_bitrate	{ shift->{tc_vorbis_bitrate}	= $_[1]	}
74sub set_tc_vorbis_samplerate	{ shift->{tc_vorbis_samplerate}	= $_[1]	}
75#sub set_tc_vorbis_quality	{ shift->{tc_vorbis_quality}	= $_[1]	}
76#sub set_tc_vorbis_quality_enable{ shift->{tc_vorbis_quality_enable}=$_[1]}
77#sub set_tc_mp2_bitrate		{ shift->{tc_mp2_bitrate}	= $_[1]	}
78sub set_tc_mp2_samplerate	{ shift->{tc_mp2_samplerate}	= $_[1]	}
79sub set_tc_pcm_bitrate		{ shift->{tc_pcm_bitrate}	= $_[1]	}
80
81sub set_tc_audio_filter {
82    my $self = shift;
83    my ($value) = @_;
84    $self->{tc_audio_filter} = $value;
85    if ( $value eq 'rescale' ) {
86        $self->set_tc_volume_rescale( $self->volume_rescale );
87    }
88    else {
89        $self->set_tc_volume_rescale("");
90    }
91    return $value;
92}
93
94sub set_tc_audio_codec {
95    my $self = shift;
96    my ($value) = @_;
97    $self->{tc_audio_codec} = $value;
98    $self->title->calc_video_bitrate;
99    return $value;
100}
101
102sub set_tc_mp3_bitrate {
103    my $self = shift;
104    my ($value) = @_;
105    $self->{tc_mp3_bitrate} = $value;
106    $self->title->calc_video_bitrate;
107    return $value;
108}
109
110sub set_tc_mp2_bitrate {
111    my $self = shift;
112    my ($value) = @_;
113    $self->{tc_mp2_bitrate} = $value;
114    $self->title->calc_video_bitrate;
115    return $value;
116}
117
118sub set_tc_vorbis_bitrate {
119    my $self = shift;
120    my ($value) = @_;
121    $self->{tc_vorbis_bitrate} = $value;
122    $self->title->calc_video_bitrate;
123    return $value;
124}
125
126sub set_tc_vorbis_quality {
127    my $self = shift;
128    my ($value) = @_;
129    $self->{tc_vorbis_quality} = $value;
130    $self->title->calc_video_bitrate;
131    return $value;
132}
133
134sub set_tc_vorbis_quality_enable {
135    my $self = shift;
136    my ($value) = @_;
137    $self->{tc_vorbis_quality_enable} = $value;
138    $self->title->calc_video_bitrate;
139    return $value;
140}
141
142sub new {
143    my $class = shift;
144    my %par   = @_;
145    my ( $type, $lang, $channels, $bitrate, $volume_rescale )
146        = @par{ 'type', 'lang', 'channels', 'bitrate', 'volume_rescale' };
147    my ( $title, $sample_rate, $scan_output )
148        = @par{ 'title', 'sample_rate', 'scan_output' };
149    my ( $tc_target_track, $tc_audio_codec, $tc_bitrate )
150        = @par{ 'tc_target_track', 'tc_audio_codec', 'tc_bitrate' };
151    my ( $tc_ac3_passthrough, $tc_mp3_quality, $tc_audio_filter )
152        = @par{ 'tc_ac3_passthrough', 'tc_mp3_quality', 'tc_audio_filter' };
153    my ( $tc_option_n, $tc_volume_rescale, $tc_nr )
154        = @par{ 'tc_option_n', 'tc_volume_rescale', 'tc_nr' };
155    my ( $tc_ac3_bitrate, $tc_samplerate )
156        = @par{ 'tc_ac3_bitrate', 'tc_samplerate' };
157
158    $tc_target_track = -1    if not defined $tc_target_track;
159    $tc_audio_codec  = "ac3" if $tc_ac3_passthrough;
160    $tc_audio_codec    ||= "mp3";
161    $tc_bitrate        ||= 128;
162    $tc_mp3_quality    ||= 2;
163    $tc_audio_filter   ||= 'rescale';
164    $tc_option_n       ||= '';
165    $tc_volume_rescale ||= 0;
166
167    my $self = {
168        title                    => $title,
169        type                     => $type,
170        lang                     => $lang,
171        channels                 => $channels,
172        bitrate                  => $bitrate,
173        sample_rate              => $sample_rate,
174        volume_rescale           => $volume_rescale,
175        scan_output              => $scan_output,
176        tc_nr                    => $tc_nr,
177        tc_target_track          => $tc_target_track,
178        tc_audio_codec           => $tc_audio_codec,
179        tc_ac3_bitrate           => $tc_ac3_bitrate,
180        tc_mp3_bitrate           => $tc_bitrate,
181        tc_mp2_bitrate           => $tc_bitrate,
182        tc_vorbis_bitrate        => $tc_bitrate,
183        tc_mp3_samplerate        => $tc_samplerate,
184        tc_vorbis_samplerate     => $tc_samplerate,
185        tc_mp3_quality           => $tc_mp3_quality,
186        tc_audio_filter          => $tc_audio_filter,
187        tc_option_n              => $tc_option_n,
188        tc_volume_rescale        => $tc_volume_rescale,
189        tc_vorbis_quality        => 3.00,
190        tc_vorbis_quality_enable => 0,
191    };
192
193    return bless $self, $class;
194}
195
196sub tc_bitrate {
197    my $self        = shift;
198    my $audio_codec = $self->tc_audio_codec;
199    my $method      = "tc_" . $audio_codec . "_bitrate";
200    return $self->$method();
201}
202
203sub set_tc_bitrate {
204    my $self        = shift;
205    my ($val)       = @_;
206    my $audio_codec = $self->tc_audio_codec;
207    my $method      = "set_tc_" . $audio_codec . "_bitrate";
208    return $self->$method($val);
209}
210
211sub tc_samplerate {
212    my $self        = shift;
213    my $audio_codec = $self->tc_audio_codec;
214    my $method      = "tc_" . $audio_codec . "_samplerate";
215    return $self->$method();
216}
217
218sub ac3_ok {
219    my $self = shift;
220
221    my $ok = ( $self->type eq 'ac3' and $self->bitrate ne '' );
222
223    return $ok;
224}
225
226sub pcm_ok {
227    my $self = shift;
228
229    my $ok = ( $self->type eq 'lpcm' and $self->bitrate ne '' );
230
231    return $ok;
232}
233
234sub is_passthrough {
235    my $self = shift;
236    return $self->tc_audio_codec eq 'ac3'
237        || $self->tc_audio_codec eq 'pcm';
238}
239
240sub info {
241    my $self          = shift;
242    my %par           = @_;
243    my ($with_target) = @par{'with_target'};
244
245    my $sample_rate = $self->sample_rate;
246    $sample_rate = "48kHz"   if $sample_rate == 48000;
247    $sample_rate = "41.1kHz" if $sample_rate == 44100;
248
249    my $target;
250    if ($with_target) {
251        if ( $self->tc_target_track < 0 ) {
252            $target = " => " . __ "Discard";
253        }
254        else {
255            $target = " => " . ( $self->tc_target_track + 1 );
256        }
257    }
258
259    return $self->lang . " "
260        . $self->type . " "
261        . "$sample_rate "
262        . $self->channels
263        . "Ch$target";
264}
265
2661;
267