test/testaudiocapture.c
changeset 10246 d7ae395db9f2
parent 10244 656799817375
child 10261 e8facf18d5bd
equal deleted inserted replaced
10245:f912cd5baa5d 10246:d7ae395db9f2
    13 
    13 
    14 #ifdef __EMSCRIPTEN__
    14 #ifdef __EMSCRIPTEN__
    15 #include <emscripten/emscripten.h>
    15 #include <emscripten/emscripten.h>
    16 #endif
    16 #endif
    17 
    17 
    18 #define CAPTURE_SECONDS 5
       
    19 
       
    20 #define DO_VIDEO defined(__ANDROID__) || defined(__IPHONEOS__) || defined(__EMSCRIPTEN__)
       
    21 #if DO_VIDEO
       
    22 static SDL_Window *window = NULL;
    18 static SDL_Window *window = NULL;
    23 static SDL_Renderer *renderer = NULL;
    19 static SDL_Renderer *renderer = NULL;
    24 #endif
       
    25 
       
    26 static SDL_AudioSpec spec;
    20 static SDL_AudioSpec spec;
    27 static Uint8 *sound = NULL;     /* Pointer to wave data */
    21 static SDL_AudioDeviceID devid_in = 0;
    28 static Uint32 soundlen = 0;     /* Length of wave data */
    22 static SDL_AudioDeviceID devid_out = 0;
    29 static Uint32 processed = 0;
       
    30 static SDL_AudioDeviceID devid = 0;
       
    31 
    23 
    32 void SDLCALL
    24 void SDLCALL
    33 capture_callback(void *arg, Uint8 * stream, int len)
    25 capture_callback(void *arg, Uint8 * stream, int len)
    34 {
    26 {
    35     const int avail = (int) (soundlen - processed);
    27     SDL_QueueAudio(devid_out, stream, len);
    36     if (len > avail) {
       
    37         len = avail;
       
    38     }
       
    39 
       
    40     /*SDL_Log("CAPTURE CALLBACK: %d more bytes\n", len);*/
       
    41     SDL_memcpy(sound + processed, stream, len);
       
    42     processed += len;
       
    43 }
       
    44 
       
    45 void SDLCALL
       
    46 play_callback(void *arg, Uint8 * stream, int len)
       
    47 {
       
    48     const Uint8 *waveptr = sound + processed;
       
    49     const int avail = soundlen - processed;
       
    50     int cpy = len;
       
    51     if (cpy > avail) {
       
    52         cpy = avail;
       
    53     }
       
    54 
       
    55     /*SDL_Log("PLAY CALLBACK: %d more bytes\n", cpy);*/
       
    56     SDL_memcpy(stream, waveptr, cpy);
       
    57     processed += cpy;
       
    58 
       
    59     len -= cpy;
       
    60     if (len > 0) {
       
    61         SDL_memset(stream + cpy, spec.silence, len);
       
    62     }
       
    63 }
    28 }
    64 
    29 
    65 static void
    30 static void
    66 loop()
    31 loop()
    67 {
    32 {
       
    33     SDL_bool please_quit = SDL_FALSE;
    68     SDL_Event e;
    34     SDL_Event e;
    69     SDL_bool please_quit = SDL_FALSE;
       
    70 
    35 
    71     while (SDL_PollEvent(&e)) {
    36     while (SDL_PollEvent(&e)) {
    72         if (e.type == SDL_QUIT) {
    37         if (e.type == SDL_QUIT) {
    73             please_quit = SDL_TRUE;
    38             please_quit = SDL_TRUE;
       
    39         } else if (e.type == SDL_KEYDOWN) {
       
    40             if (e.key.keysym.sym == SDLK_ESCAPE) {
       
    41                 please_quit = SDL_TRUE;
       
    42             }
       
    43         } else if (e.type == SDL_MOUSEBUTTONDOWN) {
       
    44             if (e.button.button == 1) {
       
    45                 SDL_PauseAudioDevice(devid_out, SDL_TRUE);
       
    46                 SDL_PauseAudioDevice(devid_in, SDL_FALSE);
       
    47             }
       
    48         } else if (e.type == SDL_MOUSEBUTTONUP) {
       
    49             if (e.button.button == 1) {
       
    50                 SDL_PauseAudioDevice(devid_in, SDL_TRUE);
       
    51                 SDL_PauseAudioDevice(devid_out, SDL_FALSE);
       
    52             }
    74         }
    53         }
    75     }
    54     }
    76 
    55 
    77     #if DO_VIDEO
    56     if (SDL_GetAudioDeviceStatus(devid_in) == SDL_AUDIO_PLAYING) {
    78     if (spec.callback == capture_callback) {
       
    79         SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
    57         SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
    80     } else {
    58     } else {
    81         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    59         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    82     }
    60     }
    83     SDL_RenderClear(renderer);
    61     SDL_RenderClear(renderer);
    84     SDL_RenderPresent(renderer);
    62     SDL_RenderPresent(renderer);
    85     #endif
       
    86 
       
    87     if ((!please_quit) && (processed >= soundlen)) {
       
    88         processed = 0;
       
    89         if (spec.callback == capture_callback) {
       
    90             SDL_Log("Done recording, playing back...\n");
       
    91             SDL_PauseAudioDevice(devid, 1);
       
    92             SDL_CloseAudioDevice(devid);
       
    93 
       
    94             spec.callback = play_callback;
       
    95             devid = SDL_OpenAudioDevice(NULL, 0, &spec, &spec, 0);
       
    96             if (!devid) {
       
    97                 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback!\n");
       
    98                 SDL_Quit();
       
    99                 exit(1);
       
   100             }
       
   101 
       
   102             SDL_PauseAudioDevice(devid, 0);
       
   103         } else {
       
   104             SDL_Log("Done playing back.\n");
       
   105             please_quit = SDL_TRUE;
       
   106         }
       
   107     }
       
   108 
    63 
   109     if (please_quit) {
    64     if (please_quit) {
   110         /* stop playing back, quit. */
    65         /* stop playing back, quit. */
   111         SDL_Log("Shutting down.\n");
    66         SDL_Log("Shutting down.\n");
   112         SDL_PauseAudioDevice(devid, 1);
    67         SDL_PauseAudioDevice(devid_in, 1);
   113         SDL_CloseAudioDevice(devid);
    68         SDL_CloseAudioDevice(devid_in);
   114         SDL_free(sound);
    69         SDL_PauseAudioDevice(devid_out, 1);
   115         sound = NULL;
    70         SDL_CloseAudioDevice(devid_out);
       
    71         SDL_DestroyRenderer(renderer);
       
    72         SDL_DestroyWindow(window);
   116         SDL_Quit();
    73         SDL_Quit();
   117         #ifdef __EMSCRIPTEN__
    74         #ifdef __EMSCRIPTEN__
   118         emscripten_cancel_main_loop();
    75         emscripten_cancel_main_loop();
   119         #endif
    76         #endif
   120         exit(0);
    77         exit(0);
   136     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
    93     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
   137         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
    94         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   138         return (1);
    95         return (1);
   139     }
    96     }
   140 
    97 
   141     /* Android apparently needs a window...? */
       
   142     #if DO_VIDEO
       
   143     window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, SDL_WINDOW_FULLSCREEN_DESKTOP);
    98     window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, SDL_WINDOW_FULLSCREEN_DESKTOP);
   144     renderer = SDL_CreateRenderer(window, -1, 0);
    99     renderer = SDL_CreateRenderer(window, -1, 0);
   145     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
   100     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
   146     SDL_RenderClear(renderer);
   101     SDL_RenderClear(renderer);
   147     SDL_RenderPresent(renderer);
   102     SDL_RenderPresent(renderer);
   148     #endif
       
   149 
   103 
   150     SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   104     SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
   151 
   105 
   152     devcount = SDL_GetNumAudioDevices(SDL_TRUE);
   106     devcount = SDL_GetNumAudioDevices(SDL_TRUE);
   153     for (i = 0; i < devcount; i++) {
   107     for (i = 0; i < devcount; i++) {
   159     spec.format = AUDIO_F32SYS;
   113     spec.format = AUDIO_F32SYS;
   160     spec.channels = 1;
   114     spec.channels = 1;
   161     spec.samples = 1024;
   115     spec.samples = 1024;
   162     spec.callback = capture_callback;
   116     spec.callback = capture_callback;
   163 
   117 
   164     soundlen = spec.freq * (SDL_AUDIO_BITSIZE(spec.format) / 8) * spec.channels * CAPTURE_SECONDS;
   118     SDL_Log("Opening capture device %s%s%s...\n",
   165     sound = (Uint8 *) SDL_malloc(soundlen);
       
   166     if (!sound) {
       
   167         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
       
   168         SDL_Quit();
       
   169         return 1;
       
   170     }
       
   171 
       
   172     SDL_Log("Opening device %s%s%s...\n",
       
   173             devname ? "'" : "",
   119             devname ? "'" : "",
   174             devname ? devname : "[[default]]",
   120             devname ? devname : "[[default]]",
   175             devname ? "'" : "");
   121             devname ? "'" : "");
   176 
   122 
   177     devid = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
   123     devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
   178     if (!devid) {
   124     if (!devid_in) {
   179         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
   125         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
   180         SDL_free(sound);
       
   181         SDL_Quit();
   126         SDL_Quit();
   182         exit(1);
   127         exit(1);
   183     }
   128     }
   184 
   129 
   185     SDL_Log("Recording for %d seconds...\n", CAPTURE_SECONDS);
   130     SDL_Log("Opening default playback device...\n");
   186     SDL_PauseAudioDevice(devid, 0);
   131     spec.callback = NULL;
       
   132     devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
       
   133     if (!devid_out) {
       
   134         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
       
   135         SDL_Quit();
       
   136         exit(1);
       
   137     }
       
   138 
       
   139     SDL_Log("Ready! Hold down mouse or finger to record!\n");
   187 
   140 
   188 #ifdef __EMSCRIPTEN__
   141 #ifdef __EMSCRIPTEN__
   189     emscripten_set_main_loop(loop, 0, 1);
   142     emscripten_set_main_loop(loop, 0, 1);
   190 #else
   143 #else
   191     while (1) { loop(); SDL_Delay(16); }
   144     while (1) { loop(); SDL_Delay(16); }