wavefile 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +2 -2
  3. data/README.markdown +67 -47
  4. data/Rakefile +23 -0
  5. data/lib/wavefile.rb +4 -2
  6. data/lib/wavefile/buffer.rb +40 -25
  7. data/lib/wavefile/chunk_readers.rb +7 -1
  8. data/lib/wavefile/chunk_readers/base_chunk_reader.rb +10 -0
  9. data/lib/wavefile/chunk_readers/data_chunk_reader.rb +77 -0
  10. data/lib/wavefile/chunk_readers/format_chunk_reader.rb +59 -0
  11. data/lib/wavefile/chunk_readers/generic_chunk_reader.rb +15 -0
  12. data/lib/wavefile/chunk_readers/riff_chunk_reader.rb +19 -0
  13. data/lib/wavefile/chunk_readers/riff_reader.rb +67 -0
  14. data/lib/wavefile/duration.rb +47 -12
  15. data/lib/wavefile/format.rb +44 -23
  16. data/lib/wavefile/reader.rb +101 -111
  17. data/lib/wavefile/unvalidated_format.rb +36 -6
  18. data/lib/wavefile/writer.rb +138 -40
  19. data/test/buffer_test.rb +21 -17
  20. data/test/chunk_readers/format_chunk_reader_test.rb +130 -0
  21. data/test/duration_test.rb +42 -1
  22. data/test/fixtures/actual_output/total_duration_mono_float_32_44100.wav +0 -0
  23. data/test/fixtures/actual_output/total_duration_mono_float_64_44100.wav +0 -0
  24. data/test/fixtures/actual_output/total_duration_mono_pcm_16_44100.wav +0 -0
  25. data/test/fixtures/actual_output/total_duration_mono_pcm_24_44100.wav +0 -0
  26. data/test/fixtures/actual_output/total_duration_mono_pcm_32_44100.wav +0 -0
  27. data/test/fixtures/actual_output/total_duration_mono_pcm_8_44100.wav +0 -0
  28. data/test/fixtures/actual_output/total_duration_stereo_float_32_44100.wav +0 -0
  29. data/test/fixtures/actual_output/total_duration_stereo_float_64_44100.wav +0 -0
  30. data/test/fixtures/actual_output/total_duration_stereo_pcm_16_44100.wav +0 -0
  31. data/test/fixtures/actual_output/total_duration_stereo_pcm_24_44100.wav +0 -0
  32. data/test/fixtures/actual_output/total_duration_stereo_pcm_32_44100.wav +0 -0
  33. data/test/fixtures/actual_output/total_duration_stereo_pcm_8_44100.wav +0 -0
  34. data/test/fixtures/actual_output/total_duration_tri_float_32_44100.wav +0 -0
  35. data/test/fixtures/actual_output/total_duration_tri_float_64_44100.wav +0 -0
  36. data/test/fixtures/actual_output/total_duration_tri_pcm_16_44100.wav +0 -0
  37. data/test/fixtures/actual_output/total_duration_tri_pcm_24_44100.wav +0 -0
  38. data/test/fixtures/actual_output/total_duration_tri_pcm_32_44100.wav +0 -0
  39. data/test/fixtures/actual_output/total_duration_tri_pcm_8_44100.wav +0 -0
  40. data/test/fixtures/unsupported/README.markdown +1 -1
  41. data/test/fixtures/unsupported/bad_channel_count.wav +0 -0
  42. data/test/fixtures/unsupported/extensible_container_size_bigger_than_sample_size.wav +0 -0
  43. data/test/fixtures/unsupported/extensible_unsupported_subformat_guid.wav +0 -0
  44. data/test/fixtures/valid/valid_extensible_mono_float_32_44100.wav +0 -0
  45. data/test/fixtures/valid/valid_extensible_mono_float_64_44100.wav +0 -0
  46. data/test/fixtures/valid/valid_extensible_mono_pcm_16_44100.wav +0 -0
  47. data/test/fixtures/valid/valid_extensible_mono_pcm_24_44100.wav +0 -0
  48. data/test/fixtures/valid/valid_extensible_mono_pcm_32_44100.wav +0 -0
  49. data/test/fixtures/valid/valid_extensible_mono_pcm_8_44100.wav +0 -0
  50. data/test/fixtures/valid/valid_extensible_stereo_float_32_44100.wav +0 -0
  51. data/test/fixtures/valid/valid_extensible_stereo_float_64_44100.wav +0 -0
  52. data/test/fixtures/valid/valid_extensible_stereo_pcm_16_44100.wav +0 -0
  53. data/test/fixtures/valid/valid_extensible_stereo_pcm_24_44100.wav +0 -0
  54. data/test/fixtures/valid/valid_extensible_stereo_pcm_32_44100.wav +0 -0
  55. data/test/fixtures/valid/valid_extensible_stereo_pcm_8_44100.wav +0 -0
  56. data/test/fixtures/valid/valid_extensible_tri_float_32_44100.wav +0 -0
  57. data/test/fixtures/valid/valid_extensible_tri_float_64_44100.wav +0 -0
  58. data/test/fixtures/valid/valid_extensible_tri_pcm_16_44100.wav +0 -0
  59. data/test/fixtures/valid/valid_extensible_tri_pcm_24_44100.wav +0 -0
  60. data/test/fixtures/valid/valid_extensible_tri_pcm_32_44100.wav +0 -0
  61. data/test/fixtures/valid/valid_extensible_tri_pcm_8_44100.wav +0 -0
  62. data/test/format_test.rb +22 -48
  63. data/test/reader_test.rb +188 -93
  64. data/test/unvalidated_format_test.rb +130 -4
  65. data/test/wavefile_io_test_helper.rb +6 -4
  66. data/test/writer_test.rb +118 -95
  67. metadata +47 -4
  68. data/lib/wavefile/chunk_readers/header_reader.rb +0 -163
  69. data/test/fixtures/actual_output/no_samples.wav +0 -0
