[flac-dev] Encoder example for 24-bit files

Bastiaan Timmer basjetimmer at yahoo.com
Thu Aug 14 11:30:19 PDT 2014


> pcm[i] = (FLAC__int32)buffer[3*i+2];
> pcm[i] <<= 8;
> pcm[i] |= (FLAC__int32)buffer[3*i+1];
> pcm[i] <<= 8;
> pcm[i] |= (FLAC__int32)buffer[3*i];



(I might be mistaken but) I think you should realize the 24 bit sample has a sign bit in the last of the bytes. Casting the FLAC__byte from the buffer to FLAC__int32 will not trat the sign bit as such leading to huge, incorrect values (and in a quick tes, to an encoding error). So you should change the cast in the first line to a FLAC__int8. The casts in the next lines aren't needed at all I think. I was writing up an example for you and came up with the following (this is extremely ugly and ineffecient, but should accept 24, 16, or any other bits-per-sample):

size_t i;
for (i = 0; i < need*channels; ++i)
{
  pcm[i] = 0;
  size_t s;
  for (s = 0; s < (bps / 8 - 1); ++s)
    pcm[i] |= buffer[(bps / 8) * i + s] << (8 * s);
  pcm[i] |= (FLAC__int8)buffer[(bps / 8) * i + s] << (8 * s);
}


More information about the flac-dev mailing list