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

Example of playing sound chunk by chunk? #536

Closed SC-One closed 2 years ago

SC-One commented 2 years ago

I could not find example of playing a sound that using callbacks or other ways that can use chunks of data. [Just found this] [2] 3

I mean Consumer/Provider data. I have custom provider that provide data in random periods(enough fast) , And I wanna use miniaudio as consumer that play my chunks data. Where I can find an example or basic concepts that can help me to accomplish the goal? (or what's High level api for providing chunks data on sounds instead using files)

In addition my application is complete multi threaded.

SC-One commented 2 years ago

All examples using _file api , I dont know why there is no example that preparing data by callback (or any way that represent data as chunks) . I just need a reference link to same aspects.

SC-One commented 2 years ago

I know something same to this is for reading from memory , But need process data that produce real time (background thread) , I mean play a sound that has dynamic buffer.

below example is whole of Buffer(sound) , it's not my goal.


    ma_result result = ma_decoder_init_memory(buffer, len, NULL, &decoder);

    if (result != MA_SUCCESS)
    {
        std::cout << "ERROR:   Failed decoder initialization." << std::endl;
        ma_decoder_uninit(&decoder);
        return;
    }
mackron commented 2 years ago

Are your data chunks in uncompressed PCM format, or do you mean you have chunks of encoded data such as compressed MP3 data?

If your data is already decoded (PCM), it's just a matter of giving the data to miniaudio via the data callback. If you're using the high level API (ma_engine) you would need to implement your own data source and use ma_sound_init_from_data_source(). The ring buffer might help (ma_pcm_rb) - that's a single-producer, single-consumer buffer (miniaudio would be the consumer).

If your data is still encoded (such as chunk of MP3 data), that is not supported by miniaudio and you will need to decode that separately before handing it to miniaudio.

(Moving this to the Discussions section.)