@@ -3,16 +3,19 @@ require 'wavefile.rb'
3
3
 
4
4
  include WaveFile
5
5
 
6
- class UnvalidatedFormatTest < MiniTest::Unit::TestCase
6
+ class UnvalidatedFormatTest < Minitest::Test
7
7
  def test_initialize
8
- format = UnvalidatedFormat.new({:audio_format => 1,
8
+ format = UnvalidatedFormat.new({:audio_format => 65534,
9
+ :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
9
10
  :channels => 2,
10
11
  :sample_rate => 44100,
11
12
  :byte_rate => 176400,
12
13
  :block_align => 4,
13
- :bits_per_sample => 16})
14
+ :bits_per_sample => 16,
15
+ :valid_bits_per_sample => 14})
14
16
 
15
- assert_equal(1, format.audio_format)
17
+ assert_equal(65534, format.audio_format)
18
+ assert_equal(SUB_FORMAT_GUID_PCM, format.sub_audio_format_guid)
16
19
  assert_equal(2, format.channels)
17
20
  assert_equal(false, format.mono?)
18
21
  assert_equal(true, format.stereo?)
@@ -20,5 +23,128 @@ class UnvalidatedFormatTest < MiniTest::Unit::TestCase
20
23
  assert_equal(176400, format.byte_rate)
21
24
  assert_equal(4, format.block_align)
22
25
  assert_equal(16, format.bits_per_sample)
