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 102722b8bb76140a5
parent 10271 4a1faf81a620
child 10273 77a266c9c786
testaudiocapture: open capture device to same spec as output device.

...since our resampler is still terrible (sorry!).
test/testaudiocapture.c
     1.1 --- a/test/testaudiocapture.c	Tue Aug 09 00:44:59 2016 -0400
     1.2 +++ b/test/testaudiocapture.c	Tue Aug 09 16:57:49 2016 -0400
     1.3 @@ -11,6 +11,8 @@
     1.4  */
     1.5  #include "SDL.h"
     1.6  
     1.7 +#include <stdlib.h>
     1.8 +
     1.9  #ifdef __EMSCRIPTEN__
    1.10  #include <emscripten/emscripten.h>
    1.11  #endif
    1.12 @@ -89,6 +91,7 @@
    1.13  {
    1.14      /* (argv[1] == NULL means "open default device.") */
    1.15      const char *devname = argv[1];
    1.16 +    SDL_AudioSpec wanted;
    1.17      int devcount;
    1.18      int i;
    1.19  
    1.20 @@ -114,12 +117,28 @@
    1.21          SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
    1.22      }
    1.23  
    1.24 +    SDL_zero(wanted);
    1.25 +    wanted.freq = 44100;
    1.26 +    wanted.format = AUDIO_F32SYS;
    1.27 +    wanted.channels = 1;
    1.28 +    wanted.samples = 1024;
    1.29 +    wanted.callback = NULL;
    1.30 +
    1.31      SDL_zero(spec);
    1.32 -    spec.freq = 44100;
    1.33 -    spec.format = AUDIO_F32SYS;
    1.34 -    spec.channels = 1;
    1.35 -    spec.samples = 1024;
    1.36 -    spec.callback = NULL;
    1.37 +
    1.38 +    /* DirectSound can fail in some instances if you open the same hardware
    1.39 +       for both capture and output and didn't open the output end first,
    1.40 +       according to the docs, so if you're doing something like this, always
    1.41 +       open your capture devices second in case you land in those bizarre
    1.42 +       circumstances. */
    1.43 +
    1.44 +    SDL_Log("Opening default playback device...\n");
    1.45 +    devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wanted, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
    1.46 +    if (!devid_out) {
    1.47 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
    1.48 +        SDL_Quit();
    1.49 +        exit(1);
    1.50 +    }
    1.51  
    1.52      SDL_Log("Opening capture device %s%s%s...\n",
    1.53              devname ? "'" : "",
    1.54 @@ -133,14 +152,6 @@
    1.55          exit(1);
    1.56      }
    1.57  
    1.58 -    SDL_Log("Opening default playback device...\n");
    1.59 -    devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
    1.60 -    if (!devid_out) {
    1.61 -        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
    1.62 -        SDL_Quit();
    1.63 -        exit(1);
    1.64 -    }
    1.65 -
    1.66      SDL_Log("Ready! Hold down mouse or finger to record!\n");
    1.67  
    1.68  #ifdef __EMSCRIPTEN__