client/tests/audio.c (35250B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <string.h> 5 #ifdef _WIN32 6 #define WIN32_LEAN_AND_MEAN 7 #include <windows.h> 8 #include <mmsystem.h> 9 static HMODULE miles; 10 #define POINTER(p) ((void *)(uintptr_t)(p)) 11 #else 12 #include "linux_audio.h" 13 #include "guest_memory.h" 14 #include "guest_heap.h" 15 #include <SDL2/SDL.h> 16 #include <sndfile.h> 17 #include <stdatomic.h> 18 #include <pthread.h> 19 #include <unistd.h> 20 #define POINTER(p) ((void *)ADDR(p)) 21 static SDL_AudioCallback device_callback; 22 static void *device_userdata; 23 static _Atomic int capture, nonzero_samples, bad_samples; 24 static _Atomic int block_audio_open, audio_open_entered; 25 #ifndef __EMSCRIPTEN__ 26 static _Atomic unsigned audio_conversions; 27 28 SDL_AudioStream *__real_SDL_NewAudioStream(SDL_AudioFormat, Uint8, int, SDL_AudioFormat, Uint8, int); 29 SDL_AudioStream *__wrap_SDL_NewAudioStream(SDL_AudioFormat source_format, Uint8 source_channels, int source_rate, 30 SDL_AudioFormat destination_format, Uint8 destination_channels, int destination_rate) 31 { 32 ++audio_conversions; 33 return __real_SDL_NewAudioStream(source_format, source_channels, source_rate, 34 destination_format, destination_channels, destination_rate); 35 } 36 #endif 37 38 /* Hold the loader inside file I/O until playback controls have been exercised. */ 39 SNDFILE *__real_sf_open(const char *, int, SF_INFO *); 40 SNDFILE *__wrap_sf_open(const char *path, int mode, SF_INFO *info) 41 { 42 if (block_audio_open) { 43 audio_open_entered = 1; 44 while (block_audio_open) SDL_Delay(1); 45 } 46 return __real_sf_open(path, mode, info); 47 } 48 49 /* Capture actual output from the public playback path and output worker. */ 50 static void capture_audio(void *userdata, Uint8 *output, int length) 51 { 52 device_callback(device_userdata, output, length); 53 if (!capture) return; 54 for (int i = 0; i < length; i += 2) { 55 int16_t sample; 56 memcpy(&sample, output + i, 2); 57 if (sample) ++nonzero_samples; 58 if (sample && sample != 8192) ++bad_samples; 59 } 60 } 61 62 #ifdef __EMSCRIPTEN__ 63 SDL_AudioDeviceID __real_audio_output_open(const SDL_AudioSpec *, SDL_AudioSpec *); 64 SDL_AudioDeviceID __wrap_audio_output_open(const SDL_AudioSpec *desired, SDL_AudioSpec *obtained) 65 #else 66 SDL_AudioDeviceID __real_SDL_OpenAudioDevice(const char *, int, const SDL_AudioSpec *, SDL_AudioSpec *, int); 67 SDL_AudioDeviceID __wrap_SDL_OpenAudioDevice(const char *name, int input, const SDL_AudioSpec *desired, 68 SDL_AudioSpec *obtained, int changes) 69 #endif 70 { 71 SDL_AudioSpec spec = *desired; 72 device_callback = spec.callback; 73 device_userdata = spec.userdata; 74 spec.callback = capture_audio; 75 spec.userdata = NULL; 76 #ifdef __EMSCRIPTEN__ 77 return __real_audio_output_open(&spec, obtained); 78 #else 79 return __real_SDL_OpenAudioDevice(name, input, &spec, obtained, changes); 80 #endif 81 } 82 #endif 83 84 static uint32_t call(const char *name, unsigned count, const uint32_t *args) 85 { 86 #ifdef _WIN32 87 FARPROC function = GetProcAddress(miles, name); 88 assert(function); 89 switch (count) { 90 case 0: return ((uint32_t (__stdcall *)(void))function)(); 91 case 1: return ((uint32_t (__stdcall *)(uint32_t))function)(args[0]); 92 case 2: return ((uint32_t (__stdcall *)(uint32_t, uint32_t))function)(args[0], args[1]); 93 case 3: return ((uint32_t (__stdcall *)(uint32_t, uint32_t, uint32_t))function)(args[0], args[1], args[2]); 94 case 4: return ((uint32_t (__stdcall *)(uint32_t, uint32_t, uint32_t, uint32_t))function)(args[0], args[1], args[2], args[3]); 95 default: abort(); 96 } 97 #else 98 g_esp = 0x5000ff00; 99 g_ebp = 0x11223344; g_ebx = 0x22334455; 100 g_esi = 0x33445566; g_edi = 0x44556677; 101 MEM32(g_esp) = RECOMP_RETADDR; 102 for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = args[i]; 103 recomp_func_t function = linux_audio_lookup("mss32.dll", name); 104 assert(function); 105 function(); 106 assert(g_esp == 0x5000ff04 + count * 4); 107 assert(g_ebp == 0x11223344 && g_ebx == 0x22334455); 108 assert(g_esi == 0x33445566 && g_edi == 0x44556677); 109 return g_eax; 110 #endif 111 } 112 113 static void file_types(uint32_t scratch) 114 { 115 const struct { unsigned format, bits, mpeg, result; } cases[] = { 116 {1, 8, 0, 1}, {1, 16, 0, 1}, {17, 4, 0, 2}, {17, 8, 0, 3}, 117 {2, 4, 0, 3}, {3, 32, 0, 3}, {85, 0, 0xfffb9064, 13}, 118 {85, 0, 0xfffd9064, 12}, {85, 0, 0xffff9064, 11}, 119 {85, 0, 0, 0} 120 }; 121 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { 122 unsigned char *bytes = POINTER(scratch); 123 memset(bytes, 0, 128); 124 memcpy(bytes, "RIFF", 4); 125 bytes[4] = 120; 126 memcpy(bytes + 8, "WAVEfmt ", 8); 127 bytes[16] = 16; 128 bytes[20] = cases[i].format; 129 bytes[22] = 1; 130 bytes[24] = 0x44; bytes[25] = 0xac; 131 bytes[28] = 0x88; bytes[29] = 0x58; bytes[30] = 1; 132 bytes[32] = 2; 133 bytes[34] = cases[i].bits; 134 memcpy(bytes + 36, "data", 4); 135 bytes[40] = 84; 136 for (unsigned j = 0; j < 4; ++j) bytes[44 + j] = cases[i].mpeg >> (24 - j * 8); 137 uint32_t args[] = {scratch, 128}; 138 139 unsigned type = call("_AIL_file_type@8", 2, args); 140 141 assert(type == cases[i].result); 142 143 /* Also recognize a raw MPEG payload without its RIFF wrapper. */ 144 if (cases[i].format == 85) { 145 args[0] = scratch + 44; args[1] = 84; 146 assert(call("_AIL_file_type@8", 2, args) == cases[i].result); 147 } 148 149 /* Skip an odd-sized RIFF metadata chunk and its alignment byte. */ 150 memmove(bytes + 22, bytes + 12, 116); 151 memcpy(bytes + 12, "JUNK", 4); 152 memset(bytes + 16, 0, 6); 153 bytes[16] = 1; 154 bytes[4] = 130; 155 args[0] = scratch; args[1] = 138; 156 assert(call("_AIL_file_type@8", 2, args) == cases[i].result); 157 } 158 uint32_t empty[] = {0, 0}; 159 assert(!call("_AIL_file_type@8", 2, empty)); 160 empty[0] = scratch; empty[1] = 7; 161 assert(!call("_AIL_file_type@8", 2, empty)); 162 puts("audio types: PCM, IMA ADPCM, other WAV, three MPEG layers and empty passed"); 163 } 164 165 static void read_files(uint32_t scratch) 166 { 167 const unsigned sizes[] = {0, 7, 32769}; 168 for (unsigned i = 0; i < 3; ++i) { 169 FILE *file = fopen("miles-file-test.tmp", "wb"); 170 assert(file); 171 for (unsigned j = 0; j < sizes[i]; ++j) assert(fputc(j & 255, file) != EOF); 172 assert(!fclose(file)); 173 strcpy(POINTER(scratch), ".\\miles-file-test.tmp"); 174 for (unsigned mode = 0; mode < (i == 2 ? 2u : 3u); ++mode) { 175 uint32_t args[] = {scratch, mode == 0 ? 0 : mode == 1 ? UINT32_MAX : scratch + 128}; 176 memset(POINTER(scratch + 124), 0xa5, 32); 177 178 uint32_t result = call("_AIL_file_read@8", 2, args); 179 180 assert(result); 181 unsigned offset = mode == 1 ? 4 : 0; 182 if (mode == 1) assert(*(uint32_t *)POINTER(result) == sizes[i]); 183 for (unsigned j = 0; j < sizes[i]; ++j) 184 assert(((unsigned char *)POINTER(result))[offset + j] == (j & 255)); 185 if (mode == 2) { 186 assert(result == scratch + 128); 187 assert(*(uint32_t *)POINTER(scratch + 124) == 0xa5a5a5a5); 188 assert(((unsigned char *)POINTER(result))[sizes[i]] == 0xa5); 189 } else call("_AIL_mem_free_lock@4", 1, &result); 190 } 191 assert(!remove("miles-file-test.tmp")); 192 } 193 194 uint32_t args[] = {scratch, 0}; 195 196 uint32_t result = call("_AIL_file_read@8", 2, args); 197 198 assert(!result); 199 puts("audio files: allocated, length-prefixed, caller buffer, empty, multi-chunk and missing passed"); 200 } 201 202 static void redist_paths(uint32_t input) 203 { 204 const char *paths[] = {NULL, "", ".", "C:", "C:\\ao", "C:\\ao\\", "audio/"}; 205 const char *expected[] = {"", "", ".\\", "C:", "C:\\ao\\", "C:\\ao\\", "audio/\\"}; 206 uint32_t address = 0; 207 for (unsigned i = 0; i < 7; ++i) { 208 if (paths[i]) strcpy(POINTER(input), paths[i]); 209 uint32_t argument = paths[i] ? input : 0; 210 211 uint32_t result = call("_AIL_set_redist_directory@4", 1, &argument); 212 213 assert(result && !strcmp(POINTER(result), expected[i])); 214 if (i) assert(result == address); 215 if (paths[i]) assert(!strcmp(POINTER(input), paths[i])); 216 address = result; 217 } 218 } 219 220 static void driver_lifetime(uint32_t scratch) 221 { 222 for (unsigned i = 0; i < 4; ++i) { 223 uint8_t *bytes = POINTER(scratch); 224 uint16_t format[] = {1, i & 1 ? 2 : 1, 0, 0, 0, 0, 0, i & 2 ? 16 : 8}; 225 uint32_t rate = i & 1 ? 44100 : 22050; 226 uint32_t bandwidth = rate * format[1] * format[7] / 8; 227 format[6] = format[1] * format[7] / 8; 228 memcpy(bytes + 16, format, sizeof(format)); 229 memcpy(bytes + 20, &rate, 4); 230 memcpy(bytes + 24, &bandwidth, 4); 231 memset(bytes, 0xa5, 12); 232 uint32_t args[] = {scratch + 4, 0, UINT32_MAX, scratch + 16}; 233 234 uint32_t result = call("_AIL_waveOutOpen@16", 4, args); 235 uint32_t driver; 236 memcpy(&driver, bytes + 4, 4); 237 238 assert(result == 0 && driver && driver != 0xa5a5a5a5); 239 assert(bytes[0] == 0xa5 && bytes[3] == 0xa5 && bytes[8] == 0xa5 && bytes[11] == 0xa5); 240 uint32_t latency = call("_AIL_digital_latency@4", 1, &driver); 241 assert(latency > 0 && latency < 1000); 242 assert(call("_AIL_digital_latency@4", 1, &driver) == latency); 243 #ifdef __EMSCRIPTEN__ 244 assert(latency == 11); 245 #elif !defined(_WIN32) 246 assert(latency == (i & 1 ? 11 : 23)); 247 #endif 248 uint32_t volume[] = {driver, i & 1 ? 127 : 0}; 249 call("_AIL_set_digital_master_volume@8", 2, volume); 250 if (i == 3) { 251 call("_AIL_shutdown@0", 0, NULL); 252 assert(call("_AIL_startup@0", 0, NULL) == 1); 253 } else call("_AIL_waveOutClose@4", 1, &driver); 254 } 255 } 256 257 static void rejected_formats(uint32_t scratch) 258 { 259 for (unsigned i = 0; i < 3; ++i) { 260 uint8_t *bytes = POINTER(scratch); 261 uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; 262 if (i == 0) format[0] = 2; 263 if (i == 1) format[1] = 3; 264 if (i == 2) format[7] = 24; 265 memcpy(bytes + 16, format, sizeof(format)); 266 memset(bytes, 0xa5, 12); 267 uint32_t args[] = {scratch + 4, 0, UINT32_MAX, scratch + 16}; 268 269 uint32_t result = call("_AIL_waveOutOpen@16", 4, args); 270 271 assert(result == 1); 272 for (unsigned j = 0; j < 12; ++j) assert(bytes[j] == 0xa5); 273 } 274 } 275 276 static void quick_shutdown(uint32_t scratch) 277 { 278 uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; 279 memcpy(POINTER(scratch + 16), format, sizeof(format)); 280 uint32_t open[] = {scratch, 0, UINT32_MAX, scratch + 16}; 281 assert(!call("_AIL_waveOutOpen@16", 4, open)); 282 uint32_t driver = *(uint32_t *)POINTER(scratch); 283 uint32_t latency = call("_AIL_digital_latency@4", 1, &driver); 284 285 call("_AIL_quick_shutdown@0", 0, NULL); 286 287 assert(call("_AIL_startup@0", 0, NULL) == 0); 288 assert(call("_AIL_digital_latency@4", 1, &driver) == latency); 289 uint32_t sample = call("_AIL_allocate_sample_handle@4", 1, &driver); 290 assert(sample && call("_AIL_sample_status@4", 1, &sample) == 2); 291 call("_AIL_release_sample_handle@4", 1, &sample); 292 call("_AIL_waveOutClose@4", 1, &driver); 293 puts("audio quick shutdown: preserves manually opened driver and startup state"); 294 } 295 296 static void empty_wave(uint32_t scratch) 297 { 298 const unsigned char wave[] = { 299 'R','I','F','F', 52,0,0,0, 'W','A','V','E', 300 'f','m','t',' ', 20,0,0,0, 17,0,1,0, 0x44,0xac,0,0, 301 0x6d,0x56,0,0, 0,4,4,0, 2,0,0xf9,7, 302 'f','a','c','t', 4,0,0,0, 0,0,0,0, 'd','a','t','a', 0,0,0,0 303 }; 304 memcpy(POINTER(scratch + 64), wave, sizeof(wave)); 305 uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; 306 memcpy(POINTER(scratch + 16), format, sizeof(format)); 307 uint32_t open[] = {scratch, 0, UINT32_MAX, scratch + 16}; 308 assert(!call("_AIL_waveOutOpen@16", 4, open)); 309 uint32_t driver = *(uint32_t *)POINTER(scratch); 310 uint32_t sample = call("_AIL_allocate_sample_handle@4", 1, &driver); 311 assert(sample); 312 uint32_t load[] = {sample, scratch + 64, 1}; 313 314 uint32_t loaded = call("_AIL_set_sample_file@12", 3, load); 315 316 assert(!loaded); 317 assert(call("_AIL_sample_status@4", 1, &sample) == 2); 318 assert(call("_AIL_sample_playback_rate@4", 1, &sample) == 11025); 319 call("_AIL_start_sample@4", 1, &sample); 320 assert(call("_AIL_sample_status@4", 1, &sample) == 2); 321 call("_AIL_release_sample_handle@4", 1, &sample); 322 323 FILE *file = fopen("miles-empty-test.wav", "wb"); 324 assert(file); 325 assert(fwrite(wave, 1, sizeof(wave), file) == sizeof(wave)); 326 assert(!fclose(file)); 327 strcpy(POINTER(scratch + 64), "miles-empty-test.wav"); 328 uint32_t stream_open[] = {driver, scratch + 64, 0}; 329 330 uint32_t stream = call("_AIL_open_stream@12", 3, stream_open); 331 332 assert(stream && call("_AIL_stream_status@4", 1, &stream) == 2); 333 call("_AIL_start_stream@4", 1, &stream); 334 for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) { 335 #ifdef _WIN32 336 Sleep(1); 337 #else 338 SDL_Delay(1); 339 #endif 340 } 341 assert(call("_AIL_stream_status@4", 1, &stream) == 2); 342 call("_AIL_close_stream@4", 1, &stream); 343 call("_AIL_waveOutClose@4", 1, &driver); 344 assert(!remove("miles-empty-test.wav")); 345 puts("audio empty IMA WAV: sample remains uninitialized; stream opens and completes"); 346 } 347 348 static void sample_handles(uint32_t scratch) 349 { 350 uint32_t preference[] = {1, 4}; 351 uint32_t previous = call("_AIL_set_preference@8", 2, preference); 352 uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; 353 memcpy(POINTER(scratch + 16), format, sizeof(format)); 354 uint32_t open[] = {scratch, 0, UINT32_MAX, scratch + 16}; 355 assert(!call("_AIL_waveOutOpen@16", 4, open)); 356 uint32_t driver = *(uint32_t *)POINTER(scratch), handles[4]; 357 358 for (unsigned i = 0; i < 4; ++i) handles[i] = call("_AIL_allocate_sample_handle@4", 1, &driver); 359 360 for (unsigned i = 0; i < 4; ++i) { 361 assert(handles[i]); 362 for (unsigned j = 0; j < i; ++j) assert(handles[i] != handles[j]); 363 assert(call("_AIL_sample_status@4", 1, &handles[i]) == 2); 364 assert(call("_AIL_sample_playback_rate@4", 1, &handles[i]) == 11025); 365 } 366 assert(!call("_AIL_allocate_sample_handle@4", 1, &driver)); 367 call("_AIL_release_sample_handle@4", 1, &handles[1]); 368 assert(call("_AIL_sample_status@4", 1, &handles[1]) == 1); 369 assert(call("_AIL_allocate_sample_handle@4", 1, &driver) == handles[1]); 370 uint32_t empty[] = {handles[0], 0, 1}; 371 assert(!call("_AIL_set_sample_file@12", 3, empty)); 372 call("_AIL_start_sample@4", 1, handles); 373 assert(call("_AIL_sample_status@4", 1, handles) == 2); 374 375 const unsigned char wave[] = { 376 'R','I','F','F', 40,0,0,0, 'W','A','V','E', 377 'f','m','t',' ', 16,0,0,0, 1,0,1,0, 0x40,0x1f,0,0, 378 0x80,0x3e,0,0, 2,0,16,0, 'd','a','t','a', 4,0,0,0, 0,32,0,32 379 }; 380 memcpy(POINTER(scratch + 64), wave, sizeof(wave)); 381 uint32_t file[] = {handles[0], scratch + 64, 1}; 382 383 assert(call("_AIL_set_sample_file@12", 3, file) == 1); 384 uint32_t loops[] = {handles[0], 0}; 385 call("_AIL_set_sample_loop_count@8", 2, loops); 386 call("_AIL_start_sample@4", 1, handles); 387 388 assert(call("_AIL_sample_playback_rate@4", 1, handles) == 8000); 389 assert(call("_AIL_sample_status@4", 1, handles) == 4); 390 call("_AIL_end_sample@4", 1, handles); 391 assert(call("_AIL_sample_status@4", 1, handles) == 2); 392 uint32_t rate[] = {handles[0], 22050}; 393 call("_AIL_set_sample_playback_rate@8", 2, rate); 394 assert(call("_AIL_sample_playback_rate@4", 1, handles) == 22050); 395 call("_AIL_init_sample@4", 1, handles); 396 assert(call("_AIL_sample_playback_rate@4", 1, handles) == 11025); 397 call("_AIL_start_sample@4", 1, handles); 398 assert(call("_AIL_sample_status@4", 1, handles) == 2); 399 for (unsigned i = 0; i < 4; ++i) call("_AIL_release_sample_handle@4", 1, &handles[i]); 400 call("_AIL_waveOutClose@4", 1, &driver); 401 preference[1] = previous; 402 call("_AIL_set_preference@8", 2, preference); 403 puts("audio samples: capacity, reuse, defaults, file, start/end, looping and rate passed"); 404 } 405 406 static void adpcm_sample(uint32_t scratch) 407 { 408 uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; 409 memcpy(POINTER(scratch + 16), format, sizeof(format)); 410 uint32_t open[] = {scratch, 0, UINT32_MAX, scratch + 16}; 411 assert(!call("_AIL_waveOutOpen@16", 4, open)); 412 uint32_t driver = *(uint32_t *)POINTER(scratch); 413 uint32_t sample = call("_AIL_allocate_sample_handle@4", 1, &driver); 414 assert(sample); 415 const unsigned char header[] = { 416 'R','I','F','F', 0x34,1,0,0, 'W','A','V','E', 417 'f','m','t',' ', 20,0,0,0, 17,0,1,0, 0x44,0xac,0,0, 418 0x53,0x57,0,0, 0,1,4,0, 2,0,0xf9,1, 419 'f','a','c','t', 4,0,0,0, 0xf9,1,0,0, 420 'd','a','t','a', 0,1,0,0 421 }; 422 unsigned char *wave = POINTER(scratch + 64); 423 memset(wave, 0, 316); 424 memcpy(wave, header, sizeof(header)); 425 wave[61] = 32; /* Predictor 8192, index zero, all zero nibbles: 505 constant samples. */ 426 uint32_t file[] = {sample, scratch + 64, 1}; 427 assert(call("_AIL_set_sample_file@12", 3, file) == 1); 428 #ifndef _WIN32 429 nonzero_samples = bad_samples = 0; 430 capture = 1; 431 #endif 432 433 call("_AIL_start_sample@4", 1, &sample); 434 for (unsigned i = 0; call("_AIL_sample_status@4", 1, &sample) == 4 && i < 1000; ++i) { 435 #ifdef _WIN32 436 Sleep(1); 437 #else 438 SDL_Delay(1); 439 #endif 440 } 441 442 assert(call("_AIL_sample_status@4", 1, &sample) == 2); 443 assert(call("_AIL_sample_playback_rate@4", 1, &sample) == 44100); 444 #ifndef _WIN32 445 capture = 0; 446 assert(nonzero_samples == 1010 && !bad_samples); 447 #endif 448 call("_AIL_release_sample_handle@4", 1, &sample); 449 call("_AIL_waveOutClose@4", 1, &driver); 450 puts("audio ADPCM: one 505-frame IMA block decodes and completes playback"); 451 } 452 453 static void stream_seek(uint32_t scratch) 454 { 455 FILE *file = fopen("miles-seek-test.wav", "wb"); 456 assert(file); 457 const unsigned char header[] = { 458 'R','I','F','F', 0x34,0xb1,2,0, 'W','A','V','E', 459 'f','m','t',' ', 16,0,0,0, 1,0,1,0, 0x44,0xac,0,0, 460 0x88,0x58,1,0, 2,0,16,0, 'd','a','t','a', 0x10,0xb1,2,0 461 }; 462 assert(fwrite(header, 1, sizeof(header), file) == sizeof(header)); 463 for (unsigned i = 0; i < 176400; ++i) assert(fputc(0, file) != EOF); 464 assert(!fclose(file)); 465 uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; 466 memcpy(POINTER(scratch + 16), format, sizeof(format)); 467 uint32_t args[] = {scratch, 0, UINT32_MAX, scratch + 16}; 468 assert(!call("_AIL_waveOutOpen@16", 4, args)); 469 uint32_t driver = *(uint32_t *)POINTER(scratch); 470 strcpy(POINTER(scratch + 64), "miles-seek-test.wav"); 471 uint32_t open[] = {driver, scratch + 64, 0}; 472 uint32_t stream = call("_AIL_open_stream@12", 3, open); 473 assert(stream); 474 const unsigned positions[] = {0, 1, 9, 250, 1999, 2000, 2500, 0}; 475 for (unsigned i = 0; i < 8; ++i) { 476 uint32_t seek[] = {stream, positions[i]}; 477 call("_AIL_set_stream_ms_position@8", 2, seek); 478 uint32_t position[] = {stream, scratch + 128, scratch + 132}; 479 call("_AIL_stream_ms_position@12", 3, position); 480 481 assert(*(uint32_t *)POINTER(scratch + 132) == 0); 482 assert(*(uint32_t *)POINTER(scratch + 128) == 2000); 483 assert(call("_AIL_stream_status@4", 1, &stream) == 2); 484 } 485 call("_AIL_close_stream@4", 1, &stream); 486 call("_AIL_waveOutClose@4", 1, &driver); 487 assert(!remove("miles-seek-test.wav")); 488 puts("audio seek: pending seeks preserve the stopped playback cursor"); 489 } 490 491 #ifndef _WIN32 492 static void *open_blocked_stream(void *argument) 493 { 494 const uint32_t *args = argument; 495 g_esp = 0x5000f000; 496 MEM32(g_esp + 4) = args[0]; 497 MEM32(g_esp + 8) = args[1]; 498 MEM32(g_esp + 12) = 0; 499 linux_audio_lookup("mss32.dll", "_AIL_open_stream@12")(); 500 return (void *)(uintptr_t)g_eax; 501 } 502 503 static void retained_music_driver(uint32_t scratch) 504 { 505 uint32_t open[] = {scratch, 0, UINT32_MAX, scratch + 16}; 506 assert(!call("_AIL_waveOutOpen@16", 4, open)); 507 uint32_t driver = MEM32(scratch); 508 linux_audio_retain_driver(driver); 509 uint32_t stream_open[] = {driver, scratch + 64, 0}; 510 uint32_t stream = call("_AIL_open_stream@12", 3, stream_open); 511 assert(stream); 512 uint32_t stale_generation = linux_audio_mute_music(driver, 0); 513 linux_audio_mute_music(driver, 1); 514 uint32_t generation = linux_audio_mute_music(driver, 0); 515 linux_audio_enable_music(driver, stale_generation); 516 nonzero_samples = bad_samples = 0; 517 capture = 1; 518 519 call("_AIL_start_stream@4", 1, &stream); 520 SDL_Delay(30); 521 522 assert(!nonzero_samples); 523 linux_audio_enable_music(driver, generation); 524 /* Restart the short clip so this check does not depend on output queue depth. */ 525 call("_AIL_start_stream@4", 1, &stream); 526 for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1); 527 assert(nonzero_samples > 0 && !bad_samples); 528 capture = 0; 529 530 call("_AIL_waveOutClose@4", 1, &driver); 531 call("_AIL_shutdown@0", 0, NULL); 532 533 assert(call("_AIL_stream_status@4", 1, &stream) == 2); 534 call("_AIL_close_stream@4", 1, &stream); 535 linux_audio_release_driver(driver); 536 assert(!call("_AIL_waveOutOpen@16", 4, open)); 537 driver = MEM32(scratch); 538 call("_AIL_waveOutClose@4", 1, &driver); 539 puts("Music driver: immediate mute, retained shutdown, worker release and reopening passed"); 540 } 541 542 static void asynchronous_sound(uint32_t scratch) 543 { 544 char path[] = "/tmp/ao-async-audio-XXXXXX"; 545 int descriptor = mkstemp(path); 546 assert(descriptor >= 0); 547 SF_INFO info = {.samplerate = 44100, .channels = 1, .format = SF_FORMAT_WAV | SF_FORMAT_PCM_16}; 548 SNDFILE *file = sf_open_fd(descriptor, SFM_WRITE, &info, 1); 549 assert(file); 550 float samples[4410]; 551 for (unsigned i = 0; i < 4410; ++i) samples[i] = 0.25f; 552 assert(sf_writef_float(file, samples, 4410) == 4410 && !sf_close(file)); 553 uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; 554 memcpy(POINTER(scratch + 16), format, sizeof(format)); 555 uint32_t args[] = {scratch, 0, UINT32_MAX, scratch + 16}; 556 assert(!call("_AIL_waveOutOpen@16", 4, args)); 557 uint32_t driver = MEM32(scratch); 558 uint32_t voice = call("_AIL_allocate_sample_handle@4", 1, &driver); 559 strcpy(POINTER(scratch + 64), path); 560 MEM32(scratch + 512 + 12) = driver; 561 MEM8(scratch + 512 + 16) = 1; 562 block_audio_open = 1; 563 audio_open_entered = 0; 564 565 g_ecx = scratch + 512; 566 g_esp = 0x5000ff00; 567 MEM32(g_esp + 4) = scratch + 128; 568 MEM32(g_esp + 8) = scratch + 64; 569 MEM32(g_esp + 12) = 42; 570 linux_audio_load_sound(); 571 uint32_t sound = MEM32(scratch + 128); 572 assert(g_esp == 0x5000ff10 && sound && MEM8(sound + 12) == 42); 573 for (unsigned i = 0; !audio_open_entered && i < 1000; ++i) SDL_Delay(1); 574 uint32_t sample_file[] = {voice, MEM32(sound + 4), 1}; 575 assert(call("_AIL_set_sample_file@12", 3, sample_file)); 576 nonzero_samples = bad_samples = 0; 577 capture = 1; 578 call("_AIL_start_sample@4", 1, &voice); 579 SDL_Delay(30); 580 581 assert(audio_open_entered && !nonzero_samples); 582 assert(call("_AIL_sample_status@4", 1, &voice) == 4); 583 assert(call("_AIL_sample_playback_rate@4", 1, &voice) == 44100); 584 call("_AIL_end_sample@4", 1, &voice); 585 assert(call("_AIL_sample_status@4", 1, &voice) == 2); 586 call("_AIL_release_sample_handle@4", 1, &voice); 587 uint32_t allocation = MEM32(sound + 8); 588 call("_AIL_mem_free_lock@4", 1, &allocation); 589 guest_free(sound); 590 assert(call("_AIL_allocate_sample_handle@4", 1, &driver) == voice); 591 592 /* Reuse the stopped voice while its previous asset is still being loaded. */ 593 g_ecx = scratch + 512; 594 g_esp = 0x5000ff00; 595 MEM32(g_esp + 4) = scratch + 128; 596 MEM32(g_esp + 8) = scratch + 64; 597 MEM32(g_esp + 12) = 0; 598 linux_audio_load_sound(); 599 sound = MEM32(scratch + 128); 600 sample_file[1] = MEM32(sound + 4); 601 assert(call("_AIL_set_sample_file@12", 3, sample_file)); 602 uint32_t rate[] = {voice, 22050}, loops[] = {voice, 2}; 603 call("_AIL_set_sample_playback_rate@8", 2, rate); 604 call("_AIL_set_sample_loop_count@8", 2, loops); 605 call("_AIL_start_sample@4", 1, &voice); 606 block_audio_open = 0; 607 for (unsigned i = 0; call("_AIL_sample_status@4", 1, &voice) == 4 && i < 2000; ++i) SDL_Delay(1); 608 assert(call("_AIL_sample_status@4", 1, &voice) == 2); 609 assert(nonzero_samples == 35280 && !bad_samples); 610 assert(call("_AIL_sample_playback_rate@4", 1, &voice) == 22050); 611 612 /* Freeing the guest cache entry leaves a playing voice's decoded data alive. */ 613 allocation = MEM32(sound + 8); 614 call("_AIL_mem_free_lock@4", 1, &allocation); 615 guest_free(sound); 616 nonzero_samples = 0; 617 call("_AIL_start_sample@4", 1, &voice); 618 for (unsigned i = 0; call("_AIL_sample_status@4", 1, &voice) == 4 && i < 1000; ++i) SDL_Delay(1); 619 assert(nonzero_samples == 17640 && !bad_samples); 620 capture = 0; 621 call("_AIL_release_sample_handle@4", 1, &voice); 622 623 /* A music load also cannot hold the common playback lock during file I/O. */ 624 block_audio_open = 1; 625 audio_open_entered = 0; 626 uint32_t open[] = {driver, scratch + 64}; 627 pthread_t music; 628 assert(!pthread_create(&music, NULL, open_blocked_stream, open)); 629 for (unsigned i = 0; !audio_open_entered && i < 1000; ++i) SDL_Delay(1); 630 assert(audio_open_entered); 631 voice = call("_AIL_allocate_sample_handle@4", 1, &driver); 632 assert(voice && call("_AIL_sample_status@4", 1, &voice) == 2); 633 call("_AIL_waveOutClose@4", 1, &driver); 634 block_audio_open = 0; 635 void *result; 636 assert(!pthread_join(music, &result) && !result); 637 retained_music_driver(scratch); 638 assert(!unlink(path)); 639 fprintf(stderr, "Async sound: blocked I/O, immediate stop, voice reuse, playback and cache ownership passed.\n"); 640 } 641 642 static void failed_sound(uint32_t scratch) 643 { 644 uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; 645 memcpy(POINTER(scratch + 16), format, sizeof(format)); 646 uint32_t args[] = {scratch, 0, UINT32_MAX, scratch + 16}; 647 assert(!call("_AIL_waveOutOpen@16", 4, args)); 648 uint32_t driver = MEM32(scratch); 649 uint32_t voice = call("_AIL_allocate_sample_handle@4", 1, &driver); 650 strcpy(POINTER(scratch + 64), "/no-such-ao-audio-test.wav"); 651 MEM32(scratch + 512 + 12) = driver; 652 MEM8(scratch + 512 + 16) = 1; 653 654 g_ecx = scratch + 512; 655 g_esp = 0x5000ff00; 656 MEM32(g_esp + 4) = scratch + 128; 657 MEM32(g_esp + 8) = scratch + 64; 658 MEM32(g_esp + 12) = 0; 659 linux_audio_load_sound(); 660 uint32_t sound = MEM32(scratch + 128); 661 uint32_t sample_file[] = {voice, MEM32(sound + 4), 1}; 662 assert(call("_AIL_set_sample_file@12", 3, sample_file)); 663 call("_AIL_start_sample@4", 1, &voice); 664 for (unsigned i = 0; call("_AIL_sample_status@4", 1, &voice) == 4 && i < 1000; ++i) SDL_Delay(1); 665 666 assert(call("_AIL_sample_status@4", 1, &voice) == 2); 667 call("_AIL_start_sample@4", 1, &voice); 668 assert(call("_AIL_sample_status@4", 1, &voice) == 2); 669 call("_AIL_waveOutClose@4", 1, &driver); 670 uint32_t allocation = MEM32(sound + 8); 671 call("_AIL_mem_free_lock@4", 1, &allocation); 672 guest_free(sound); 673 fprintf(stderr, "Async sound: missing file completes without leaving a voice busy.\n"); 674 } 675 676 static void stream_playback(uint32_t scratch) 677 { 678 char path[] = "/tmp/ao-audio-XXXXXX"; 679 int descriptor = mkstemp(path); 680 assert(descriptor >= 0); 681 SF_INFO info = {.samplerate = 44100, .channels = 2, .format = SF_FORMAT_WAV | SF_FORMAT_PCM_16}; 682 SNDFILE *file = sf_open_fd(descriptor, SFM_WRITE, &info, 1); 683 assert(file); 684 float samples[8820]; 685 for (unsigned i = 0; i < 8820; ++i) samples[i] = 0.25f; 686 assert(sf_writef_float(file, samples, 4410) == 4410 && !sf_close(file)); 687 uint16_t format[] = {1, 2, 44100, 0, 45328, 2, 4, 16}; 688 memcpy(POINTER(scratch + 16), format, sizeof(format)); 689 uint32_t args[] = {scratch, 0, UINT32_MAX, scratch + 16}; 690 assert(!call("_AIL_waveOutOpen@16", 4, args)); 691 uint32_t driver = MEM32(scratch); 692 strcpy(POINTER(scratch + 64), path); 693 uint32_t open[] = {driver, scratch + 64, 0}; 694 #ifndef __EMSCRIPTEN__ 695 unsigned conversions_before = audio_conversions; 696 #endif 697 698 uint32_t stream = call("_AIL_open_stream@12", 3, open); 699 assert(stream && call("_AIL_stream_status@4", 1, &stream) == 2); 700 memset(POINTER(scratch + 128), 0xa5, 16); 701 uint32_t position[] = {stream, scratch + 132, scratch + 136}; 702 call("_AIL_stream_ms_position@12", 3, position); 703 assert(MEM32(scratch + 132) == 100 && MEM32(scratch + 136) == 0); 704 nonzero_samples = bad_samples = 0; 705 capture = 1; 706 call("_AIL_start_stream@4", 1, &stream); 707 assert(call("_AIL_stream_status@4", 1, &stream) == 4); 708 for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1); 709 710 assert(call("_AIL_stream_status@4", 1, &stream) == 2); 711 assert(nonzero_samples == 8820 && !bad_samples); 712 #ifndef __EMSCRIPTEN__ 713 assert(audio_conversions == conversions_before); 714 #endif 715 call("_AIL_stream_ms_position@12", 3, position); 716 assert(MEM32(scratch + 132) == 100 && MEM32(scratch + 136) == 100); 717 assert(MEM32(scratch + 128) == 0xa5a5a5a5 && MEM32(scratch + 140) == 0xa5a5a5a5); 718 capture = 0; 719 720 /* Pause retains the current position; unpausing completes the remaining audio. */ 721 call("_AIL_start_stream@4", 1, &stream); 722 uint32_t pause[] = {stream, 1}; 723 call("_AIL_pause_stream@8", 2, pause); 724 call("_AIL_stream_ms_position@12", 3, position); 725 uint32_t paused_position = MEM32(scratch + 136); 726 SDL_Delay(30); 727 assert(call("_AIL_stream_status@4", 1, &stream) == 8); 728 call("_AIL_stream_ms_position@12", 3, position); 729 assert(MEM32(scratch + 136) == paused_position); 730 pause[1] = 0; 731 call("_AIL_pause_stream@8", 2, pause); 732 for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1); 733 assert(call("_AIL_stream_status@4", 1, &stream) == 2); 734 735 uint32_t seek[] = {stream, 50}; 736 nonzero_samples = bad_samples = 0; 737 738 call("_AIL_set_stream_ms_position@8", 2, seek); 739 capture = 1; 740 call("_AIL_start_stream@4", 1, &stream); 741 for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1); 742 capture = 0; 743 744 assert(call("_AIL_stream_status@4", 1, &stream) == 2); 745 assert(nonzero_samples == 4410 && !bad_samples); 746 call("_AIL_stream_ms_position@12", 3, position); 747 assert(MEM32(scratch + 136) == 100); 748 749 /* A muted stream still advances to completion while producing only silence. */ 750 uint32_t volume[] = {stream, 0}; 751 call("_AIL_set_stream_volume@8", 2, volume); 752 nonzero_samples = bad_samples = 0; 753 capture = 1; 754 call("_AIL_start_stream@4", 1, &stream); 755 for (unsigned i = 0; call("_AIL_stream_status@4", 1, &stream) == 4 && i < 1000; ++i) SDL_Delay(1); 756 assert(call("_AIL_stream_status@4", 1, &stream) == 2); 757 assert(!nonzero_samples && !bad_samples); 758 capture = 0; 759 call("_AIL_close_stream@4", 1, &stream); 760 761 /* Compare actual mixed PCM counts for single, repeated, half-rate and muted samples. */ 762 uint32_t read[] = {scratch + 64, 0}; 763 uint32_t wave = call("_AIL_file_read@8", 2, read); 764 assert(wave); 765 uint32_t sample = call("_AIL_allocate_sample_handle@4", 1, &driver); 766 assert(sample); 767 for (unsigned mode = 0; mode < 4; ++mode) { 768 call("_AIL_init_sample@4", 1, &sample); 769 uint32_t file_args[] = {sample, wave, 1}; 770 assert(call("_AIL_set_sample_file@12", 3, file_args)); 771 uint32_t settings[] = {sample, mode == 1 ? 2 : 1}; 772 call("_AIL_set_sample_loop_count@8", 2, settings); 773 settings[1] = mode == 2 ? 22050 : 44100; 774 call("_AIL_set_sample_playback_rate@8", 2, settings); 775 settings[1] = mode == 3 ? 0 : 127; 776 call("_AIL_set_sample_volume@8", 2, settings); 777 nonzero_samples = bad_samples = 0; 778 779 capture = 1; 780 call("_AIL_start_sample@4", 1, &sample); 781 for (unsigned i = 0; call("_AIL_sample_status@4", 1, &sample) == 4 && i < 1000; ++i) SDL_Delay(1); 782 capture = 0; 783 784 assert(call("_AIL_sample_status@4", 1, &sample) == 2); 785 const unsigned expected[] = {8820, 17640, 17640, 0}; 786 assert(nonzero_samples == expected[mode] && !bad_samples); 787 } 788 call("_AIL_release_sample_handle@4", 1, &sample); 789 call("_AIL_mem_free_lock@4", 1, &wave); 790 call("_AIL_waveOutClose@4", 1, &driver); 791 assert(!unlink(path)); 792 fprintf(stderr, "Audio output: 8820 PCM samples verified, pause/resume, restart, mute and stream cleanup passed.\n"); 793 } 794 #endif 795 796 int main(int argc, char **argv) 797 { 798 #ifdef _WIN32 799 assert(argc == 2); 800 miles = LoadLibraryA(argv[1]); 801 assert(miles); 802 uint8_t scratch[1024]; 803 uint32_t address = (uint32_t)(uintptr_t)scratch; 804 unsigned devices = waveOutGetNumDevs(); 805 #else 806 assert(guest_map("audio test", 0x50000000, 0x10000)); 807 uint32_t address = 0x50000100; 808 g_esp = 0x5000ff00; 809 linux_audio_lookup("winmm.dll", "waveOutGetNumDevs")(); 810 assert(g_eax > 0 && g_eax == (unsigned)SDL_GetNumAudioDevices(0)); 811 assert(g_esp == 0x5000ff04); 812 #endif 813 file_types(address); 814 read_files(address); 815 redist_paths(address); 816 assert(call("_AIL_startup@0", 0, NULL) == 1); 817 assert(call("_AIL_startup@0", 0, NULL) == 0); 818 uint32_t null_driver = 0; 819 assert(call("_AIL_digital_latency@4", 1, &null_driver) == 0); 820 uint32_t null_stream[] = {0, 0, 0}; 821 assert(!call("_AIL_open_stream@12", 3, null_stream)); 822 assert(call("_AIL_stream_status@4", 1, null_stream) == UINT32_MAX); 823 memset(POINTER(address + 128), 0xa5, 16); 824 uint32_t null_position[] = {0, address + 132, address + 136}; 825 call("_AIL_stream_ms_position@12", 3, null_position); 826 for (unsigned i = 128; i < 144; ++i) assert(((unsigned char *)POINTER(address))[i] == 0xa5); 827 uint32_t preference[] = {1, 16}; 828 assert(call("_AIL_set_preference@8", 2, preference) == 64); 829 preference[1] = 32; 830 assert(call("_AIL_set_preference@8", 2, preference) == 16); 831 rejected_formats(address); 832 #ifdef _WIN32 833 if (!devices) { 834 fprintf(stderr, "Windows reports no wave output device; device opening is not compared.\n"); 835 call("_AIL_shutdown@0", 0, NULL); 836 assert(call("_AIL_startup@0", 0, NULL) == 1); 837 } else { 838 driver_lifetime(address); 839 quick_shutdown(address); 840 empty_wave(address); 841 sample_handles(address); 842 adpcm_sample(address); 843 stream_seek(address); 844 } 845 #else 846 driver_lifetime(address); 847 quick_shutdown(address); 848 empty_wave(address); 849 sample_handles(address); 850 adpcm_sample(address); 851 stream_seek(address); 852 stream_playback(address); 853 asynchronous_sound(address); 854 failed_sound(address); 855 fprintf(stderr, "Audio output: device enumeration and 4 PCM format open/close/shutdown cases passed.\n"); 856 #endif 857 assert(call("_AIL_set_preference@8", 2, preference) == 64); 858 call("_AIL_shutdown@0", 0, NULL); 859 #ifdef _WIN32 860 FreeLibrary(miles); 861 #endif 862 puts("audio: 7 directory cases, startup/reset, preferences and invalid PCM formats passed"); 863 }