lame_encoder 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ require 'spec/rake/spectask'
2
+ require 'rake/gempackagetask'
3
+
4
+ namespace :spec do
5
+ Spec::Rake::SpecTask.new :lame_encoder do |t|
6
+ t.spec_files = FileList['spec/**/*_spec.rb']
7
+ t.spec_opts << "-f s"
8
+ end
9
+ end
10
+
11
+ task :default => 'spec:lame_encoder'
12
+
13
+ PKG_FILES = FileList[
14
+ 'lib/**/*.rb',
15
+ 'spec/**/*.rb',
16
+ 'spec/**/test.mp3',
17
+ 'Rakefile'
18
+ ]
19
+
20
+ specification = Gem::Specification.new do |spec|
21
+ spec.author = 'Chris Anderson'
22
+ spec.email = 'jchris@mfdz.com'
23
+
24
+ spec.name = 'lame_encoder'
25
+ spec.platform = Gem::Platform::RUBY
26
+ spec.version = '0.1.1'
27
+
28
+ spec.files = PKG_FILES.to_a
29
+
30
+ spec.autorequire = 'lame_encoder'
31
+ spec.add_dependency 'rake', '>= 0.7.0'
32
+ spec.add_dependency 'rspec', '0.5'
33
+ spec.requirements << "LAME (Lame Ain't an MP3 Encoder) http://www.mp3dev.org"
34
+
35
+ spec.summary = 'A simple wrapper for LAME.'
36
+ spec.description = %q{
37
+ A Ruby interface to the command line options for lame. Support for most common features.
38
+ }
39
+ spec.has_rdoc = false
40
+ end
41
+
42
+ Rake::GemPackageTask.new specification do |pkg|
43
+ pkg.need_zip = true
44
+ pkg.need_tar = true
45
+ end
@@ -0,0 +1,128 @@
1
+ def Object.sequence *args
2
+ args.each do |arg|
3
+ self.call arg, rest
4
+ end
5
+ end
6
+
7
+
8
+ class LameEncoder
9
+
10
+ # These constants come from the LAME documentation
11
+ MP3_Bitrates = [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320]
12
+ Sample_Rates = [8, 11.025, 12, 16, 22.05, 24, 32, 44.1, 48]
13
+ Encoding_Quality = (0..9)
14
+ VBR_Quality = (0..6)
15
+ Channels = {:mono => 'm', :stereo => 's', :joint => 'j', :auto => 'a', :mid_side => 'f'}
16
+ Replay_Gain = {:fast => "--replaygain-fast", :accurate => "--replaygain-accurate",
17
+ :none => "--noreplaygain", :clip_detect => "--clipdetect", :default => nil}
18
+
19
+ attr_accessor :options, :id3_options
20
+
21
+ def initialize
22
+ @options = {}
23
+ end
24
+
25
+ def argument_list
26
+ @options.delete :id3_version unless @id3_options
27
+ @options.collect {|k,v| v}.join(' ')
28
+ end
29
+
30
+ def command_line
31
+ raise ArgumentError, "No input file specified." unless @input_file
32
+ ['lame', argument_list, @input_file, @output_file, id3_arguments].select{|x| !(x.nil? || x.empty?)}.join(' ')
33
+ end
34
+
35
+ def convert!
36
+ system command_line
37
+ end
38
+
39
+ # methods for setting options on the encoder
40
+
41
+ def sample_rate rate
42
+ raise ArgumentError unless Sample_Rates.include? rate
43
+ @options[:sample_rate] = "--resample #{rate}"
44
+ end
45
+
46
+ def bitrate kbps
47
+ raise ArgumentError, "legal bitrates: #{MP3_Bitrates.join(', ')}" unless MP3_Bitrates.include? kbps
48
+ @options[:bitrate] = "-b #{kbps}"
49
+ end
50
+
51
+ def encode_quality quality
52
+ quality_keys = Hash.new { |h,k| k }.merge( { :high => 2, :fast => 7 } )
53
+ quality = quality_keys[quality]
54
+ raise ArgumentError, "legal qualities: #{Encoding_Quality.to_a.join(', ')}" unless Encoding_Quality.include? quality
55
+ @options[:encode_quality] = "-q #{quality}"
56
+ end
57
+
58
+ def vbr_quality quality
59
+ raise ArgumentError, "legal qualities: #{VBR_Quality.to_a.join(', ')}" unless VBR_Quality.include? quality
60
+ @options[:vbr_quality], @options[:vbr] = "-V #{quality}", "-v"
61
+ end
62
+
63
+ def mode channels
64
+ @options[:mode] = Channels[channels] ? "-m #{Channels[channels]}" : nil
65
+ end
66
+
67
+ def replay_gain(gain)
68
+ @options[:replay_gain] = Replay_Gain[gain]
69
+ end
70
+
71
+ # options for dealing with the input and output files
72
+
73
+ def input_file filename
74
+ @input_file = filename
75
+ mark_as_copy! if filename =~ /\.mp3$/
76
+ end
77
+
78
+ def output_file filename
79
+ @output_file = filename
80
+ end
81
+
82
+ def decode_mp3!
83
+ @options.clear
84
+ @options[:decode_mp3] = "--decode"
85
+ @options.freeze
86
+ end
87
+
88
+ def output_ogg!
89
+ @options[:output_ogg] = "--ogg"
90
+ end
91
+
92
+ def input_mp3!
93
+ @options[:input_mp3] = "--mp3input"
94
+ mark_as_copy!
95
+ end
96
+
97
+ def input_raw(sample_rate, swapbytes = false)
98
+ @options[:input_raw] = "-r -s #{sample_rate}#{' -x' if swapbytes}"
99
+ end
100
+
101
+
102
+ def mark_as_copy!
103
+ @options[:copy] = "-o"
104
+ end
105
+
106
+ # id3 options
107
+
108
+ def id3 options
109
+ @id3_options = @id3_options ? @id3_options.merge(options) : options
110
+ end
111
+
112
+ def id3_arguments
113
+ id3_fields = { :title => 'tt', :artist => 'ta', :album => 'tl',
114
+ :year => 'ty', :comment => 'tc', :track_number => 'tn',
115
+ :genre => 'tg' }
116
+ return nil if @id3_options.nil? || @id3_options.empty?
117
+ @id3_options.select{|k,v| id3_fields[k]}.collect {|k,v| "--#{id3_fields[k]} #{v}"}.join(' ')
118
+ end
119
+
120
+ def id3_version_only version
121
+ @options[:id3_version] = "--id3v#{version}-only"
122
+ end
123
+
124
+ def id3_add_v2!
125
+ @options[:id3_version] = "--add-id3v2"
126
+ end
127
+
128
+ end
@@ -0,0 +1,302 @@
1
+ $: << File.join( File.dirname( __FILE__ ), '../lib' )
2
+
3
+ require 'rubygems'
4
+ require_gem 'rspec'
5
+ require 'lame_encoder'
6
+
7
+ context "A new LameEncoder" do
8
+
9
+ setup do
10
+ @la = LameEncoder.new
11
+ end
12
+
13
+ specify "has a blank argument list" do
14
+ @la.argument_list.should_be_empty
15
+ end
16
+
17
+ specify "records valid bitrates" do
18
+ @la.bitrate 128
19
+ @la.options[:bitrate].should_equal "-b 128"
20
+ end
21
+
22
+ specify "records valid sample rate" do
23
+ @la.sample_rate 44.1
24
+ @la.options[:sample_rate].should_equal "--resample 44.1"
25
+ end
26
+
27
+ specify "records valid VBR quality" do
28
+ @la.vbr_quality 5
29
+ @la.options[:vbr_quality].should_equal "-V 5"
30
+ @la.options[:vbr].should_equal "-v"
31
+ end
32
+
33
+ specify "records valid quality" do
34
+ @la.encode_quality 1
35
+ @la.options[:encode_quality].should_equal "-q 1"
36
+ end
37
+
38
+ specify "records valid quality" do
39
+ @la.encode_quality 5
40
+ @la.options[:encode_quality].should_equal "-q 5"
41
+ end
42
+
43
+ specify "records valid quality shortcut" do
44
+ @la.encode_quality :high
45
+ @la.options[:encode_quality].should_equal "-q 2"
46
+ @la.encode_quality :fast
47
+ @la.options[:encode_quality].should_equal "-q 7"
48
+ end
49
+
50
+ specify "balks at invalid bitrate" do
51
+ lambda {@la.bitrate 113}.should_raise ArgumentError
52
+ end
53
+
54
+ specify "balks at invalid sample rate" do
55
+ lambda {@la.sample_rate 113}.should_raise ArgumentError
56
+ end
57
+
58
+ specify "balks at invalid VBR quality" do
59
+ lambda {@la.vbr_quality 113}.should_raise ArgumentError
60
+ end
61
+
62
+ specify "balks at invalid encode quality" do
63
+ lambda {@la.encode_quality 113}.should_raise ArgumentError
64
+ end
65
+
66
+ specify "sets mode to stereo or mono" do
67
+ {:stereo => '-m s', :mono => '-m m', :joint => '-m j'}.each do |option, setting|
68
+ @la.mode option
69
+ @la.options[:mode].should_equal setting
70
+ end
71
+ end
72
+
73
+ specify "sets mode to nil on bad option" do
74
+ @la.mode :bugz
75
+ @la.options[:mode].should_be nil
76
+ end
77
+
78
+ specify "decodes mp3s" do
79
+ @la.decode_mp3!
80
+ @la.options[:decode_mp3].should_equal "--decode"
81
+ lambda {@la.options[:new_value] = 'something'}.should_raise TypeError
82
+ end
83
+
84
+ specify "decodes mp3s without other options" do
85
+ @la.vbr_quality 4
86
+ @la.decode_mp3!
87
+ @la.options.length.should_equal 1
88
+ end
89
+
90
+ specify "accepts flag that input is mp3" do
91
+ @la.input_mp3!
92
+ @la.options[:input_mp3].should_equal "--mp3input"
93
+ end
94
+
95
+ specify "accepts an input filename" do
96
+ @la.input_file "/Path/to/my/audio_file.wav"
97
+ @la.command_line.should_equal "lame /Path/to/my/audio_file.wav"
98
+ end
99
+
100
+ specify "accepts replygain options" do
101
+ {:accurate => "--replaygain-accurate",
102
+ :fast => "--replaygain-fast",
103
+ :none => "--noreplaygain",
104
+ :clip_detect => "--clipdetect",
105
+ :default => nil
106
+ }.each do |option, setting|
107
+ @la.replay_gain option
108
+ @la.options[:replay_gain].should_equal setting
109
+ end
110
+ end
111
+
112
+ specify "accepts raw PCM files" do
113
+ @la.input_raw 44.1
114
+ @la.options[:input_raw].should_equal "-r -s 44.1"
115
+ @la.input_raw 32, true
116
+ @la.options[:input_raw].should_equal "-r -s 32 -x"
117
+ end
118
+
119
+ specify "marks as copy when requested" do
120
+ @la.mark_as_copy!
121
+ @la.options[:copy].should_equal "-o"
122
+ end
123
+ specify "marks as copy when starting from a file ending in .mp3" do
124
+ @la.input_file "/Path/to/my/audio_file.mp3"
125
+ @la.options[:copy].should_equal "-o"
126
+ end
127
+ specify "marks as copy when starting from an mp3 file" do
128
+ @la.input_mp3!
129
+ @la.options[:copy].should_equal "-o"
130
+ end
131
+ specify "does not mark as copy when starting from a file not ending in .mp3" do
132
+ @la.input_file "/Path/to/my/audio_file.aif"
133
+ @la.options[:copy].should_be_nil
134
+ end
135
+
136
+ specify "outputs ogg files when requested" do
137
+ @la.output_ogg!
138
+ @la.options[:output_ogg].should_equal "--ogg"
139
+ end
140
+
141
+ end
142
+
143
+ context "A high-quality VBR encoder without a file" do
144
+
145
+ setup do
146
+ @la = LameEncoder.new
147
+ @la.vbr_quality 4
148
+ @la.sample_rate 44.1
149
+ end
150
+
151
+ specify "ouputs the correct command line options" do
152
+ [ /-v/,/-V 4/,/--resample 44\.1/].each do |match|
153
+ @la.argument_list.should_match match
154
+ end
155
+ end
156
+
157
+ specify "balks at returning the command line" do
158
+ lambda {@la.command_line}.should_raise ArgumentError
159
+ end
160
+
161
+ specify "balks at running the conversion" do
162
+ lambda {@la.convert!}.should_raise ArgumentError
163
+ end
164
+
165
+ end
166
+
167
+ context "An encoder with a file specified" do
168
+
169
+ setup do
170
+ @la = LameEncoder.new
171
+ @la.input_file "/Path/to/my/audio_file.wav"
172
+ end
173
+
174
+ specify "provides the right command line" do
175
+ @la.command_line.should_equal "lame /Path/to/my/audio_file.wav"
176
+ end
177
+
178
+ end
179
+
180
+ context "An encoder with options and an input and output file specified" do
181
+
182
+ setup do
183
+ @la = LameEncoder.new
184
+ @la.encode_quality :high
185
+ @la.input_file "/Path/to/my/audio_file.wav"
186
+ @la.output_file "/Path/to/my/audio_file.mp3"
187
+ end
188
+
189
+ specify "provides the right command line" do
190
+ @la.command_line.should_equal "lame -q 2 /Path/to/my/audio_file.wav /Path/to/my/audio_file.mp3"
191
+ end
192
+
193
+ end
194
+
195
+ context "An encoder sent various id3 information" do
196
+
197
+ setup do
198
+ @la = LameEncoder.new
199
+ end
200
+
201
+ specify "sets the title" do
202
+ @la.id3 :title => "The All-Knowning Mind of Minolta"
203
+ @la.id3_options[:title].should_equal "The All-Knowning Mind of Minolta"
204
+ @la.id3_arguments.should_equal "--tt The All-Knowning Mind of Minolta"
205
+ end
206
+
207
+ specify "sets multiple values" do
208
+ @la.id3 :title => "The All-Knowning Mind of Minolta"
209
+ @la.id3 :artist => "Tin Man Shuffler"
210
+ @la.id3_options[:title].should_equal "The All-Knowning Mind of Minolta"
211
+ @la.id3_options[:artist].should_equal "Tin Man Shuffler"
212
+ @la.id3_arguments.should_match /--tt The All-Knowning Mind of Minolta/
213
+ @la.id3_arguments.should_match /--ta Tin Man Shuffler/
214
+ end
215
+
216
+ specify "sets title, artist, album, year, comment, track number, and genre" do
217
+ @la.id3 :title => 'title', :artist => 'artist', :album => 'album',
218
+ :year => 1998, :comment => 'comment', :track_number => 1,
219
+ :genre => 'genre'
220
+ [/--tt title/,/--ta artist/, /--tl album/,/--ty 1998/,/--tc comment/,/--tn 1/,/--tg genre/].each do |match|
221
+ @la.id3_arguments.should_match match
222
+ end
223
+ end
224
+
225
+ specify "ignores nonsense values" do
226
+ @la.id3 :bugz => "Not Real"
227
+ @la.id3_arguments.should_not_match /Real/
228
+ @la.id3 :title => "Coolbeans"
229
+ @la.id3_arguments.should_match /Coolbeans/
230
+ end
231
+
232
+ specify "adds v1 or v2 id3 tags as requested" do
233
+ 2.times do |n|
234
+ @la.id3_version_only n
235
+ @la.options[:id3_version].should_equal "--id3v#{n}-only"
236
+ end
237
+ end
238
+
239
+ specify "allows adding v2 tags on top of default behaviour" do
240
+ @la.id3_add_v2!
241
+ @la.options[:id3_version].should_equal "--add-id3v2"
242
+ end
243
+
244
+ specify "does not output id3_version unless tags are set" do
245
+ @la.id3_version_only 1
246
+ @la.argument_list.should_equal ""
247
+ end
248
+
249
+ context "An encoder with id3 options and an input file" do
250
+ setup do
251
+ @la = LameEncoder.new
252
+ @la.id3 :title => 'title', :artist => 'artist', :album => 'album',
253
+ :year => 1998, :comment => 'comment', :track_number => 1,
254
+ :genre => 'genre'
255
+ @la.input_file File.join( File.dirname( __FILE__ ), 'test.mp3' )
256
+ end
257
+
258
+ specify "outputs those options to the command line" do
259
+ [/--tt title/,/--ta artist/, /--tl album/,/--ty 1998/,/--tc comment/,/--tn 1/,/--tg genre/].each do |match|
260
+ @la.command_line.should_match match
261
+ end
262
+ end
263
+ end
264
+
265
+ context "An encoder starting with an mp3 file" do
266
+ setup do
267
+ @root = './spec/'
268
+ begin
269
+ File.delete File.join( File.dirname( __FILE__ ), 'output.mp3' )
270
+ rescue
271
+ end
272
+ @la = LameEncoder.new
273
+ @la.input_file "#{@root}test.mp3"
274
+ @la.output_file "#{@root}output.mp3"
275
+ @la.input_mp3!
276
+ end
277
+
278
+ specify "provides the right command line" do
279
+ [/lame/,/--mp3input/, /-o/,/#{@root}test.mp3/,/#{@root}output.mp3/].each do |match|
280
+ @la.command_line.should_match match
281
+ end
282
+ end
283
+
284
+ specify "successfully outputs a low bitrate version" do
285
+ @la.bitrate 32
286
+ @la.mode :mono
287
+ File.should_not_exist "#{@root}output.mp3"
288
+ @la.convert!
289
+ File.should_exist "#{@root}output.mp3"
290
+ end
291
+ end
292
+
293
+ context "The runtime environment" do
294
+ specify "should have lame version 3.96 installed" do
295
+ version = `lame --help`
296
+ version.should_match /LAME/
297
+ version.should_match /3\.96/
298
+ end
299
+ end
300
+
301
+ end
302
+
Binary file
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: lame_encoder
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.1
7
+ date: 2006-06-27 00:00:00 -07:00
8
+ summary: A simple wrapper for LAME.
9
+ require_paths:
10
+ - lib
11
+ email: jchris@mfdz.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: A Ruby interface to the command line options for lame. Support for most common features.
15
+ autorequire: lame_encoder
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Chris Anderson
30
+ files:
31
+ - lib/lame_encoder.rb
32
+ - spec/lame_encoder_spec.rb
33
+ - spec/test.mp3
34
+ - Rakefile
35
+ test_files: []
36
+
37
+ rdoc_options: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ requirements:
46
+ - LAME (Lame Ain't an MP3 Encoder) http://www.mp3dev.org
47
+ dependencies:
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ version_requirement:
51
+ version_requirements: !ruby/object:Gem::Version::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 0.7.0
56
+ version:
57
+ - !ruby/object:Gem::Dependency
58
+ name: rspec
59
+ version_requirement:
60
+ version_requirements: !ruby/object:Gem::Version::Requirement
61
+ requirements:
62
+ - - "="
63
+ - !ruby/object:Gem::Version
64
+ version: "0.5"
65
+ version: