How to save a 24 bit wav file?

Hello,

After opening a wav file (24 bit depth) with the LibSndFile library (and not WAV because I get InexactError when reading some files that are opened well with LibSndFile), the data is in 32 bit format (Q0f31 (alias for Fixed{Int32, 31})), then if I save the data, which were not modified, I get a larger file because the audio data is in 32 bit and not 24. If I convert the data to 24 bit then I save, I get an error…

using FileIO
using LibSndFile
using SampledSignals

soundfile = load("original.wav")
soundfile = SampleBuf(PCM24Sample.(soundfile.data), soundfile.samplerate)  # Is there a better way to do that?
save("result.wav", soundfile)

soundfile:

7838720-frame, 2-channel SampleBuf{Q0f31, 2}
177.74875283446713s sampled at 44100.0Hz
β–†β–†β–†β–‡β–†β–†β–‡β–†β–†β–‡β–‡β–‡β–‡β–‡β–‡β–†β–†β–‡β–‡β–‡β–†β–ƒβ–‡β–†β–†β–ƒβ–‡β–†β–†β–‡β–‡β–†β–‡β–‡β–‡β–…β–‡β–†β–†β–…β–†β–„β–‡β–‡β–†β–‡β–ƒβ–ƒβ–†β–‡β–‡β–‡β–‡β–‡β–†β–„β–ƒβ–‡β–‡β–‡β–‡β–‡β–‡β–†β–‡β–…β–ˆβ–‡β–‡β–‡β–‚β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡
β–†β–†β–‡β–‡β–‡β–†β–‡β–†β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–†β–†β–‡β–‡β–‡β–†β–ƒβ–‡β–†β–‡β–ƒβ–‡β–‡β–‡β–‡β–‡β–†β–‡β–‡β–‡β–ƒβ–‡β–‡β–‡β–„β–†β–ƒβ–‡β–‡β–†β–‡β–ƒβ–‚β–‡β–‡β–‡β–‡β–‡β–‡β–†β–„β–‚β–‡β–‡β–‡β–‡β–‡β–†β–‡β–‡β–„β–‡β–ˆβ–‡β–‡β–‚β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–‡β–†

Error:

Errors encountered while save File{DataFormat{:WAV}, String}("test.wav").
All errors:
===========================================
ArgumentError: Package WAV [8149f6b0-98f6-5db9-b78f-408fbbb8ef88] is required but does not seem to be installed:
 - Run `Pkg.instantiate()` to install all recorded dependencies.

===========================================
LibSndFile writer error: neither save nor fileio_save is defined
  due to MethodError(LibSndFile.subformatcode, (FixedPointNumbers.Q8f23,), 0x00000000000073ea)
  Will try next loader.

Of course, if I have WAV installed (not the case in this MWE), I just have the LibSndFile writer error: neither save nor fileio_save is defined.

The practical goal is not to load and save the file without modifications, of course, it is also to separate channels in case of stereo…

I really don’t know how I can simply convert and save in 24 bit depth (or more generally keep the same β€œformat” as original if it is not 16 or 32 bit).

Thank you.

OK, finally it seems I found the good way:

using FileIO
using LibSndFile

LibSndFile.subformatcode(::Type{LibSndFile.PCM32Sample}) = 0x00000003  # Default is 0x00000004 (for 32 bit); see https://github.com/JuliaAudio/LibSndFile.jl/blob/master/src/libsndfile_h.jl.
soundfile = load("original.wav")
save("test.wav", soundfile)

I don’t know if there is a better way to do that, without involving some arguments or easier configuration, but it seems to work!

1 Like

Probably worth opening an issue at GitHub - JuliaAudio/LibSndFile.jl: Julia Interface to libsndfile

1 Like

I did it, by commenting an already opened related ticket.