| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "../miniaudio.c" |
| 18 |
|
| 19 |
#define MAX_DEVICES 2 |
| 20 |
#define MAX_SOUNDS 32 |
| 21 |
|
| 22 |
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) |
| 23 |
{ |
| 24 |
(void)pInput; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
ma_engine_read_pcm_frames((ma_engine*)pDevice->pUserData, pOutput, frameCount, NULL); |
| 33 |
} |
| 34 |
|
| 35 |
int main(int argc, char** argv) |
| 36 |
{ |
| 37 |
ma_result result; |
| 38 |
ma_context context; |
| 39 |
ma_resource_manager_config resourceManagerConfig; |
| 40 |
ma_resource_manager resourceManager; |
| 41 |
ma_engine engines[MAX_DEVICES]; |
| 42 |
ma_device devices[MAX_DEVICES]; |
| 43 |
ma_uint32 engineCount = 0; |
| 44 |
ma_uint32 iEngine; |
| 45 |
ma_device_info* pPlaybackDeviceInfos; |
| 46 |
ma_uint32 playbackDeviceCount; |
| 47 |
ma_uint32 iAvailableDevice; |
| 48 |
ma_uint32 iChosenDevice; |
| 49 |
ma_sound sounds[MAX_SOUNDS]; |
| 50 |
ma_uint32 soundCount; |
| 51 |
ma_uint32 iSound; |
| 52 |
|
| 53 |
if (argc < 2) { |
| 54 |
printf("No input file."); |
| 55 |
return -1; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
resourceManagerConfig = ma_resource_manager_config_init(); |
| 64 |
resourceManagerConfig.decodedFormat = ma_format_f32; |
| 65 |
resourceManagerConfig.decodedChannels = 0; |
| 66 |
resourceManagerConfig.decodedSampleRate = 48000; |
| 67 |
|
| 68 |
result = ma_resource_manager_init(&resourceManagerConfig, &resourceManager); |
| 69 |
if (result != MA_SUCCESS) { |
| 70 |
printf("Failed to initialize resource manager."); |
| 71 |
return -1; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
result = ma_context_init(NULL, 0, NULL, &context); |
| 77 |
if (result != MA_SUCCESS) { |
| 78 |
printf("Failed to initialize context."); |
| 79 |
return -1; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
result = ma_context_get_devices(&context, &pPlaybackDeviceInfos, &playbackDeviceCount, NULL, NULL); |
| 87 |
if (result != MA_SUCCESS) { |
| 88 |
printf("Failed to enumerate playback devices."); |
| 89 |
ma_context_uninit(&context); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
engineCount = 0; |
| 96 |
|
| 97 |
for (iChosenDevice = 0; iChosenDevice < MAX_DEVICES; iChosenDevice += 1) { |
| 98 |
int c = 0; |
| 99 |
for (;;) { |
| 100 |
printf("Select playback device %d ([%d - %d], Q to quit):\n", iChosenDevice+1, 0, ma_min((int)playbackDeviceCount, 9)); |
| 101 |
|
| 102 |
for (iAvailableDevice = 0; iAvailableDevice < playbackDeviceCount; iAvailableDevice += 1) { |
| 103 |
printf(" %d: %s\n", iAvailableDevice, pPlaybackDeviceInfos[iAvailableDevice].name); |
| 104 |
} |
| 105 |
|
| 106 |
for (;;) { |
| 107 |
c = getchar(); |
| 108 |
if (c != '\n') { |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if (c == 'q' || c == 'Q') { |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
if (c >= '0' && c <= '9') { |
| 118 |
c -= '0'; |
| 119 |
|
| 120 |
if (c < (int)playbackDeviceCount) { |
| 121 |
ma_device_config deviceConfig; |
| 122 |
ma_engine_config engineConfig; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
deviceConfig = ma_device_config_init(ma_device_type_playback); |
| 133 |
deviceConfig.playback.pDeviceID = &pPlaybackDeviceInfos[c].id; |
| 134 |
deviceConfig.playback.format = resourceManager.config.decodedFormat; |
| 135 |
deviceConfig.playback.channels = 0; |
| 136 |
deviceConfig.sampleRate = resourceManager.config.decodedSampleRate; |
| 137 |
deviceConfig.dataCallback = data_callback; |
| 138 |
deviceConfig.pUserData = &engines[engineCount]; |
| 139 |
|
| 140 |
result = ma_device_init(&context, &deviceConfig, &devices[engineCount]); |
| 141 |
if (result != MA_SUCCESS) { |
| 142 |
printf("Failed to initialize device for %s.\n", pPlaybackDeviceInfos[c].name); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
engineConfig = ma_engine_config_init(); |
| 148 |
engineConfig.pDevice = &devices[engineCount]; |
| 149 |
engineConfig.pResourceManager = &resourceManager; |
| 150 |
engineConfig.noAutoStart = MA_TRUE; |
| 151 |
|
| 152 |
result = ma_engine_init(&engineConfig, &engines[engineCount]); |
| 153 |
if (result != MA_SUCCESS) { |
| 154 |
printf("Failed to initialize engine for %s.\n", pPlaybackDeviceInfos[c].name); |
| 155 |
ma_device_uninit(&devices[engineCount]); |
| 156 |
return -1; |
| 157 |
} |
| 158 |
|
| 159 |
engineCount += 1; |
| 160 |
break; |
| 161 |
} else { |
| 162 |
printf("Invalid device number.\n"); |
| 163 |
} |
| 164 |
} else { |
| 165 |
printf("Invalid device number.\n"); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
printf("Device %d: %s\n", iChosenDevice+1, pPlaybackDeviceInfos[c].name); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
for (iEngine = 0; iEngine < engineCount; iEngine += 1) { |
| 175 |
result = ma_engine_start(&engines[iEngine]); |
| 176 |
if (result != MA_SUCCESS) { |
| 177 |
printf("WARNING: Failed to start engine %d.\n", iEngine); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
soundCount = 0; |
| 189 |
for (iEngine = 0; iEngine < engineCount; iEngine += 1) { |
| 190 |
|
| 191 |
result = ma_sound_init_from_file(&engines[iEngine], argv[1], MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_DECODE | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_ASYNC | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_STREAM, NULL, NULL, &sounds[iEngine]); |
| 192 |
if (result != MA_SUCCESS) { |
| 193 |
printf("WARNING: Failed to load sound \"%s\"", argv[1]); |
| 194 |
break; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
result = ma_sound_start(&sounds[iEngine]); |
| 204 |
if (result != MA_SUCCESS) { |
| 205 |
printf("WARNING: Failed to start sound."); |
| 206 |
} |
| 207 |
|
| 208 |
soundCount += 1; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
printf("Press Enter to quit..."); |
| 213 |
getchar(); |
| 214 |
|
| 215 |
for (;;) { |
| 216 |
int c = getchar(); |
| 217 |
if (c == '\n') { |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
for (iSound = 0; iSound < soundCount; iSound += 1) { |
| 226 |
ma_sound_uninit(&sounds[iSound]); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
for (iEngine = 0; iEngine < engineCount; iEngine += 1) { |
| 231 |
ma_engine_uninit(&engines[iEngine]); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
ma_device_uninit(&devices[iEngine]); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
ma_context_uninit(&context); |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
ma_resource_manager_uninit(&resourceManager); |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |