lame 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ class WaveFileGenerator
2
+ attr_accessor :mode, :bits, :bitrate, :length, :hertz
3
+
4
+ def initialize(options = {})
5
+ @mode = options[:mode] || :stereo
6
+ @bits = options[:bits] || 16
7
+ @bitrate = options[:bitate] || 44100
8
+ @length = options[:length] || 1
9
+ @hertz = options[:hertz] || 440
10
+ end
11
+
12
+ def generate
13
+ wav_file = Tempfile.new('sine.wav')
14
+
15
+ format = WaveFile::Format.new(mode, pcm_type, bitrate)
16
+ WaveFile::Writer.new(wav_file.path, format) do |writer|
17
+ samples = (0...bitrate*length).map do |offset|
18
+ sample = (Math.sin((Math::PI*2 / scale) * offset) * max).round
19
+ [sample, sample]
20
+ end
21
+
22
+ buffer = WaveFile::Buffer.new(samples, format)
23
+ writer.write(buffer)
24
+ end
25
+
26
+ wav_file
27
+ end
28
+
29
+ def max
30
+ 2**(bits-1) - 1
31
+ end
32
+
33
+ def scale
34
+ bitrate / hertz
35
+ end
36
+
37
+ def pcm_type
38
+ case bits
39
+ when 16
40
+ :pcm_16
41
+ when 24
42
+ :pcm_24
43
+ end
44
+ end
45
+
46
+ end
@@ -14,6 +14,10 @@ end
14
14
  require 'pry'
15
15
  require 'lame'
16
16
 
17
+ require 'lib/wave_file_generator'
18
+ require 'lib/setter_getter'
19
+ require 'lib/custom_matchers'
20
+
17
21
  RSpec.configure do |config|
18
22
  config.treat_symbols_as_metadata_keys_with_true_values = true
19
23
  config.run_all_when_everything_filtered = true
@@ -25,151 +29,4 @@ def pointer_from_string(str)
25
29
  ::FFI::MemoryPointer.from_string(str)
26
30
  end
27
31
 
28
- module SetterGetter
29
- def has_getter?(lame, flag)
30
- lame.respond_to?(:"lame_get_#{flag}")
31
- end
32
-
33
- def has_setter?(lame, flag)
34
- lame.respond_to?(:"lame_set_#{flag}")
35
- end
36
-
37
- def set_value(lame, flag)
38
- lame.send(:"lame_set_#{flag}", @flags_pointer, @value) == return_value
39
- end
40
-
41
- def return_value
42
- defined?(@return) ? @return : 0
43
- end
44
32
 
