wavefile 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.markdown +29 -56
- data/Rakefile +6 -0
- data/lib/wavefile.rb +28 -452
- data/lib/wavefile/buffer.rb +147 -0
- data/lib/wavefile/format.rb +69 -0
- data/lib/wavefile/info.rb +53 -0
- data/lib/wavefile/reader.rb +296 -0
- data/lib/wavefile/writer.rb +128 -0
- data/test/buffer_test.rb +121 -0
- data/test/fixtures/actual_output/valid_mono_8_44100_with_padding_byte.wav +0 -0
- data/test/fixtures/expected_output/no_samples.wav +0 -0
- data/test/fixtures/expected_output/valid_mono_16_44100.wav +0 -0
- data/test/fixtures/expected_output/valid_mono_32_44100.wav +0 -0
- data/test/fixtures/expected_output/valid_mono_8_44100.wav +0 -0
- data/test/fixtures/expected_output/valid_mono_8_44100_with_padding_byte.wav +0 -0
- data/test/fixtures/expected_output/valid_stereo_16_44100.wav +0 -0
- data/test/fixtures/expected_output/valid_stereo_32_44100.wav +0 -0
- data/test/fixtures/expected_output/valid_stereo_8_44100.wav +0 -0
- data/test/fixtures/expected_output/valid_tri_16_44100.wav +0 -0
- data/test/fixtures/expected_output/valid_tri_32_44100.wav +0 -0
- data/test/fixtures/expected_output/valid_tri_8_44100.wav +0 -0
- data/test/fixtures/invalid/README.markdown +10 -0
- data/test/fixtures/invalid/bad_riff_header.wav +1 -0
- data/test/fixtures/invalid/bad_wavefile_format.wav +0 -0
- data/test/fixtures/invalid/empty.wav +0 -0
- data/test/fixtures/invalid/empty_format_chunk.wav +0 -0
- data/test/fixtures/invalid/incomplete_riff_header.wav +1 -0
- data/test/fixtures/invalid/insufficient_format_chunk.wav +0 -0
- data/test/fixtures/invalid/no_data_chunk.wav +0 -0
- data/test/fixtures/invalid/no_format_chunk.wav +0 -0
- data/test/fixtures/unsupported/README.markdown +6 -0
- data/test/fixtures/unsupported/bad_audio_format.wav +0 -0
- data/test/fixtures/unsupported/bad_channel_count.wav +0 -0
- data/test/fixtures/unsupported/bad_sample_rate.wav +0 -0
- data/test/fixtures/unsupported/unsupported_audio_format.wav +0 -0
- data/test/fixtures/unsupported/unsupported_bits_per_sample.wav +0 -0
- data/test/fixtures/valid/README.markdown +3 -0
- data/test/fixtures/valid/valid_mono_16_44100.wav +0 -0
- data/test/fixtures/valid/valid_mono_32_44100.wav +0 -0
- data/test/fixtures/valid/valid_mono_8_44100.wav +0 -0
- data/test/fixtures/valid/valid_mono_8_44100_with_padding_byte.wav +0 -0
- data/test/fixtures/valid/valid_stereo_16_44100.wav +0 -0
- data/test/fixtures/valid/valid_stereo_32_44100.wav +0 -0
- data/test/fixtures/valid/valid_stereo_8_44100.wav +0 -0
- data/test/fixtures/valid/valid_tri_16_44100.wav +0 -0
- data/test/fixtures/valid/valid_tri_32_44100.wav +0 -0
- data/test/fixtures/valid/valid_tri_8_44100.wav +0 -0
- data/test/format_test.rb +105 -0
- data/test/info_test.rb +60 -0
- data/test/reader_test.rb +222 -0
- data/test/wavefile_io_test_helper.rb +47 -0
- data/test/writer_test.rb +118 -0
- metadata +72 -33
- data/test/wavefile_test.rb +0 -339
metadata
CHANGED
@@ -1,58 +1,97 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: wavefile
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
+
authors:
|
7
8
|
- Joel Strait
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
date: 2010-01-12 00:00:00 -05:00
|
13
|
-
default_executable:
|
12
|
+
date: 2012-01-01 00:00:00.000000000 Z
|
14
13
|
dependencies: []
|
15
|
-
|
16
14
|
description:
|
17
|
-
email: joel
|
15
|
+
email: joel dot strait at Google's popular web mail service
|
18
16
|
executables: []
|
19
|
-
|
20
17
|
extensions: []
|
21
|
-
|
22
18
|
extra_rdoc_files: []
|
23
|
-
|
24
|
-
files:
|
19
|
+
files:
|
25
20
|
- LICENSE
|
26
21
|
- README.markdown
|
22
|
+
- Rakefile
|
23
|
+
- lib/wavefile/buffer.rb
|
24
|
+
- lib/wavefile/format.rb
|
25
|
+
- lib/wavefile/info.rb
|
26
|
+
- lib/wavefile/reader.rb
|
27
|
+
- lib/wavefile/writer.rb
|
27
28
|
- lib/wavefile.rb
|
28
|
-
- test/
|
29
|
-
|
29
|
+
- test/buffer_test.rb
|
30
|
+
- test/fixtures/actual_output/valid_mono_8_44100_with_padding_byte.wav
|
31
|
+
- test/fixtures/expected_output/no_samples.wav
|
32
|
+
- test/fixtures/expected_output/valid_mono_16_44100.wav
|
33
|
+
- test/fixtures/expected_output/valid_mono_32_44100.wav
|
34
|
+
- test/fixtures/expected_output/valid_mono_8_44100.wav
|
35
|
+
- test/fixtures/expected_output/valid_mono_8_44100_with_padding_byte.wav
|
36
|
+
- test/fixtures/expected_output/valid_stereo_16_44100.wav
|
37
|
+
- test/fixtures/expected_output/valid_stereo_32_44100.wav
|
38
|
+
- test/fixtures/expected_output/valid_stereo_8_44100.wav
|
39
|
+
- test/fixtures/expected_output/valid_tri_16_44100.wav
|
40
|
+
- test/fixtures/expected_output/valid_tri_32_44100.wav
|
41
|
+
- test/fixtures/expected_output/valid_tri_8_44100.wav
|
42
|
+
- test/fixtures/invalid/bad_riff_header.wav
|
43
|
+
- test/fixtures/invalid/bad_wavefile_format.wav
|
44
|
+
- test/fixtures/invalid/empty.wav
|
45
|
+
- test/fixtures/invalid/empty_format_chunk.wav
|
46
|
+
- test/fixtures/invalid/incomplete_riff_header.wav
|
47
|
+
- test/fixtures/invalid/insufficient_format_chunk.wav
|
48
|
+
- test/fixtures/invalid/no_data_chunk.wav
|
49
|
+
- test/fixtures/invalid/no_format_chunk.wav
|
50
|
+
- test/fixtures/invalid/README.markdown
|
51
|
+
- test/fixtures/unsupported/bad_audio_format.wav
|
52
|
+
- test/fixtures/unsupported/bad_channel_count.wav
|
53
|
+
- test/fixtures/unsupported/bad_sample_rate.wav
|
54
|
+
- test/fixtures/unsupported/README.markdown
|
55
|
+
- test/fixtures/unsupported/unsupported_audio_format.wav
|
56
|
+
- test/fixtures/unsupported/unsupported_bits_per_sample.wav
|
57
|
+
- test/fixtures/valid/README.markdown
|
58
|
+
- test/fixtures/valid/valid_mono_16_44100.wav
|
59
|
+
- test/fixtures/valid/valid_mono_32_44100.wav
|
60
|
+
- test/fixtures/valid/valid_mono_8_44100.wav
|
61
|
+
- test/fixtures/valid/valid_mono_8_44100_with_padding_byte.wav
|
62
|
+
- test/fixtures/valid/valid_stereo_16_44100.wav
|
63
|
+
- test/fixtures/valid/valid_stereo_32_44100.wav
|
64
|
+
- test/fixtures/valid/valid_stereo_8_44100.wav
|
65
|
+
- test/fixtures/valid/valid_tri_16_44100.wav
|
66
|
+
- test/fixtures/valid/valid_tri_32_44100.wav
|
67
|
+
- test/fixtures/valid/valid_tri_8_44100.wav
|
68
|
+
- test/format_test.rb
|
69
|
+
- test/info_test.rb
|
70
|
+
- test/reader_test.rb
|
71
|
+
- test/wavefile_io_test_helper.rb
|
72
|
+
- test/writer_test.rb
|
30
73
|
homepage: http://www.joelstrait.com/
|
31
74
|
licenses: []
|
32
|
-
|
33
75
|
post_install_message:
|
34
76
|
rdoc_options: []
|
35
|
-
|
36
|
-
require_paths:
|
77
|
+
require_paths:
|
37
78
|
- lib
|
38
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
50
91
|
requirements: []
|
51
|
-
|
52
92
|
rubyforge_project:
|
53
|
-
rubygems_version: 1.
|
93
|
+
rubygems_version: 1.8.10
|
54
94
|
signing_key:
|
55
95
|
specification_version: 3
|
56
96
|
summary: A class for reading and writing Wave sound files (*.wav)
|
57
97
|
test_files: []
|
58
|
-
|
data/test/wavefile_test.rb
DELETED
@@ -1,339 +0,0 @@
|
|
1
|
-
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
-
|
3
|
-
require 'test/unit'
|
4
|
-
require 'wavefile'
|
5
|
-
|
6
|
-
class WaveFileTest < Test::Unit::TestCase
|
7
|
-
def test_initialize
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_read_empty_file
|
12
|
-
assert_raise(StandardError) { w = WaveFile.open("examples/invalid/empty.wav") }
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_read_nonexistent_file
|
16
|
-
assert_raise(Errno::ENOENT) { w = WaveFile.open("examples/invalid/nonexistent.wav") }
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_read_valid_file
|
20
|
-
# Mono file
|
21
|
-
w = WaveFile.open("examples/valid/sine-mono-8bit.wav")
|
22
|
-
assert_equal(w.num_channels, 1)
|
23
|
-
assert_equal(w.mono?, true)
|
24
|
-
assert_equal(w.stereo?, false)
|
25
|
-
assert_equal(w.sample_rate, 44100)
|
26
|
-
assert_equal(w.bits_per_sample, 8)
|
27
|
-
assert_equal(w.byte_rate, 44100)
|
28
|
-
assert_equal(w.block_align, 1)
|
29
|
-
assert_equal(w.sample_data.length, 44100)
|
30
|
-
# Test that sample array is in format [sample, sample ... sample]
|
31
|
-
valid = true
|
32
|
-
w.sample_data.each{|sample| valid &&= (sample.class == Fixnum)}
|
33
|
-
assert_equal(valid, true)
|
34
|
-
|
35
|
-
# Stereo file
|
36
|
-
w = WaveFile.open("examples/valid/sine-stereo-8bit.wav")
|
37
|
-
assert_equal(w.num_channels, 2)
|
38
|
-
assert_equal(w.mono?, false)
|
39
|
-
assert_equal(w.stereo?, true)
|
40
|
-
assert_equal(w.sample_rate, 44100)
|
41
|
-
assert_equal(w.bits_per_sample, 8)
|
42
|
-
assert_equal(w.byte_rate, 88200)
|
43
|
-
assert_equal(w.block_align, 2)
|
44
|
-
assert_equal(w.sample_data.length, 44100)
|
45
|
-
# Test that sample array is in format [[left, right], [left, right] ... [left,right]]
|
46
|
-
valid = true
|
47
|
-
w.sample_data.each{|sample| valid &&= (sample.class == Array) && (sample.length == 2)}
|
48
|
-
assert_equal(valid, true)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_new_file
|
52
|
-
# Mono
|
53
|
-
w = WaveFile.new(1, 44100, 8)
|
54
|
-
assert_equal(w.num_channels, 1)
|
55
|
-
assert_equal(w.sample_rate, 44100)
|
56
|
-
assert_equal(w.bits_per_sample, 8)
|
57
|
-
assert_equal(w.byte_rate, 44100)
|
58
|
-
assert_equal(w.block_align, 1)
|
59
|
-
|
60
|
-
# Mono
|
61
|
-
w = WaveFile.new(:mono, 44100, 8)
|
62
|
-
assert_equal(w.num_channels, 1)
|
63
|
-
assert_equal(w.sample_rate, 44100)
|
64
|
-
assert_equal(w.bits_per_sample, 8)
|
65
|
-
assert_equal(w.byte_rate, 44100)
|
66
|
-
assert_equal(w.block_align, 1)
|
67
|
-
|
68
|
-
# Stereo
|
69
|
-
w = WaveFile.new(2, 44100, 16)
|
70
|
-
assert_equal(w.num_channels, 2)
|
71
|
-
assert_equal(w.sample_rate, 44100)
|
72
|
-
assert_equal(w.bits_per_sample, 16)
|
73
|
-
assert_equal(w.byte_rate, 176400)
|
74
|
-
assert_equal(w.block_align, 4)
|
75
|
-
|
76
|
-
# Stereo
|
77
|
-
w = WaveFile.new(:stereo, 44100, 16)
|
78
|
-
assert_equal(w.num_channels, 2)
|
79
|
-
assert_equal(w.sample_rate, 44100)
|
80
|
-
assert_equal(w.bits_per_sample, 16)
|
81
|
-
assert_equal(w.byte_rate, 176400)
|
82
|
-
assert_equal(w.block_align, 4)
|
83
|
-
|
84
|
-
# Quad
|
85
|
-
w = WaveFile.new(4, 44100, 16)
|
86
|
-
assert_equal(w.num_channels, 4)
|
87
|
-
assert_equal(w.sample_rate, 44100)
|
88
|
-
assert_equal(w.bits_per_sample, 16)
|
89
|
-
assert_equal(w.byte_rate, 352800)
|
90
|
-
assert_equal(w.block_align, 8)
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_normalized_sample_data
|
94
|
-
# Mono 8-bit
|
95
|
-
w = WaveFile.new(:mono, 44100, 8)
|
96
|
-
w.sample_data = [0, 32, 64, 96, 128, 160, 192, 223, 255]
|
97
|
-
assert_equal(w.normalized_sample_data, [-1.0, -0.75, -0.5, -0.25, 0.0,
|
98
|
-
(32.0 / 127.0), (64.0 / 127.0), (95.0 / 127.0), 1.0])
|
99
|
-
|
100
|
-
# Mono 16-bit
|
101
|
-
w = WaveFile.new(:mono, 44100, 16)
|
102
|
-
w.sample_data = [-32768, -24576, -16384, -8192, 0, 8192, 16383, 24575, 32767]
|
103
|
-
assert_equal(w.normalized_sample_data, [-1.0, -0.75, -0.5, -0.25, 0.0,
|
104
|
-
(8192.0 / 32767.0), (16383.0 / 32767.0), (24575.0 / 32767.0), 1.0])
|
105
|
-
|
106
|
-
# Stereo 8-bit
|
107
|
-
w = WaveFile.new(:stereo, 44100, 8)
|
108
|
-
w.sample_data = [[0, 255], [32, 223], [64, 192], [96, 160], [128, 128], [160, 96], [192, 64], [223, 32], [255, 0]]
|
109
|
-
assert_equal(w.normalized_sample_data, [[-1.0, 1.0],
|
110
|
-
[-0.75, (95.0 / 127.0)],
|
111
|
-
[-0.5, (64.0 / 127.0)],
|
112
|
-
[-0.25, (32.0 / 127.0)],
|
113
|
-
[0.0, 0.0],
|
114
|
-
[(32.0 / 127.0), -0.25],
|
115
|
-
[(64.0 / 127.0), -0.5],
|
116
|
-
[(95.0 / 127.0), -0.75],
|
117
|
-
[1.0, -1.0]])
|
118
|
-
|
119
|
-
# Stereo 16-bit
|
120
|
-
w = WaveFile.new(:stereo, 44100, 16)
|
121
|
-
w.sample_data = [[-32768, 32767], [-24576, 24575], [-16384, 16384], [-8192, 8192], [0, 0], [8192, -8192], [16384, -16384], [24575, -24576], [32767, -32768]]
|
122
|
-
assert_equal(w.normalized_sample_data, [[-1.0, 1.0],
|
123
|
-
[-0.75, (24575.0 / 32767.0)],
|
124
|
-
[-0.5, (16384.0 / 32767.0)],
|
125
|
-
[-0.25, (8192.0 / 32767.0)],
|
126
|
-
[0.0, 0.0],
|
127
|
-
[(8192.0 / 32767.0), -0.25],
|
128
|
-
[(16384.0 / 32767.0), -0.5],
|
129
|
-
[(24575.0 / 32767.0), -0.75],
|
130
|
-
[1.0, -1.0]])
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_sample_data=
|
134
|
-
# Mono 8-bit
|
135
|
-
w = WaveFile.new(:mono, 44100, 8)
|
136
|
-
w.sample_data = [-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0]
|
137
|
-
assert_equal(w.sample_data, [0, 32, 64, 96, 128, 160, 192, 223, 255])
|
138
|
-
|
139
|
-
# Mono 16-bit
|
140
|
-
w = WaveFile.new(:mono, 44100, 16)
|
141
|
-
w.sample_data = [-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0]
|
142
|
-
assert_equal(w.sample_data, [-32768, -24576, -16384, -8192, 0, 8192, 16384, 24575, 32767])
|
143
|
-
|
144
|
-
# Stereo 8-bit
|
145
|
-
w = WaveFile.new(:stereo, 44100, 8)
|
146
|
-
w.sample_data = [[-1.0, 1.0], [-0.75, 0.75], [-0.5, 0.5], [-0.25, 0.25], [0.0, 0.0],
|
147
|
-
[0.25, -0.25], [0.5, -0.5], [0.75, -0.75], [1.0, -1.0]]
|
148
|
-
assert_equal(w.sample_data, [[0, 255], [32, 223], [64, 192], [96, 160], [128, 128],
|
149
|
-
[160, 96], [192, 64], [223, 32], [255, 0]])
|
150
|
-
|
151
|
-
# Stereo 16-bit
|
152
|
-
w = WaveFile.new(:stereo, 44100, 16)
|
153
|
-
w.sample_data = [[-1.0, 1.0], [-0.75, 0.75], [-0.5, 0.5], [-0.25, 0.25], [0.0, 0.0],
|
154
|
-
[0.25, -0.25], [0.5, -0.5], [0.75, -0.75], [1.0, -1.0]]
|
155
|
-
assert_equal(w.sample_data, [[-32768, 32767], [-24576, 24575], [-16384, 16384], [-8192, 8192], [0, 0],
|
156
|
-
[8192, -8192], [16384, -16384], [24575, -24576], [32767, -32768]])
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_mono?
|
160
|
-
w = WaveFile.new(1, 44100, 16)
|
161
|
-
assert_equal(w.mono?, true)
|
162
|
-
|
163
|
-
w = WaveFile.open("examples/valid/sine-mono-8bit.wav")
|
164
|
-
assert_equal(w.mono?, true)
|
165
|
-
|
166
|
-
w = WaveFile.new(2, 44100, 16)
|
167
|
-
assert_equal(w.mono?, false)
|
168
|
-
|
169
|
-
w = WaveFile.new(4, 44100, 16)
|
170
|
-
assert_equal(w.mono?, false)
|
171
|
-
end
|
172
|
-
|
173
|
-
def test_stereo?
|
174
|
-
w = WaveFile.new(1, 44100, 16)
|
175
|
-
assert_equal(w.stereo?, false)
|
176
|
-
|
177
|
-
w = WaveFile.open("examples/valid/sine-mono-8bit.wav")
|
178
|
-
assert_equal(w.stereo?, false)
|
179
|
-
|
180
|
-
w = WaveFile.new(2, 44100, 16)
|
181
|
-
assert_equal(w.stereo?, true)
|
182
|
-
|
183
|
-
w = WaveFile.new(4, 44100, 16)
|
184
|
-
assert_equal(w.stereo?, false)
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_reverse
|
188
|
-
# Mono
|
189
|
-
w = WaveFile.new(:mono, 44100, 16)
|
190
|
-
w.sample_data = [1, 2, 3, 4, 5]
|
191
|
-
w.reverse
|
192
|
-
assert_equal(w.sample_data, [5, 4, 3, 2, 1])
|
193
|
-
|
194
|
-
# Stereo
|
195
|
-
w = WaveFile.new(:stereo, 44100, 16)
|
196
|
-
w.sample_data = [[1, 9], [2, 8], [3, 7], [4, 6], [5, 5]]
|
197
|
-
w.reverse
|
198
|
-
assert_equal(w.sample_data, [[5, 5], [4, 6], [3, 7], [2, 8], [1, 9]])
|
199
|
-
end
|
200
|
-
|
201
|
-
def test_duration()
|
202
|
-
sample_rate = 44100
|
203
|
-
|
204
|
-
[30001, 22050, 44100].each {|bits_per_sample|
|
205
|
-
[8, 16].each {|bits_per_sample|
|
206
|
-
[:mono, :stereo].each {|num_channels|
|
207
|
-
w = WaveFile.new(num_channels, sample_rate, bits_per_sample)
|
208
|
-
|
209
|
-
w.sample_data = []
|
210
|
-
assert_equal(w.duration, {:hours => 0, :minutes => 0, :seconds => 0, :milliseconds => 0})
|
211
|
-
w.sample_data = get_duration_test_samples(num_channels, (sample_rate.to_f / 1000.0).floor)
|
212
|
-
assert_equal(w.duration, {:hours => 0, :minutes => 0, :seconds => 0, :milliseconds => 0})
|
213
|
-
w.sample_data = get_duration_test_samples(num_channels, (sample_rate.to_f / 1000.0).ceil)
|
214
|
-
assert_equal(w.duration, {:hours => 0, :minutes => 0, :seconds => 0, :milliseconds => 1})
|
215
|
-
w.sample_data = get_duration_test_samples(num_channels, sample_rate / 2)
|
216
|
-
assert_equal(w.duration, {:hours => 0, :minutes => 0, :seconds => 0, :milliseconds => 500})
|
217
|
-
w.sample_data = get_duration_test_samples(num_channels, sample_rate - 1)
|
218
|
-
assert_equal(w.duration, {:hours => 0, :minutes => 0, :seconds => 0, :milliseconds => 999})
|
219
|
-
w.sample_data = get_duration_test_samples(num_channels, sample_rate)
|
220
|
-
assert_equal(w.duration, {:hours => 0, :minutes => 0, :seconds => 1, :milliseconds => 0})
|
221
|
-
w.sample_data = get_duration_test_samples(num_channels, sample_rate * 2)
|
222
|
-
assert_equal(w.duration, {:hours => 0, :minutes => 0, :seconds => 2, :milliseconds => 0})
|
223
|
-
w.sample_data = get_duration_test_samples(num_channels, (sample_rate / 2) * 3)
|
224
|
-
assert_equal(w.duration, {:hours => 0, :minutes => 0, :seconds => 1, :milliseconds => 500})
|
225
|
-
|
226
|
-
# These tests currently take too long to run...
|
227
|
-
#w.sample_data = [].fill(0.0, 0, sample_rate * 60)
|
228
|
-
#assert_equal(w.duration, {:hours => 0, :minutes => 1, :seconds => 0, :milliseconds => 0})
|
229
|
-
#w.sample_data = [].fill(0.0, 0, sample_rate * 60 * 60)
|
230
|
-
#assert_equal(w.duration, {:hours => 1, :minutes => 0, :seconds => 0, :milliseconds => 0})
|
231
|
-
}
|
232
|
-
}
|
233
|
-
}
|
234
|
-
end
|
235
|
-
|
236
|
-
def get_duration_test_samples(num_channels, num_samples)
|
237
|
-
if num_channels == :mono || num_channels == 1
|
238
|
-
return [].fill(0.0, 0, num_samples)
|
239
|
-
elsif num_channels == :stereo || num_channels == 2
|
240
|
-
return [].fill([0.0, 0.0], 0, num_samples)
|
241
|
-
else
|
242
|
-
return "error"
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
def test_bits_per_sample=()
|
247
|
-
# Set bits_per_sample to invalid value (non-8 or non-16)
|
248
|
-
w = WaveFile.open("examples/valid/sine-mono-8bit.wav")
|
249
|
-
assert_raise(StandardError) { w.bits_per_sample = 20 }
|
250
|
-
w = WaveFile.new(:mono, 44100, 16)
|
251
|
-
assert_raise(StandardError) { w.bits_per_sample = 4 }
|
252
|
-
|
253
|
-
w_before = WaveFile.open("examples/valid/sine-mono-8bit.wav")
|
254
|
-
w_after = WaveFile.open("examples/valid/sine-mono-8bit.wav")
|
255
|
-
w_after.bits_per_sample = 8
|
256
|
-
assert_equal(w_before.sample_data, w_after.sample_data)
|
257
|
-
|
258
|
-
w_before = WaveFile.open("examples/valid/sine-stereo-8bit.wav")
|
259
|
-
w_after = WaveFile.open("examples/valid/sine-stereo-8bit.wav")
|
260
|
-
w_after.bits_per_sample = 8
|
261
|
-
assert_equal(w_before.sample_data, w_after.sample_data)
|
262
|
-
|
263
|
-
# Open mono 16 bit file, change to 16 bit, still the same
|
264
|
-
# Open stereo 16 bit file, change to 16 bit, still the same
|
265
|
-
|
266
|
-
# Open mono 8 bit file, convert to 16 bit
|
267
|
-
w = WaveFile.new(:mono, 44100, 8)
|
268
|
-
w.sample_data = [0, 32, 64, 96, 128, 160, 192, 223, 255]
|
269
|
-
w.bits_per_sample = 16
|
270
|
-
assert_equal(w.sample_data, [-32768, -24576, -16384, -8192, 0, 8256, 16513, 24511, 32767])
|
271
|
-
|
272
|
-
# Open stereo 8 bit file, convert to 16 bit
|
273
|
-
w = WaveFile.new(:stereo, 44100, 8)
|
274
|
-
w.sample_data = [[0, 255], [32, 223], [64, 192], [96, 160], [128, 128],
|
275
|
-
[160, 96], [192, 64], [223, 32], [255, 0]]
|
276
|
-
w.bits_per_sample = 16
|
277
|
-
assert_equal(w.sample_data, [[-32768, 32767], [-24576, 24511], [-16384, 16513], [-8192, 8256], [0, 0],
|
278
|
-
[8256, -8192], [16513, -16384], [24511, -24576], [32767, -32768]])
|
279
|
-
|
280
|
-
# Open mono 16 bit file, convert to 8 bit
|
281
|
-
w = WaveFile.new(:mono, 44100, 16)
|
282
|
-
w.sample_data = [-32768, -24576, -16384, -8192, 0, 8256, 16513, 24511, 32767]
|
283
|
-
w.bits_per_sample = 8
|
284
|
-
assert_equal(w.sample_data, [0, 32, 64, 96, 128, 160, 192, 223, 255])
|
285
|
-
|
286
|
-
# Open stereo 16 bit file, convert to 8 bit, conversion successful
|
287
|
-
w = WaveFile.new(:stereo, 44100, 16)
|
288
|
-
w.sample_data = [[-32768, 32767], [-24576, 24511], [-16384, 16513], [-8192, 8256], [0, 0],
|
289
|
-
[8256, -8192], [16513, -16384], [24511, -24576], [32767, -32768]]
|
290
|
-
w.bits_per_sample = 8
|
291
|
-
assert_equal(w.sample_data, [[0, 255], [32, 223], [64, 192], [96, 160], [128, 128],
|
292
|
-
[160, 96], [192, 64], [223, 32], [255, 0]])
|
293
|
-
|
294
|
-
# Open 8 bit mono, convert to 16 bit, back to 8 bit.
|
295
|
-
w_before = WaveFile.open("examples/valid/sine-mono-8bit.wav")
|
296
|
-
w_after = WaveFile.open("examples/valid/sine-mono-8bit.wav")
|
297
|
-
w_after.bits_per_sample = 16
|
298
|
-
assert_not_equal(w_before.sample_data, w_after.sample_data)
|
299
|
-
w_after.bits_per_sample = 8
|
300
|
-
assert_equal(w_before.sample_data, w_after.sample_data)
|
301
|
-
|
302
|
-
# Open 8 bit stereo, convert to 16 bit, back to 8 bit.
|
303
|
-
w_before = WaveFile.open("examples/valid/sine-stereo-8bit.wav")
|
304
|
-
w_after = WaveFile.open("examples/valid/sine-stereo-8bit.wav")
|
305
|
-
w_after.bits_per_sample = 16
|
306
|
-
assert_not_equal(w_before.sample_data, w_after.sample_data)
|
307
|
-
w_after.bits_per_sample = 8
|
308
|
-
assert_equal(w_before.sample_data, w_after.sample_data)
|
309
|
-
|
310
|
-
# Open 16 bit mono, convert to 8 bit, back to 16 bit.
|
311
|
-
# Open 16 bit stereo, convert to 8 bit, back to 16 bit.
|
312
|
-
end
|
313
|
-
|
314
|
-
def test_num_channels=()
|
315
|
-
w = WaveFile.new(:mono, 44100, 16)
|
316
|
-
w.sample_data = [-32768, -24576, -16384, -8192, 0, 8256, 16513, 24511, 32767]
|
317
|
-
w.num_channels = 2
|
318
|
-
assert_equal(w.sample_data, [[-32768, -32768], [-24576, -24576], [-16384, -16384], [-8192, -8192], [0, 0],
|
319
|
-
[8256, 8256], [16513, 16513], [24511, 24511], [32767, 32767]])
|
320
|
-
|
321
|
-
w = WaveFile.new(:mono, 44100, 16)
|
322
|
-
w.sample_data = [-32768, -24576, -16384, -8192, 0, 8256, 16513, 24511, 32767]
|
323
|
-
w.num_channels = 3
|
324
|
-
assert_equal(w.sample_data, [[-32768, -32768, -32768], [-24576, -24576, -24576], [-16384, -16384, -16384], [-8192, -8192, -8192], [0, 0, 0],
|
325
|
-
[8256, 8256, 8256], [16513, 16513, 16513], [24511, 24511, 24511], [32767, 32767, 32767]])
|
326
|
-
|
327
|
-
w = WaveFile.new(:stereo, 44100, 16)
|
328
|
-
w.sample_data = [[-32768, -32768], [-24576, -24576], [-16384, -16384], [-8192, -8192], [0, 0],
|
329
|
-
[8256, 8256], [16513, 16513], [24511, 24511], [32767, 32767]]
|
330
|
-
w.num_channels = 1
|
331
|
-
assert_equal(w.sample_data, [-32768, -24576, -16384, -8192, 0, 8256, 16513, 24511, 32767])
|
332
|
-
|
333
|
-
w = WaveFile.new(3, 44100, 16)
|
334
|
-
w.sample_data = [[-32768, -32768, -32768], [-24576, -24576, -24576], [-16384, -16384, -16384], [-8192, -8192, -8192], [0, 0, 0],
|
335
|
-
[8256, 8256, 8256], [16513, 16513, 16513], [24511, 24511, 24511], [32767, 32767, 32767]]
|
336
|
-
w.num_channels = 1
|
337
|
-
assert_equal(w.sample_data, [-32768, -24576, -16384, -8192, 0, 8256, 16513, 24511, 32767])
|
338
|
-
end
|
339
|
-
end
|