testaudiocapture: open capture device to same spec as output device.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 09 Aug 2016 16:57:49 -0400
changeset 10254 2b8bb76140a5
parent 10253 4a1faf81a620
child 10255 77a266c9c786
testaudiocapture: open capture device to same spec as output device. ...since our resampler is still terrible (sorry!).
test/testaudiocapture.c
--- a/test/testaudiocapture.c	Tue Aug 09 00:44:59 2016 -0400
+++ b/test/testaudiocapture.c	Tue Aug 09 16:57:49 2016 -0400
@@ -11,6 +11,8 @@
 */
 #include "SDL.h"
 
+#include <stdlib.h>
+
 #ifdef __EMSCRIPTEN__
 #include <emscripten/emscripten.h>
 #endif
@@ -89,6 +91,7 @@
 {
     /* (argv[1] == NULL means "open default device.") */
     const char *devname = argv[1];
+    SDL_AudioSpec wanted;
     int devcount;
     int i;
 
@@ -114,12 +117,28 @@
         SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
     }
 
+    SDL_zero(wanted);
+    wanted.freq = 44100;
+    wanted.format = AUDIO_F32SYS;
+    wanted.channels = 1;
+    wanted.samples = 1024;
+    wanted.callback = NULL;
+
     SDL_zero(spec);
-    spec.freq = 44100;
-    spec.format = AUDIO_F32SYS;
-    spec.channels = 1;
-    spec.samples = 1024;
-    spec.callback = NULL;
+
+    /* DirectSound can fail in some instances if you open the same hardware
+       for both capture and output and didn't open the output end first,
+       according to the docs, so if you're doing something like this, always
+       open your capture devices second in case you land in those bizarre
+       circumstances. */
+
+    SDL_Log("Opening default playback device...\n");
+    devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wanted, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
+    if (!devid_out) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
+        SDL_Quit();
+        exit(1);
+    }
 
     SDL_Log("Opening capture device %s%s%s...\n",
             devname ? "'" : "",
@@ -133,14 +152,6 @@
         exit(1);
     }
 
-    SDL_Log("Opening default playback device...\n");
-    devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
-    if (!devid_out) {
-        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
-        SDL_Quit();
-        exit(1);
-    }
-
     SDL_Log("Ready! Hold down mouse or finger to record!\n");
 
 #ifdef __EMSCRIPTEN__