qoa icon indicating copy to clipboard operation
qoa copied to clipboard

PCM float planar to qoa format

Open emmanuel-jorge opened this issue 2 years ago • 1 comments

Is there a way to convert planar audio data (float 32 : LLLLLL.......RRRRRR) to qoa format with minimum data loss?

emmanuel-jorge avatar Jun 27 '23 12:06 emmanuel-jorge

Ok, I answer myself :

qoa_desc qoa = {...}
// Conversion from planar to interleaved PCM format
// and conversion from float32 to int16 (possible data loss)
// 0..............qoa.samples.......qoa.samples * 2
//                     |                    |
//                     v                    v
// LLLLL..........LLLLLRRRRR...........RRRRR
const float *planar_data = ...;
// 0.....................qoa.samples * 2
//                             |
//                             v
// LRLRLRLR............LRLRLRLR
short interleaved_data[5120 * QOA_MAX_CHANNELS];
for (uint c = 0; c < qoa.channels; c++) {
    for (int i = 0; i < qoa.samples; i++) {
        int planar_index = i + (qoa.samples * c);
        float f = planar_data[planar_index];
        if (f > 1.0) f = 1.0;
        if (f < -1.0) f = -1.0;

        int16_t s = (int16_t)(f * 0x7fff);
        int interleaved_index = (i * qoa.channels) + c;
        interleaved_data[interleaved_index] = s;
    }
}

unsigned char *encoded_data = qoa_encode(interleaved_data, &qoa, &out_len);
...

emmanuel-jorge avatar Jun 29 '23 09:06 emmanuel-jorge