Skip to content

Latest commit

 

History

History
149 lines (126 loc) · 4.38 KB

testaudiocapture.c

File metadata and controls

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