45
- def has_value?(lame, flag)
46
- if @value && @value.is_a?(Float)
47
- actual = actual_value(lame, flag)
48
- (actual - @value).abs < 0.0001
49
- elsif @value
50
- actual_value(lame, flag) == @value
51
- else
52
- true
53
- end
54
- end
55
-
56
- def actual_value(lame, flag)
57
- lame.send(:"lame_get_#{flag}", @flags_pointer)
58
- end
59
- end
60
-
61
- # Validate existence of a getter, setter and the default value.
62
- RSpec::Matchers.define :have_flag do |expected|
63
- include SetterGetter
64
-
65
- chain :for do |flags_pointer|
66
- @flags_pointer = flags_pointer
67
- end
68
-
69
- chain :with_value do |value|
70
- @value = value
71
- end
72
-
73
- match do |actual|
74
- has_getter?(actual, expected) &&
75
- has_setter?(actual, expected) &&
76
- has_value?(actual, expected)
77
- end
78
- end
79
-
80
- # Validate setting a value.
81
- RSpec::Matchers.define :be_able_to_set do |expected|
82
- include SetterGetter
83
-
84
- chain :for do |flags_pointer|
85
- @flags_pointer = flags_pointer
86
- end
87
-
88
- chain :to do |value|
89
- @value = value
90
- end
91
-
92
- chain :and_return do |value|
93
- @return = value
94
- end
95
-
96
- match do |actual|
97
- set_value(actual, expected) &&
98
- has_value?(actual, expected)
99
- end
100
- end
101
-
102
- # Validate getting a value.
103
- RSpec::Matchers.define :have_getter do |expected|
104
- include SetterGetter
105
-
106
- chain :for do |flags_pointer|
107
- @flags_pointer = flags_pointer
108
- end
109
-
110
- chain :with_value do |value|
111
- @value = value
112
- end
113
-
114
- match do |actual|
115
- has_getter?(actual, expected) &&
116
- has_value?(actual, expected)
117
- end
118
-
119
- failure_message_for_should do |actual|
120
- if !has_getter?(actual, expected)
121
- "expected that #{actual} would have a getter for field :#{expected}"
122
- elsif @value && !has_value?(actual, expected)
123
- actual_value = actual_value(actual, expected)
124
- "expected field :#{expected} to have a value of #{@value}, but got #{actual_value}"
125
- end
126
- end
127
- end
128
-
129
- # Validate delegation to global_flags.
130
- RSpec::Matchers.define :delegate do |from|
131
- include SetterGetter
132
-
133
- chain :to do |target|
134
- @target = target
135
- end
136
-
137
- match do |subject|
138
- @from = from
139
- has_setter?(LAME, target) &&
140
- delegates_setter? &&
141
- has_getter?(LAME, target) &&
142
- delegates_getter?
143
- end
144
-
145
- def delegates_setter?
146
- LAME.should_receive(:"lame_set_#{target}").with(subject.global_flags, anything)
147
- subject.send(:"#{from}=", double)
148
- true
149
- rescue => e
150
- # TODO: save raised exception for better failure message
151
- false
152
- end
153
-
154
- def delegates_getter?
155
- LAME.should_receive(:"lame_get_#{target}").with(subject.global_flags)
156
- subject.send(:"#{from}")
157
- true
158
- rescue => e
159
- # TODO: save raised exception for better failure message
160
- false
161
- end
162
-
163
- failure_message_for_should do |actual|
164
- "expected #{subject.class} to delegate :#{from} to LAME.lame_set_#{target}"
165
- end
166
-
167
- def target
168
- @target || from
169
- end
170
-
171
- def from
172
- @from
173
- end
174
-
175
- end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: lame
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Roel van Dijk
@@ -154,9 +154,15 @@ files:
154
154
  - lib/lame/decoding/stream_decoder.rb
155
155
  - lib/lame/delegation.rb
156
156
  - lib/lame/encoder.rb
157
- - lib/lame/encoding/encode_short_buffer.rb
157
+ - lib/lame/encoding/float_buffer_encoder.rb
158
158
  - lib/lame/encoding/flusher.rb
159
159
  - lib/lame/encoding/id3.rb
160
+ - lib/lame/encoding/interleaved_buffer_encoder.rb
161
+ - lib/lame/encoding/interleaved_float_buffer_encoder.rb
162
+ - lib/lame/encoding/interleaved_short_buffer_encoder.rb
163
+ - lib/lame/encoding/long_buffer_encoder.rb
164
+ - lib/lame/encoding/short_buffer_encoder.rb
165
+ - lib/lame/encoding/stereo_buffer_encoder.rb
160
166
  - lib/lame/encoding/vbr_info.rb
161
167
  - lib/lame/error.rb
162
168
  - lib/lame/ffi.rb
@@ -179,9 +185,10 @@ files:
179
185
  - spec/decoding/stream_decoder_spec.rb
180
186
  - spec/delegation_spec.rb
181
187
  - spec/encoder_spec.rb
182
- - spec/encoding/encode_short_buffer_spec.rb
183
188
  - spec/encoding/flusher_spec.rb
184
189
  - spec/encoding/id3_spec.rb
