# Convert 16-bit wav file to 12-bit raw audio file

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?)

• 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? – Marcus Müller Jun 8 '17 at 5:46
• That makes sense. Is there a way to get sox to do the truncation ? – bcattle Jun 8 '17 at 5:48
• 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. – Marcus Müller Jun 8 '17 at 5:53
• Your sample rate is 4410..? did you mean 44100 instead ? – Fat32 Jun 8 '17 at 12:13