Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCM float planar to qoa format #30

Open
ejorge31 opened this issue Jun 27, 2023 · 1 comment
Open

PCM float planar to qoa format #30

ejorge31 opened this issue Jun 27, 2023 · 1 comment

Comments

@ejorge31
Copy link

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

@ejorge31
Copy link
Author

ejorge31 commented Jun 29, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant