mackron / miniaudio

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

Is it possible to "push" audio to a device, like SDL_QueueAudio? #744

Open SomeGuyDoinWork opened 12 months ago

SomeGuyDoinWork commented 12 months ago

Trying to get audio data from another library (pl_mpeg) and it has a callback that pushes the audio to a device. Does miniaudio support anything similar?

mackron commented 12 months ago

I'm not familiar with SDL_QueueAudio, but I suspect it's just the ability for you post a chunk of audio data to the device and it'll get played when the device get's to it. No, this is not currently supported directly (though of course you can do this yourself with a custom data source). This is something that a lot of people have asked for, and is something I want to support in some form. It's unlikely I'll support it directly with ma_device, such as ma_device_push_data() or whatever, but I might have a queue style data source where you can just push your audio to the data source, and then in the device callback you just read from the data source.

I'll keep this issue open as a feature request, but there is no timeline on when I'll get to this (probably won't be this year).

foxtacles commented 3 months ago

+1 from me. I also have a use case where I already have a callback that receives audio data, and if there was an API in miniaudio to directly push the the PCM data into a data source, that would be great.

mackron commented 3 months ago

It should be feasible to have something like ma_audio_queue which is a type of data source, and then initialize a sound with ma_sound_init_from_data_source():

ma_audio_queue queue;
ma_audio_queue_init(..., &queue);

ma_sound_init_from_data_source(..., &queue, ..., &sound);

And then when new data needs to be pushed, do something like this:

// Push data directly to the underlying data source. The sound will pick it up at playback time.
ma_audio_queue_push(&queue, dataSize, pData);

So long as ma_audio_queue_push() is thread safe with ma_data_source_read_pcm_frames() I can't see why that couldn't work.