Class: LameEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/lame_encoder.rb

Constant Summary collapse

MP3_Bitrates =

These constants come from the LAME documentation

[32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320]
Sample_Rates =
[8, 11.025, 12, 16, 22.05, 24, 32, 44.1, 48]
Encoding_Quality =
(0..9)
VBR_Quality =
(0..6)
Channels =
{:mono => 'm', :stereo => 's', :joint => 'j', :auto => 'a', :mid_side => 'f'}
Replay_Gain =
{:fast => "--replaygain-fast", :accurate => "--replaygain-accurate", 
:none => "--noreplaygain", :clip_detect =>  "--clipdetect", :default => nil}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLameEncoder

Returns a new instance of LameEncoder.



21
22
23
# File 'lib/lame_encoder.rb', line 21

def initialize
  @options = {}
end

Instance Attribute Details

#id3_optionsObject

Returns the value of attribute id3_options.



19
20
21
# File 'lib/lame_encoder.rb', line 19

def id3_options
  @id3_options
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/lame_encoder.rb', line 19

def options
  @options
end

Instance Method Details

#argument_listObject



25
26
27
28
# File 'lib/lame_encoder.rb', line 25

def argument_list
  @options.delete :id3_version unless @id3_options
  @options.collect {|k,v| v}.join(' ')
end

#bitrate(kbps) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
# File 'lib/lame_encoder.rb', line 46

def bitrate kbps
  raise ArgumentError, "legal bitrates: #{MP3_Bitrates.join(', ')}" unless MP3_Bitrates.include? kbps
  @options[:bitrate] = "-b #{kbps}"
end

#command_lineObject

Raises:

  • (ArgumentError)


30
31
32
33
# File 'lib/lame_encoder.rb', line 30

def command_line
  raise ArgumentError, "No input file specified." unless @input_file
  ['lame', argument_list, @input_file, @output_file, id3_arguments].select{|x| !(x.nil? || x.empty?)}.join(' ')
end

#convert!Object



35
36
37
# File 'lib/lame_encoder.rb', line 35

def convert!
  system command_line
end

#decode_mp3!Object



82
83
84
85
86
# File 'lib/lame_encoder.rb', line 82

def decode_mp3!
  @options.clear
  @options[:decode_mp3] = "--decode"
  @options.freeze
end

#encode_quality(quality) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
# File 'lib/lame_encoder.rb', line 51

def encode_quality quality
  quality_keys = Hash.new { |h,k| k }.merge( { :high => 2, :fast => 7 } )
  quality = quality_keys[quality]
  raise ArgumentError, "legal qualities: #{Encoding_Quality.to_a.join(', ')}" unless Encoding_Quality.include? quality
  @options[:encode_quality] = "-q #{quality}"
end

#id3(options) ⇒ Object

id3 options



108
109
110
# File 'lib/lame_encoder.rb', line 108

def id3 options 
  @id3_options = @id3_options ? @id3_options.merge(options) : options
end

#id3_add_v2!Object



124
125
126
# File 'lib/lame_encoder.rb', line 124

def id3_add_v2!
  @options[:id3_version] = "--add-id3v2"
end

#id3_argumentsObject



112
113
114
115
116
117
118
# File 'lib/lame_encoder.rb', line 112

def id3_arguments
  id3_fields = { :title => 'tt', :artist => 'ta', :album => 'tl', 
                :year => 'ty', :comment => 'tc', :track_number => 'tn',
                :genre => 'tg' }
  return nil if @id3_options.nil? || @id3_options.empty?
  @id3_options.select{|k,v| id3_fields[k]}.collect {|k,v| "--#{id3_fields[k]} #{v}"}.join(' ')
end

#id3_version_only(version) ⇒ Object



120
121
122
# File 'lib/lame_encoder.rb', line 120

def id3_version_only version
  @options[:id3_version] = "--id3v#{version}-only"
end

#input_file(filename) ⇒ Object

options for dealing with the input and output files



73
74
75
76
# File 'lib/lame_encoder.rb', line 73

def input_file filename
  @input_file = filename
  mark_as_copy! if filename =~ /\.mp3$/
end

#input_mp3!Object



92
93
94
95
# File 'lib/lame_encoder.rb', line 92

def input_mp3!
  @options[:input_mp3] = "--mp3input"
  mark_as_copy!
end

#input_raw(sample_rate, swapbytes = false) ⇒ Object



97
98
99
# File 'lib/lame_encoder.rb', line 97

def input_raw(sample_rate, swapbytes = false)
  @options[:input_raw] = "-r -s #{sample_rate}#{' -x' if swapbytes}"
end

#mark_as_copy!Object



102
103
104
# File 'lib/lame_encoder.rb', line 102

def mark_as_copy!
  @options[:copy] = "-o"
end

#mode(channels) ⇒ Object



63
64
65
# File 'lib/lame_encoder.rb', line 63

def mode channels
  @options[:mode] = Channels[channels] ? "-m #{Channels[channels]}" : nil
end

#output_file(filename) ⇒ Object



78
79
80
# File 'lib/lame_encoder.rb', line 78

def output_file filename
  @output_file = filename
end

#output_ogg!Object



88
89
90
# File 'lib/lame_encoder.rb', line 88

def output_ogg!
  @options[:output_ogg] = "--ogg"
end

#replay_gain(gain) ⇒ Object



67
68
69
# File 'lib/lame_encoder.rb', line 67

def replay_gain(gain)
  @options[:replay_gain] = Replay_Gain[gain]
end

#sample_rate(rate) ⇒ Object

methods for setting options on the encoder

Raises:

  • (ArgumentError)


41
42
43
44
# File 'lib/lame_encoder.rb', line 41

def sample_rate rate
  raise ArgumentError unless Sample_Rates.include? rate
  @options[:sample_rate] = "--resample #{rate}"
end

#vbr_quality(quality) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
# File 'lib/lame_encoder.rb', line 58

def vbr_quality quality 
  raise ArgumentError, "legal qualities: #{VBR_Quality.to_a.join(', ')}" unless VBR_Quality.include? quality
  @options[:vbr_quality], @options[:vbr] = "-V #{quality}", "-v"
end