From 971eb610c0c4b8d82cc625f05e67347a2e8f4e40 Mon Sep 17 00:00:00 2001 From: David Reid Date: Sun, 5 May 2019 19:03:26 +1000 Subject: [PATCH] Update examples to C89. --- examples/advanced_config.c | 224 ++++++++++-------- examples/build/ma_build_examples_bsd | 12 +- .../build/ma_build_examples_emscripten.bat | 2 +- examples/build/ma_build_examples_linux | 12 +- examples/build/ma_build_examples_mac | 12 +- examples/build/ma_build_examples_win32.bat | 12 +- examples/simple_capture.c | 31 +-- examples/simple_enumeration.c | 23 +- examples/simple_mixing.c | 44 ++-- examples/simple_playback.c | 16 +- examples/simple_playback_emscripten.c | 30 ++- 11 files changed, 231 insertions(+), 187 deletions(-) diff --git a/examples/advanced_config.c b/examples/advanced_config.c index 51a59bca..67b9b89d 100644 --- a/examples/advanced_config.c +++ b/examples/advanced_config.c @@ -16,7 +16,7 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin (void)pOutput; (void)pInput; (void)frameCount; - return; // Just output silence for this example. + return; /* Just output silence for this example. */ } void stop_callback(ma_device* pDevice) @@ -27,63 +27,24 @@ void stop_callback(ma_device* pDevice) int main(int argc, char** argv) { - (void)argc; - (void)argv; + ma_result result; + ma_context_config contextConfig; + ma_context context; + ma_device_config deviceConfig; + ma_device playbackDevice; + ma_device_info* pPlaybackDeviceInfos; + ma_uint32 playbackDeviceCount; + ma_device_info* pCaptureDeviceInfos; + ma_uint32 captureDeviceCount; + ma_uint32 iDevice; - // When initializing a context, you can pass in an optional configuration object that allows you to control - // context-level configuration. The ma_context_config_init() function will initialize a config object with - // common configuration settings, but you can set other members for more detailed control. - ma_context_config contextConfig = ma_context_config_init(); - contextConfig.logCallback = log_callback; - - // The priority of the worker thread can be set with the following. The default priority is - // ma_thread_priority_highest. - contextConfig.threadPriority = ma_thread_priority_normal; - - - // PulseAudio - // ---------- - - // PulseAudio allows you to set the name of the application. miniaudio exposes this through the following - // config. - contextConfig.pulse.pApplicationName = "My Application"; - - // PulseAudio also allows you to control the server you want to connect to, in which case you can specify - // it with the config below. - contextConfig.pulse.pServerName = "my_server"; - - // During initialization, PulseAudio can try to automatically start the PulseAudio daemon. This does not - // suit miniaudio's trial and error backend initialization architecture so it's disabled by default, but you - // can enable it like so: - contextConfig.pulse.tryAutoSpawn = MA_TRUE; - - - // ALSA - // ---- - - // Typically, ALSA enumerates many devices, which unfortunately is not very friendly for the end user. To - // combat this, miniaudio will include only unique card/device pairs by default. The problem with this is that - // you lose a bit of flexibility and control. Setting alsa.useVerboseDeviceEnumeration makes it so the ALSA - // backend includes all devices (and there's a lot of them!). - contextConfig.alsa.useVerboseDeviceEnumeration = MA_TRUE; - - - // JACK - // ---- - - // Like PulseAudio, JACK allows you to specify the name of your application, which you can set like so: - contextConfig.jack.pClientName = "My Application"; - - // Also like PulseAudio, you can have JACK try to automatically start using the following: - contextConfig.jack.tryStartServer = MA_TRUE; - - - - // The prioritization of backends can be controlled by the application. You need only specify the backends - // you care about. If the context cannot be initialized for any of the specified backends ma_context_init() - // will fail. + /* + The prioritization of backends can be controlled by the application. You need only specify the backends + you care about. If the context cannot be initialized for any of the specified backends ma_context_init() + will fail. + */ ma_backend backends[] = { - ma_backend_wasapi, // Higest priority. + ma_backend_wasapi, /* Higest priority. */ ma_backend_dsound, ma_backend_winmm, ma_backend_coreaudio, @@ -96,92 +57,156 @@ int main(int argc, char** argv) ma_backend_aaudio, ma_backend_opensl, ma_backend_webaudio, - ma_backend_null // Lowest priority. + ma_backend_null /* Lowest priority. */ }; - ma_context context; + + /* + When initializing a context, you can pass in an optional configuration object that allows you to control + context-level configuration. The ma_context_config_init() function will initialize a config object with + common configuration settings, but you can set other members for more detailed control. + */ + contextConfig = ma_context_config_init(); + contextConfig.logCallback = log_callback; + + /* + The priority of the worker thread can be set with the following. The default priority is + ma_thread_priority_highest. + */ + contextConfig.threadPriority = ma_thread_priority_normal; + + + /* PulseAudio */ + + /* PulseAudio allows you to set the name of the application. miniaudio exposes this through the following config. */ + contextConfig.pulse.pApplicationName = "My Application"; + + /* + PulseAudio also allows you to control the server you want to connect to, in which case you can specify + it with the config below. + */ + contextConfig.pulse.pServerName = "my_server"; + + /* + During initialization, PulseAudio can try to automatically start the PulseAudio daemon. This does not + suit miniaudio's trial and error backend initialization architecture so it's disabled by default, but you + can enable it like so: + */ + contextConfig.pulse.tryAutoSpawn = MA_TRUE; + + + /* ALSA */ + + /* + Typically, ALSA enumerates many devices, which unfortunately is not very friendly for the end user. To + combat this, miniaudio will include only unique card/device pairs by default. The problem with this is that + you lose a bit of flexibility and control. Setting alsa.useVerboseDeviceEnumeration makes it so the ALSA + backend includes all devices (and there's a lot of them!). + */ + contextConfig.alsa.useVerboseDeviceEnumeration = MA_TRUE; + + + /* JACK */ + + /* Like PulseAudio, JACK allows you to specify the name of your application, which you can set like so: */ + contextConfig.jack.pClientName = "My Application"; + + /* Also like PulseAudio, you can have JACK try to automatically start using the following: */ + contextConfig.jack.tryStartServer = MA_TRUE; + + + if (ma_context_init(backends, sizeof(backends)/sizeof(backends[0]), &contextConfig, &context) != MA_SUCCESS) { printf("Failed to initialize context."); return -2; } - // Enumerate devices. - ma_device_info* pPlaybackDeviceInfos; - ma_uint32 playbackDeviceCount; - ma_device_info* pCaptureDeviceInfos; - ma_uint32 captureDeviceCount; - ma_result result = ma_context_get_devices(&context, &pPlaybackDeviceInfos, &playbackDeviceCount, &pCaptureDeviceInfos, &captureDeviceCount); + /* Enumerate devices. */ + result = ma_context_get_devices(&context, &pPlaybackDeviceInfos, &playbackDeviceCount, &pCaptureDeviceInfos, &captureDeviceCount); if (result != MA_SUCCESS) { printf("Failed to retrieve device information.\n"); return -3; } printf("Playback Devices (%d)\n", playbackDeviceCount); - for (ma_uint32 iDevice = 0; iDevice < playbackDeviceCount; ++iDevice) { + for (iDevice = 0; iDevice < playbackDeviceCount; ++iDevice) { printf(" %u: %s\n", iDevice, pPlaybackDeviceInfos[iDevice].name); } printf("\n"); printf("Capture Devices (%d)\n", captureDeviceCount); - for (ma_uint32 iDevice = 0; iDevice < captureDeviceCount; ++iDevice) { + for (iDevice = 0; iDevice < captureDeviceCount; ++iDevice) { printf(" %u: %s\n", iDevice, pCaptureDeviceInfos[iDevice].name); } - // Open the device. - // - // Unlike context configs, device configs are required. Similar to context configs, an API exists to help you - // initialize a config object called ma_device_config_init(). - // - // When using full-duplex you may want to use a different sample format, channel count and channel map. To - // support this, the device configuration splits these into "playback" and "capture" as shown below. - ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback); + /* + Open the device. + + Unlike context configs, device configs are required. Similar to context configs, an API exists to help you + initialize a config object called ma_device_config_init(). + + When using full-duplex you may want to use a different sample format, channel count and channel map. To + support this, the device configuration splits these into "playback" and "capture" as shown below. + */ + deviceConfig = ma_device_config_init(ma_device_type_playback); deviceConfig.playback.format = ma_format_s16; deviceConfig.playback.channels = 2; deviceConfig.sampleRate = 48000; deviceConfig.dataCallback = data_callback; deviceConfig.pUserData = NULL; - // Applications can specify a callback for when a device is stopped. + /* Applications can specify a callback for when a device is stopped. */ deviceConfig.stopCallback = stop_callback; - // Applications can request exclusive control of the device using the config variable below. Note that not all - // backends support this feature, so this is actually just a hint. + /* + Applications can request exclusive control of the device using the config variable below. Note that not all + backends support this feature, so this is actually just a hint. + */ deviceConfig.playback.shareMode = ma_share_mode_exclusive; - // miniaudio allows applications to control the mapping of channels. The config below swaps the left and right - // channels. Normally in an interleaved audio stream, the left channel comes first, but we can change that - // like the following: + /* + miniaudio allows applications to control the mapping of channels. The config below swaps the left and right + channels. Normally in an interleaved audio stream, the left channel comes first, but we can change that + like the following: + */ deviceConfig.playback.channelMap[0] = MA_CHANNEL_FRONT_RIGHT; deviceConfig.playback.channelMap[1] = MA_CHANNEL_FRONT_LEFT; - // The ALSA backend has two ways of delivering data to and from a device: memory mapping and read/write. By - // default memory mapping will be used over read/write because it avoids a single point of data movement - // internally and is thus, theoretically, more efficient. In testing, however, this has been less stable than - // read/write mode so an option exists to disable it if need be. This is mainly for debugging, but is left - // here in case it might be useful for others. If you find a bug specific to mmap mode, please report it! + /* + The ALSA backend has two ways of delivering data to and from a device: memory mapping and read/write. By + default memory mapping will be used over read/write because it avoids a single point of data movement + internally and is thus, theoretically, more efficient. In testing, however, this has been less stable than + read/write mode so an option exists to disable it if need be. This is mainly for debugging, but is left + here in case it might be useful for others. If you find a bug specific to mmap mode, please report it! + */ deviceConfig.alsa.noMMap = MA_TRUE; - // This is not used in this example, but miniaudio allows you to directly control the device ID that's used - // for device selection by ma_device_init(). Below is an example for ALSA. In this example it forces - // ma_device_init() to try opening the "hw:0,0" device. This is useful for debugging in case you have - // audio glitches or whatnot with specific devices. + /* + This is not used in this example, but miniaudio allows you to directly control the device ID that's used + for device selection by ma_device_init(). Below is an example for ALSA. In this example it forces + ma_device_init() to try opening the "hw:0,0" device. This is useful for debugging in case you have + audio glitches or whatnot with specific devices. + */ #ifdef MA_SUPPORT_ALSA - ma_device_id customDeviceID; - if (context.backend == ma_backend_alsa) { - strcpy(customDeviceID.alsa, "hw:0,0"); + { + ma_device_id customDeviceID; + if (context.backend == ma_backend_alsa) { + strcpy(customDeviceID.alsa, "hw:0,0"); - // The ALSA backend also supports a miniaudio-specific format which looks like this: ":0,0". In this case, - // miniaudio will try different plugins depending on the shareMode setting. When using shared mode it will - // convert ":0,0" to "dmix:0,0"/"dsnoop:0,0". For exclusive mode (or if dmix/dsnoop fails) it will convert - // it to "hw:0,0". This is how the ALSA backend honors the shareMode hint. - strcpy(customDeviceID.alsa, ":0,0"); + /* + The ALSA backend also supports a miniaudio-specific format which looks like this: ":0,0". In this case, + miniaudio will try different plugins depending on the shareMode setting. When using shared mode it will + convert ":0,0" to "dmix:0,0"/"dsnoop:0,0". For exclusive mode (or if dmix/dsnoop fails) it will convert + it to "hw:0,0". This is how the ALSA backend honors the shareMode hint. + */ + strcpy(customDeviceID.alsa, ":0,0"); + } } #endif - ma_device playbackDevice; if (ma_device_init(&context, &deviceConfig, &playbackDevice) != MA_SUCCESS) { printf("Failed to initialize playback device.\n"); ma_context_uninit(&context); @@ -202,5 +227,8 @@ int main(int argc, char** argv) ma_context_uninit(&context); + + (void)argc; + (void)argv; return 0; } diff --git a/examples/build/ma_build_examples_bsd b/examples/build/ma_build_examples_bsd index bd54977e..493c4c77 100644 --- a/examples/build/ma_build_examples_bsd +++ b/examples/build/ma_build_examples_bsd @@ -1,6 +1,6 @@ -cc ../simple_playback.c -o ../bin/simple_playback -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../simple_capture.c -o ../bin/simple_capture -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../simple_enumeration.c -o ../bin/simple_enumeration -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../simple_mixing.c -o ../bin/simple_mixing -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../simple_playback_emscripten.c -o ../bin/simple_playback_emscripten -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../advanced_config.c -o ../bin/advanced_config -Wall -Wpedantic -std=c99 -lpthread -lm +cc ../simple_playback.c -o ../bin/simple_playback -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../simple_capture.c -o ../bin/simple_capture -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../simple_enumeration.c -o ../bin/simple_enumeration -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../simple_mixing.c -o ../bin/simple_mixing -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../simple_playback_emscripten.c -o ../bin/simple_playback_emscripten -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../advanced_config.c -o ../bin/advanced_config -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm diff --git a/examples/build/ma_build_examples_emscripten.bat b/examples/build/ma_build_examples_emscripten.bat index f3151305..bb42d5e2 100644 --- a/examples/build/ma_build_examples_emscripten.bat +++ b/examples/build/ma_build_examples_emscripten.bat @@ -1 +1 @@ -emcc ../simple_playback_emscripten.c -o ../bin/simple_playback_emscripten.html \ No newline at end of file +emcc ../simple_playback_emscripten.c -o ../bin/simple_playback_emscripten.html -std=c89 -ansi -Wall \ No newline at end of file diff --git a/examples/build/ma_build_examples_linux b/examples/build/ma_build_examples_linux index 9eb47c83..92fbde6d 100644 --- a/examples/build/ma_build_examples_linux +++ b/examples/build/ma_build_examples_linux @@ -1,6 +1,6 @@ -cc ../simple_playback.c -o ../bin/simple_playback -Wall -Wpedantic -std=c99 -lpthread -ldl -lm -cc ../simple_capture.c -o ../bin/simple_capture -Wall -Wpedantic -std=c99 -lpthread -ldl -lm -cc ../simple_enumeration.c -o ../bin/simple_enumeration -Wall -Wpedantic -std=c99 -lpthread -ldl -lm -cc ../simple_mixing.c -o ../bin/simple_mixing -Wall -Wpedantic -std=c99 -lpthread -ldl -lm -cc ../simple_playback_emscripten.c -o ../bin/simple_playback_emscripten -Wall -Wpedantic -std=c99 -lpthread -ldl -lm -cc ../advanced_config.c -o ../bin/advanced_config -Wall -Wpedantic -std=c99 -lpthread -ldl -lm \ No newline at end of file +cc ../simple_playback.c -o ../bin/simple_playback -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -ldl -lm +cc ../simple_capture.c -o ../bin/simple_capture -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -ldl -lm +cc ../simple_enumeration.c -o ../bin/simple_enumeration -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -ldl -lm +cc ../simple_mixing.c -o ../bin/simple_mixing -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -ldl -lm +cc ../simple_playback_emscripten.c -o ../bin/simple_playback_emscripten -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -ldl -lm +cc ../advanced_config.c -o ../bin/advanced_config -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -ldl -lm diff --git a/examples/build/ma_build_examples_mac b/examples/build/ma_build_examples_mac index a579110a..bc21301d 100755 --- a/examples/build/ma_build_examples_mac +++ b/examples/build/ma_build_examples_mac @@ -1,6 +1,6 @@ -cc ../simple_playback.c -o ../bin/simple_playback -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../simple_capture.c -o ../bin/simple_capture -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../simple_enumeration.c -o ../bin/simple_enumeration -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../advanced_config.c -o ../bin/advanced_config -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../simple_mixing.c -o ../bin/simple_mixing -Wall -Wpedantic -std=c99 -lpthread -lm -cc ../simple_playback_emscripten.c -o ../bin/simple_playback_emscripten -Wall -Wpedantic -std=c99 -lpthread -lm +cc ../simple_playback.c -o ../bin/simple_playback -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../simple_capture.c -o ../bin/simple_capture -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../simple_enumeration.c -o ../bin/simple_enumeration -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../advanced_config.c -o ../bin/advanced_config -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../simple_mixing.c -o ../bin/simple_mixing -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm +cc ../simple_playback_emscripten.c -o ../bin/simple_playback_emscripten -Wall -Wpedantic -std=c89 -ansi -pedantic -lpthread -lm diff --git a/examples/build/ma_build_examples_win32.bat b/examples/build/ma_build_examples_win32.bat index b09c4922..3cb2ace9 100644 --- a/examples/build/ma_build_examples_win32.bat +++ b/examples/build/ma_build_examples_win32.bat @@ -1,10 +1,12 @@ @echo off SET c_compiler=gcc SET cpp_compiler=g++ -SET options=-Wall -Wpedantic -std=c99 +SET options=-Wall -Wpedantic -std=c89 -ansi -pedantic @echo on -%c_compiler% ../simple_playback.c -o ../bin/simple_playback.exe %options% -%c_compiler% ../simple_capture.c -o ../bin/simple_capture.exe %options% -%c_compiler% ../simple_enumeration.c -o ../bin/simple_enumeration.exe %options% -%c_compiler% ../advanced_config.c -o ../bin/advanced_config.exe %options% \ No newline at end of file +%c_compiler% ../simple_playback.c -o ../bin/simple_playback.exe %options% +%c_compiler% ../simple_capture.c -o ../bin/simple_capture.exe %options% +%c_compiler% ../simple_enumeration.c -o ../bin/simple_enumeration.exe %options% +%c_compiler% ../simple_mixing.c -o ../bin/simple_mixing.exe %options% +%c_compiler% ../simple_playback_emscripten.c -o ../bin/simple_playback_emscripten.exe %options% +%c_compiler% ../advanced_config.c -o ../bin/advanced_config.exe %options% \ No newline at end of file diff --git a/examples/simple_capture.c b/examples/simple_capture.c index 724eb2a9..ced74071 100644 --- a/examples/simple_capture.c +++ b/examples/simple_capture.c @@ -1,4 +1,4 @@ -// This example simply captures data from your default microphone until you press Enter. The output is saved to the file specified on the command line. +/* This example simply captures data from your default microphone until you press Enter. The output is saved to the file specified on the command line. */ #define MINIAUDIO_IMPLEMENTATION #include "../miniaudio.h" @@ -11,45 +11,46 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) { - (void)pOutput; - drwav* pWav = (drwav*)pDevice->pUserData; ma_assert(pWav != NULL); drwav_write_pcm_frames(pWav, frameCount, pInput); + + (void)pOutput; } int main(int argc, char** argv) { + ma_result result; + drwav_data_format wavFormat; + drwav wav; + ma_device_config deviceConfig; + ma_device device; + if (argc < 2) { printf("No input file.\n"); return -1; } - ma_result result; - - drwav_data_format wavFormat; wavFormat.container = drwav_container_riff; wavFormat.format = DR_WAVE_FORMAT_IEEE_FLOAT; wavFormat.channels = 2; wavFormat.sampleRate = 44100; wavFormat.bitsPerSample = 32; - drwav wav; if (drwav_init_file_write(&wav, argv[1], &wavFormat) == DRWAV_FALSE) { printf("Failed to initialize output file.\n"); return -1; } - ma_device_config config = ma_device_config_init(ma_device_type_capture); - config.capture.format = ma_format_f32; - config.capture.channels = wavFormat.channels; - config.sampleRate = wavFormat.sampleRate; - config.dataCallback = data_callback; - config.pUserData = &wav; + deviceConfig = ma_device_config_init(ma_device_type_capture); + deviceConfig.capture.format = ma_format_f32; + deviceConfig.capture.channels = wavFormat.channels; + deviceConfig.sampleRate = wavFormat.sampleRate; + deviceConfig.dataCallback = data_callback; + deviceConfig.pUserData = &wav; - ma_device device; - result = ma_device_init(NULL, &config, &device); + result = ma_device_init(NULL, &deviceConfig, &device); if (result != MA_SUCCESS) { printf("Failed to initialize capture device.\n"); return -2; diff --git a/examples/simple_enumeration.c b/examples/simple_enumeration.c index 96281f0a..a600b61e 100644 --- a/examples/simple_enumeration.c +++ b/examples/simple_enumeration.c @@ -5,38 +5,41 @@ int main(int argc, char** argv) { - (void)argc; - (void)argv; - + ma_result result; ma_context context; + ma_device_info* pPlaybackDeviceInfos; + ma_uint32 playbackDeviceCount; + ma_device_info* pCaptureDeviceInfos; + ma_uint32 captureDeviceCount; + ma_uint32 iDevice; + if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS) { printf("Failed to initialize context.\n"); return -2; } - ma_device_info* pPlaybackDeviceInfos; - ma_uint32 playbackDeviceCount; - ma_device_info* pCaptureDeviceInfos; - ma_uint32 captureDeviceCount; - ma_result result = ma_context_get_devices(&context, &pPlaybackDeviceInfos, &playbackDeviceCount, &pCaptureDeviceInfos, &captureDeviceCount); + result = ma_context_get_devices(&context, &pPlaybackDeviceInfos, &playbackDeviceCount, &pCaptureDeviceInfos, &captureDeviceCount); if (result != MA_SUCCESS) { printf("Failed to retrieve device information.\n"); return -3; } printf("Playback Devices\n"); - for (ma_uint32 iDevice = 0; iDevice < playbackDeviceCount; ++iDevice) { + for (iDevice = 0; iDevice < playbackDeviceCount; ++iDevice) { printf(" %u: %s\n", iDevice, pPlaybackDeviceInfos[iDevice].name); } printf("\n"); printf("Capture Devices\n"); - for (ma_uint32 iDevice = 0; iDevice < captureDeviceCount; ++iDevice) { + for (iDevice = 0; iDevice < captureDeviceCount; ++iDevice) { printf(" %u: %s\n", iDevice, pCaptureDeviceInfos[iDevice].name); } ma_context_uninit(&context); + + (void)argc; + (void)argv; return 0; } diff --git a/examples/simple_mixing.c b/examples/simple_mixing.c index 8bf5acad..a77e532c 100644 --- a/examples/simple_mixing.c +++ b/examples/simple_mixing.c @@ -4,11 +4,11 @@ Example: simple_mixing file1.wav file2.flac */ #define DR_FLAC_IMPLEMENTATION -#include "../extras/dr_flac.h" // Enables FLAC decoding. +#include "../extras/dr_flac.h" /* Enables FLAC decoding. */ #define DR_MP3_IMPLEMENTATION -#include "../extras/dr_mp3.h" // Enables MP3 decoding. +#include "../extras/dr_mp3.h" /* Enables MP3 decoding. */ #define DR_WAV_IMPLEMENTATION -#include "../extras/dr_wav.h" // Enables WAV decoding. +#include "../extras/dr_wav.h" /* Enables WAV decoding. */ #define MINIAUDIO_IMPLEMENTATION #include "../miniaudio.h" @@ -30,7 +30,8 @@ ma_event g_stopEvent; /* <-- Signaled by the audio thread, waited on by the main ma_bool32 are_all_decoders_at_end() { - for (ma_uint32 iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { + ma_uint32 iDecoder; + for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { if (g_pDecodersAtEnd[iDecoder] == MA_FALSE) { return MA_FALSE; } @@ -51,6 +52,7 @@ ma_uint32 read_and_mix_pcm_frames_f32(ma_decoder* pDecoder, float* pOutputF32, m ma_uint32 totalFramesRead = 0; while (totalFramesRead < frameCount) { + ma_uint32 iSample; ma_uint32 framesReadThisIteration; ma_uint32 totalFramesRemaining = frameCount - totalFramesRead; ma_uint32 framesToReadThisIteration = tempCapInFrames; @@ -64,7 +66,7 @@ ma_uint32 read_and_mix_pcm_frames_f32(ma_decoder* pDecoder, float* pOutputF32, m } /* Mix the frames together. */ - for (ma_uint32 iSample = 0; iSample < framesReadThisIteration*CHANNEL_COUNT; ++iSample) { + for (iSample = 0; iSample < framesReadThisIteration*CHANNEL_COUNT; ++iSample) { pOutputF32[totalFramesRead*CHANNEL_COUNT + iSample] += temp[iSample]; } @@ -81,10 +83,11 @@ ma_uint32 read_and_mix_pcm_frames_f32(ma_decoder* pDecoder, float* pOutputF32, m void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) { float* pOutputF32 = (float*)pOutput; + ma_uint32 iDecoder; ma_assert(pDevice->playback.format == SAMPLE_FORMAT); /* <-- Important for this example. */ - for (ma_uint32 iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { + for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { if (!g_pDecodersAtEnd[iDecoder]) { ma_uint32 framesRead = read_and_mix_pcm_frames_f32(&g_pDecoders[iDecoder], pOutputF32, frameCount); if (framesRead < frameCount) { @@ -108,6 +111,9 @@ int main(int argc, char** argv) { ma_result result; ma_decoder_config decoderConfig; + ma_device_config deviceConfig; + ma_device device; + ma_uint32 iDecoder; if (argc < 2) { printf("No input files.\n"); @@ -120,10 +126,11 @@ int main(int argc, char** argv) /* In this example, all decoders need to have the same output format. */ decoderConfig = ma_decoder_config_init(SAMPLE_FORMAT, CHANNEL_COUNT, SAMPLE_RATE); - for (ma_uint32 iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { + for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { result = ma_decoder_init_file(argv[1+iDecoder], &decoderConfig, &g_pDecoders[iDecoder]); if (result != MA_SUCCESS) { - for (ma_uint32 iDecoder2 = 0; iDecoder2 < iDecoder; ++iDecoder2) { + ma_uint32 iDecoder2; + for (iDecoder2 = 0; iDecoder2 < iDecoder; ++iDecoder2) { ma_decoder_uninit(&g_pDecoders[iDecoder2]); } free(g_pDecoders); @@ -136,16 +143,15 @@ int main(int argc, char** argv) } /* Create only a single device. The decoders will be mixed together in the callback. In this example the data format needs to be the same as the decoders. */ - ma_device_config config = ma_device_config_init(ma_device_type_playback); - config.playback.format = SAMPLE_FORMAT; - config.playback.channels = CHANNEL_COUNT; - config.sampleRate = SAMPLE_RATE; - config.dataCallback = data_callback; - config.pUserData = NULL; + deviceConfig = ma_device_config_init(ma_device_type_playback); + deviceConfig.playback.format = SAMPLE_FORMAT; + deviceConfig.playback.channels = CHANNEL_COUNT; + deviceConfig.sampleRate = SAMPLE_RATE; + deviceConfig.dataCallback = data_callback; + deviceConfig.pUserData = NULL; - ma_device device; - if (ma_device_init(NULL, &config, &device) != MA_SUCCESS) { - for (ma_uint32 iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { + if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) { + for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { ma_decoder_uninit(&g_pDecoders[iDecoder]); } free(g_pDecoders); @@ -165,7 +171,7 @@ int main(int argc, char** argv) /* Now we start playback and wait for the audio thread to tell us to stop. */ if (ma_device_start(&device) != MA_SUCCESS) { ma_device_uninit(&device); - for (ma_uint32 iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { + for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { ma_decoder_uninit(&g_pDecoders[iDecoder]); } free(g_pDecoders); @@ -181,7 +187,7 @@ int main(int argc, char** argv) /* Getting here means the audio thread has signaled that the device should be stopped. */ ma_device_uninit(&device); - for (ma_uint32 iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { + for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { ma_decoder_uninit(&g_pDecoders[iDecoder]); } free(g_pDecoders); diff --git a/examples/simple_playback.c b/examples/simple_playback.c index 82f4cbe5..16cc68f4 100644 --- a/examples/simple_playback.c +++ b/examples/simple_playback.c @@ -26,7 +26,7 @@ int main(int argc, char** argv) { ma_result result; ma_decoder decoder; - ma_device_config config; + ma_device_config deviceConfig; ma_device device; if (argc < 2) { @@ -39,14 +39,14 @@ int main(int argc, char** argv) return -2; } - config = ma_device_config_init(ma_device_type_playback); - config.playback.format = decoder.outputFormat; - config.playback.channels = decoder.outputChannels; - config.sampleRate = decoder.outputSampleRate; - config.dataCallback = data_callback; - config.pUserData = &decoder; + deviceConfig = ma_device_config_init(ma_device_type_playback); + deviceConfig.playback.format = decoder.outputFormat; + deviceConfig.playback.channels = decoder.outputChannels; + deviceConfig.sampleRate = decoder.outputSampleRate; + deviceConfig.dataCallback = data_callback; + deviceConfig.pUserData = &decoder; - if (ma_device_init(NULL, &config, &device) != MA_SUCCESS) { + if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) { printf("Failed to open playback device.\n"); ma_decoder_uninit(&decoder); return -3; diff --git a/examples/simple_playback_emscripten.c b/examples/simple_playback_emscripten.c index f965df8c..cfd0b9cb 100644 --- a/examples/simple_playback_emscripten.c +++ b/examples/simple_playback_emscripten.c @@ -17,32 +17,34 @@ void main_loop__em() void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) { - (void)pInput; /* Unused. */ + ma_sine_wave* pSineWave; + ma_assert(pDevice->playback.channels == DEVICE_CHANNELS); - ma_sine_wave* pSineWave = (ma_sine_wave*)pDevice->pUserData; + pSineWave = (ma_sine_wave*)pDevice->pUserData; ma_assert(pSineWave != NULL); ma_sine_wave_read_f32(pSineWave, frameCount, (float*)pOutput); + + (void)pInput; /* Unused. */ } int main(int argc, char** argv) { - (void)argc; - (void)argv; - ma_sine_wave sineWave; + ma_device_config deviceConfig; + ma_device device; + ma_sine_wave_init(0.2, 400, DEVICE_SAMPLE_RATE, &sineWave); - ma_device_config config = ma_device_config_init(ma_device_type_playback); - config.playback.format = DEVICE_FORMAT; - config.playback.channels = DEVICE_CHANNELS; - config.sampleRate = DEVICE_SAMPLE_RATE; - config.dataCallback = data_callback; - config.pUserData = &sineWave; + deviceConfig = ma_device_config_init(ma_device_type_playback); + deviceConfig.playback.format = DEVICE_FORMAT; + deviceConfig.playback.channels = DEVICE_CHANNELS; + deviceConfig.sampleRate = DEVICE_SAMPLE_RATE; + deviceConfig.dataCallback = data_callback; + deviceConfig.pUserData = &sineWave; - ma_device device; - if (ma_device_init(NULL, &config, &device) != MA_SUCCESS) { + if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) { printf("Failed to open playback device.\n"); return -4; } @@ -64,5 +66,7 @@ int main(int argc, char** argv) ma_device_uninit(&device); + (void)argc; + (void)argv; return 0; }