wavefile 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE +1 -1
  3. data/README.markdown +71 -18
  4. data/lib/wavefile/buffer.rb +63 -39
  5. data/lib/wavefile/duration.rb +34 -0
  6. data/lib/wavefile/format.rb +42 -16
  7. data/lib/wavefile/info.rb +5 -38
  8. data/lib/wavefile/reader.rb +88 -61
  9. data/lib/wavefile/writer.rb +57 -25
  10. data/lib/wavefile.rb +6 -4
  11. data/test/buffer_test.rb +227 -37
  12. data/test/duration_test.rb +73 -0
  13. data/test/fixtures/actual_output/{valid_mono_8_44100_with_padding_byte.wav → valid_mono_pcm_8_44100_with_padding_byte.wav} +0 -0
  14. data/test/fixtures/{expected_output → valid}/no_samples.wav +0 -0
  15. data/test/fixtures/valid/valid_mono_float_32_44100.wav +0 -0
  16. data/test/fixtures/valid/valid_mono_float_64_44100.wav +0 -0
  17. data/test/fixtures/{expected_output/valid_mono_16_44100.wav → valid/valid_mono_pcm_16_44100.wav} +0 -0
  18. data/test/fixtures/{expected_output/valid_mono_32_44100.wav → valid/valid_mono_pcm_32_44100.wav} +0 -0
  19. data/test/fixtures/{expected_output/valid_mono_8_44100.wav → valid/valid_mono_pcm_8_44100.wav} +0 -0
  20. data/test/fixtures/{expected_output/valid_mono_8_44100_with_padding_byte.wav → valid/valid_mono_pcm_8_44100_with_padding_byte.wav} +0 -0
  21. data/test/fixtures/valid/valid_stereo_float_32_44100.wav +0 -0
  22. data/test/fixtures/valid/valid_stereo_float_64_44100.wav +0 -0
  23. data/test/fixtures/{expected_output/valid_stereo_16_44100.wav → valid/valid_stereo_pcm_16_44100.wav} +0 -0
  24. data/test/fixtures/{expected_output/valid_stereo_32_44100.wav → valid/valid_stereo_pcm_32_44100.wav} +0 -0
  25. data/test/fixtures/{expected_output/valid_stereo_8_44100.wav → valid/valid_stereo_pcm_8_44100.wav} +0 -0
  26. data/test/fixtures/valid/valid_tri_float_32_44100.wav +0 -0
  27. data/test/fixtures/valid/valid_tri_float_64_44100.wav +0 -0
  28. data/test/fixtures/{expected_output/valid_tri_16_44100.wav → valid/valid_tri_pcm_16_44100.wav} +0 -0
  29. data/test/fixtures/{expected_output/valid_tri_32_44100.wav → valid/valid_tri_pcm_32_44100.wav} +0 -0
  30. data/test/fixtures/{expected_output/valid_tri_8_44100.wav → valid/valid_tri_pcm_8_44100.wav} +0 -0
  31. data/test/format_test.rb +88 -58
  32. data/test/info_test.rb +9 -37
  33. data/test/reader_test.rb +160 -63
  34. data/test/wavefile_io_test_helper.rb +40 -30
  35. data/test/writer_test.rb +124 -37
  36. metadata +29 -31
  37. data/test/fixtures/valid/valid_mono_16_44100.wav +0 -0
  38. data/test/fixtures/valid/valid_mono_32_44100.wav +0 -0
  39. data/test/fixtures/valid/valid_mono_8_44100.wav +0 -0
  40. data/test/fixtures/valid/valid_mono_8_44100_with_padding_byte.wav +0 -0
  41. data/test/fixtures/valid/valid_stereo_16_44100.wav +0 -0
  42. data/test/fixtures/valid/valid_stereo_32_44100.wav +0 -0
  43. data/test/fixtures/valid/valid_stereo_8_44100.wav +0 -0
  44. data/test/fixtures/valid/valid_tri_16_44100.wav +0 -0
  45. data/test/fixtures/valid/valid_tri_32_44100.wav +0 -0
  46. data/test/fixtures/valid/valid_tri_8_44100.wav +0 -0
data/test/buffer_test.rb CHANGED
@@ -5,8 +5,8 @@ include WaveFile
5
5
 
6
6
  class BufferTest < Test::Unit::TestCase
7
7
  def test_convert
8
- old_format = Format.new(1, 16, 44100)
9
- new_format = Format.new(2, 16, 22050)
8
+ old_format = Format.new(:mono, :pcm_16, 44100)
9
+ new_format = Format.new(:stereo, :pcm_16, 22050)
10
10
 
11
11
  old_buffer = Buffer.new([-100, 0, 200], old_format)
12
12
  new_buffer = old_buffer.convert(new_format)
@@ -23,8 +23,8 @@ class BufferTest < Test::Unit::TestCase
23
23
  end
24
24
 
25
25
  def test_convert!
26
- old_format = Format.new(1, 16, 44100)
27
- new_format = Format.new(2, 16, 22050)
26
+ old_format = Format.new(:mono, :pcm_16, 44100)
27
+ new_format = Format.new(:stereo, :pcm_16, 22050)
28
28
 
