test/testaudiocapture.c
changeset 10254 2b8bb76140a5
parent 10245 ec1844380edb
child 10267 b1f031450499
equal deleted inserted replaced
10253:4a1faf81a620 10254:2b8bb76140a5
     8   Permission is granted to anyone to use this software for any purpose,
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    10   freely.
    11 */
    11 */
    12 #include "SDL.h"
    12 #include "SDL.h"
       
    13 
       
    14 #include <stdlib.h>
    13 
    15 
    14 #ifdef __EMSCRIPTEN__
    16 #ifdef __EMSCRIPTEN__
    15 #include <emscripten/emscripten.h>
    17 #include <emscripten/emscripten.h>
    16 #endif
    18 #endif
    17 
    19 
    87 int
    89 int
    88 main(int argc, char **argv)
    90 main(int argc, char **argv)
    89 {
    91 {
    90     /* (argv[1] == NULL means "open default device.") */
    92     /* (argv[1] == NULL means "open default device.") */
    91     const char *devname = argv[1];
    93     const char *devname = argv[1];
       
    94     SDL_AudioSpec wanted;
    92     int devcount;
    95     int devcount;
    93     int i;
    96     int i;
    94 
    97 
    95     /* Enable standard application logging */
    98     /* Enable standard application logging */
    96     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    99     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   112     devcount = SDL_GetNumAudioDevices(SDL_TRUE);
   115     devcount = SDL_GetNumAudioDevices(SDL_TRUE);
   113     for (i = 0; i < devcount; i++) {
   116     for (i = 0; i < devcount; i++) {
   114         SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
   117         SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
   115     }
   118     }
   116 
   119 
       
   120     SDL_zero(wanted);
       
   121     wanted.freq = 44100;
       
   122     wanted.format = AUDIO_F32SYS;
       
   123     wanted.channels = 1;
       
   124     wanted.samples = 1024;
       
   125     wanted.callback = NULL;
       
   126 
   117     SDL_zero(spec);
   127     SDL_zero(spec);
   118     spec.freq = 44100;
   128 
   119     spec.format = AUDIO_F32SYS;
   129     /* DirectSound can fail in some instances if you open the same hardware
   120     spec.channels = 1;
   130        for both capture and output and didn't open the output end first,
   121     spec.samples = 1024;
   131        according to the docs, so if you're doing something like this, always
   122     spec.callback = NULL;
   132        open your capture devices second in case you land in those bizarre
       
   133        circumstances. */
       
   134 
       
   135     SDL_Log("Opening default playback device...\n");
       
   136     devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wanted, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
       
   137     if (!devid_out) {
       
   138         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
       
   139         SDL_Quit();
       
   140         exit(1);
       
   141     }
   123 
   142 
   124     SDL_Log("Opening capture device %s%s%s...\n",
   143     SDL_Log("Opening capture device %s%s%s...\n",
   125             devname ? "'" : "",
   144             devname ? "'" : "",
   126             devname ? devname : "[[default]]",
   145             devname ? devname : "[[default]]",
   127             devname ? "'" : "");
   146             devname ? "'" : "");
   128 
   147 
   129     devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
   148     devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
   130     if (!devid_in) {
   149     if (!devid_in) {
   131         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
       
   132         SDL_Quit();
       
   133         exit(1);
       
   134     }
       
   135 
       
   136     SDL_Log("Opening default playback device...\n");
       
   137     devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
       
   138     if (!devid_out) {
       
   139         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
   150         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
   140         SDL_Quit();
   151         SDL_Quit();
   141         exit(1);
   152         exit(1);
   142     }
   153     }
   143 
   154