26
+ assert_equal(14, format.valid_bits_per_sample)
27
+ end
28
+
29
+ def test_to_validated_format_pcm
30
+ unvalidated_format = UnvalidatedFormat.new({:audio_format => 1,
31
+ :channels => 2,
32
+ :sample_rate => 44100,
33
+ :byte_rate => 176400,
34
+ :block_align => 4,
35
+ :bits_per_sample => 16})
36
+
37
+ validated_format = unvalidated_format.to_validated_format
38
+ assert_equal(:pcm, validated_format.sample_format)
39
+ assert_equal(2, validated_format.channels)
40
+ assert_equal(16, validated_format.bits_per_sample)
41
+ assert_equal(44100, validated_format.sample_rate)
42
+ assert_equal(176400, validated_format.byte_rate)
43
+ assert_equal(4, validated_format.block_align)
44
+ end
45
+
46
+ def test_to_validated_format_float
47
+ unvalidated_format = UnvalidatedFormat.new({:audio_format => 3,
48
+ :channels => 2,
49
+ :sample_rate => 44100,
50
+ :byte_rate => 352800,
51
+ :block_align => 8,
52
+ :bits_per_sample => 32})
53
+
54
+ validated_format = unvalidated_format.to_validated_format
55
+ assert_equal(:float, validated_format.sample_format)
56
+ assert_equal(2, validated_format.channels)
57
+ assert_equal(32, validated_format.bits_per_sample)
58
+ assert_equal(44100, validated_format.sample_rate)
59
+ assert_equal(352800, validated_format.byte_rate)
60
+ assert_equal(8, validated_format.block_align)
61
+ end
62
+
63
+ def test_to_validated_format_unsupported
64
+ unvalidated_format = UnvalidatedFormat.new({:audio_format => 2,
65
+ :channels => 2,
66
+ :sample_rate => 44100,
67
+ :byte_rate => 176400,
68
+ :block_align => 4,
69
+ :bits_per_sample => 16})
70
+
71
+ assert_raises(InvalidFormatError) { unvalidated_format.to_validated_format }
72
+ end
73
+
74
+ def test_to_validated_format_wave_format_extensible_pcm
75
+ unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
76
+ :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
77
+ :channels => 2,
78
+ :sample_rate => 44100,
79
+ :byte_rate => 176400,
80
+ :block_align => 4,
81
+ :bits_per_sample => 16,
82
+ :valid_bits_per_sample => 16})
83
+
84
+ validated_format = unvalidated_format.to_validated_format
85
+ assert_equal(:pcm, validated_format.sample_format)
86
+ assert_equal(2, validated_format.channels)
87
+ assert_equal(16, validated_format.bits_per_sample)
88
+ assert_equal(44100, validated_format.sample_rate)
89
+ assert_equal(176400, validated_format.byte_rate)
90
+ assert_equal(4, validated_format.block_align)
91
+ end
92
+
93
+ def test_to_validated_format_wave_format_extensible_float
94
+ unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
95
+ :sub_audio_format_guid => SUB_FORMAT_GUID_FLOAT,
96
+ :channels => 2,
97
+ :sample_rate => 44100,
98
+ :byte_rate => 352800,
99
+ :block_align => 8,
100
+ :bits_per_sample => 32,
101
+ :valid_bits_per_sample => 32})
102
+
103
+ validated_format = unvalidated_format.to_validated_format
104
+ assert_equal(:float, validated_format.sample_format)
105
+ assert_equal(2, validated_format.channels)
106
+ assert_equal(32, validated_format.bits_per_sample)
107
+ assert_equal(44100, validated_format.sample_rate)
108
+ assert_equal(352800, validated_format.byte_rate)
109
+ assert_equal(8, validated_format.block_align)
110
+ end
111
+
112
+ def test_to_validated_format_wave_format_extensible_unsupported_sub_format
113
+ unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
114
+ :sub_audio_format_guid => "\x02\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71", # ADPCM
115
+ :channels => 2,
116
+ :sample_rate => 44100,
117
+ :byte_rate => 176400,
118
+ :block_align => 4,
119
+ :bits_per_sample => 16,
120
+ :valid_bits_per_sample => 16})
121
+
122
+ assert_raises(InvalidFormatError) { unvalidated_format.to_validated_format }
123
+ end
124
+
125
+ def test_to_validated_format_wave_format_extensible_unsupported_valid_bits_per_sample
126
+ unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
127
+ :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
128
+ :channels => 2,
129
+ :sample_rate => 44100,
130
+ :byte_rate => 176400,
131
+ :block_align => 4,
132
+ :bits_per_sample => 14,
133
+ :valid_bits_per_sample => 14})
134
+
135
+ assert_raises(InvalidFormatError) { unvalidated_format.to_validated_format }
136
+ end
137
+
138
+ def test_to_validated_format_wave_format_extensible_valid_bits_per_sample_differs_from_container_size
139
+ unvalidated_format = UnvalidatedFormat.new({:audio_format => 65534,
140
+ :sub_audio_format_guid => SUB_FORMAT_GUID_PCM,
141
+ :channels => 2,
142
+ :sample_rate => 44100,
143
+ :byte_rate => 176400,
144
+ :block_align => 4,
145
+ :bits_per_sample => 16,
146
+ :valid_bits_per_sample => 14})
147
+
148
+ assert_raises(UnsupportedFormatError) { unvalidated_format.to_validated_format }
23
149
  end
