mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

Add ma_decoder_get_encoding_format #794

Closed luigi-rosso closed 8 months ago

luigi-rosso commented 10 months ago

There are cases where we want to show a user the audio format, so being able to retrieve it after a decoder has initialized is helpful. I couldn't find a way to do this with the existing API so I added ma_decoder_get_encoding_format. Let me know if I missed something!

ma_decoder decoder;
ma_decoder_config config = ma_decoder_config_init(ma_format_f32, 0, 0);
if (ma_decoder_init_memory(..., ..., &config, &decoder) != MA_SUCCESS) {
    return;
}

// ... later

ma_encoding_format encodingFormat;
if (ma_decoder_get_encoding_format(&decoder, &encodingFormat) == MA_SUCCESS) {
    switch (encodingFormat)
    {
        case ma_encoding_format_mp3:
            printf("format is mp3\n");
            break;
        case ma_encoding_format_wav:
            printf("format is wav\n");
            break;
        case ma_encoding_format_vorbis:
            printf("format is ogg\n");
            break;
        case ma_encoding_format_flac:
            printf("format is flag\n");
            break;
        default:
            printf("format is unknown\n");
            break;
    }
}
mackron commented 10 months ago

Thanks. I support the idea of a ma_decoder_get_encoding_format() function, but I'm going to sit on this for a bit because I'm not entirely sure I like this particular implementation, specifically with how the vtable pointer is used for discrimination. I'm wondering if a new vtable function should be added instead for querying the format. This would allow it to work for replacement implementations over the stock ones. For example, if someone doesn't like the stock MP3 decoder, they could replace it with their own and the ma_decoder_get_encoding_format() function can still return a valid encoding format instead of unknown.

There's another issue that has been raised, and I would like to consider some ideas on how to improve some format stuff before deciding on how I would like to handle this: https://github.com/mackron/miniaudio/issues/765. I support the general idea of this PR though so will leave this open to remind me to look at this.

mackron commented 8 months ago

I've implemented a solution for this, but it's in the dev-0.12 branch which means it won't be released for a while. I ended up going with a callback based approach. A new callback has been added to ma_decoder_backend_vtable which will be used to report the encoding format.

I would recommend you just stick with your local changes for the time being until I get 0.12 released.