audio: Initial bits to enable audio capture support.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
diff --git a/.hgignore b/.hgignore
index 84122f8..fc65a7f 100644
--- a/.hgignore
+++ b/.hgignore
@@ -120,6 +120,7 @@ test/testbounds
test/torturethread
test/testdisplayinfo
test/testqsort
+test/testaudiocapture
test/*.exe
test/*.dSYM
buildbot
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 05674eb..eb1dd5e 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -317,7 +317,7 @@ add_audio_device(const char *name, void *handle, SDL_AudioDeviceItem **devices,
static SDL_INLINE int
add_capture_device(const char *name, void *handle)
{
- /* !!! FIXME: add this later. SDL_assert(current_audio.impl.HasCaptureSupport);*/
+ SDL_assert(current_audio.impl.HasCaptureSupport);
return add_audio_device(name, handle, ¤t_audio.inputDevices, ¤t_audio.inputDeviceCount);
}
diff --git a/test/Makefile.in b/test/Makefile.in
index b5fbc73..68f0d3d 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -13,6 +13,7 @@ TARGETS = \
loopwavequeue$(EXE) \
testatomic$(EXE) \
testaudioinfo$(EXE) \
+ testaudiocapture$(EXE) \
testautomation$(EXE) \
testbounds$(EXE) \
testcustomcursor$(EXE) \
@@ -113,6 +114,9 @@ testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c
testaudiohotplug$(EXE): $(srcdir)/testaudiohotplug.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
+testaudiocapture$(EXE): $(srcdir)/testaudiocapture.c
+ $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
+
testatomic$(EXE): $(srcdir)/testatomic.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
diff --git a/test/testaudiocapture.c b/test/testaudiocapture.c
new file mode 100644
index 0000000..ce96c61
--- /dev/null
+++ b/test/testaudiocapture.c
@@ -0,0 +1,161 @@
+/*
+ Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
+
+ 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.
+*/
+#include "SDL.h"
+
+#ifdef __EMSCRIPTEN__
+#include <emscripten/emscripten.h>
+#endif
+
+#define CAPTURE_SECONDS 5
+
+static SDL_AudioSpec spec;
+static Uint8 *sound = NULL; /* Pointer to wave data */
+static Uint32 soundlen = 0; /* Length of wave data */
+static Uint32 processed = 0;
+static SDL_AudioDeviceID devid = 0;
+
+void SDLCALL
+capture_callback(void *arg, Uint8 * stream, int len)
+{
+ const int avail = (int) (soundlen - processed);
+ if (len > avail) {
+ len = avail;
+ }
+
+ /*SDL_Log("CAPTURE CALLBACK: %d more bytes\n", len);*/
+ SDL_memcpy(sound + processed, stream, len);
+ processed += len;
+}
+
+void SDLCALL
+play_callback(void *arg, Uint8 * stream, int len)
+{
+ const Uint8 *waveptr = sound + processed;
+ const int avail = soundlen - processed;
+ int cpy = len;
+ if (cpy > avail) {
+ cpy = avail;
+ }
+
+ /*SDL_Log("PLAY CALLBACK: %d more bytes\n", cpy);*/
+ SDL_memcpy(stream, waveptr, cpy);
+ processed += cpy;
+
+ len -= cpy;
+ if (len > 0) {
+ SDL_memset(stream + cpy, spec.silence, len);
+ }
+}
+
+static void
+loop()
+{
+ SDL_Event e;
+ SDL_bool please_quit = SDL_FALSE;
+
+ while (SDL_PollEvent(&e)) {
+ if (e.type == SDL_QUIT) {
+ please_quit = SDL_TRUE;
+ }
+ }
+
+ if ((!please_quit) && (processed >= soundlen)) {
+ processed = 0;
+ if (spec.callback == capture_callback) {
+ SDL_Log("Done recording, playing back...\n");
+ SDL_PauseAudioDevice(devid, 1);
+ SDL_CloseAudioDevice(devid);
+
+ spec.callback = play_callback;
+ devid = SDL_OpenAudioDevice(NULL, 0, &spec, &spec, 0);
+ if (!devid) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback!\n");
+ SDL_Quit();
+ exit(1);
+ }
+
+ SDL_PauseAudioDevice(devid, 0);
+ } else {
+ SDL_Log("Done playing back.\n");
+ please_quit = SDL_TRUE;
+ }
+ }
+
+ if (please_quit) {
+ /* stop playing back, quit. */
+ SDL_Log("Shutting down.\n");
+ SDL_PauseAudioDevice(devid, 1);
+ SDL_CloseAudioDevice(devid);
+ SDL_free(sound);
+ sound = NULL;
+ SDL_Quit();
+ #ifdef __EMSCRIPTEN__
+ emscripten_cancel_main_loop();
+ #endif
+ exit(0);
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ /* Enable standard application logging */
+ SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
+
+ /* 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());
+ return (1);
+ }
+
+ /* Android apparently needs a window...? */
+ #ifdef __ANDROID__
+ SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
+ #endif
+
+ SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
+
+ SDL_zero(spec);
+ spec.freq = 44100;
+ spec.format = AUDIO_F32SYS;
+ spec.channels = 1;
+ spec.samples = 1024;
+ spec.callback = capture_callback;
+
+ soundlen = spec.freq * (SDL_AUDIO_BITSIZE(spec.format) / 8) * spec.channels * CAPTURE_SECONDS;
+ sound = (Uint8 *) SDL_malloc(soundlen);
+ if (!sound) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
+ SDL_Quit();
+ return 1;
+ }
+
+ devid = SDL_OpenAudioDevice(NULL, 1, &spec, &spec, 0);
+ if (!devid) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
+ SDL_free(sound);
+ SDL_Quit();
+ exit(1);
+ }
+
+ SDL_Log("Recording for %d seconds...\n", CAPTURE_SECONDS);
+ SDL_PauseAudioDevice(devid, 0);
+
+#ifdef __EMSCRIPTEN__
+ emscripten_set_main_loop(loop, 0, 1);
+#else
+ while (1) { loop(); SDL_Delay(16); }
+#endif
+
+ return 0;
+}
+