24
150
  end
@@ -53,10 +53,12 @@ module WaveFileIOTestHelper
53
53
 
54
54
  # Executes the given block against different combinations of number of channels and sample_format
55
55
  def exhaustively_test
56
- [:mono, :stereo, :tri].each do |channels|
57
- [:pcm, :float].each do |sample_format|
58
- Format::SUPPORTED_BITS_PER_SAMPLE[sample_format].each do |bits_per_sample|
59
- yield(channels, "#{sample_format}_#{bits_per_sample}".to_sym)
56
+ ["", "extensible_"].each do |format_chunk_format|
57
+ [:mono, :stereo, :tri].each do |channels|
58
+ [:pcm, :float].each do |sample_format|
59
+ Format::SUPPORTED_BITS_PER_SAMPLE[sample_format].each do |bits_per_sample|
60
+ yield(format_chunk_format, channels, "#{sample_format}_#{bits_per_sample}".to_sym)
61
+ end
60
62
  end
61
63
  end
62
64
  end
@@ -4,7 +4,7 @@ require 'wavefile_io_test_helper.rb'
4
4
 
5
5
  include WaveFile
6
6
 
7
- class WriterTest < MiniTest::Unit::TestCase
7
+ class WriterTest < Minitest::Test
8
8
  include WaveFileIOTestHelper
9
9
 
10
10
  OUTPUT_FOLDER = "test/fixtures/actual_output"
@@ -21,33 +21,37 @@ class WriterTest < MiniTest::Unit::TestCase
21
21
  end
22
22
 
23
23
  def test_write_basic_file
24
- exhaustively_test do |channels, sample_format|
24
+ exhaustively_test do |format_chunk_format, channels, sample_format|
25
25
  file_name = "valid_#{channels}_#{sample_format}_44100.wav"
26
26
  format = Format.new(CHANNEL_ALIAS[channels], sample_format, 44100)
27
27
 
28
- writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format)
29
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 128, format))
30
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 128, format))
31
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 24, format))
32
- writer.close
28
+ ["#{OUTPUT_FOLDER}/#{file_name}", StringIO.new].each do |io_or_file_name|
29
+ writer = Writer.new(io_or_file_name, format)
30
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 128, format))
31
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 128, format))
32
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 24, format))
33
+ writer.close
33
34
 
34
- assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
35
+ assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
36
+ end
35
37
  end
36
38
  end
37
39
 
38
40
  def test_write_basic_file_with_a_block
39
- exhaustively_test do |channels, sample_format|
41
+ exhaustively_test do |format_chunk_format, channels, sample_format|
40
42
  file_name = "valid_#{channels}_#{sample_format}_44100.wav"
41
43
  format = Format.new(CHANNEL_ALIAS[channels], sample_format, 44100)
42
44
 