29
29
  old_buffer = Buffer.new([-100, 0, 200], old_format)
30
30
  new_buffer = old_buffer.convert!(new_format)
@@ -38,38 +38,38 @@ class BufferTest < Test::Unit::TestCase
38
38
 
39
39
 
40
40
  def test_convert_buffer_channels
41
- Format::SUPPORTED_BITS_PER_SAMPLE.each do |bits_per_sample|
41
+ Format::SUPPORTED_BITS_PER_SAMPLE[:pcm].each do |bits_per_sample|
42
42
  [44100, 22050].each do |sample_rate|
43
43
  # Assert that not changing the number of channels is a no-op
44
- b = Buffer.new([-100, 0, 200], Format.new(1, bits_per_sample, sample_rate))
45
- b.convert!(Format.new(1, bits_per_sample, sample_rate))
44
+ b = Buffer.new([-100, 0, 200], Format.new(:mono, bits_per_sample, sample_rate))
45
+ b.convert!(Format.new(:mono, bits_per_sample, sample_rate))
46
46
  assert_equal([-100, 0, 200], b.samples)
47
47
 
48
48
  # Mono => Stereo
49
- b = Buffer.new([-100, 0, 200], Format.new(1, bits_per_sample, sample_rate))
50
- b.convert!(Format.new(2, bits_per_sample, sample_rate))
49
+ b = Buffer.new([-100, 0, 200], Format.new(:mono, bits_per_sample, sample_rate))
50
+ b.convert!(Format.new(:stereo, bits_per_sample, sample_rate))
51
51
  assert_equal([[-100, -100], [0, 0], [200, 200]], b.samples)
52
52
 
53
53
  # Mono => 3-channel
54
- b = Buffer.new([-100, 0, 200], Format.new(1, bits_per_sample, sample_rate))
54
+ b = Buffer.new([-100, 0, 200], Format.new(:mono, bits_per_sample, sample_rate))
55
55
  b.convert!(Format.new(3, bits_per_sample, sample_rate))
56
56
  assert_equal([[-100, -100, -100], [0, 0, 0], [200, 200, 200]], b.samples)
57
57
 
58
58
  # Stereo => Mono
59
- b = Buffer.new([[-100, -100], [0, 0], [200, 50], [8, 1]], Format.new(2, bits_per_sample, sample_rate))
60
- b.convert!(Format.new(1, bits_per_sample, sample_rate))
59
+ b = Buffer.new([[-100, -100], [0, 0], [200, 50], [8, 1]], Format.new(:stereo, bits_per_sample, sample_rate))
60
+ b.convert!(Format.new(:mono, bits_per_sample, sample_rate))
61
61
  assert_equal([-100, 0, 125, 4], b.samples)
62
62
 
63
63
  # 3-channel => Mono
64
64
  b = Buffer.new([[-100, -100, -100], [0, 0, 0], [200, 50, 650], [5, 1, 1], [5, 1, 2]],
65
65
  Format.new(3, bits_per_sample, sample_rate))
66
- b.convert!(Format.new(1, bits_per_sample, sample_rate))
66
+ b.convert!(Format.new(:mono, bits_per_sample, sample_rate))
67
67
  assert_equal([-100, 0, 300, 2, 2], b.samples)
68
68
 
69
69
  # 3-channel => Stereo
70
70
  b = Buffer.new([[-100, -100, -100], [1, 2, 3], [200, 50, 650]],
71
71
  Format.new(3, bits_per_sample, sample_rate))
72
- b.convert!(Format.new(2, bits_per_sample, sample_rate))
72
+ b.convert!(Format.new(:stereo, bits_per_sample, sample_rate))
73
73
  assert_equal([[-100, -100], [1, 2], [200, 50]], b.samples)
74
74
 
75
75
  # Unsupported conversion (4-channel => 3-channel)
@@ -80,42 +80,232 @@ class BufferTest < Test::Unit::TestCase
80
80
  end
81
81
  end
82
82
 
83
- def test_convert_buffer_bits_per_sample
84
- # Assert that not changing the number of channels is a no-op
85
- b = Buffer.new([-32768, -24576, -16384, -8192, 0, 8192, 16384, 24575, 32767], Format.new(1, 16, 44100))
86
- b.convert!(Format.new(1, 16, 44100))
87
- assert_equal([-32768, -24576, -16384, -8192, 0, 8192, 16384, 24575, 32767], b.samples)
83
+ def test_convert_buffer_bits_per_sample_no_op
84
+ Format::SUPPORTED_BITS_PER_SAMPLE[:pcm].each do |bits_per_sample|
85
+ b = Buffer.new([0, 128, 255], Format.new(:mono, bits_per_sample, 44100))
86
+ b.convert!(Format.new(:mono, bits_per_sample, 44100))
87
+
88
+ # Target format is the same as the original format, so the sample data should not change
89
+ assert_equal([0, 128, 255], b.samples)
90
+ end
91
+ end
88
92
 
