Struggling with LibSndFile loading a wav file

I’m struggling with loading a wav file using LibSndFile.jl

julia> using FileIO: load, save, loadstreaming, savestreaming

julia> import LibSndFile

julia> x = load("JolieHollandSacha.wav")
([0.0 0.0; 0.0 0.0; … ; 0.0 0.0; 0.0 0.0], 44100.0f0, 0x0010, WAV.WAVChunk[WAV.WAVChunk(Symbol("fmt "), UInt8[0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, 0x00, 0x10, 0x00])])

julia> x[1:10]
ERROR: BoundsError: attempt to access Tuple{Matrix{Float64}, Float32, UInt16, Vector{WAV.WAVChunk}} at index [5]
Stacktrace:
 [1] getindex
   @ ./tuple.jl:31 [inlined]
 [2] #87
   @ ./range.jl:434 [inlined]
 [3] ntuple
   @ ./ntuple.jl:19 [inlined]
 [4] getindex(t::Tuple, r::UnitRange{Int64})
   @ Base ./range.jl:434
 [5] top-level scope
   @ REPL[4]:1

julia> 

according to the LibSndFile main Git page, this should be the result:

Julia> using FileIO: load, save, loadstreaming, savestreaming
julia> import LibSndFile
julia> load("audiofile.wav")
2938384-frame, 1-channel SampleBuf{FixedPointNumbers.Fixed{Int16,15}, 2}
66.63002267573697s sampled at 44100.0Hz`

x = load("myfile.wav")
plot(x[:, 1]) # plots with samples on the x axis

according to sox, the wav file I’m trying to load is fine, and I can load it into Matlab

perrin@perrinamdlaptop:~/perrin2013/SCaRF_Julia/LibSndFile$ soxi JolieHollandSacha.wav 

Input File     : 'JolieHollandSacha.wav'
Channels       : 2
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:03:08.89 = 8330196 samples = 14167 CDDA sectors
File Size      : 33.3M
Bit Rate       : 1.41M
Sample Encoding: 16-bit Signed Integer PCM

Any help would be greatly appreciated.

ok, it appears

x = load("JolieHollandSacha.wav")
```julia> x[1]
8330196×2 Matrix{Float64}:
 0.0  0.0
 [...]

now returns a Tubple and it appears to have re scaled the 16btit Int wav data into Float64 normalized to -1 … +1 (that’s standard).

julia> maximum(x[1][:,1])
0.988494521927549

so now I need the magic incantation to re-save a wav file in 16 bit int (or 24 bit int…) and also how to specify the sample rate…

thanks…

If typeof(x) tells you that what you got returned is a value of type SampledSignals.SampleBuf{FixedPointNumbers.Q0f15, 1}, that would be simply a struct of the form

mutable struct SampleBuf{T, N} <: AbstractSampleBuf{T, N}
    data::Array{T, N}
    samplerate::Float64
end

and hence reading the SampledSignals.jl and FixedPointNumbers.jl README may help, and x.data[1:10] may be closer to what you wanted.

However, I suspect in addition to LibSndFile.jl you also previously had loaded the WAV.jl package, which also overloads load, and that your load call was therefore actually dispatched to WAV.wavread instead and returned the same tuple that calling wavread directly would have (type ?WAV and ?wavread for the manual).

[I generally would advise against using FileIO and its overloaded load function, as that just hinders users finding the documentation for the actual file-loading function being called. FileIO is one of those things that may have seemed like a good idea at the time, but actually causes more confusion than being user friendly.]

thanks, but I was literally cutting and pasting from the very top README of the LibSndFile.jl git site… and No, I started a fresh session, so I did not load WAV.jl )

I’ve used Matlab for 20+ years but I’m not a julia power user so I really just need examples to start with… the “advanced” type stuff of Julia is a little beyond my Matlab/Fortran roots.. thanks…

https://github.com/JuliaAudio/LibSndFile.jl