| 1 | /* |
| 2 | Demonstrates how you can use the resource manager to manage loaded sounds. |
| 3 | |
| 4 | This example loads the first sound specified on the command line via the resource manager and then plays it using the |
| 5 | low level API. |
| 6 | |
| 7 | You can control whether or not you want to load the sound asynchronously and whether or not you want to store the data |
| 8 | in-memory or stream it. When storing the sound in-memory you can also control whether or not it is decoded. To do this, |
| 9 | specify a combination of the following options in `ma_resource_manager_data_source_init()`: |
| 10 | |
| 11 | * MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_ASYNC - Load asynchronously. |
| 12 | * MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_DECODE - Store the sound in-memory in uncompressed/decoded format. |
| 13 | * MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_STREAM - Stream the sound from disk rather than storing entirely in memory. Useful for music. |
| 14 | |
| 15 | The object returned by the resource manager is just a standard data source which means it can be plugged into any of |
| 16 | `ma_data_source_*()` APIs just like any other data source and it should just work. |
| 17 | |
| 18 | Internally, there's a background thread that's used to process jobs and enable asynchronicity. By default there is only |
| 19 | a single job thread, but this can be configured in the resource manager config. You can also implement your own threads |
| 20 | for processing jobs. That is more advanced, and beyond the scope of this example. |
| 21 | |
| 22 | When you initialize a resource manager you can specify the sample format, channels and sample rate to use when reading |
| 23 | data from the data source. This means the resource manager will ensure all sounds will have a standard format. When not |
| 24 | set, each sound will have their own formats and you'll need to do the necessary data conversion yourself. |
| 25 | */ |
| 26 | #define MA_NO_ENGINE /* We're intentionally not using the ma_engine API here. */ |
| 27 | #include "../miniaudio.c" |
| 28 | |
| 29 | #ifdef __EMSCRIPTEN__ |
| 30 | #include <emscripten.h> |
| 31 | |
| 32 | void main_loop__em(void* pUserData) |
| 33 | { |
| 34 | ma_resource_manager* pResourceManager = (ma_resource_manager*)pUserData; |
| 35 | |
| 36 | /* |
| 37 | The Emscripten build does not support threading which means we need to process jobs manually. If |
| 38 | there are no jobs needing to be processed this will return immediately with MA_NO_DATA_AVAILABLE. |
| 39 | */ |
| 40 | ma_resource_manager_process_next_job(pResourceManager); |
| 41 | } |
| 42 | #endif |
| 43 | |
| 44 | void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) |
| 45 | { |
| 46 | ma_data_source_read_pcm_frames((ma_data_source*)pDevice->pUserData, pOutput, frameCount, NULL); |
| 47 | |
| 48 | (void)pInput; |
| 49 | } |
| 50 | |
| 51 | int main(int argc, char** argv) |
| 52 | { |
| 53 | ma_result result; |
| 54 | ma_device_config deviceConfig; |
| 55 | ma_device device; |
| 56 | ma_resource_manager_config resourceManagerConfig; |
| 57 | ma_resource_manager resourceManager; |
| 58 | ma_resource_manager_data_source dataSource; |
| 59 | |
| 60 | if (argc < 2) { |
| 61 | printf("No input file."); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /* We'll initialize the device first. */ |
| 67 | deviceConfig = ma_device_config_init(ma_device_type_playback); |
| 68 | deviceConfig.dataCallback = data_callback; |
| 69 | deviceConfig.pUserData = &dataSource; /* <-- We'll be reading from this in the data callback. */ |
| 70 | |
| 71 | result = ma_device_init(NULL, &deviceConfig, &device); |
| 72 | if (result != MA_SUCCESS) { |
| 73 | printf("Failed to initialize device."); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | We have the device so now we want to initialize the resource manager. We'll use the resource manager to load a |
| 80 | sound based on the command line. |
| 81 | */ |
| 82 | resourceManagerConfig = ma_resource_manager_config_init(); |
| 83 | resourceManagerConfig.decodedFormat = device.playback.format; |
| 84 | resourceManagerConfig.decodedChannels = device.playback.channels; |
| 85 | resourceManagerConfig.decodedSampleRate = device.sampleRate; |
| 86 | |
| 87 | /* |
| 88 | We're not supporting threading with Emscripten so go ahead and disable threading. It's important |
| 89 | that we set the appropriate flag and also the job thread count to 0. |
| 90 | */ |
| 91 | #ifdef __EMSCRIPTEN__ |
| 92 | resourceManagerConfig.flags |= MA_RESOURCE_MANAGER_FLAG_NO_THREADING; |
| 93 | resourceManagerConfig.jobThreadCount = 0; |
| 94 | #endif |
| 95 | |
| 96 | result = ma_resource_manager_init(&resourceManagerConfig, &resourceManager); |
| 97 | if (result != MA_SUCCESS) { |
| 98 | ma_device_uninit(&device); |
| 99 | printf("Failed to initialize the resource manager."); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | /* Now that we have a resource manager we can load a sound. */ |
| 104 | result = ma_resource_manager_data_source_init( |
| 105 | &resourceManager, |
| 106 | argv[1], |
| 107 | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_DECODE | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_ASYNC | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_STREAM, |
| 108 | NULL, /* Async notification. */ |
| 109 | &dataSource); |
| 110 | if (result != MA_SUCCESS) { |
| 111 | printf("Failed to load sound \"%s\".", argv[1]); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | /* In this example we'll enable looping. */ |
| 116 | ma_data_source_set_looping(&dataSource, MA_TRUE); |
| 117 | |
| 118 | |
| 119 | /* Now that we have a sound we can start the device. */ |
| 120 | result = ma_device_start(&device); |
| 121 | if (result != MA_SUCCESS) { |
| 122 | ma_device_uninit(&device); |
| 123 | printf("Failed to start device."); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | #ifdef __EMSCRIPTEN__ |
| 128 | emscripten_set_main_loop_arg(main_loop__em, &resourceManager, 0, 1); |
| 129 | #else |
| 130 | printf("Press Enter to quit...\n"); |
| 131 | getchar(); |
| 132 | #endif |
| 133 | |
| 134 | /* Teardown. */ |
| 135 | |
| 136 | /* Uninitialize the device first to ensure the data callback is stopped and doesn't try to access any data. */ |
| 137 | ma_device_uninit(&device); |
| 138 | |
| 139 | /* |
| 140 | Before uninitializing the resource manager we need to uninitialize every data source. The data source is owned by |
| 141 | the caller which means you're responsible for uninitializing it. |
| 142 | */ |
| 143 | ma_resource_manager_data_source_uninit(&dataSource); |
| 144 | |
| 145 | /* Uninitialize the resource manager after each data source. */ |
| 146 | ma_resource_manager_uninit(&resourceManager); |
| 147 | |
| 148 | return 0; |
| 149 | } |