Skip to content

Latest commit

 

History

History
154 lines (131 loc) · 4.64 KB

testaudiocapture.c

File metadata and controls

154 lines (131 loc) · 4.64 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
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
Aug 2, 2016
Aug 2, 2016
18
19
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
20
static SDL_AudioSpec spec;
Aug 3, 2016
Aug 3, 2016
21
22
static SDL_AudioDeviceID devid_in = 0;
static SDL_AudioDeviceID devid_out = 0;
23
24
25
26
27
static void
loop()
{
SDL_bool please_quit = SDL_FALSE;
Aug 3, 2016
Aug 3, 2016
28
SDL_Event e;
29
30
31
32
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
please_quit = SDL_TRUE;
Aug 3, 2016
Aug 3, 2016
33
34
35
36
37
38
39
40
41
42
43
44
45
46
} 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
50
if (SDL_GetAudioDeviceStatus(devid_in) == SDL_AUDIO_PLAYING) {
Aug 2, 2016
Aug 2, 2016
51
52
53
54
55
56
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} else {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
57
58
59
60
if (please_quit) {
/* stop playing back, quit. */
SDL_Log("Shutting down.\n");
Aug 3, 2016
Aug 3, 2016
61
62
63
64
65
66
SDL_PauseAudioDevice(devid_in, 1);
SDL_CloseAudioDevice(devid_in);
SDL_PauseAudioDevice(devid_out, 1);
SDL_CloseAudioDevice(devid_out);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
67
68
69
70
71
72
SDL_Quit();
#ifdef __EMSCRIPTEN__
emscripten_cancel_main_loop();
#endif
exit(0);
}
Aug 6, 2016
Aug 6, 2016
73
74
75
76
77
78
79
80
81
82
83
84
/* 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;
}
}
85
86
87
88
89
}
int
main(int argc, char **argv)
{
Aug 2, 2016
Aug 2, 2016
90
91
92
93
94
/* (argv[1] == NULL means "open default device.") */
const char *devname = argv[1];
int devcount;
int i;
95
96
97
98
99
100
101
102
103
/* 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
104
window = SDL_CreateWindow("testaudiocapture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
Aug 2, 2016
Aug 2, 2016
105
106
107
108
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
109
110
111
SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
Aug 2, 2016
Aug 2, 2016
112
113
114
115
116
devcount = SDL_GetNumAudioDevices(SDL_TRUE);
for (i = 0; i < devcount; i++) {
SDL_Log(" Capture device #%d: '%s'\n", i, SDL_GetAudioDeviceName(i, SDL_TRUE));
}
117
118
119
120
121
SDL_zero(spec);
spec.freq = 44100;
spec.format = AUDIO_F32SYS;
spec.channels = 1;
spec.samples = 1024;
Aug 6, 2016
Aug 6, 2016
122
spec.callback = NULL;
Aug 3, 2016
Aug 3, 2016
124
SDL_Log("Opening capture device %s%s%s...\n",
Aug 2, 2016
Aug 2, 2016
125
126
127
128
devname ? "'" : "",
devname ? devname : "[[default]]",
devname ? "'" : "");
Aug 3, 2016
Aug 3, 2016
129
130
131
132
133
134
135
136
137
138
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("Opening default playback device...\n");
devid_out = SDL_OpenAudioDevice(NULL, SDL_FALSE, &spec, &spec, 0);
if (!devid_out) {
139
140
141
142
143
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open an audio device for capture: %s!\n", SDL_GetError());
SDL_Quit();
exit(1);
}
Aug 3, 2016
Aug 3, 2016
144
SDL_Log("Ready! Hold down mouse or finger to record!\n");
145
146
147
148
149
150
151
152
153
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while (1) { loop(); SDL_Delay(16); }
#endif
return 0;
}