43
- writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format) do |writer|
44
- 4.times do
45
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 70, format))
45
+ ["#{OUTPUT_FOLDER}/#{file_name}", StringIO.new].each do |io_or_file_name|
46
+ writer = Writer.new(io_or_file_name, format) do |w|
47
+ 4.times do
48
+ w.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 70, format))
49
+ end
46
50
  end
47
- end
48
51
 
49
- assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
50
- assert(writer.closed?)
52
+ assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
53
+ assert(writer.closed?)
54
+ end
51
55
  end
52
56
  end
53
57
 
@@ -57,103 +61,119 @@ class WriterTest < MiniTest::Unit::TestCase
57
61
  format_16_bit_mono = Format.new(:mono, :pcm_16, 22050)
58
62
  format_16bit_stereo = Format.new(:stereo, :pcm_16, 44100)
59
63
 
60
- writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format_8bit_mono)
61
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:stereo][:pcm_16] * 128, format_16bit_stereo))
62
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][:pcm_16] * 128, format_16_bit_mono))
63
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:stereo][:pcm_16] * 24, format_16bit_stereo))
64
- writer.close
64
+ ["#{OUTPUT_FOLDER}/#{file_name}", StringIO.new].each do |io_or_file_name|
65
+ writer = Writer.new(io_or_file_name, format_8bit_mono)
66
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:stereo][:pcm_16] * 128, format_16bit_stereo))
67
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][:pcm_16] * 128, format_16_bit_mono))
68
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:stereo][:pcm_16] * 24, format_16bit_stereo))
69
+ writer.close
65
70
 
66
- assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
71
+ assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
72
+ end
67
73
  end
68
74
 
69
75
  def test_write_file_with_padding_byte
70
76
  file_name = "valid_mono_pcm_8_44100_with_padding_byte.wav"
71
77
  format = Format.new(:mono, :pcm_8, 44100)
72
78
 
73
- writer = Writer.new("#{OUTPUT_FOLDER}/#{file_name}", format)
74
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][:pcm_8] * 128, format))
75
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][:pcm_8] * 128, format))
76
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][:pcm_8] * 23 + [88, 88, 88, 88, 167, 167, 167], format))
77
- writer.close
79
+ ["#{OUTPUT_FOLDER}/#{file_name}", StringIO.new].each do |io_or_file_name|
80
+ writer = Writer.new(io_or_file_name, format)
81
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][:pcm_8] * 128, format))
82
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][:pcm_8] * 128, format))
83
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][:pcm_8] * 23 + [88, 88, 88, 88, 167, 167, 167], format))
84
+ writer.close
78
85
 
79
- assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
86
+ assert_equal(read_file(:expected, file_name), read_file(:actual, file_name))
87
+ end
80
88
  end
81
89
 
82
- def test_file_name
83
- file_name = "#{OUTPUT_FOLDER}/example.wav"
84
-
85
- writer = Writer.new(file_name, Format.new(:mono, :pcm_8, 44100))
86
- assert_equal("#{OUTPUT_FOLDER}/example.wav", writer.file_name)
87
-
90
+ def test_close_when_constructed_from_file_name
91
+ writer = Writer.new("#{OUTPUT_FOLDER}/closed_test.wav", Format.new(:mono, :pcm_16, 44100))
92
+ assert_equal(false, writer.closed?)
88
93
  writer.close
89
- assert_equal("#{OUTPUT_FOLDER}/example.wav", writer.file_name)
94
+ assert(writer.closed?)
90
95
  end
91
96
 
92
- def test_closed?
93
- writer = Writer.new("#{OUTPUT_FOLDER}/closed_test.wav", Format.new(:mono, :pcm_16, 44100))
97
+ def test_close_when_constructed_from_io
98
+ io = StringIO.new
99
+ assert_equal(false, io.closed?)
100
+ assert_equal(0, io.pos)
101
+
102
+ writer = Writer.new(io, Format.new(:mono, :pcm_16, 44100))
94
103
  assert_equal(false, writer.closed?)
