Skip to content

Commit 54d136c

Browse files
authored
Merge pull request #28 from ssfrr/int32
adds workaround for int32 files, fixes #27
2 parents c5c70ed + 96fde1b commit 54d136c

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

src/WAVDisplay.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,15 @@ end
129129
const WAVE_FORMAT_PCM = 0x0001
130130
const SAMPLE_TYPE = PCM16Sample
131131

132-
# we always write 16-bit PCM wav data, because that's what Firefox understands
132+
# floating-point and 16-bit fixed point buffer display works, but Int32 is
133+
# broken, so for now we convert to Float32 first. This hack should go away once
134+
# we switch over to just using WAV.jl
133135
function wavwrite(io::IO, buf::SampleBuf)
136+
wavwrite(io, Float32.(buf))
137+
end
138+
139+
# we always write 16-bit PCM wav data, because that's what Firefox understands
140+
function wavwrite(io::IO, buf::SampleBuf{<:Union{Int16, SAMPLE_TYPE, AbstractFloat}, N}) where N
134141
nbits = 16
135142
nchans = nchannels(buf)
136143
blockalign = 2 * nchans

test/WAVDisplay.jl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ end
6565
# @test !hasiconbtn(output, "fa-stop")
6666
# end
6767

68-
@testset "wavwrite Generates valid WAV file" begin
69-
buf = SampleBuf(rand(Int16, 16, 2), 48000)
68+
@testset "wavwrite Generates valid WAV file with raw Int16" begin
69+
buf = SampleBuf(rand(Int16, 4, 2), 48000)
7070
io = IOBuffer()
7171
SampledSignals.wavwrite(io, buf)
7272
samples, fs, nbits, opt = wavread(IOBuffer(take!(io)), format="native")
@@ -75,8 +75,18 @@ end
7575
@test nbits == 16
7676
end
7777

78+
@testset "wavwrite Generates valid WAV file with raw 16-bit Fixed-point" begin
79+
buf = SampleBuf(reinterpret.(Fixed{Int16, 15}, rand(Int16, 4, 2)), 48000)
80+
io = IOBuffer()
81+
SampledSignals.wavwrite(io, buf)
82+
samples, fs, nbits, opt = wavread(IOBuffer(take!(io)), format="native")
83+
@test samples == reinterpret.(convert(Array, buf))
84+
@test fs == 48000
85+
@test nbits == 16
86+
end
87+
7888
@testset "wavwrite converts float values to 16-bit int wav" begin
79-
buf = SampleBuf(rand(16, 2), 48000)
89+
buf = SampleBuf(rand(4, 2), 48000)
8090
io = IOBuffer()
8191
SampledSignals.wavwrite(io, buf)
8292
samples, fs, nbits, opt = wavread(IOBuffer(take!(io)), format="native")
@@ -85,6 +95,18 @@ end
8595
@test nbits == 16
8696
end
8797

98+
@testset "wavwrite converts Int32 values to 16-bit int wav" begin
99+
data = rand(4, 2)*0.9
100+
buf = SampleBuf(Fixed{Int32, 31}.(data), 48000)
101+
io = IOBuffer()
102+
SampledSignals.wavwrite(io, buf)
103+
samples, fs, nbits, opt = wavread(IOBuffer(take!(io)), format="native")
104+
# convert 32-bit int buf to float, then to 16-bit, for testing
105+
@test samples == reinterpret.(convert(Array{PCM16Sample}, Float32.(buf)))
106+
@test fs == 48000
107+
@test nbits == 16
108+
end
109+
88110
# this is used to display spectrum magnitudes using the same infrastructure
89111
# as displaying/playing time-domain buffers
90112
# @testset "wavwrite converts complex float values to 16-bit int wav" begin

0 commit comments

Comments
 (0)