Skip to content

Latest commit

 

History

History
165 lines (139 loc) · 5.06 KB

testaudiocapture.c

File metadata and controls

165 lines (139 loc) · 5.06 KB
 
Jan 2, 2017
Jan 2, 2017
2
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
3
4
5
6
7
8
9
10
11
12
13
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"
Aug 9, 2016
Aug 9, 2016
14
15
#include <stdlib.h>
16
17
18
19
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
Aug 2, 2016
Aug 2, 2016
20
21
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
22
static SDL_AudioSpec spec;
Aug 3, 2016
Aug 3, 2016
23
24
static SDL_AudioDeviceID devid_in = 0;
static SDL_AudioDeviceID devid_out = 0;
25
26
27
28
29
static void
loop()
{
SDL_bool please_quit = SDL_FALSE;
Aug 3, 2016
Aug 3, 2016
30
SDL_Event e;
31
32
33
34
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
please_quit = SDL_TRUE;
Aug 3, 2016
Aug 3, 2016
35
36
37
38
39
40
41
42
43
44
45
46
47
48
} else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_ESCAPE) {
please_quit = SDL_TRUE;
}
} else if (e.type == SDL_MOUSEBUTTONDOWN) {
if (e.button.button == 1) {
SDL_PauseAudioDevice(devid_out, SDL_TRUE);
SDL_PauseAudioDevice(devid_in, SDL_FALSE);
}
} else if (e.type == SDL_MOUSEBUTTONUP) {
if (e.button.button == 1) {
SDL_PauseAudioDevice(devid_in, SDL_TRUE);
SDL_PauseAudioDevice(devid_out, SDL_FALSE);
}
Aug 3, 2016
Aug 3, 2016
52
if (SDL_GetAudioDeviceStatus(devid_in) == SDL_AUDIO_PLAYING) {
Aug 2, 2016
Aug 2, 2016
53
54
55
56
57
58
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} else {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
59
60
61
62
if (please_quit) {
/* stop playing back, quit. */
SDL_Log("Shutting down.\n");
Aug 3, 2016
Aug 3, 2016
63
64
65
66
67
68
SDL_PauseAudioDevice(devid_in, 1);
SDL_CloseAudioDevice(devid_in);
SDL_PauseAudioDevice(devid_out, 1);
SDL_CloseAudioDevice(devid_out);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
69
70
71
72
73
74
SDL_Quit();
#ifdef __EMSCRIPTEN__
emscripten_cancel_main_loop();
#endif
exit(0);
}
Aug 6, 2016
Aug 6, 2016
75
76
77
78
79
80
81
82
83
84
85
86
/* Note that it would be easier to just have a one-line function that
calls SDL_QueueAudio() as a capture device callback, but we're
trying to test the API, so we use SDL_DequeueAudio() here. */
while (SDL_TRUE) {
Uint8 buf[1024];
const Uint32 br = SDL_DequeueAudio(devid_in, buf, sizeof (buf));
SDL_QueueAudio(devid_out, buf, br);
if (br < sizeof (buf)) {
break;
}
}
87
88
89
90
91
}
int
main(int argc, char **argv)
{
Aug 2, 2016
Aug 2, 2016
92
93
/* (argv[1] == NULL means "open default device.") */
const char *devname = argv[1];
Aug 9, 2016
Aug 9, 2016
94
SDL_AudioSpec wanted;
Aug 2, 2016
Aug 2, 2016
95
96
97
int devcount;
int i;
98
99
100
101
102
103
104
105
106
/* 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);
}
Aug 6, 2016
Aug 6, 2016
107
window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
Aug 2, 2016
Aug 2, 2016
108
109
110
111
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
112
113
114
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
Aug 2, 2016
Aug 2, 2016
115
116
117
118
119
devcount = SDL_GetNumAudioDevices(SDL_TRUE);
for (i = 0; i < devcount; i++) {
SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
}
Aug 9, 2016
Aug 9, 2016
120
121
122
123
SDL_zero(wanted);
wanted.freq = 44100;
wanted.format = AUDIO_F32SYS;
wanted.channels = 1;
Aug 13, 2016
Aug 13, 2016
124
wanted.samples = 4096;
Aug 9, 2016
Aug 9, 2016
125
126
wanted.callback = NULL;
127
SDL_zero(spec);
Aug 9, 2016
Aug 9, 2016
128
129
130
131
132
133
134
135
136
137
/* 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");
devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wanted, &spec, SDL_AUDIO_ALLOW_ANY_CHANGE);
if (!devid_out) {
Aug 30, 2016
Aug 30, 2016
138
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for playback: %s!\n", SDL_GetError());
Aug 9, 2016
Aug 9, 2016
139
140
141
SDL_Quit();
exit(1);
}
Aug 3, 2016
Aug 3, 2016
143
SDL_Log("Opening capture device %s%s%s...\n",
Aug 2, 2016
Aug 2, 2016
144
145
146
147
devname ? "'" : "",
devname ? devname : "[[default]]",
devname ? "'" : "");
Aug 3, 2016
Aug 3, 2016
148
149
150
151
152
153
154
155
devid_in = SDL_OpenAudioDevice(argv[1], SDL_TRUE, &spec, &spec, 0);
if (!devid_in) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
SDL_Quit();
exit(1);
}
SDL_Log("Ready! Hold down mouse or finger to record!\n");
156
157
158
159
160
161
162
163
164
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while (1) { loop(); SDL_Delay(16); }
#endif
return 0;
}