# How to save a 24 bit wav file?

**URL:** <https://discourse.julialang.org/t/how-to-save-a-24-bit-wav-file/62583>\
**Category:** General Usage\
**Tags:** question, audio\
**Created:** [June 8, 2021, 2:08pm UTC](https://discourse.julialang.org/t/how-to-save-a-24-bit-wav-file/62583 "2021-06-08T14:08:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![D\_A](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@D\_A](https://discourse.julialang.org/u/D_A)\
**Post date:** [June 8, 2021, 2:08pm UTC](https://discourse.julialang.org/t/how-to-save-a-24-bit-wav-file/62583/1 "2021-06-08T14:08:19Z")

</div>

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…

```julia
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`:

```julia
7838720-frame, 2-channel SampleBuf{Q0f31, 2}
177.74875283446713s sampled at 44100.0Hz
▆▆▆▇▆▆▇▆▆▇▇▇▇▇▇▆▆▇▇▇▆▃▇▆▆▃▇▆▆▇▇▆▇▇▇▅▇▆▆▅▆▄▇▇▆▇▃▃▆▇▇▇▇▇▆▄▃▇▇▇▇▇▇▆▇▅█▇▇▇▂▇▇▇▇▇▇▇▇▇
▆▆▇▇▇▆▇▆▇▇▇▇▇▇▇▆▆▇▇▇▆▃▇▆▇▃▇▇▇▇▇▆▇▇▇▃▇▇▇▄▆▃▇▇▆▇▃▂▇▇▇▇▇▇▆▄▂▇▇▇▇▇▆▇▇▄▇█▇▇▂▇▇▇▇▇▇▇▇▆

```

Error:

```julia
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.

---

<div class="post-metadata">

**Author:** ![D\_A](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@D\_A](https://discourse.julialang.org/u/D_A)\
**Post date:** [June 9, 2021, 10:10pm UTC](https://discourse.julialang.org/t/how-to-save-a-24-bit-wav-file/62583/2 "2021-06-09T22:10:39Z")

</div>

OK, finally it seems I found the good way:

```julia
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!

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [June 9, 2021, 11:10pm UTC](https://discourse.julialang.org/t/how-to-save-a-24-bit-wav-file/62583/3 "2021-06-09T23:10:47Z")

</div>

Probably worth opening an issue at [https://github.com/JuliaAudio/LibSndFile.jl](https://github.com/JuliaAudio/LibSndFile.jl)

---

<div class="post-metadata">

**Author:** ![D\_A](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@D\_A](https://discourse.julialang.org/u/D_A)\
**Post date:** [June 10, 2021, 10:49am UTC](https://discourse.julialang.org/t/how-to-save-a-24-bit-wav-file/62583/4 "2021-06-10T10:49:14Z")

</div>

I did it, by commenting an already opened related [ticket](https://github.com/JuliaAudio/LibSndFile.jl/issues/23).
