From 96fde1be05a3eee587b2424ab42c5c3dc3d90d20 Mon Sep 17 00:00:00 2001 From: Spencer Russell Date: Wed, 31 Jan 2018 11:45:42 -0500 Subject: [PATCH] adds workaround for int32 files, fixes #27 --- src/WAVDisplay.jl | 9 ++++++++- test/WAVDisplay.jl | 28 +++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/WAVDisplay.jl b/src/WAVDisplay.jl index 9028df8..d18a02f 100644 --- a/src/WAVDisplay.jl +++ b/src/WAVDisplay.jl @@ -129,8 +129,15 @@ end const WAVE_FORMAT_PCM = 0x0001 const SAMPLE_TYPE = PCM16Sample -# we always write 16-bit PCM wav data, because that's what Firefox understands +# floating-point and 16-bit fixed point buffer display works, but Int32 is +# broken, so for now we convert to Float32 first. This hack should go away once +# we switch over to just using WAV.jl function wavwrite(io::IO, buf::SampleBuf) + wavwrite(io, Float32.(buf)) +end + +# we always write 16-bit PCM wav data, because that's what Firefox understands +function wavwrite(io::IO, buf::SampleBuf{<:Union{Int16, SAMPLE_TYPE, AbstractFloat}, N}) where N nbits = 16 nchans = nchannels(buf) blockalign = 2 * nchans diff --git a/test/WAVDisplay.jl b/test/WAVDisplay.jl index d26bbc3..08cdfab 100644 --- a/test/WAVDisplay.jl +++ b/test/WAVDisplay.jl @@ -65,8 +65,8 @@ end # @test !hasiconbtn(output, "fa-stop") # end - @testset "wavwrite Generates valid WAV file" begin - buf = SampleBuf(rand(Int16, 16, 2), 48000) + @testset "wavwrite Generates valid WAV file with raw Int16" begin + buf = SampleBuf(rand(Int16, 4, 2), 48000) io = IOBuffer() SampledSignals.wavwrite(io, buf) samples, fs, nbits, opt = wavread(IOBuffer(take!(io)), format="native") @@ -75,8 +75,18 @@ end @test nbits == 16 end + @testset "wavwrite Generates valid WAV file with raw 16-bit Fixed-point" begin + buf = SampleBuf(reinterpret.(Fixed{Int16, 15}, rand(Int16, 4, 2)), 48000) + io = IOBuffer() + SampledSignals.wavwrite(io, buf) + samples, fs, nbits, opt = wavread(IOBuffer(take!(io)), format="native") + @test samples == reinterpret.(convert(Array, buf)) + @test fs == 48000 + @test nbits == 16 + end + @testset "wavwrite converts float values to 16-bit int wav" begin - buf = SampleBuf(rand(16, 2), 48000) + buf = SampleBuf(rand(4, 2), 48000) io = IOBuffer() SampledSignals.wavwrite(io, buf) samples, fs, nbits, opt = wavread(IOBuffer(take!(io)), format="native") @@ -85,6 +95,18 @@ end @test nbits == 16 end + @testset "wavwrite converts Int32 values to 16-bit int wav" begin + data = rand(4, 2)*0.9 + buf = SampleBuf(Fixed{Int32, 31}.(data), 48000) + io = IOBuffer() + SampledSignals.wavwrite(io, buf) + samples, fs, nbits, opt = wavread(IOBuffer(take!(io)), format="native") + # convert 32-bit int buf to float, then to 16-bit, for testing + @test samples == reinterpret.(convert(Array{PCM16Sample}, Float32.(buf))) + @test fs == 48000 + @test nbits == 16 + end + # this is used to display spectrum magnitudes using the same infrastructure # as displaying/playing time-domain buffers # @testset "wavwrite converts complex float values to 16-bit int wav" begin