I want to convert a 16-bit wav file to 12 bit raw audio samples (to simulate the output of an ADC).

I'm trying to run

$ sox file.wav --bits 12 --encoding signed-integer --endian little 12_bit_signed_le/file.raw

And I get the error

sox WARN formats: raw can't encode Signed Integer PCM to 12-bit

Output File    : '12_bit_signed_le/file.raw'
Channels       : 1
Sample Rate    : 4410
Precision      : 16-bit
Duration       : 00:59:53.47 = 15847219 samples ~ 269511 CDDA sectors
Sample Encoding: 16-bit Signed Integer PCM
Endian Type    : little
Reverse Nibbles: no
Reverse Bits   : no
Comment        : 'Processed by SoX'

sox INFO sox: effects chain: input         4410Hz  1 channels
sox INFO sox: effects chain: output        4410Hz  1 channels

As you can see, the command resulted in 16-bit output. How do I force it to truncate to 12 bits?

Also, do I need to be worried about clipping (as this post suggests?)

  • 2
    $\begingroup$ Your PC can't natively work with 12 bit numbers. So it's a very usual thing to use one 16 bit number for each 12 bit number, but only use the most significant 12 bits. How's that a problem in simulation? $\endgroup$ – Marcus Müller Jun 8 '17 at 5:46
  • $\begingroup$ That makes sense. Is there a way to get sox to do the truncation ? $\endgroup$ – bcattle Jun 8 '17 at 5:48
  • 1
    $\begingroup$ Maybe. I'd just write 10 lines of code in any reasonable language to take two input samples and generate three bytes of output and write that to a file. $\endgroup$ – Marcus Müller Jun 8 '17 at 5:53
  • $\begingroup$ Your sample rate is 4410..? did you mean 44100 instead ? $\endgroup$ – Fat32 Jun 8 '17 at 12:13

Sox will not support bit widths for wav files other than multiples of bytes. If you really need such unusual binary format then I suggest to write a short Python program that packs values in arbitrary format. Just please keep in mind that the maximum amplitude will be 2047 (instead of 32767 as for 16 bits).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.