190
+ - spec/encoding/interleaved_buffer_encoder_spec.rb
191
+ - spec/encoding/stereo_buffer_encoder_spec.rb
185
192
  - spec/encoding/vbr_info_spec.rb
186
193
  - spec/ffi/decode_flags_spec.rb
187
194
  - spec/ffi/encoding_spec.rb
@@ -189,10 +196,12 @@ files:
189
196
  - spec/ffi/id3tag_spec.rb
190
197
  - spec/ffi/mp3_data_spec.rb
191
198
  - spec/files/dies-irae-cli-id3v2.mp3
192
- - spec/files/dies-irae.wav
193
199
  - spec/integration/decoding_spec.rb
194
200
  - spec/integration/encoding_spec.rb
195
201
  - spec/integration/id3_tags_spec.rb
202
+ - spec/lib/custom_matchers.rb
203
+ - spec/lib/setter_getter.rb
204
+ - spec/lib/wave_file_generator.rb
196
205
  - spec/spec_helper.rb
197
206
  homepage: http://github.com/rdvdijk/lame
198
207
  licenses: []
@@ -204,7 +213,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
204
213
  requirements:
205
214
  - - ! '>='
206
215
  - !ruby/object:Gem::Version
207
- hash: 3287054386049799145
216
+ hash: -3758969818972899808
208
217
  segments:
209
218
  - 0
210
219
  version: '0'
@@ -213,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
222
  requirements:
214
223
  - - ! '>='
215
224
  - !ruby/object:Gem::Version
216
- hash: 3287054386049799145
225
+ hash: -3758969818972899808
217
226
  segments:
218
227
  - 0
219
228
  version: '0'
@@ -237,9 +246,10 @@ test_files:
237
246
  - spec/decoding/stream_decoder_spec.rb
238
247
  - spec/delegation_spec.rb
239
248
  - spec/encoder_spec.rb
240
- - spec/encoding/encode_short_buffer_spec.rb
241
249
  - spec/encoding/flusher_spec.rb
242
250
  - spec/encoding/id3_spec.rb
251
+ - spec/encoding/interleaved_buffer_encoder_spec.rb
252
+ - spec/encoding/stereo_buffer_encoder_spec.rb
243
253
  - spec/encoding/vbr_info_spec.rb
244
254
  - spec/ffi/decode_flags_spec.rb
245
255
  - spec/ffi/encoding_spec.rb
@@ -247,8 +257,10 @@ test_files:
247
257
  - spec/ffi/id3tag_spec.rb
248
258
  - spec/ffi/mp3_data_spec.rb
249
259
  - spec/files/dies-irae-cli-id3v2.mp3
250
- - spec/files/dies-irae.wav
251
260
  - spec/integration/decoding_spec.rb
252
261
  - spec/integration/encoding_spec.rb
253
262
  - spec/integration/id3_tags_spec.rb
263
+ - spec/lib/custom_matchers.rb
264
+ - spec/lib/setter_getter.rb
265
+ - spec/lib/wave_file_generator.rb
254
266
  - spec/spec_helper.rb
@@ -1,26 +0,0 @@
1
- module LAME
2
- module Encoding
3
- class EncodeShortBuffer
4
- extend Forwardable
5
-
6
- def_delegators :@configuration, :global_flags, :framesize, :output_buffer_size
7
-
8
- def initialize(configuration)
9
- @configuration = configuration
10
- end
11
-
12
- def encode_frame(left, right)
13
- left_buffer = Buffer.create(:short, left)
14
- right_buffer = Buffer.create(:short, right)
15
- output = Buffer.create_empty(:uchar, output_buffer_size)
16
-
17
- mp3_size = LAME.lame_encode_buffer(global_flags,
18
- left_buffer, right_buffer, left.size,
19
- output, output_buffer_size)
20
-
21
- output.get_bytes(0, mp3_size)
22
- end
23
-
24
- end
25
- end
26
- end
Binary file