From d9d9901e694a7d1b1afc417bd847ecee9a26c444 Mon Sep 17 00:00:00 2001 From: David Reid Date: Wed, 25 Apr 2018 18:47:10 +1000 Subject: [PATCH] Add tests for Emscripten. --- tests/README.md | 14 +++++++- tests/mal_test_0.c | 48 +++++++++++++++++++++++++++ tests/mal_test_0_build_emscripten.bat | 1 + 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/mal_test_0_build_emscripten.bat diff --git a/tests/README.md b/tests/README.md index e35f9e4c..7af8b3ef 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,7 +1,19 @@ +Building +======== Build and run these test from this folder. Example: clear && ./mal_test_0_build && ./bin/mal_test_0 These tests load resources from hard coded paths which point to the "res" folder. These paths are based on the assumption that the current directory is where the build files -are located. \ No newline at end of file +are located. + +Emscripten +---------- +On Windows, you need to move into this directory and run emsdk_env.bat from a command +prompt using an absolute path like "C:\emsdk\emsdk_env.bat". Note that PowerShell doesn't +work for me for some reason. Then, run the relevant batch file: + + mal_test_0_build_emscripten.bat + +The output will be placed in the bin folder. \ No newline at end of file diff --git a/tests/mal_test_0.c b/tests/mal_test_0.c index c4f42d68..1b15b662 100644 --- a/tests/mal_test_0.c +++ b/tests/mal_test_0.c @@ -13,6 +13,14 @@ #define MAL_IMPLEMENTATION #include "../mini_al.h" +#ifdef __EMSCRIPTEN__ +#include +void main_loop__em() +{ +} +#endif + + mal_backend g_Backends[] = { mal_backend_wasapi, mal_backend_dsound, @@ -2209,6 +2217,7 @@ int do_backend_tests() typedef struct { mal_decoder* pDecoder; + mal_sine_wave* pSineWave; mal_event endOfPlaybackEvent; } playback_test_callback_data; @@ -2217,12 +2226,29 @@ mal_uint32 on_send__playback_test(mal_device* pDevice, mal_uint32 frameCount, vo playback_test_callback_data* pData = (playback_test_callback_data*)pDevice->pUserData; mal_assert(pData != NULL); +#if !defined(__EMSCRIPTEN__) mal_uint64 framesRead = mal_decoder_read(pData->pDecoder, frameCount, pFrames); if (framesRead == 0) { mal_event_signal(&pData->endOfPlaybackEvent); } return (mal_uint32)framesRead; +#else + if (pDevice->format == mal_format_f32) { + for (mal_uint32 iFrame = 0; iFrame < frameCount; ++iFrame) { + float sample; + mal_sine_wave_read(pData->pSineWave, 1, &sample); + + for (mal_uint32 iChannel = 0; iChannel < pDevice->channels; ++iChannel) { + ((float*)pFrames)[iFrame*pDevice->channels + iChannel] = sample; + } + } + + return frameCount; + } else { + return 0; + } +#endif } int do_playback_test(mal_backend backend) @@ -2230,11 +2256,13 @@ int do_playback_test(mal_backend backend) mal_result result = MAL_SUCCESS; mal_device device; mal_decoder decoder; + mal_sine_wave sineWave; mal_bool32 haveDevice = MAL_FALSE; mal_bool32 haveDecoder = MAL_FALSE; playback_test_callback_data callbackData; callbackData.pDecoder = &decoder; + callbackData.pSineWave = &sineWave; printf("--- %s ---\n", mal_get_backend_name(backend)); @@ -2244,6 +2272,10 @@ int do_playback_test(mal_backend backend) mal_context_config contextConfig = mal_context_config_init(on_log); mal_device_config deviceConfig = mal_device_config_init_default_playback(on_send__playback_test); + #if defined(__EMSCRIPTEN__) + deviceConfig.format = mal_format_f32; + #endif + result = mal_device_init_ex(&backend, 1, &contextConfig, mal_device_type_playback, NULL, &deviceConfig, &callbackData, &device); if (result == MAL_SUCCESS) { printf("Done\n"); @@ -2271,6 +2303,7 @@ int do_playback_test(mal_backend backend) goto done; } +#if !defined(__EMSCRIPTEN__) mal_decoder_config decoderConfig = mal_decoder_config_init(device.format, device.channels, device.sampleRate); result = mal_decoder_init_file("res/sine_s16_mono_48000.wav", &decoderConfig, &decoder); if (result == MAL_SUCCESS) { @@ -2280,6 +2313,17 @@ int do_playback_test(mal_backend backend) goto done; } haveDecoder = MAL_TRUE; +#else + result = mal_sine_wave_init(0.5f, 400, device.sampleRate, &sineWave); + if (result == MAL_SUCCESS) { + printf("Done\n"); + } else { + printf("Failed to init sine wave.\n"); + goto done; + } +#endif + + } printf(" Press Enter to start playback... "); @@ -2291,6 +2335,10 @@ int do_playback_test(mal_backend backend) goto done; } +#if defined(__EMSCRIPTEN__) + emscripten_set_main_loop(main_loop__em, 0, 1); +#endif + mal_event_wait(&callbackData.endOfPlaybackEvent); // Wait for the sound to finish. printf("Done\n"); } diff --git a/tests/mal_test_0_build_emscripten.bat b/tests/mal_test_0_build_emscripten.bat new file mode 100644 index 00000000..83a3b69a --- /dev/null +++ b/tests/mal_test_0_build_emscripten.bat @@ -0,0 +1 @@ +emcc ./mal_test_0.c -o ./bin/mal_test_0_emscripten.html \ No newline at end of file