phoboslab/qoa

PCM float planar to qoa format

Opened this issue · 1 comments

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

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);
...