89
- # 8 => 16, Mono
90
- b = Buffer.new([0, 32, 64, 96, 128, 160, 192, 223, 255], Format.new(1, 8, 44100))
91
- b.convert!(Format.new(1, 16, 44100))
93
+ def test_convert_buffer_bits_per_sample_8_to_16
94
+ # Mono
95
+ b = Buffer.new([0, 32, 64, 96, 128, 160, 192, 223, 255], Format.new(:mono, :pcm_8, 44100))
96
+ b.convert!(Format.new(:mono, :pcm_16, 44100))
92
97
  assert_equal([-32768, -24576, -16384, -8192, 0, 8192, 16384, 24320, 32512], b.samples)
93
98
 
94
- # 8 => 32, Mono
95
- b = Buffer.new([0, 32, 64, 96, 128, 160, 192, 223, 255], Format.new(1, 8, 44100))
96
- b.convert!(Format.new(1, 32, 44100))
99
+ # Stereo
100
+ b = Buffer.new([[0, 255], [32, 223], [64, 192], [96, 160], [128, 128],
101
+ [160, 96], [192, 64], [223, 32], [255, 0]],
102
+ Format.new(:stereo, :pcm_8, 44100))
103
+ b.convert!(Format.new(:stereo, :pcm_16, 44100))
104
+ assert_equal([[-32768, 32512], [-24576, 24320], [-16384, 16384], [-8192, 8192], [0, 0],
105
+ [8192, -8192], [16384, -16384], [24320, -24576], [32512, -32768]],
106
+ b.samples)
107
+ end
108
+
109
+ def test_convert_buffer_bits_per_sample_8_to_32
110
+ # Mono
111
+ b = Buffer.new([0, 32, 64, 96, 128, 160, 192, 223, 255], Format.new(:mono, :pcm_8, 44100))
112
+ b.convert!(Format.new(:mono, :pcm_32, 44100))
97
113
  assert_equal([-2147483648, -1610612736, -1073741824, -536870912, 0, 536870912, 1073741824, 1593835520, 2130706432], b.samples)
98
114
 
99
- # 16 => 8, Mono
100
- b = Buffer.new([-32768, -24576, -16384, -8192, 0, 8192, 16384, 24575, 32767], Format.new(1, 16, 44100))
101
- b.convert!(Format.new(1, 8, 44100))
115
+ # Stereo
116
+ b = Buffer.new([[0, 255], [32, 223], [64, 192], [96, 160], [128, 128],
117
+ [160, 96], [192, 64], [223, 32], [255, 0]],
118
+ Format.new(:stereo, :pcm_8, 44100))
119
+ b.convert!(Format.new(:stereo, :pcm_32, 44100))
120
+ assert_equal([[-2147483648, 2130706432], [-1610612736, 1593835520], [-1073741824, 1073741824], [-536870912, 536870912], [0, 0],
121
+ [536870912, -536870912], [1073741824, -1073741824], [1593835520, -1610612736], [2130706432, -2147483648]],
122
+ b.samples)
123
+ end
124
+
125
+ def test_convert_buffer_bits_per_sample_8_to_float
126
+ Format::SUPPORTED_BITS_PER_SAMPLE[:float].each do |bits_per_sample|
127
+ float_format = "float_#{bits_per_sample}".to_sym
128
+
129
+ # Mono
130
+ b = Buffer.new([0, 32, 64, 96, 128, 160, 192, 223, 255], Format.new(:mono, :pcm_8, 44100))
131
+ b.convert!(Format.new(:mono, float_format, 44100))
132
+ assert_equal([-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.7421875, 0.9921875], b.samples)
133
+
134
+ # Stereo
135
+ b = Buffer.new([[0, 255], [32, 223], [64, 192], [96, 160], [128, 128],
136
+ [160, 96], [192, 64], [223, 32], [255, 0]],
137
+ Format.new(:stereo, :pcm_8, 44100))
138
+ b.convert!(Format.new(:stereo, float_format, 44100))
139
+ assert_equal([[-1.0, 0.9921875], [-0.75, 0.7421875], [-0.5, 0.5], [-0.25, 0.25], [0.0, 0.0],
140
+ [0.25, -0.25], [0.5, -0.5], [0.7421875, -0.75], [0.9921875, -1.0]],
141
+ b.samples)
142
+ end
143
+ end
144
+
145
+ def test_convert_buffer_bits_per_sample_16_to_8
146
+ # Mono
147
+ b = Buffer.new([-32768, -24576, -16384, -8192, 0, 8192, 16384, 24575, 32767], Format.new(:mono, :pcm_16, 44100))
148
+ b.convert!(Format.new(:mono, :pcm_8, 44100))
102
149
  assert_equal([0, 32, 64, 96, 128, 160, 192, 223, 255], b.samples)
103
150
 
