| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#define MA_NO_ENGINE |
| 18 |
#include "../miniaudio.c" |
| 19 |
|
| 20 |
static ma_resource_manager_data_source g_dataSources[16]; |
| 21 |
static ma_uint32 g_dataSourceCount; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static ma_result ma_data_source_read_pcm_frames_f32_ex(ma_data_source* pDataSource, float* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead, ma_format dataSourceFormat, ma_uint32 dataSourceChannels) |
| 29 |
{ |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
if (dataSourceFormat == ma_format_f32) { |
| 35 |
|
| 36 |
return ma_data_source_read_pcm_frames(pDataSource, pFramesOut, frameCount, pFramesRead); |
| 37 |
} else { |
| 38 |
|
| 39 |
ma_result result; |
| 40 |
ma_uint64 totalFramesRead; |
| 41 |
ma_uint8 temp[MA_DATA_CONVERTER_STACK_BUFFER_SIZE]; |
| 42 |
ma_uint64 tempCapInFrames = sizeof(temp) / ma_get_bytes_per_frame(dataSourceFormat, dataSourceChannels); |
| 43 |
|
| 44 |
if (pFramesRead != NULL) { |
| 45 |
*pFramesRead = 0; |
| 46 |
} |
| 47 |
|
| 48 |
totalFramesRead = 0; |
| 49 |
while (totalFramesRead < frameCount) { |
| 50 |
ma_uint64 framesJustRead; |
| 51 |
ma_uint64 framesToRead = frameCount - totalFramesRead; |
| 52 |
if (framesToRead > tempCapInFrames) { |
| 53 |
framesToRead = tempCapInFrames; |
| 54 |
} |
| 55 |
|
| 56 |
result = ma_data_source_read_pcm_frames(pDataSource, temp, framesToRead, &framesJustRead); |
| 57 |
if (result != MA_SUCCESS) { |
| 58 |
break; |
| 59 |
} |
| 60 |
|
| 61 |
ma_convert_pcm_frames_format(ma_offset_pcm_frames_ptr_f32(pFramesOut, totalFramesRead, dataSourceChannels), ma_format_f32, temp, dataSourceFormat, framesJustRead, dataSourceChannels, ma_dither_mode_none); |
| 62 |
totalFramesRead += framesJustRead; |
| 63 |
|
| 64 |
if (result != MA_SUCCESS) { |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if (pFramesRead != NULL) { |
| 70 |
*pFramesRead = totalFramesRead; |
| 71 |
} |
| 72 |
|
| 73 |
return MA_SUCCESS; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
MA_API ma_result ma_data_source_read_pcm_frames_f32(ma_data_source* pDataSource, float* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead) |
| 78 |
{ |
| 79 |
ma_result result; |
| 80 |
ma_format format; |
| 81 |
ma_uint32 channels; |
| 82 |
|
| 83 |
result = ma_data_source_get_data_format(pDataSource, &format, &channels, NULL, NULL, 0); |
| 84 |
if (result != MA_SUCCESS) { |
| 85 |
return result; |
| 86 |
} |
| 87 |
|
| 88 |
return ma_data_source_read_pcm_frames_f32_ex(pDataSource, pFramesOut, frameCount, pFramesRead, format, channels); |
| 89 |
} |
| 90 |
|
| 91 |
MA_API ma_result ma_data_source_read_pcm_frames_and_mix_f32(ma_data_source* pDataSource, float* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead, float volume) |
| 92 |
{ |
| 93 |
ma_result result; |
| 94 |
ma_format format; |
| 95 |
ma_uint32 channels; |
| 96 |
ma_uint64 totalFramesRead; |
| 97 |
|
| 98 |
if (pFramesRead != NULL) { |
| 99 |
*pFramesRead = 0; |
| 100 |
} |
| 101 |
|
| 102 |
if (pDataSource == NULL) { |
| 103 |
return MA_INVALID_ARGS; |
| 104 |
} |
| 105 |
|
| 106 |
result = ma_data_source_get_data_format(pDataSource, &format, &channels, NULL, NULL, 0); |
| 107 |
if (result != MA_SUCCESS) { |
| 108 |
return result; |
| 109 |
} |
| 110 |
|
| 111 |
totalFramesRead = 0; |
| 112 |
while (totalFramesRead < frameCount) { |
| 113 |
float temp[MA_DATA_CONVERTER_STACK_BUFFER_SIZE/sizeof(float)]; |
| 114 |
ma_uint64 tempCapInFrames = ma_countof(temp) / channels; |
| 115 |
ma_uint64 framesJustRead; |
| 116 |
ma_uint64 framesToRead = frameCount - totalFramesRead; |
| 117 |
if (framesToRead > tempCapInFrames) { |
| 118 |
framesToRead = tempCapInFrames; |
| 119 |
} |
| 120 |
|
| 121 |
result = ma_data_source_read_pcm_frames_f32_ex(pDataSource, temp, framesToRead, &framesJustRead, format, channels); |
| 122 |
|
| 123 |
ma_mix_pcm_frames_f32(ma_offset_pcm_frames_ptr_f32(pFramesOut, totalFramesRead, channels), temp, framesJustRead, channels, volume); |
| 124 |
totalFramesRead += framesJustRead; |
| 125 |
|
| 126 |
if (result != MA_SUCCESS) { |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (pFramesRead != NULL) { |
| 132 |
*pFramesRead = totalFramesRead; |
| 133 |
} |
| 134 |
|
| 135 |
return MA_SUCCESS; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) |
| 140 |
{ |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
ma_uint32 iDataSource; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
for (iDataSource = 0; iDataSource < g_dataSourceCount; iDataSource += 1) { |
| 155 |
ma_data_source_read_pcm_frames_and_mix_f32(&g_dataSources[iDataSource], (float*)pOutput, frameCount, NULL, 1); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
(void)pInput; |
| 160 |
(void)pDevice; |
| 161 |
} |
| 162 |
|
| 163 |
static ma_thread_result MA_THREADCALL custom_job_thread(void* pUserData) |
| 164 |
{ |
| 165 |
ma_resource_manager* pResourceManager = (ma_resource_manager*)pUserData; |
| 166 |
|
| 167 |
for (;;) { |
| 168 |
ma_result result; |
| 169 |
ma_resource_manager_job job; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
result = ma_resource_manager_next_job(pResourceManager, &job); |
| 181 |
if (result != MA_SUCCESS) { |
| 182 |
if (result == MA_CANCELLED) { |
| 183 |
printf("CUSTOM JOB THREAD TERMINATING VIA MA_CANCELLED... "); |
| 184 |
} else { |
| 185 |
printf("CUSTOM JOB THREAD ERROR: %s. TERMINATING... ", ma_result_description(result)); |
| 186 |
} |
| 187 |
|
| 188 |
break; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
if (job.toc.breakup.code == MA_JOB_TYPE_QUIT) { |
| 202 |
printf("CUSTOM JOB THREAD TERMINATING VIA MA_JOB_TYPE_QUIT... "); |
| 203 |
break; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
printf("PROCESSING IN CUSTOM JOB THREAD: %d\n", job.toc.breakup.code); |
| 208 |
ma_resource_manager_process_job(pResourceManager, &job); |
| 209 |
} |
| 210 |
|
| 211 |
printf("TERMINATED\n"); |
| 212 |
return (ma_thread_result)0; |
| 213 |
} |
| 214 |
|
| 215 |
int main(int argc, char** argv) |
| 216 |
{ |
| 217 |
ma_result result; |
| 218 |
ma_device_config deviceConfig; |
| 219 |
ma_device device; |
| 220 |
ma_resource_manager_config resourceManagerConfig; |
| 221 |
ma_resource_manager resourceManager; |
| 222 |
ma_thread jobThread; |
| 223 |
int iFile; |
| 224 |
|
| 225 |
deviceConfig = ma_device_config_init(ma_device_type_playback); |
| 226 |
deviceConfig.playback.format = ma_format_f32; |
| 227 |
deviceConfig.dataCallback = data_callback; |
| 228 |
deviceConfig.pUserData = NULL; |
| 229 |
|
| 230 |
result = ma_device_init(NULL, &deviceConfig, &device); |
| 231 |
if (result != MA_SUCCESS) { |
| 232 |
printf("Failed to initialize device."); |
| 233 |
return -1; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
result = ma_device_start(&device); |
| 239 |
if (result != MA_SUCCESS) { |
| 240 |
ma_device_uninit(&device); |
| 241 |
printf("Failed to start device."); |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
resourceManagerConfig = ma_resource_manager_config_init(); |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
resourceManagerConfig.decodedFormat = device.playback.format; |
| 258 |
resourceManagerConfig.decodedChannels = device.playback.channels; |
| 259 |
resourceManagerConfig.decodedSampleRate = device.sampleRate; |
| 260 |
|
| 261 |
|
| 262 |
resourceManagerConfig.jobThreadCount = 4; |
| 263 |
|
| 264 |
result = ma_resource_manager_init(&resourceManagerConfig, &resourceManager); |
| 265 |
if (result != MA_SUCCESS) { |
| 266 |
ma_device_uninit(&device); |
| 267 |
printf("Failed to initialize the resource manager."); |
| 268 |
return -1; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
ma_thread_create(&jobThread, ma_thread_priority_default, 0, custom_job_thread, &resourceManager, NULL); |
| 277 |
|
| 278 |
|
| 279 |
for (iFile = 0; iFile < (int)ma_countof(g_dataSources) && iFile < argc-1; iFile += 1) { |
| 280 |
result = ma_resource_manager_data_source_init( |
| 281 |
&resourceManager, |
| 282 |
argv[iFile+1], |
| 283 |
MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_DECODE | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_ASYNC , |
| 284 |
NULL, |
| 285 |
&g_dataSources[iFile]); |
| 286 |
|
| 287 |
if (result != MA_SUCCESS) { |
| 288 |
break; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
ma_data_source_set_looping(&g_dataSources[iFile], MA_TRUE); |
| 293 |
|
| 294 |
g_dataSourceCount += 1; |
| 295 |
} |
| 296 |
|
| 297 |
printf("Press Enter to quit..."); |
| 298 |
getchar(); |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
ma_device_uninit(&device); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
for (iFile = 0; (size_t)iFile < g_dataSourceCount; iFile += 1) { |
| 317 |
ma_resource_manager_data_source_uninit(&g_dataSources[iFile]); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
ma_resource_manager_post_job_quit(&resourceManager); |
| 330 |
ma_thread_wait(&jobThread); |
| 331 |
|
| 332 |
|
| 333 |
ma_resource_manager_uninit(&resourceManager); |
| 334 |
|
| 335 |
return 0; |
| 336 |
} |