-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinput_wavein.cpp
More file actions
336 lines (242 loc) · 9.97 KB
/
input_wavein.cpp
File metadata and controls
336 lines (242 loc) · 9.97 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* Libvisual-plugins - Standard plugins for libvisual
*
* Copyright (C) 2012 Chong Kai Xiong <kaixiong@codeleft.sg>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "gettext.h"
#include <libvisual/libvisual.h>
#include <windows.h>
VISUAL_PLUGIN_API_VERSION_VALIDATOR
#define PCM_BUFFER_SIZE 4096
struct WaveInPrivate {
HWAVEIN device_handle;
bool loaded;
WAVEHDR buffer_header;
int16_t buffers[2][PCM_BUFFER_SIZE];
bool buffer_ready[2];
int active_buffer;
HANDLE mutex;
};
namespace {
int inp_wavein_init (VisPluginData *plugin);
int inp_wavein_cleanup (VisPluginData *plugin);
int inp_wavein_events (VisPluginData *plugin, VisEventQueue* events);
int inp_wavein_upload (VisPluginData *plugin, VisAudio* audio);
} // anonymous namespace
VisPluginInfo const* get_plugin_info ()
{
static VisInputPlugin input = {
{ 0 },
inp_wavein_upload
};
static VisPluginInfo info = {
{ 0 },
VISUAL_PLUGIN_TYPE_INPUT,
"wavein",
"Wave/In input",
"Chong Kai Xiong <kaixiong@codeleft.sg>",
"0.1",
N_("Wave/In capture plugin"),
N_("Use this plugin to capture PCM data on Windows through the Wave/In API"),
VISUAL_PLUGIN_LICENSE_LGPL,
inp_wavein_init,
inp_wavein_cleanup,
inp_wavein_events,
0,
VISUAL_OBJECT (&input)
};
return &info;
}
namespace {
void log_wavein_error (std::string const& message, MMRESULT error)
{
char error_text[MAXERRORLENGTH];
waveInGetErrorText (error, error_text, MAXERRORLENGTH);
visual_log (VISUAL_LOG_ERROR, "%s: %s", message.c_str (), error_text);
}
void prepare_buffer (WaveInPrivate* priv, unsigned int buffer_id)
{
priv->buffer_ready[buffer_id] = false;
priv->buffer_header.lpData = reinterpret_cast<LPSTR> (priv->buffers[buffer_id]);
priv->buffer_header.dwBufferLength = PCM_BUFFER_SIZE * sizeof (int16_t);
waveInPrepareHeader (priv->device_handle, &priv->buffer_header, sizeof (WAVEHDR));
waveInAddBuffer (priv->device_handle, &priv->buffer_header, sizeof (WAVEHDR));
}
void CALLBACK handle_wavein_message (HWAVEIN device, UINT msg, DWORD_PTR instance, DWORD_PTR param1, DWORD_PTR param2)
{
DWORD thread_id = DWORD (instance);
// Forward the message to our processing thread
PostThreadMessage (thread_id, msg, WPARAM (device), LPARAM (param1));
}
DWORD WINAPI process_buffers (LPVOID param)
{
WaveInPrivate* priv = reinterpret_cast<WaveInPrivate*> (param);
MSG message;
while (true) {
if (PeekMessage (&message, HWND (-1), 0, 0, PM_REMOVE)) {
switch (message.message) {
case WIM_OPEN:
// Nothing to do
break;
case WIM_CLOSE:
// Exit if device is closed. We use return instead of ExitThread() so
// that C++ destructors can run
return 0;
case WIM_DATA:
WaitForSingleObject (priv->mutex, INFINITE);
waveInUnprepareHeader (priv->device_handle, &priv->buffer_header, sizeof (WAVEHDR));
priv->buffer_ready[priv->active_buffer] = true;
priv->active_buffer ^= 0x1;
prepare_buffer (priv, priv->active_buffer);
ReleaseMutex (priv->mutex);
break;
}
}
SwitchToThread ();
}
}
bool check_available_devices ()
{
visual_log (VISUAL_LOG_DEBUG, "Checking the number of input devices available");
UINT num_devices = waveInGetNumDevs ();
if (num_devices == 0) {
return false;
}
WAVEINCAPS device_caps;
for (unsigned int device_id = 0; device_id < num_devices; device_id++) {
visual_log (VISUAL_LOG_DEBUG, "Querying device #%u", device_id);
MMRESULT result = waveInGetDevCaps (UINT_PTR (device_id), &device_caps, sizeof (WAVEINCAPS));
if (result != MMSYSERR_NOERROR) {
log_wavein_error ("Failed to query device capabilities", result);
continue;
}
visual_log (VISUAL_LOG_DEBUG, "Name: %s", device_caps.szPname);
visual_log (VISUAL_LOG_DEBUG, "Supported format flags: 0x%08x", (unsigned int) device_caps.dwFormats);
visual_log (VISUAL_LOG_DEBUG, "Number of channels: %d", device_caps.wChannels);
}
return true;
}
int inp_wavein_init (VisPluginData* plugin)
{
visual_return_val_if_fail (plugin != NULL, -1);
if (!check_available_devices ()) {
visual_log (VISUAL_LOG_ERROR, "No input device can be found!");
return -1;
}
WaveInPrivate* priv = visual_mem_new0 (WaveInPrivate, 1);
visual_object_set_private (VISUAL_OBJECT (plugin), priv);
visual_log (VISUAL_LOG_DEBUG, "Querying audio formats supported by input device");
WAVEFORMATEX format = {
WAVE_FORMAT_PCM, // Audio format
2, // Number of channels
44100, // Samples per second
44100*2*2, // Bytes per second (samples per second * block alignment
2*2, // Block alignment (channels * bits per sample / 8)
16, // Bits per sample
0 // Size of extra format information
};
// Check if the input device supports our audio format
MMRESULT result = waveInOpen (&priv->device_handle, WAVE_MAPPER, &format, 0, 0, WAVE_FORMAT_QUERY);
if (result == WAVERR_BADFORMAT) {
log_wavein_error ("Required audio format is not supported by device", result);
return -1;
}
// Create background processing thread
visual_log (VISUAL_LOG_DEBUG, "Creating processing thread");
DWORD thread_id;
HANDLE thread = CreateThread (0, 0, process_buffers, priv, CREATE_SUSPENDED, &thread_id);
// Open the input device
visual_log (VISUAL_LOG_DEBUG, "Opening input device");
result = waveInOpen (&priv->device_handle, WAVE_MAPPER, &format, DWORD_PTR (&handle_wavein_message),
DWORD_PTR (thread_id), CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR) {
// FIXME: Do we need to destroy the thread?
log_wavein_error ("Failed to open capture device", result);
return -1;
}
priv->loaded = true;
priv->mutex = CreateMutex (NULL, FALSE, NULL);
// Initialize buffer statuses
priv->buffer_ready[0] = false;
priv->buffer_ready[1] = false;
// Register the first buffer with the API for capture
priv->active_buffer = 0;
prepare_buffer (priv, priv->active_buffer);
// Start capturing samples
visual_log (VISUAL_LOG_DEBUG, "Starting audio capture");
ResumeThread (thread);
waveInStart (priv->device_handle);
return 0;
}
int inp_wavein_cleanup (VisPluginData* plugin)
{
visual_return_val_if_fail (plugin != NULL, -1);
WaveInPrivate* priv = static_cast<WaveInPrivate*> (visual_object_get_private (VISUAL_OBJECT (plugin)));
visual_return_val_if_fail (priv != NULL, -1);
int retval = 0;
if (priv->loaded) {
visual_log (VISUAL_LOG_DEBUG, "Stopping audio capturing and clearing all pending buffers");
MMRESULT result = waveInReset (priv->device_handle);
if (result != MMSYSERR_NOERROR) {
log_wavein_error ("Failed to clear pending buffers", result);
retval = -1;
}
visual_log (VISUAL_LOG_DEBUG, "Closing capture device");
result = waveInClose (priv->device_handle);
if (result != MMSYSERR_NOERROR) {
log_wavein_error ("Failed to close capture device", result);
retval = -1;
}
CloseHandle (priv->mutex);
}
visual_mem_free (priv);
return retval;
}
int inp_wavein_events (VisPluginData* plugin, VisEventQueue* events)
{
VisEvent ev;
while (visual_event_queue_poll (events, &ev)) {
switch (ev.type) {
default:; // discard
}
}
return 0;
}
int inp_wavein_upload (VisPluginData* plugin, VisAudio* audio)
{
visual_return_val_if_fail (plugin != NULL, -1);
visual_return_val_if_fail (audio != NULL, -1);
WaveInPrivate* priv = static_cast<WaveInPrivate*> (visual_object_get_private (VISUAL_OBJECT (plugin)));
visual_return_val_if_fail (priv != NULL, -1);
int buffer_to_read;
while (true) {
WaitForSingleObject (priv->mutex, INFINITE);
buffer_to_read = priv->active_buffer ^ 0x1;
if (priv->buffer_ready[buffer_to_read]) {
VisBuffer buffer;
visual_buffer_init (&buffer, priv->buffers[buffer_to_read], PCM_BUFFER_SIZE, NULL);
visual_audio_samplepool_input (audio->samplepool, &buffer, VISUAL_AUDIO_SAMPLE_RATE_44100,
VISUAL_AUDIO_SAMPLE_FORMAT_S16, VISUAL_AUDIO_SAMPLE_CHANNEL_STEREO);
priv->buffer_ready[buffer_to_read] = false;
ReleaseMutex (priv->mutex);
break;
}
ReleaseMutex (priv->mutex);
}
return 0;
}
} // anonymous namespace