104
- # 16 => 32, Mono
105
- b = Buffer.new([-32768, -24576, -16384, -8192, 0, 8192, 16384, 24575, 32767], Format.new(1, 16, 44100))
106
- b.convert!(Format.new(1, 32, 44100))
151
+ # Stereo
152
+ b = Buffer.new([[-32768, 32767], [-24576, 24575], [-16384, 16384], [-8192, 8192], [0, 0],
153
+ [8192, -8192], [16384, -16384], [24575, -24576], [32767, -32768]],
154
+ Format.new(:stereo, :pcm_16, 44100))
155
+ b.convert!(Format.new(:stereo, :pcm_8, 44100))
156
+ assert_equal([[0, 255], [32, 223], [64, 192], [96, 160], [128, 128],
157
+ [160, 96], [192, 64], [223, 32], [255, 0]],
158
+ b.samples)
159
+ end
160
+
161
+ def test_convert_buffer_bits_per_sample_16_to_32
162
+ # Mono
163
+ b = Buffer.new([-32768, -24576, -16384, -8192, 0, 8192, 16384, 24575, 32767], Format.new(:mono, :pcm_16, 44100))
164
+ b.convert!(Format.new(:mono, :pcm_32, 44100))
107
165
  assert_equal([-2147483648, -1610612736, -1073741824, -536870912, 0, 536870912, 1073741824, 1610547200, 2147418112], b.samples)
108
166
 
109
- # 32 => 8, Mono
167
+ # Stereo
168
+ b = Buffer.new([[-32768, 32767], [-24576, 24575], [-16384, 16384], [-8192, 8192], [0, 0],
169
+ [8192, -8192], [16384, -16384], [24575, -24576], [32767, -32768]],
170
+ Format.new(:stereo, :pcm_16, 44100))
171
+ b.convert!(Format.new(:stereo, :pcm_32, 44100))
172
+ assert_equal([[-2147483648, 2147418112], [-1610612736, 1610547200], [-1073741824, 1073741824], [-536870912, 536870912], [0, 0],
173
+ [536870912, -536870912], [1073741824, -1073741824], [1610547200, -1610612736], [2147418112, -2147483648]],
174
+ b.samples)
175
+ end
176
+
177
+ def test_convert_buffer_bits_per_sample_16_to_float
178
+ Format::SUPPORTED_BITS_PER_SAMPLE[:float].each do |bits_per_sample|
179
+ float_format = "float_#{bits_per_sample}".to_sym
180
+
181
+ # Mono
182
+ b = Buffer.new([-32768, -24576, -16384, -8192, 0, 8192, 16384, 24575, 32767], Format.new(:mono, :pcm_16, 44100))
183
+ b.convert!(Format.new(:mono, float_format, 44100))
184
+ assert_equal([-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.749969482421875, 0.999969482421875], b.samples)
185
+
186
+ # Stereo
187
+ b = Buffer.new([[-32768, 32767], [-24576, 24575], [-16384, 16384], [-8192, 8192], [0, 0],
188
+ [8192, -8192], [16384, -16384], [24575, -24576], [32767, -32768]],
189
+ Format.new(:stereo, :pcm_16, 44100))
190
+ b.convert!(Format.new(:stereo, float_format, 44100))
191
+ assert_equal([[-1.0, 0.999969482421875], [-0.75, 0.749969482421875], [-0.5, 0.5], [-0.25, 0.25], [0.0, 0.0],
192
+ [0.25, -0.25], [0.5, -0.5], [0.749969482421875, -0.75], [0.999969482421875, -1.0]],
193
+ b.samples)
194
+ end
195
+ end
196
+
197
+ def test_convert_buffer_bits_per_sample_32_to_8
198
+ # Mono
110
199
  b = Buffer.new([-2147483648, -1610612736, -1073741824, -536870912, 0, 536870912, 1073741824, 1610612735, 2147483647],
111
- Format.new(1, 32, 44100))
112
- b.convert!(Format.new(1, 8, 44100))
200
+ Format.new(:mono, :pcm_32, 44100))
201
+ b.convert!(Format.new(:mono, :pcm_8, 44100))
113
202
  assert_equal([0, 32, 64, 96, 128, 160, 192, 223, 255], b.samples)
114
203
 
115
- # 32 => 16, Mono
204
+ # Stereo
205
+ b = Buffer.new([[-2147483648, 2147483647], [-1610612736, 1610612735], [-1073741824, 1073741824], [-536870912, 536870912], [0, 0],
206
+ [536870912, -536870912], [1073741824, -1073741824], [1610612735, -1610612736], [2147483647, -2147483648]],
207
+ Format.new(:stereo, :pcm_32, 44100))
208
+ b.convert!(Format.new(:stereo, :pcm_8, 44100))
209
+ assert_equal([[0, 255], [32, 223], [64, 192], [96, 160], [128, 128],
210
+ [160, 96], [192, 64], [223, 32], [255, 0]],
211
+ b.samples)
212
+ end
213
+
214
+ def test_convert_buffer_bits_per_sample_32_to_16
215
+ # Mono
116
216
  b = Buffer.new([-2147483648, -1610612736, -1073741824, -536870912, 0, 536870912, 1073741824, 1610612735, 2147483647],
117
- Format.new(1, 32, 44100))
118
- b.convert!(Format.new(1, 16, 44100))
217
+ Format.new(:mono, :pcm_32, 44100))
218
+ b.convert!(Format.new(:mono, :pcm_16, 44100))
119
219
  assert_equal([-32768, -24576, -16384, -8192, 0, 8192, 16384, 24575, 32767], b.samples)
