Skip to content

Commit

Permalink
fix type stability of read_pcm_samples
Browse files Browse the repository at this point in the history
  • Loading branch information
ymtoo committed Nov 26, 2020
1 parent bf329e5 commit 634312c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/WAV.jl
Expand Up @@ -313,13 +313,13 @@ end

ieee_float_container_type(nbits) = (nbits == 32 ? Float32 : (nbits == 64 ? Float64 : error("$nbits bits is not supported for WAVE_FORMAT_IEEE_FLOAT.")))

function read_pcm_samples(io::IO, chunk_size, fmt::WAVFormat, subrange)
function read_pcm_samples(io::IO, chunk_size, fmt::WAVFormat, subrange,
::Type{sample_type}) where {sample_type}
nbits = bits_per_sample(fmt)
if isempty(subrange)
return Array{pcm_container_type(nbits), 2}(undef, 0, fmt.nchannels)
return Array{sample_type, 2}(undef, 0, fmt.nchannels)
end
samples = Array{UInt64, 2}(undef, length(subrange), fmt.nchannels)
sample_type = pcm_container_type(nbits)
samples = Array{sample_type, 2}(undef, length(subrange), fmt.nchannels)
nbytes = ceil(Integer, nbits / 8)
bitshift = [0x0, 0x8, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38, 0x40]
mask = UInt64(0x1) << (nbits - 1)
Expand All @@ -339,10 +339,11 @@ function read_pcm_samples(io::IO, chunk_size, fmt::WAVFormat, subrange)
end
my_sample >>= nbytes * 8 - nbits
# sign extend negative values
samples[i, j] = xor(my_sample, mask) - mask
my_sample = xor(my_sample, mask) - mask
samples[i, j] = convert(sample_type, signed(my_sample))
end
end
convert.(sample_type, signed.(samples))
samples
end

function read_ieee_float_samples(io::IO, chunk_size, fmt::WAVFormat, subrange, ::Type{floatType}) where {floatType}
Expand Down Expand Up @@ -604,7 +605,8 @@ function read_data(io::IO, chunk_size, fmt::WAVFormat, format, subrange)
subrange = 1:convert(UInt, chunk_size / fmt.block_align)
end
if isformat(fmt, WAVE_FORMAT_PCM)
samples = read_pcm_samples(io, chunk_size, fmt, subrange)
samples = read_pcm_samples(io, chunk_size, fmt, subrange,
pcm_container_type(bits_per_sample(fmt)))
convert_to_double = x -> convert_pcm_to_double(x, bits_per_sample(fmt))
elseif isformat(fmt, WAVE_FORMAT_IEEE_FLOAT)
samples = read_ieee_float_samples(io, chunk_size, fmt, subrange)
Expand Down

0 comments on commit 634312c

Please sign in to comment.