104
+
105
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[:mono][:pcm_16] * 128, Format.new(:mono, :pcm_16, 44100)))
106
+
95
107
  writer.close
96
108
  assert(writer.closed?)
109
+
110
+ assert_equal(false, io.closed?)
111
+ assert_equal(2092, io.pos) # 44 bytes for header, plus 2 bytes per sample, with 8 * 128 samples
112
+ io.close
97
113
  end
98
114
 
99
115
  def test_attempt_to_write_after_close
100
116
  format = Format.new(:mono, :pcm_8, 44100)
101
117
 
102
- writer = Writer.new("#{OUTPUT_FOLDER}/write_after_close.wav", format)
103
- writer.write(Buffer.new([1, 2, 3, 4], format))
104
- writer.close
118
+ ["#{OUTPUT_FOLDER}/write_after_close.wav", StringIO.new].each do |io_or_file_name|
119
+ writer = Writer.new(io_or_file_name, format)
120
+ writer.write(Buffer.new([1, 2, 3, 4], format))
121
+ writer.close
105
122
 
106
- assert_raises(IOError) { writer.write(Buffer.new([5, 6, 7, 8], format)) }
123
+ assert_raises(WriterClosedError) { writer.write(Buffer.new([5, 6, 7, 8], format)) }
124
+ end
107
125
  end
108
126
 
109
127
  def test_total_duration
110
- exhaustively_test do |channels, sample_format|
128
+ exhaustively_test do |format_chunk_format, channels, sample_format|
111
129
  format = Format.new(CHANNEL_ALIAS[channels], sample_format, 44100)
112
130
 
113
- writer = Writer.new("#{OUTPUT_FOLDER}/total_duration_#{channels}_#{sample_format}_44100.wav", format)
114
-
115
- assert_equal(0, writer.total_sample_frames)
116
- duration = writer.total_duration
117
- assert_equal(0, duration.sample_frame_count)
118
- assert_equal(44100, duration.sample_rate)
119
- assert_equal(0, duration.hours)
120
- assert_equal(0, duration.minutes)
121
- assert_equal(0, duration.seconds)
122
- assert_equal(0, duration.milliseconds)
123
-
124
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 2756, format))
125
-
126
- assert_equal(8 * 2756, writer.total_sample_frames)
127
- duration = writer.total_duration
128
- assert_equal(8 * 2756, duration.sample_frame_count)
129
- assert_equal(44100, duration.sample_rate)
130
- assert_equal(0, duration.hours)
131
- assert_equal(0, duration.minutes)
132
- assert_equal(0, duration.seconds)
133
- assert_equal(499, duration.milliseconds)
134
-
135
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 2756, format))
136
- writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 2756, format))
137
-
138
- assert_equal(8 * 2756 * 3, writer.total_sample_frames)
139
- duration = writer.total_duration
140
- assert_equal(8 * 2756 * 3, duration.sample_frame_count)
141
- assert_equal(44100, duration.sample_rate)
142
- assert_equal(0, duration.hours)
143
- assert_equal(0, duration.minutes)
144
- assert_equal(1, duration.seconds)
145
- assert_equal(499, duration.milliseconds)
146
-
147
- writer.close
148
-
149
- assert_equal(8 * 2756 * 3, writer.total_sample_frames)
150
- duration = writer.total_duration
151
- assert_equal(8 * 2756 * 3, duration.sample_frame_count)
152
- assert_equal(44100, duration.sample_rate)
153
- assert_equal(0, duration.hours)
154
- assert_equal(0, duration.minutes)
155
- assert_equal(1, duration.seconds)
156
- assert_equal(499, duration.milliseconds)
131
+ ["#{OUTPUT_FOLDER}/total_duration_#{channels}_#{sample_format}_44100.wav", StringIO.new].each do |io_or_file_name|
132
+ writer = Writer.new(io_or_file_name, format)
133
+
134
+ assert_equal(0, writer.total_sample_frames)
135
+ duration = writer.total_duration
136
+ assert_equal(0, duration.sample_frame_count)
137
+ assert_equal(44100, duration.sample_rate)
138
+ assert_equal(0, duration.hours)
139
+ assert_equal(0, duration.minutes)
140
+ assert_equal(0, duration.seconds)
141
+ assert_equal(0, duration.milliseconds)
142
+
143
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 2756, format))
144
+
145
+ assert_equal(8 * 2756, writer.total_sample_frames)
146
+ duration = writer.total_duration
147
+ assert_equal(8 * 2756, duration.sample_frame_count)
148
+ assert_equal(44100, duration.sample_rate)
149
+ assert_equal(0, duration.hours)
150
+ assert_equal(0, duration.minutes)
151
+ assert_equal(0, duration.seconds)
152
+ assert_equal(499, duration.milliseconds)
153
+
154
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 2756, format))
155
+ writer.write(Buffer.new(SQUARE_WAVE_CYCLE[channels][sample_format] * 2756, format))
156
+
157
+ assert_equal(8 * 2756 * 3, writer.total_sample_frames)
158
+ duration = writer.total_duration
159
+ assert_equal(8 * 2756 * 3, duration.sample_frame_count)
160
+ assert_equal(44100, duration.sample_rate)
161
+ assert_equal(0, duration.hours)
162
+ assert_equal(0, duration.minutes)
163
+ assert_equal(1, duration.seconds)
164
+ assert_equal(499, duration.milliseconds)
165
+
166
+ writer.close
167
+
168
+ assert_equal(8 * 2756 * 3, writer.total_sample_frames)
169
+ duration = writer.total_duration
170
+ assert_equal(8 * 2756 * 3, duration.sample_frame_count)
171
+ assert_equal(44100, duration.sample_rate)
172
+ assert_equal(0, duration.hours)
173
+ assert_equal(0, duration.minutes)
174
+ assert_equal(1, duration.seconds)
175
+ assert_equal(499, duration.milliseconds)
176
+ end
157
177
  end
