Class: Lamer
- Inherits:
-
Object
- Object
- Lamer
- Defined in:
- lib/lamer.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
-
#id3_options ⇒ Object
Returns the value of attribute id3_options.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #argument_list ⇒ Object
- #bitrate(kbps) ⇒ Object
- #command_line ⇒ Object
- #convert! ⇒ Object
- #decode_mp3! ⇒ Object
- #encode_quality(quality) ⇒ Object
-
#id3(options) ⇒ Object
id3 options.
- #id3_add_v2! ⇒ Object
- #id3_arguments ⇒ Object
- #id3_version_only(version) ⇒ Object
-
#initialize ⇒ Lamer
constructor
A new instance of Lamer.
-
#input_file(filename) ⇒ Object
options for dealing with the input and output files.
- #input_mp3! ⇒ Object
- #input_raw(sample_rate, swapbytes = false) ⇒ Object
- #mark_as_copy! ⇒ Object
- #mode(channels) ⇒ Object
- #output_file(filename) ⇒ Object
- #output_ogg! ⇒ Object
- #replay_gain(gain) ⇒ Object
-
#sample_rate(rate) ⇒ Object
methods for setting options on the encoder.
- #vbr_quality(quality) ⇒ Object
Constructor Details
#initialize ⇒ Lamer
Returns a new instance of Lamer.
14 15 16 |
# File 'lib/lamer.rb', line 14 def initialize @options = {} end |
Instance Attribute Details
#id3_options ⇒ Object
Returns the value of attribute id3_options.
12 13 14 |
# File 'lib/lamer.rb', line 12 def @id3_options end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/lamer.rb', line 12 def @options end |
Instance Method Details
#argument_list ⇒ Object
18 19 20 21 |
# File 'lib/lamer.rb', line 18 def argument_list @options.delete :id3_version unless @id3_options @options.collect {|k,v| v}.join(' ') end |
#bitrate(kbps) ⇒ Object
39 40 41 42 |
# File 'lib/lamer.rb', line 39 def bitrate(kbps) raise ArgumentError, "legal bitrates: #{MP3_Bitrates.join(', ')}" unless MP3_Bitrates.include? kbps @options[:bitrate] = "-b #{kbps}" end |
#command_line ⇒ Object
23 24 25 26 |
# File 'lib/lamer.rb', line 23 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
28 29 30 |
# File 'lib/lamer.rb', line 28 def convert! system command_line end |
#decode_mp3! ⇒ Object
75 76 77 78 |
# File 'lib/lamer.rb', line 75 def decode_mp3! @options.clear @options[:decode_mp3] = "--decode" end |
#encode_quality(quality) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/lamer.rb', line 44 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
99 100 101 |
# File 'lib/lamer.rb', line 99 def id3 @id3_options = @id3_options ? @id3_options.merge() : end |
#id3_add_v2! ⇒ Object
115 116 117 |
# File 'lib/lamer.rb', line 115 def id3_add_v2! @options[:id3_version] = "--add-id3v2" end |
#id3_arguments ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/lamer.rb', line 103 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
111 112 113 |
# File 'lib/lamer.rb', line 111 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
66 67 68 69 |
# File 'lib/lamer.rb', line 66 def input_file(filename) @input_file = filename mark_as_copy! if filename =~ /\.mp3$/ end |
#input_mp3! ⇒ Object
84 85 86 87 |
# File 'lib/lamer.rb', line 84 def input_mp3! @options[:input_mp3] = "--mp3input" mark_as_copy! end |
#input_raw(sample_rate, swapbytes = false) ⇒ Object
89 90 91 |
# File 'lib/lamer.rb', line 89 def input_raw(sample_rate, swapbytes = false) @options[:input_raw] = "-r -s #{sample_rate}#{' -x' if swapbytes}" end |
#mark_as_copy! ⇒ Object
93 94 95 |
# File 'lib/lamer.rb', line 93 def mark_as_copy! @options[:copy] = "-o" end |
#mode(channels) ⇒ Object
56 57 58 |
# File 'lib/lamer.rb', line 56 def mode(channels) @options[:mode] = Channels[channels] ? "-m #{Channels[channels]}" : nil end |
#output_file(filename) ⇒ Object
71 72 73 |
# File 'lib/lamer.rb', line 71 def output_file(filename) @output_file = filename end |
#output_ogg! ⇒ Object
80 81 82 |
# File 'lib/lamer.rb', line 80 def output_ogg! @options[:output_ogg] = "--ogg" end |
#replay_gain(gain) ⇒ Object
60 61 62 |
# File 'lib/lamer.rb', line 60 def replay_gain(gain) @options[:replay_gain] = Replay_Gain[gain] end |
#sample_rate(rate) ⇒ Object
methods for setting options on the encoder
34 35 36 37 |
# File 'lib/lamer.rb', line 34 def sample_rate(rate) raise ArgumentError unless Sample_Rates.include? rate @options[:sample_rate] = "--resample #{rate}" end |
#vbr_quality(quality) ⇒ Object
51 52 53 54 |
# File 'lib/lamer.rb', line 51 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 |