220
+
221
+ # Stereo
222
+ b = Buffer.new([[-2147483648, 2147483647], [-1610612736, 1610612735], [-1073741824, 1073741824], [-536870912, 536870912], [0, 0],
223
+ [536870912, -536870912], [1073741824, -1073741824], [1610612735, -1610612736], [2147483647, -2147483648]],
224
+ Format.new(:stereo, :pcm_32, 44100))
225
+ b.convert!(Format.new(:stereo, :pcm_16, 44100))
226
+ assert_equal([[-32768, 32767], [-24576, 24575], [-16384, 16384], [-8192, 8192], [0, 0],
227
+ [8192, -8192], [16384, -16384], [24575, -24576], [32767, -32768]],
228
+ b.samples)
229
+ end
230
+
231
+ def test_convert_buffer_bits_per_sample_32_to_float
232
+ Format::SUPPORTED_BITS_PER_SAMPLE[:float].each do |bits_per_sample|
233
+ float_format = "float_#{bits_per_sample}".to_sym
234
+
235
+ # Mono
236
+ b = Buffer.new([-2147483648, -1610612736, -1073741824, -536870912, 0, 536870912, 1073741824, 1610612735, 2147483647],
237
+ Format.new(:mono, :pcm_32, 44100))
238
+ b.convert!(Format.new(:mono, float_format, 44100))
239
+ assert_equal([-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.7499999995343387, 0.9999999995343387], b.samples)
240
+
241
+ # Stereo
242
+ b = Buffer.new([[-2147483648, 2147483647], [-1610612736, 1610612735], [-1073741824, 1073741824], [-536870912, 536870912], [0, 0],
243
+ [536870912, -536870912], [1073741824, -1073741824], [1610612735, -1610612736], [2147483647, -2147483648]],
244
+ Format.new(:stereo, :pcm_32, 44100))
245
+ b.convert!(Format.new(:stereo, float_format, 44100))
246
+ assert_equal([[-1.0, 0.9999999995343387], [-0.75, 0.7499999995343387], [-0.5, 0.5], [-0.25, 0.25], [0.0, 0.0],
247
+ [0.25, -0.25], [0.5, -0.5], [0.7499999995343387, -0.75], [0.9999999995343387, -1.0]],
248
+ b.samples)
249
+ end
250
+ end
251
+
252
+ def test_convert_buffer_bits_per_sample_float_to_8
253
+ Format::SUPPORTED_BITS_PER_SAMPLE[:float].each do |bits_per_sample|
254
+ float_format = "float_#{bits_per_sample}".to_sym
255
+
256
+ # Mono
257
+ b = Buffer.new([-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0], Format.new(:mono, float_format, 44100))
258
+ b.convert!(Format.new(:mono, :pcm_8, 44100))
259
+ assert_equal([1, 33, 64, 96, 128, 160, 192, 223, 255], b.samples)
260
+
261
+ # Stereo
262
+ b = Buffer.new([[-1.0, 1.0], [-0.75, 0.75], [-0.5, 0.5], [-0.25, 0.25], [0.0, 0.0],
263
+ [0.25, -0.25], [0.5, -0.5], [0.75, -0.75], [1.0, -1.0]],
264
+ Format.new(:stereo, float_format, 44100))
265
+ b.convert!(Format.new(:stereo, :pcm_8, 44100))
266
+ assert_equal([[1, 255], [33, 223], [64, 192], [96, 160], [128, 128],
267
+ [160, 96], [192, 64], [223, 33], [255, 1]],
268
+ b.samples)
269
+ end
270
+ end
271
+
272
+ def test_convert_buffer_bits_per_sample_float_to_16
273
+ Format::SUPPORTED_BITS_PER_SAMPLE[:float].each do |bits_per_sample|
274
+ float_format = "float_#{bits_per_sample}".to_sym
275
+
276
+ # Mono
277
+ b = Buffer.new([-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0], Format.new(:mono, float_format, 44100))
278
+ b.convert!(Format.new(:mono, :pcm_16, 44100))
279
+ assert_equal([-32767, -24575, -16384, -8192, 0, 8192, 16384, 24575, 32767], b.samples)
280
+
281
+ # Stereo
282
+ b = Buffer.new([[-1.0, 1.0], [-0.75, 0.75], [-0.5, 0.5], [-0.25, 0.25], [0.0, 0.0],
283
+ [0.25, -0.25], [0.5, -0.5], [0.75, -0.75], [1.0, -1.0]],
284
+ Format.new(:stereo, float_format, 44100))
285
+ b.convert!(Format.new(:stereo, :pcm_16, 44100))
286
+ assert_equal([[-32767, 32767], [-24575, 24575], [-16384, 16384], [-8192, 8192], [0, 0],
287
+ [8192, -8192], [16384, -16384], [24575, -24575], [32767, -32767]],
288
+ b.samples)
289
+ end
290
+ end
291
+
292
+ def test_convert_buffer_bits_per_sample_float_to_32
293
+ Format::SUPPORTED_BITS_PER_SAMPLE[:float].each do |bits_per_sample|
294
+ float_format = "float_#{bits_per_sample}".to_sym
295
+
296
+ # Mono
297
+ b = Buffer.new([-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0], Format.new(:mono, float_format, 44100))
298
+ b.convert!(Format.new(:mono, :pcm_32, 44100))
299
+ assert_equal([-2147483647, -1610612735, -1073741824, -536870912, 0, 536870912, 1073741824, 1610612735, 2147483647], b.samples)
300
+
301
+ # Stereo
302
+ b = Buffer.new([[-1.0, 1.0], [-0.75, 0.75], [-0.5, 0.5], [-0.25, 0.25], [0.0, 0.0],
303
+ [0.25, -0.25], [0.5, -0.5], [0.75, -0.75], [1.0, -1.0]],
304
+ Format.new(:stereo, float_format, 44100))
305
+ b.convert!(Format.new(:stereo, :pcm_32, 44100))
306
+ assert_equal([[-2147483647, 2147483647], [-1610612735, 1610612735], [-1073741824, 1073741824], [-536870912, 536870912], [0, 0],
307
+ [536870912, -536870912], [1073741824, -1073741824], [1610612735, -1610612735], [2147483647, -2147483647]],
308
+ b.samples)
309
+ end
120
310
  end
