2016-07-31 22:18:56 -06:00
|
|
|
/*
|
2023-01-09 10:41:41 -07:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2016-07-31 22:18:56 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
2016-08-09 14:57:49 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-16 17:25:39 -06:00
|
|
|
#include <SDL3/SDL_test.h>
|
2022-11-26 02:41:46 -07:00
|
|
|
|
2016-07-31 22:18:56 -06:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2016-08-02 17:17:51 -06:00
|
|
|
static SDL_Window *window = NULL;
|
|
|
|
static SDL_Renderer *renderer = NULL;
|
2023-06-24 12:57:12 -06:00
|
|
|
static SDL_AudioStream *stream_in = NULL;
|
|
|
|
static SDL_AudioStream *stream_out = NULL;
|
2023-04-12 03:42:27 -06:00
|
|
|
static int done = 0;
|
2016-07-31 22:18:56 -06:00
|
|
|
|
2023-03-08 08:12:45 -07:00
|
|
|
static void loop(void)
|
2016-07-31 22:18:56 -06:00
|
|
|
{
|
2023-08-27 14:34:26 -06:00
|
|
|
const SDL_AudioDeviceID devid_in = SDL_GetAudioStreamDevice(stream_in);
|
|
|
|
const SDL_AudioDeviceID devid_out = SDL_GetAudioStreamDevice(stream_out);
|
2016-07-31 22:18:56 -06:00
|
|
|
SDL_bool please_quit = SDL_FALSE;
|
2016-08-02 22:31:08 -06:00
|
|
|
SDL_Event e;
|
2016-07-31 22:18:56 -06:00
|
|
|
|
|
|
|
while (SDL_PollEvent(&e)) {
|
2023-01-23 18:54:09 -07:00
|
|
|
if (e.type == SDL_EVENT_QUIT) {
|
2016-07-31 22:18:56 -06:00
|
|
|
please_quit = SDL_TRUE;
|
2023-01-23 18:54:09 -07:00
|
|
|
} else if (e.type == SDL_EVENT_KEY_DOWN) {
|
2016-08-02 22:31:08 -06:00
|
|
|
if (e.key.keysym.sym == SDLK_ESCAPE) {
|
|
|
|
please_quit = SDL_TRUE;
|
|
|
|
}
|
2023-01-29 20:06:08 -07:00
|
|
|
} else if (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
2016-08-02 22:31:08 -06:00
|
|
|
if (e.button.button == 1) {
|
2023-01-06 05:59:45 -07:00
|
|
|
SDL_PauseAudioDevice(devid_out);
|
2023-08-05 17:20:14 -06:00
|
|
|
SDL_ResumeAudioDevice(devid_in);
|
2016-08-02 22:31:08 -06:00
|
|
|
}
|
2023-01-29 20:06:08 -07:00
|
|
|
} else if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) {
|
2016-08-02 22:31:08 -06:00
|
|
|
if (e.button.button == 1) {
|
2023-01-06 05:59:45 -07:00
|
|
|
SDL_PauseAudioDevice(devid_in);
|
2023-06-24 12:57:12 -06:00
|
|
|
SDL_FlushAudioStream(stream_in); /* so no samples are held back for resampling purposes. */
|
2023-08-05 17:20:14 -06:00
|
|
|
SDL_ResumeAudioDevice(devid_out);
|
2016-08-02 22:31:08 -06:00
|
|
|
}
|
2016-07-31 22:18:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 12:57:12 -06:00
|
|
|
if (!SDL_IsAudioDevicePaused(devid_in)) {
|
2016-08-02 17:17:51 -06:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
|
|
|
|
} else {
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
|
|
|
}
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_RenderPresent(renderer);
|
2016-07-31 22:18:56 -06:00
|
|
|
|
2023-06-24 12:57:12 -06:00
|
|
|
/* Feed any new data we captured to the output stream. It'll play when we unpause the device. */
|
|
|
|
while (!please_quit && (SDL_GetAudioStreamAvailable(stream_in) > 0)) {
|
|
|
|
Uint8 buf[1024];
|
|
|
|
const int br = SDL_GetAudioStreamData(stream_in, buf, sizeof(buf));
|
|
|
|
if (br < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to read from input audio stream: %s\n", SDL_GetError());
|
|
|
|
please_quit = 1;
|
|
|
|
} else if (SDL_PutAudioStreamData(stream_out, buf, br) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to write to output audio stream: %s\n", SDL_GetError());
|
|
|
|
please_quit = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-31 22:18:56 -06:00
|
|
|
if (please_quit) {
|
|
|
|
/* stop playing back, quit. */
|
|
|
|
SDL_Log("Shutting down.\n");
|
2016-08-02 22:31:08 -06:00
|
|
|
SDL_CloseAudioDevice(devid_in);
|
|
|
|
SDL_CloseAudioDevice(devid_out);
|
2023-06-24 12:57:12 -06:00
|
|
|
SDL_DestroyAudioStream(stream_in);
|
|
|
|
SDL_DestroyAudioStream(stream_out);
|
2016-08-02 22:31:08 -06:00
|
|
|
SDL_DestroyRenderer(renderer);
|
|
|
|
SDL_DestroyWindow(window);
|
2016-07-31 22:18:56 -06:00
|
|
|
SDL_Quit();
|
2022-11-30 13:51:59 -07:00
|
|
|
#ifdef __EMSCRIPTEN__
|
2016-07-31 22:18:56 -06:00
|
|
|
emscripten_cancel_main_loop();
|
2022-11-30 13:51:59 -07:00
|
|
|
#endif
|
2023-04-12 03:42:27 -06:00
|
|
|
/* Let 'main()' return normally */
|
|
|
|
done = 1;
|
|
|
|
return;
|
2016-07-31 22:18:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char **argv)
|
2016-07-31 22:18:56 -06:00
|
|
|
{
|
2023-06-24 12:57:12 -06:00
|
|
|
SDL_AudioDeviceID *devices;
|
|
|
|
SDLTest_CommonState *state;
|
2023-08-11 14:52:23 -06:00
|
|
|
SDL_AudioSpec outspec;
|
|
|
|
SDL_AudioSpec inspec;
|
2023-06-24 12:57:12 -06:00
|
|
|
SDL_AudioDeviceID device;
|
|
|
|
SDL_AudioDeviceID want_device = SDL_AUDIO_DEVICE_DEFAULT_CAPTURE;
|
2023-03-16 17:25:39 -06:00
|
|
|
const char *devname = NULL;
|
2016-08-02 11:38:56 -06:00
|
|
|
int i;
|
2023-03-16 17:25:39 -06:00
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
|
|
|
if (state == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-02 11:38:56 -06:00
|
|
|
|
2016-07-31 22:18:56 -06:00
|
|
|
/* Enable standard application logging */
|
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
/* Parse commandline */
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (!consumed) {
|
|
|
|
if (!devname) {
|
|
|
|
devname = argv[i];
|
|
|
|
consumed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed <= 0) {
|
2023-06-24 12:57:12 -06:00
|
|
|
static const char *options[] = { "[device_name]", NULL };
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
i += consumed;
|
|
|
|
}
|
|
|
|
|
2016-07-31 22:18:56 -06:00
|
|
|
/* Load the SDL library */
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
2022-11-27 09:38:43 -07:00
|
|
|
return 1;
|
2016-07-31 22:18:56 -06:00
|
|
|
}
|
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
if (SDL_CreateWindowAndRenderer(320, 240, 0, &window, &renderer) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window and renderer: %s\n", SDL_GetError());
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-02 17:17:51 -06:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_RenderPresent(renderer);
|
2016-07-31 22:18:56 -06:00
|
|
|
|
|
|
|
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
|
|
|
|
|
2023-06-24 12:57:12 -06:00
|
|
|
devices = SDL_GetAudioCaptureDevices(NULL);
|
|
|
|
for (i = 0; devices[i] != 0; i++) {
|
|
|
|
char *name = SDL_GetAudioDeviceName(devices[i]);
|
|
|
|
SDL_Log(" Capture device #%d: '%s'\n", i, name);
|
|
|
|
if (devname && (SDL_strcmp(devname, name) == 0)) {
|
|
|
|
want_device = devices[i];
|
|
|
|
}
|
|
|
|
SDL_free(name);
|
2016-08-02 11:38:56 -06:00
|
|
|
}
|
|
|
|
|
2023-06-24 12:57:12 -06:00
|
|
|
if (devname && (want_device == SDL_AUDIO_DEVICE_DEFAULT_CAPTURE)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Didn't see a capture device named '%s', using the system default instead.\n", devname);
|
|
|
|
devname = NULL;
|
|
|
|
}
|
2016-08-09 14:57:49 -06:00
|
|
|
|
|
|
|
/* 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");
|
2023-06-24 12:57:12 -06:00
|
|
|
device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, NULL);
|
|
|
|
if (!device) {
|
2016-08-30 13:16:04 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback: %s!\n", SDL_GetError());
|
2016-08-09 14:57:49 -06:00
|
|
|
SDL_Quit();
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-06-24 12:57:12 -06:00
|
|
|
SDL_PauseAudioDevice(device);
|
2023-08-11 14:52:23 -06:00
|
|
|
SDL_GetAudioDeviceFormat(device, &outspec);
|
2023-08-27 11:32:33 -06:00
|
|
|
stream_out = SDL_CreateAudioStream(&outspec, &outspec);
|
2023-06-24 12:57:12 -06:00
|
|
|
if (!stream_out) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create an audio stream for playback: %s!\n", SDL_GetError());
|
|
|
|
SDL_Quit();
|
|
|
|
exit(1);
|
2023-08-27 11:32:33 -06:00
|
|
|
} else if (SDL_BindAudioStream(device, stream_out) == -1) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't bind an audio stream for playback: %s!\n", SDL_GetError());
|
|
|
|
SDL_Quit();
|
|
|
|
exit(1);
|
2023-06-24 12:57:12 -06:00
|
|
|
}
|
2016-07-31 22:18:56 -06:00
|
|
|
|
2016-08-02 22:31:08 -06:00
|
|
|
SDL_Log("Opening capture device %s%s%s...\n",
|
2016-08-02 11:38:56 -06:00
|
|
|
devname ? "'" : "",
|
|
|
|
devname ? devname : "[[default]]",
|
|
|
|
devname ? "'" : "");
|
|
|
|
|
2023-06-24 12:57:12 -06:00
|
|
|
device = SDL_OpenAudioDevice(want_device, NULL);
|
|
|
|
if (!device) {
|
2016-08-02 22:31:08 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
|
|
|
|
SDL_Quit();
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-06-24 12:57:12 -06:00
|
|
|
SDL_PauseAudioDevice(device);
|
2023-08-11 14:52:23 -06:00
|
|
|
SDL_GetAudioDeviceFormat(device, &inspec);
|
2023-08-27 11:32:33 -06:00
|
|
|
stream_in = SDL_CreateAudioStream(&inspec, &inspec);
|
2023-06-24 12:57:12 -06:00
|
|
|
if (!stream_in) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create an audio stream for capture: %s!\n", SDL_GetError());
|
|
|
|
SDL_Quit();
|
|
|
|
exit(1);
|
2023-08-27 11:32:33 -06:00
|
|
|
} else if (SDL_BindAudioStream(device, stream_in) == -1) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't bind an audio stream for capture: %s!\n", SDL_GetError());
|
|
|
|
SDL_Quit();
|
|
|
|
exit(1);
|
2023-06-24 12:57:12 -06:00
|
|
|
}
|
2016-08-02 22:31:08 -06:00
|
|
|
|
2023-08-11 14:52:23 -06:00
|
|
|
SDL_SetAudioStreamFormat(stream_in, NULL, &outspec); /* make sure we output at the playback format. */
|
|
|
|
|
2016-08-02 22:31:08 -06:00
|
|
|
SDL_Log("Ready! Hold down mouse or finger to record!\n");
|
2016-07-31 22:18:56 -06:00
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
2023-04-12 03:42:27 -06:00
|
|
|
while (!done) {
|
2022-11-30 13:51:59 -07:00
|
|
|
loop();
|
2023-04-12 03:42:27 -06:00
|
|
|
if (!done) {
|
|
|
|
SDL_Delay(16);
|
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
}
|
2016-07-31 22:18:56 -06:00
|
|
|
#endif
|
2023-04-12 03:56:10 -06:00
|
|
|
|
2023-06-24 12:57:12 -06:00
|
|
|
SDLTest_CommonDestroyState(state);
|
2023-04-12 03:56:10 -06:00
|
|
|
|
|
|
|
return 0;
|
2016-07-31 22:18:56 -06:00
|
|
|
}
|