File: unvalidated_format_test.rb

package info (click to toggle)
ruby-wavefile 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,708 kB
  • sloc: ruby: 4,171; makefile: 2
file content (325 lines) | stat: -rw-r--r-- 15,844 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
require 'minitest/autorun'
require 'wavefile.rb'

include WaveFile

class UnvalidatedFormatTest < Minitest::Test
  def test_initialize
    format = UnvalidatedFormat.new({:audio_format => 65534,
                                    :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                    :channels => 2,
                                    :sample_rate => 44100,
                                    :byte_rate => 176400,
                                    :block_align => 4,
                                    :bits_per_sample => 16,
                                    :valid_bits_per_sample => 14,
                                    :speaker_mapping => 3})  # Bit field '11'

    assert_equal(65534,  format.audio_format)
    assert_equal(SUB_FORMAT_GUID_PCM, format.sub_audio_format_guid)
    assert_equal(2,      format.channels)
    assert_equal(false,  format.mono?)
    assert_equal(true,   format.stereo?)
    assert_equal(44100,  format.sample_rate)
    assert_equal(176400, format.byte_rate)
    assert_equal(4,      format.block_align)
    assert_equal(16,     format.bits_per_sample)
    assert_equal(14,     format.valid_bits_per_sample)
    assert_equal([:front_left, :front_right], format.speaker_mapping)
  end

  def test_to_validated_format_pcm
    unvalidated_format = UnvalidatedFormat.new({:audio_format => 1,
                                                :channels => 2,
                                                :sample_rate => 44100,
                                                :byte_rate => 176400,
                                                :block_align => 4,
                                                :bits_per_sample => 16})

    validated_format = unvalidated_format.to_validated_format
    assert_equal(:pcm, validated_format.sample_format)
    assert_equal(2, validated_format.channels)
    assert_equal(16, validated_format.bits_per_sample)
    assert_equal(44100, validated_format.sample_rate)
    assert_equal(176400, validated_format.byte_rate)
    assert_equal(4, validated_format.block_align)
    assert_equal([:front_left, :front_right], validated_format.speaker_mapping)
  end

  def test_to_validated_format_float
    unvalidated_format = UnvalidatedFormat.new({:audio_format => 3,
                                                :channels => 2,
                                                :sample_rate => 44100,
                                                :byte_rate => 352800,
                                                :block_align => 8,
                                                :bits_per_sample => 32})

    validated_format = unvalidated_format.to_validated_format
    assert_equal(:float, validated_format.sample_format)
    assert_equal(2, validated_format.channels)
    assert_equal(32, validated_format.bits_per_sample)
    assert_equal(44100, validated_format.sample_rate)
    assert_equal(352800, validated_format.byte_rate)
    assert_equal(8, validated_format.block_align)
    assert_equal([:front_left, :front_right], validated_format.speaker_mapping)
  end

  def test_to_validated_format_unsupported
    unvalidated_format = UnvalidatedFormat.new({:audio_format => 2,
                                                :channels => 2,
                                                :sample_rate => 44100,
                                                :byte_rate => 176400,
                                                :block_align => 4,
                                                :bits_per_sample => 16})

    assert_raises(InvalidFormatError) { unvalidated_format.to_validated_format }
  end

  def test_to_validated_format_wave_format_extensible_pcm
    unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
                                                :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                                :channels => 2,
                                                :sample_rate => 44100,
                                                :byte_rate => 176400,
                                                :block_align => 4,
                                                :bits_per_sample => 16,
                                                :valid_bits_per_sample => 16,
                                                :speaker_mapping => 3})

    validated_format = unvalidated_format.to_validated_format
    assert_equal(:pcm, validated_format.sample_format)
    assert_equal(2, validated_format.channels)
    assert_equal(16, validated_format.bits_per_sample)
    assert_equal(44100, validated_format.sample_rate)
    assert_equal(176400, validated_format.byte_rate)
    assert_equal(4, validated_format.block_align)
    assert_equal([:front_left, :front_right], validated_format.speaker_mapping)
  end

  def test_to_validated_format_wave_format_extensible_float
    unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
                                                :sub_audio_format_guid => SUB_FORMAT_GUID_FLOAT,
                                                :channels => 2,
                                                :sample_rate => 44100,
                                                :byte_rate => 352800,
                                                :block_align => 8,
                                                :bits_per_sample => 32,
                                                :valid_bits_per_sample => 32,
                                                :speaker_mapping => 5})

    validated_format = unvalidated_format.to_validated_format
    assert_equal(:float, validated_format.sample_format)
    assert_equal(2, validated_format.channels)
    assert_equal(32, validated_format.bits_per_sample)
    assert_equal(44100, validated_format.sample_rate)
    assert_equal(352800, validated_format.byte_rate)
    assert_equal(8, validated_format.block_align)
    assert_equal([:front_left, :front_center], validated_format.speaker_mapping)
  end

  def test_to_validated_format_wave_format_extensible_unsupported_sub_format
    unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
                                                :sub_audio_format_guid => "\x02\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71",  # ADPCM
                                                :channels => 2,
                                                :sample_rate => 44100,
                                                :byte_rate => 176400,
                                                :block_align => 4,
                                                :bits_per_sample => 16,
                                                :valid_bits_per_sample => 16,
                                                :speaker_mapping => 3})

    assert_raises(InvalidFormatError) { unvalidated_format.to_validated_format }
  end

  def test_to_validated_format_wave_format_extensible_unsupported_valid_bits_per_sample
    unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
                                                :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                                :channels => 2,
                                                :sample_rate => 44100,
                                                :byte_rate => 176400,
                                                :block_align => 4,
                                                :bits_per_sample => 14,
                                                :valid_bits_per_sample => 14,
                                                :speaker_mapping => 3})

    assert_raises(InvalidFormatError) { unvalidated_format.to_validated_format }
  end

  def test_to_validated_format_wave_format_extensible_valid_bits_per_sample_differs_from_container_size
    unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
                                                :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                                :channels => 2,
                                                :sample_rate => 44100,
                                                :byte_rate => 176400,
                                                :block_align => 4,
                                                :bits_per_sample => 16,
                                                :valid_bits_per_sample => 14,
                                                :speaker_mapping => 3})

    assert_raises(UnsupportedFormatError) { unvalidated_format.to_validated_format }
  end

  def test_speaker_mapping_no_speakers_defined
    unvalided_format = UnvalidatedFormat.new({:audio_format => 65534,
                                              :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                              :channels => 3,
                                              :sample_rate => 44100,
                                              :byte_rate => 264600,
                                              :block_align => 6,
                                              :bits_per_sample => 16,
                                              :valid_bits_per_sample => 16,
                                              :speaker_mapping => 0 })  # According to spec, 0 means speakers
                                                                        # are explicitly undefined for all channels

    assert_equal(3, unvalided_format.channels)
    assert_equal([:undefined, :undefined, :undefined], unvalided_format.speaker_mapping)
  end

  def test_speaker_mapping_more_speakers_defined_than_channels
    unvalided_format = UnvalidatedFormat.new({:audio_format => 65534,
                                              :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                              :channels => 3,
                                              :sample_rate => 44100,
                                              :byte_rate => 264600,
                                              :block_align => 6,
                                              :bits_per_sample => 16,
                                              :valid_bits_per_sample => 16,
                                              :speaker_mapping => 214 })  # Bit field '11010110'

    assert_equal(3, unvalided_format.channels)

    # All 5 channel->speaker mappings are present, even though files only has 3 channels
    assert_equal([:front_right, :front_center, :back_left, :front_left_of_center, :front_right_of_center], unvalided_format.speaker_mapping)
  end

  def test_speaker_mapping_more_speakers_than_are_defined
    expected_speaker_mapping = [
      :front_left,
      :front_right,
      :front_center,
      :low_frequency,
      :back_left,
      :back_right,
      :front_left_of_center,
      :front_right_of_center,
      :back_center,
      :side_left,
      :side_right,
      :top_center,
      :top_front_left,
      :top_front_center,
      :top_front_right,
      :top_back_left,
      :top_back_center,
      :top_back_right,
    ]

    unvalided_format = UnvalidatedFormat.new({:audio_format => 65534,
                                              :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                              :channels => 3,
                                              :sample_rate => 44100,
                                              :byte_rate => 264600,
                                              :block_align => 6,
                                              :bits_per_sample => 16,
                                              :valid_bits_per_sample => 16,
                                              :speaker_mapping => 1048575 })  # Bit field '1111_1111_1111_1111_1111'

    assert_equal(3, unvalided_format.channels)

    # All channel->speaker mappings are present, even though file doesn't have this many channels
    assert_equal(expected_speaker_mapping, unvalided_format.speaker_mapping)
  end

  def test_speaker_mapping_more_channels_than_mapped_speakers
    unvalided_format = UnvalidatedFormat.new({:audio_format => 65534,
                                              :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                              :channels => 3,
                                              :sample_rate => 44100,
                                              :byte_rate => 264600,
                                              :block_align => 6,
                                              :bits_per_sample => 16,
                                              :valid_bits_per_sample => 16,
                                              :speaker_mapping => 2 })  # Bit field '10'

    assert_equal(3, unvalided_format.channels)
    assert_equal([:front_right, :undefined, :undefined], unvalided_format.speaker_mapping)
  end

  def test_speaker_mapping_more_channels_than_defined_speakers
    expected_speaker_mapping = [
      :front_left,
      :front_right,
      :front_center,
      :low_frequency,
      :back_left,
      :back_right,
      :front_left_of_center,
      :front_right_of_center,
      :back_center,
      :side_left,
      :side_right,
      :top_center,
      :top_front_left,
      :top_front_center,
      :top_front_right,
      :top_back_left,
      :top_back_center,
      :top_back_right,
      :undefined,   # Spec only defines first 18 speakers, any subsequent ones are undefined
      :undefined,   # Spec only defines first 18 speakers, any subsequent ones are undefined
    ]

    unvalided_format = UnvalidatedFormat.new({:audio_format => 65534,
                                              :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                              :channels => 20,
                                              :sample_rate => 44100,
                                              :byte_rate => 1764000,
                                              :block_align => 40,
                                              :bits_per_sample => 16,
                                              :valid_bits_per_sample => 16,
                                              :speaker_mapping => 262143 })  # Bit field '11_1111_1111_1111_1111'

    assert_equal(20, unvalided_format.channels)
    assert_equal(expected_speaker_mapping, unvalided_format.speaker_mapping)
  end

  def test_speaker_mapping_more_channels_than_defined_speakers_2
    expected_speaker_mapping = [
      :front_left,
      :front_right,
      :front_center,
      :low_frequency,
      :back_left,
      :back_right,
      :front_left_of_center,
      :front_right_of_center,
      :back_center,
      :side_left,
      :side_right,
      :top_center,
      :top_front_left,
      :top_front_center,
      :top_front_right,
      :top_back_left,
      :top_back_center,
      :top_back_right,
      :undefined,
      :undefined,
    ]

    unvalided_format = UnvalidatedFormat.new({:audio_format => 65534,
                                              :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
                                              :channels => 20,
                                              :sample_rate => 44100,
                                              :byte_rate => 264600,
                                              :block_align => 6,
                                              :bits_per_sample => 16,
                                              :valid_bits_per_sample => 16,
                                              :speaker_mapping => 1048575 })  # Bit field '1111_1111_1111_1111_1111'

    assert_equal(20, unvalided_format.channels)

    # All channel->speaker mappings are present, even though file doesn't have this many channels
    assert_equal(expected_speaker_mapping, unvalided_format.speaker_mapping)
  end
end