158
178
  end
159
179
 
@@ -162,17 +182,20 @@ class WriterTest < MiniTest::Unit::TestCase
162
182
  def test_exception_with_block
163
183
  format = Format.new(:mono, :pcm_8, 44100)
164
184
  samples = [1, 2, 3, 4, 5, 6]
165
- Writer.new("#{OUTPUT_FOLDER}/exception_with_block.wav", format) do |writer|
166
- begin
167
- writer.write(Buffer.new(samples, format))
168
- 1 / 0 # cause divide-by-zero exception
169
- rescue
170
- # catch the exception and ignore, so test passes OK
185
+
186
+ ["#{OUTPUT_FOLDER}/exception_with_block.wav", StringIO.new].each do |io_or_file_name|
187
+ Writer.new(io_or_file_name, format) do |writer|
188
+ begin
189
+ writer.write(Buffer.new(samples, format))
190
+ 1 / 0 # cause divide-by-zero exception
191
+ rescue
192
+ # catch the exception and ignore, so test passes
193
+ end
171
194
  end
195
+
196
+ reader = Reader.new("#{OUTPUT_FOLDER}/exception_with_block.wav")
197
+ assert_equal(samples.size, reader.total_sample_frames)
172
198
  end
173
-
174
- reader = Reader.new("#{OUTPUT_FOLDER}/exception_with_block.wav")
175
- assert_equal(samples.size, reader.total_sample_frames)
176
199
  end
177
200
 
178
201
  private
@@ -192,7 +215,7 @@ private
192
215
 
193
216
  def clean_output_folder
194
217
  # Make the folder if it doesn't already exist
195
- Dir.mkdir(OUTPUT_FOLDER) unless File.exists?(OUTPUT_FOLDER)
218
+ Dir.mkdir(OUTPUT_FOLDER) unless File.exist?(OUTPUT_FOLDER)
196
219
 
197
220
  dir = Dir.new(OUTPUT_FOLDER)
198
221
  file_names = dir.entries