121
311
  end
@@ -0,0 +1,73 @@
1
+ require 'test/unit'
2
+ require 'wavefile.rb'
3
+
4
+ include WaveFile
5
+
6
+ class DurationTest < Test::Unit::TestCase
7
+ SECONDS_IN_MINUTE = 60
8
+ SECONDS_IN_HOUR = SECONDS_IN_MINUTE * 60
9
+
10
+ def test_constructor
11
+ # Test common sample rates (22050 and 44100), and some crazy arbitrary sample rate (12346)
12
+ [22050, 44100, 12346].each do |sample_rate|
13
+ duration = Duration.new(0, sample_rate)
14
+ assert_equal(0, duration.hours)
15
+ assert_equal(0, duration.minutes)
16
+ assert_equal(0, duration.seconds)
17
+ assert_equal(0, duration.milliseconds)
18
+ assert_equal(0, duration.sample_frame_count)
19
+ assert_equal(sample_rate, duration.sample_rate)
20
+
21
+ duration = Duration.new(sample_rate / 2, sample_rate)
22
+ assert_equal(0, duration.hours)
23
+ assert_equal(0, duration.minutes)
24
+ assert_equal(0, duration.seconds)
25
+ assert_equal(500, duration.milliseconds)
26
+ assert_equal(sample_rate / 2, duration.sample_frame_count)
27
+ assert_equal(sample_rate, duration.sample_rate)
28
+
29
+ duration = Duration.new(sample_rate, sample_rate)
30
+ assert_equal(0, duration.hours)
31
+ assert_equal(0, duration.minutes)
32
+ assert_equal(1, duration.seconds)
33
+ assert_equal(0, duration.milliseconds)
34
+ assert_equal(sample_rate, duration.sample_frame_count)
35
+ assert_equal(sample_rate, duration.sample_rate)
36
+
37
+ duration = Duration.new(sample_rate * SECONDS_IN_MINUTE, sample_rate)
38
+ assert_equal(0, duration.hours)
39
+ assert_equal(1, duration.minutes)
40
+ assert_equal(0, duration.seconds)
41
+ assert_equal(0, duration.milliseconds)
42
+ assert_equal(sample_rate * SECONDS_IN_MINUTE, duration.sample_frame_count)
43
+ assert_equal(sample_rate, duration.sample_rate)
44
+
45
+ duration = Duration.new(sample_rate * SECONDS_IN_HOUR, sample_rate)
46
+ assert_equal(1, duration.hours)
47
+ assert_equal(0, duration.minutes)
48
+ assert_equal(0, duration.seconds)
49
+ assert_equal(0, duration.milliseconds)
50
+ assert_equal(sample_rate * SECONDS_IN_HOUR, duration.sample_frame_count)
51
+ assert_equal(sample_rate, duration.sample_rate)
52
+
53
+ sample_frame_count = (sample_rate * SECONDS_IN_MINUTE) + sample_rate + (sample_rate / 2)
54
+ duration = Duration.new(sample_frame_count, sample_rate)
55
+ assert_equal(0, duration.hours)
56
+ assert_equal(1, duration.minutes)
57
+ assert_equal(1, duration.seconds)
58
+ assert_equal(500, duration.milliseconds)
59
+ assert_equal(sample_frame_count, duration.sample_frame_count)
60
+ assert_equal(sample_rate, duration.sample_rate)
61
+ end
62
+
63
+ # Test for when the number of hours is more than a day.
64
+ samples_per_hour = 44100 * 60 * 60
65
+ duration = Duration.new(samples_per_hour * 25, 44100)
66
+ assert_equal(25, duration.hours)
67
+ assert_equal(0, duration.minutes)
68
+ assert_equal(0, duration.seconds)
69
+ assert_equal(0, duration.milliseconds)
70
+ assert_equal(samples_per_hour * 25, duration.sample_frame_count)
71
+ assert_equal(44100, duration.sample_rate)
72
+ end
73
+ end
data/test/format_test.rb CHANGED
@@ -4,100 +4,130 @@ require 'wavefile.rb'
4
4
  include WaveFile
