| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include "../miniaudio.c" |
| 19 |
#include "../extras/decoders/libvorbis/miniaudio_libvorbis.c" |
| 20 |
#include "../extras/decoders/libopus/miniaudio_libopus.c" |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
|
| 24 |
|
| 25 |
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) |
| 26 |
{ |
| 27 |
ma_data_source* pDataSource = (ma_data_source*)pDevice->pUserData; |
| 28 |
if (pDataSource == NULL) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
ma_data_source_read_pcm_frames(pDataSource, pOutput, frameCount, NULL); |
| 33 |
|
| 34 |
(void)pInput; |
| 35 |
} |
| 36 |
|
| 37 |
int main(int argc, char** argv) |
| 38 |
{ |
| 39 |
ma_result result; |
| 40 |
ma_decoder_config decoderConfig; |
| 41 |
ma_decoder decoder; |
| 42 |
ma_device_config deviceConfig; |
| 43 |
ma_device device; |
| 44 |
ma_format format; |
| 45 |
ma_uint32 channels; |
| 46 |
ma_uint32 sampleRate; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
ma_decoding_backend_vtable* pCustomBackendVTables[] = |
| 53 |
{ |
| 54 |
ma_decoding_backend_libvorbis, |
| 55 |
ma_decoding_backend_libopus |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
if (argc < 2) { |
| 60 |
printf("No input file.\n"); |
| 61 |
return -1; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
decoderConfig = ma_decoder_config_init_default(); |
| 67 |
decoderConfig.pCustomBackendUserData = NULL; |
| 68 |
decoderConfig.ppCustomBackendVTables = pCustomBackendVTables; |
| 69 |
decoderConfig.customBackendCount = sizeof(pCustomBackendVTables) / sizeof(pCustomBackendVTables[0]); |
| 70 |
|
| 71 |
result = ma_decoder_init_file(argv[1], &decoderConfig, &decoder); |
| 72 |
if (result != MA_SUCCESS) { |
| 73 |
printf("Failed to initialize decoder."); |
| 74 |
return -1; |
| 75 |
} |
| 76 |
|
| 77 |
ma_data_source_set_looping(&decoder, MA_TRUE); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
result = ma_data_source_get_data_format(&decoder, &format, &channels, &sampleRate, NULL, 0); |
| 82 |
if (result != MA_SUCCESS) { |
| 83 |
printf("Failed to retrieve decoder data format."); |
| 84 |
ma_decoder_uninit(&decoder); |
| 85 |
return -1; |
| 86 |
} |
| 87 |
|
| 88 |
deviceConfig = ma_device_config_init(ma_device_type_playback); |
| 89 |
deviceConfig.playback.format = format; |
| 90 |
deviceConfig.playback.channels = channels; |
| 91 |
deviceConfig.sampleRate = sampleRate; |
| 92 |
deviceConfig.dataCallback = data_callback; |
| 93 |
deviceConfig.pUserData = &decoder; |
| 94 |
|
| 95 |
if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) { |
| 96 |
printf("Failed to open playback device.\n"); |
| 97 |
ma_decoder_uninit(&decoder); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
|
| 101 |
if (ma_device_start(&device) != MA_SUCCESS) { |
| 102 |
printf("Failed to start playback device.\n"); |
| 103 |
ma_device_uninit(&device); |
| 104 |
ma_decoder_uninit(&decoder); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
|
| 108 |
printf("Press Enter to quit..."); |
| 109 |
getchar(); |
| 110 |
|
| 111 |
ma_device_uninit(&device); |
| 112 |
ma_decoder_uninit(&decoder); |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |