Skip to content

Commit

Permalink
Merge pull request #28 from ssfrr/int32
Browse files Browse the repository at this point in the history
adds workaround for int32 files, fixes #27
  • Loading branch information
ssfrr authored Feb 7, 2018
2 parents c5c70ed + 96fde1b commit 54d136c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/WAVDisplay.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 25 additions & 3 deletions test/WAVDisplay.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit 54d136c

Please sign in to comment.