5
5
 
6
6
  class FormatTest < Test::Unit::TestCase
7
- def test_valid_channels()
7
+ def test_valid_channels
8
8
  [1, 2, 3, 4, 65535].each do |valid_channels|
9
- assert_equal(valid_channels, Format.new(valid_channels, 16, 44100).channels)
9
+ assert_equal(valid_channels, Format.new(valid_channels, :pcm_16, 44100).channels)
10
10
  end
11
11
 
12
- assert_equal(1, Format.new(:mono, 16, 44100).channels)
13
- assert_equal(2, Format.new(:stereo, 16, 44100).channels)
12
+ assert_equal(1, Format.new(:mono, :pcm_16, 44100).channels)
13
+ assert_equal(2, Format.new(:stereo, :pcm_16, 44100).channels)
14
14
  end
15
15
 
16
- def test_invalid_channels()
16
+ def test_invalid_channels
17
17
  ["dsfsfsdf", :foo, 0, -1, 65536].each do |invalid_channels|
18
- assert_raise(InvalidFormatError) { Format.new(invalid_channels, 16, 44100) }
18
+ assert_raise(InvalidFormatError) { Format.new(invalid_channels, :pcm_16, 44100) }
19
19
  end
20
20
  end
21
21
 
22
- def test_valid_bits_per_sample()
23
- assert_equal(8, Format.new(1, 8, 44100).bits_per_sample)
24
- assert_equal(16, Format.new(1, 16, 44100).bits_per_sample)
25
- assert_equal(32, Format.new(1, 32, 44100).bits_per_sample)
22
+ def test_valid_sample_format
23
+ assert_equal(:pcm, Format.new(:mono, 8, 44100).sample_format)
24
+ assert_equal(:pcm, Format.new(:mono, 16, 44100).sample_format)
25
+ assert_equal(:pcm, Format.new(:mono, 32, 44100).sample_format)
26
+ assert_equal(:pcm, Format.new(:mono, :pcm_8, 44100).sample_format)
27
+ assert_equal(:pcm, Format.new(:mono, :pcm_16, 44100).sample_format)
28
+ assert_equal(:pcm, Format.new(:mono, :pcm_32, 44100).sample_format)
29
+ assert_equal(:float, Format.new(:mono, :float, 44100).sample_format)
30
+ assert_equal(:float, Format.new(:mono, :float_32, 44100).sample_format)
31
+ assert_equal(:float, Format.new(:mono, :float_64, 44100).sample_format)
26
32
  end
27
33
 
28
- def test_invalid_bits_per_sample()
29
- ["dsfsfsdf", :foo, 0, 12].each do |invalid_bits_per_sample|
30
- assert_raise(InvalidFormatError) { Format.new(1, invalid_bits_per_sample, 44100) }
34
+ def test_invalid_sample_format
35
+ ["dsfsfsdf", :foo, 12, :pcm_14, :float_20].each do |invalid_sample_format|
36
+ assert_raise(InvalidFormatError) { Format.new(:mono, invalid_sample_format, 44100) }
31
37
  end
32
38
  end
33
39
 
34
- def test_valid_sample_rate()
35
- [1, 44100, 4294967296].each do |valid_sample_rate|
36
- assert_equal(valid_sample_rate, Format.new(1, 16, valid_sample_rate).sample_rate)
37
- end
40
+ def test_valid_bits_per_sample
41
+ assert_equal(8, Format.new(:mono, 8, 44100).bits_per_sample)
42
+ assert_equal(16, Format.new(:mono, 16, 44100).bits_per_sample)
43
+ assert_equal(32, Format.new(:mono, 32, 44100).bits_per_sample)
44
+ assert_equal(8, Format.new(:mono, :pcm_8, 44100).bits_per_sample)
45
+ assert_equal(16, Format.new(:mono, :pcm_16, 44100).bits_per_sample)
46
+ assert_equal(32, Format.new(:mono, :pcm_32, 44100).bits_per_sample)
47
+ assert_equal(32, Format.new(:mono, :float, 44100).bits_per_sample)
48
+ assert_equal(32, Format.new(:mono, :float_32, 44100).bits_per_sample)
49
+ assert_equal(64, Format.new(:mono, :float_64, 44100).bits_per_sample)
38
50
  end
39
51
 
40
- def test_invalid_sample_rate()
41
- ["dsfsfsdf", :foo, 0, -1, 4294967297].each do |invalid_sample_rate|
42
- assert_raise(InvalidFormatError) { Format.new(1, 16, invalid_sample_rate) }
52
+ def test_invalid_bits_per_sample
53
+ ["dsfsfsdf", :foo, :pcm, 0, 12, :pcm_14, :pcm_abc, :float_40].each do |invalid_sample_format|
54
+ assert_raise(InvalidFormatError) { Format.new(:mono, invalid_sample_format, 44100) }
43
55
  end
