zeel
zeel

Reputation: 1130

Is it possible/how to encode a WAV file with a sample size that is not a multiple of 8

I am trying to encode a WAV file from the analog input of an Arduino Uno. The analogRead(pinNumber) function returns a value from 0 - 1023. This 10 bit value then needs to be encoded into the WAV file. There is a function map(val, inMin, inMax, outMin, outMax) that could adjust this to a full 16 bit value (or to an 8 bit value). But ideally it would be perfect if I could simply make the sample size of the WAV file 10 bits. I know how to write that into the header, but I don't know if it will actually work, or how to actually write the data if it isn't a multiple of 8 bits.

If possible I assume it requires some fancy bitwise operations, but I have no idea how to make that work.

Upvotes: 3

Views: 632

Answers (1)

jaket
jaket

Reputation: 9341

It is improbable that any application would be able to read a wave file with packed 10-bit samples. Few programs will even read packed 24-bit. Probably because it is such a chore to unpack back into 32-bits.

Your best bet is to store it 16-bit signed. The question mentions calling a mapping function that presumable rescales 10-bit audio into 16-bit audio. The better solution would be to just shift the 10-bit samples up so that they are left-justified, leaving the lower 6 bits as zero. There are a couple of advantages to doing it this way. First, rescaling will not improve the quality of the audio (obviously) and will just add some extra quantization noise. Second, if you wanted to read it back out as 10-bit audio you can just shift it back down or whatever without any loss of precision.

Upvotes: 6

Related Questions