From 967174368e0a0d5b68beebf8e808f105a6f9b74f Mon Sep 17 00:00:00 2001 From: David Reid Date: Fri, 29 May 2020 19:50:04 +1000 Subject: [PATCH] Add looping example. --- examples/simple_looping.c | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 examples/simple_looping.c diff --git a/examples/simple_looping.c b/examples/simple_looping.c new file mode 100644 index 00000000..80108f3e --- /dev/null +++ b/examples/simple_looping.c @@ -0,0 +1,75 @@ +#define DR_FLAC_IMPLEMENTATION +#include "../extras/dr_flac.h" /* Enables FLAC decoding. */ +#define DR_MP3_IMPLEMENTATION +#include "../extras/dr_mp3.h" /* Enables MP3 decoding. */ +#define DR_WAV_IMPLEMENTATION +#include "../extras/dr_wav.h" /* Enables WAV decoding. */ + +#define MINIAUDIO_IMPLEMENTATION +#include "../miniaudio.h" + +#include + +void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) +{ + ma_bool32 isLooping = MA_TRUE; + + ma_decoder* pDecoder = (ma_decoder*)pDevice->pUserData; + if (pDecoder == NULL) { + return; + } + + /* + A decoder is a data source which means you can seemlessly plug it into the ma_data_source API. We can therefore take advantage of the "loop" parameter + of ma_data_source_read_pcm_frames() to handle looping for us. + */ + ma_data_source_read_pcm_frames(pDecoder, pOutput, frameCount, isLooping); + + (void)pInput; +} + +int main(int argc, char** argv) +{ + ma_result result; + ma_decoder decoder; + ma_device_config deviceConfig; + ma_device device; + + if (argc < 2) { + printf("No input file.\n"); + return -1; + } + + result = ma_decoder_init_file(argv[1], NULL, &decoder); + if (result != MA_SUCCESS) { + return -2; + } + + deviceConfig = ma_device_config_init(ma_device_type_playback); + deviceConfig.playback.format = decoder.outputFormat; + deviceConfig.playback.channels = decoder.outputChannels; + deviceConfig.sampleRate = decoder.outputSampleRate; + deviceConfig.dataCallback = data_callback; + deviceConfig.pUserData = &decoder; + + if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) { + printf("Failed to open playback device.\n"); + ma_decoder_uninit(&decoder); + return -3; + } + + if (ma_device_start(&device) != MA_SUCCESS) { + printf("Failed to start playback device.\n"); + ma_device_uninit(&device); + ma_decoder_uninit(&decoder); + return -4; + } + + printf("Press Enter to quit..."); + getchar(); + + ma_device_uninit(&device); + ma_decoder_uninit(&decoder); + + return 0; +}