44
56
  end
45
57
 
46
- def test_byte_rate()
47
- format = Format.new(1, 8, 44100)
48
- assert_equal(44100, format.byte_rate)
49
-
50
- format = Format.new(1, 16, 44100)
51
- assert_equal(88200, format.byte_rate)
52
-
53
- format = Format.new(1, 32, 44100)
54
- assert_equal(176400, format.byte_rate)
55
-
56
- format = Format.new(2, 8, 44100)
57
- assert_equal(88200, format.byte_rate)
58
-
59
- format = Format.new(2, 16, 44100)
60
- assert_equal(176400, format.byte_rate)
58
+ def test_valid_sample_rate
59
+ [1, 44100, 4294967296].each do |valid_sample_rate|
60
+ assert_equal(valid_sample_rate, Format.new(:mono, :pcm_16, valid_sample_rate).sample_rate)
61
+ end
62
+ end
61
63
 
62
- format = Format.new(2, 32, 44100)
63
- assert_equal(352800, format.byte_rate)
64
+ def test_invalid_sample_rate
65
+ ["dsfsfsdf", :foo, 0, -1, 4294967297].each do |invalid_sample_rate|
66
+ assert_raise(InvalidFormatError) { Format.new(:mono, :pcm_16, invalid_sample_rate) }
67
+ end
64
68
  end
65
69
 
66
- def test_block_align()
70
+ def test_byte_and_block_align
67
71
  [1, :mono].each do |one_channel|
68
- format = Format.new(one_channel, 8, 44100)
69
- assert_equal(1, format.block_align)
70
-
71
- format = Format.new(one_channel, 16, 44100)
72
- assert_equal(2, format.block_align)
73
-
74
- format = Format.new(one_channel, 32, 44100)
75
- assert_equal(4, format.block_align)
72
+ [:pcm_8, 8].each do |format_code|
73
+ format = Format.new(one_channel, format_code, 44100)
74
+ assert_equal(44100, format.byte_rate)
75
+ assert_equal(1, format.block_align)
76
+ end
77
+
78
+ [:pcm_16, 16].each do |format_code|
79
+ format = Format.new(one_channel, format_code, 44100)
80
+ assert_equal(88200, format.byte_rate)
81
+ assert_equal(2, format.block_align)
82
+ end
83
+
84
+ [:pcm_32, 32, :float, :float_32].each do |format_code|
85
+ format = Format.new(one_channel, format_code, 44100)
86
+ assert_equal(176400, format.byte_rate)
87
+ assert_equal(4, format.block_align)
88
+ end
89
+
90
+ format = Format.new(one_channel, :float_64, 44100)
91
+ assert_equal(352800, format.byte_rate)
92
+ assert_equal(8, format.block_align)
76
93
  end
77
94
 
78
95
  [2, :stereo].each do |two_channels|
79
- format = Format.new(two_channels, 8, 44100)
80
- assert_equal(2, format.block_align)
81
-
82
- format = Format.new(two_channels, 16, 44100)
83
- assert_equal(4, format.block_align)
84
-
85
- format = Format.new(two_channels, 32, 44100)
86
- assert_equal(8, format.block_align)
96
+ [:pcm_8, 8].each do |format_code|
97
+ format = Format.new(two_channels, format_code, 44100)
98
+ assert_equal(88200, format.byte_rate)
99
+ assert_equal(2, format.block_align)
100
+ end
101
+
102
+ [:pcm_16, 16].each do |format_code|
103
+ format = Format.new(two_channels, format_code, 44100)
104
+ assert_equal(176400, format.byte_rate)
105
+ assert_equal(4, format.block_align)
106
+ end
107
+
108
+ [:pcm_32, 32, :float, :float_32].each do |format_code|
109
+ format = Format.new(two_channels, format_code, 44100)
110
+ assert_equal(352800, format.byte_rate)
111
+ assert_equal(8, format.block_align)
112
+ end
113
+
114
+ format = Format.new(two_channels, :float_64, 44100)
115
+ assert_equal(705600, format.byte_rate)
116
+ assert_equal(16, format.block_align)
87
117
  end
88
118
  end
89
119
 
90
- def test_mono?()
120
+ def test_mono?
91
121
  [1, :mono].each do |one_channel|
92
- format = Format.new(one_channel, 8, 44100)
122
+ format = Format.new(one_channel, :pcm_8, 44100)
93
123
  assert_equal(true, format.mono?)
94
124
  assert_equal(false, format.stereo?)
95
125
  end
96
126
  end
97
127
 
98
- def test_stereo?()
128
+ def test_stereo?
99
129
  [2, :stereo].each do |two_channels|
100
- format = Format.new(two_channels, 8, 44100)
130
+ format = Format.new(two_channels, :pcm_8, 44100)
101
131
  assert_equal(false, format.mono?)
102
132
  assert_equal(true, format.stereo?)
103
133
  end