-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcapture.cc
466 lines (398 loc) · 16 KB
/
capture.cc
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <unistd.h>
#include <string>
#include <sstream>
#include <thread>
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
#include "macros.h"
DISABLE_WCAST_FUNCTION_TYPE
#include "nan.h"
DISABLE_WCAST_FUNCTION_TYPE_END
#include "streaming-worker.h"
class Capture : public StreamingWorker
{
public:
Capture(Callback *data, Callback *complete, Callback *error_callback, v8::Local<v8::Object> &options)
: StreamingWorker(data, complete, error_callback)
{
channels = 2;
device = "default";
format = SND_PCM_FORMAT_S16_LE;
period_size = 32;
period_time = 0;
rate = 44100;
error_init = false;
debug = false;
if (options->IsObject())
{
{
// v8::Local<v8::Value> channels_ = options->Get(New<v8::String>("channels").ToLocalChecked());
v8::Local<v8::Value> channels_ = Nan::Get(
options,
Nan::New("channels").ToLocalChecked())
.ToLocalChecked();
if (!channels_->IsUndefined())
{
bool channels_okay = true;
if (channels_->IsNumber())
{
channels = Nan::To<int>(channels_).FromJust();
}
else
{
channels_okay = false;
}
if (!channels_okay || channels < 0)
{
error_init = true;
Nan::ThrowError("channels has to be a positive number");
return;
}
}
}
{
// v8::Local<v8::Value> debug_ = options->Get(New<v8::String>("debug").ToLocalChecked());
v8::Local<v8::Value> debug_ = Nan::Get(
options,
Nan::New("debug").ToLocalChecked())
.ToLocalChecked();
if (!debug_->IsUndefined())
{
if (debug_->IsBoolean())
{
debug = Nan::To<bool>(debug_).FromJust();
}
else
{
error_init = true;
Nan::ThrowError("debug has to be a bool");
return;
}
}
}
{
// v8::Local<v8::Value> device_ = options->Get(New<v8::String>("device").ToLocalChecked());
v8::Local<v8::Value> device_ = Nan::Get(
options,
Nan::New("device").ToLocalChecked())
.ToLocalChecked();
if (!device_->IsUndefined())
{
if (device_->IsString())
{
// v8::Isolate *isolate = v8::Isolate::GetCurrent();
// v8::String::Utf8Value deviceUTF8(isolate, device_);
// device = std::string(*deviceUTF8);
device = *Nan::Utf8String(device_);
}
else
{
error_init = true;
Nan::ThrowError("device has to be a string");
return;
}
}
}
{
std::string format_name_;
// v8::Local<v8::Value> format_ = options->Get(New<v8::String>("format").ToLocalChecked());
v8::Local<v8::Value> format_ = Nan::Get(
options,
Nan::New("format").ToLocalChecked())
.ToLocalChecked();
if (!format_->IsUndefined())
{
if (format_->IsString())
{
// v8::Isolate *isolate = v8::Isolate::GetCurrent();
// v8::String::Utf8Value formatUTF8(isolate, format_);
// format_name_ = std::string(*formatUTF8);
format_name_ = *Nan::Utf8String(format_);
}
else
{
error_init = true;
Nan::ThrowError("format has to be a string");
return;
}
bool found = false;
std::string all_formats = "";
for (int fn = 0; fn < SND_PCM_FORMAT_LAST; fn++)
{
auto format_enum = static_cast<_snd_pcm_format>(fn);
const char *format_name = snd_pcm_format_name(format_enum);
if (!format_name)
{
continue;
}
if (strcmp(format_name, format_name_.c_str()) == 0)
{
found = true;
format = format_enum;
break;
}
if (!(!snd_pcm_format_linear(format) &&
!(format == SND_PCM_FORMAT_FLOAT_LE ||
format == SND_PCM_FORMAT_FLOAT_BE)))
{
all_formats += std::string(format_name);
all_formats += " ";
}
}
if (!found)
{
error_init = true;
auto error_msg = std::string("format not supported; supported: ");
error_msg += all_formats;
Nan::ThrowError(error_msg.c_str());
return;
}
if (!snd_pcm_format_linear(format) &&
!(format == SND_PCM_FORMAT_FLOAT_LE ||
format == SND_PCM_FORMAT_FLOAT_BE))
{
error_init = true;
Nan::ThrowError("Invalid (non-linear/float) format");
return;
}
}
}
{
// v8::Local<v8::Value> period_size_ = options->Get(New<v8::String>("periodSize").ToLocalChecked());
v8::Local<v8::Value> period_size_ = Nan::Get(
options,
Nan::New("periodSize").ToLocalChecked())
.ToLocalChecked();
if (!period_size_->IsUndefined())
{
bool period_size_okay = true;
if (period_size_->IsNumber())
{
period_size = Nan::To<int>(period_size_).FromJust();
}
else
{
period_size_okay = false;
}
if (!period_size_okay || period_size < 0)
{
error_init = true;
Nan::ThrowError("period_size has to be a positive number");
return;
}
}
}
{
// v8::Local<v8::Value> period_time_ = options->Get(New<v8::String>("periodTime").ToLocalChecked());
v8::Local<v8::Value> period_time_ = Nan::Get(
options,
Nan::New("periodTime").ToLocalChecked())
.ToLocalChecked();
if (!period_time_->IsUndefined())
{
bool period_time_okay = true;
if (period_time_->IsNumber())
{
period_time = Nan::To<int>(period_time_).FromJust();
}
else
{
period_time_okay = false;
}
if (!period_time_okay || period_time < 0)
{
error_init = true;
Nan::ThrowError("period_time has to be a positive number");
return;
}
}
}
{
// v8::Local<v8::Value> rate_ = options->Get(New<v8::String>("rate").ToLocalChecked());
v8::Local<v8::Value> rate_ = Nan::Get(
options,
Nan::New("rate").ToLocalChecked())
.ToLocalChecked();
if (!rate_->IsUndefined())
{
bool rate_okay = true;
if (rate_->IsNumber())
{
rate = Nan::To<int>(rate_).FromJust();
}
else
{
rate_okay = false;
}
if (!rate_okay || (rate < 4000 || rate > 384000))
{
error_init = true;
Nan::ThrowError("rate has to be a value between 4000 and 384000");
return;
}
}
}
}
// int size = (period_size * channels * snd_pcm_format_physical_width(format)) / 8;
// nothing needs to be here - just make sure you call base constructor
// The options parameter is for your JavaScript code to pass in
// an options object. You can use this for whatever you want.
}
~Capture()
{
}
void Execute(const AsyncProgressWorker::ExecutionProgress &progress)
{
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
if (error_init)
{
return;
}
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_CAPTURE, 0);
if (rc < 0)
{
std::ostringstream pcmDeviceError;
pcmDeviceError << "Unable to open PCM device: " << snd_strerror(rc) << "\n";
SetErrorMessage(pcmDeviceError.str().c_str());
return;
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params, format);
// snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, channels);
// snd_pcm_hw_params_set_channels(handle, params, 2);
/* 44100 bits/second sampling rate (CD quality) */
val = static_cast<unsigned int>(rate);
if (debug)
{
fprintf(stderr, "Rate: %d\n", val);
}
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
frames = period_size;
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);
auto frames_time = static_cast<unsigned int>(period_time);
if (frames_time > 0)
{
if (debug)
{
fprintf(stderr, "Set period time near: %d\n", period_time);
}
snd_pcm_hw_params_set_period_time_near(handle, params, &frames_time, &dir);
}
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0)
{
std::ostringstream hwError;
hwError << "Unable to set HW parameters: " << snd_strerror(rc) << "\n";
SetErrorMessage(hwError.str().c_str());
return;
}
// auto sizeOld = frames * 4;
// fprintf(stderr, "Frames: %d; SizeOld: %d, SizeNew: %d\n", frames, sizeOld, size);
unsigned int actualRate;
snd_pcm_hw_params_get_rate(params, &actualRate, &dir);
if (static_cast<unsigned int>(rate) != actualRate)
{
if (debug)
{
fprintf(stderr, "Requested rate != actual rate: %d != %u\n", rate, actualRate);
}
Message rateDeviating("rateDeviating", std::to_string(actualRate), "");
writeToNode(progress, rateDeviating);
}
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
if (frames != static_cast<unsigned long>(period_size))
{
if (debug)
{
fprintf(stderr, "Requested period size != actual period size: %d != %lu\n", period_size, frames);
}
Message periodSizeDeviating("periodSizeDeviating", std::to_string(frames), "");
writeToNode(progress, periodSizeDeviating);
}
unsigned int actual_period_time;
snd_pcm_hw_params_get_period_time(params, &actual_period_time, &dir);
if (debug)
{
fprintf(stderr, "Actual period time: %u\n", actual_period_time);
}
Message periodTimeMessage("periodTime", std::to_string(actual_period_time), "");
writeToNode(progress, periodTimeMessage);
size = (frames * channels * snd_pcm_format_physical_width(format)) / 8;
if (debug)
{
fprintf(stderr, "Actual rate: %d\n", actualRate);
fprintf(stderr, "Buffer size: %d\n", size);
}
char buffer[size];
while (!closed())
{
rc = snd_pcm_readi(handle, buffer, frames);
if (rc == -EPIPE)
{
/* EPIPE means overrun */
if (debug)
{
fprintf(stderr, "overrun occurred\n");
}
Message overrun("overrun", "overrun occurred", "");
writeToNode(progress, overrun);
snd_pcm_prepare(handle);
}
else if (rc < 0)
{
if (debug)
{
fprintf(stderr, "Error from read: %s\n", snd_strerror(rc));
}
Message readError("readError", std::string(snd_strerror(rc)), "");
writeToNode(progress, readError);
}
else if (rc != (int)frames)
{
if (debug)
{
fprintf(stderr, "Short read, read %d frames\n", rc);
}
Message shortRead("shortRead", std::to_string(rc), "");
writeToNode(progress, shortRead);
}
string bufferStringRaw(buffer, size);
Message tosend("audio", "", bufferStringRaw);
writeToNode(progress, tosend);
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
}
private:
int channels;
std::string device;
_snd_pcm_format format;
int period_size;
int period_time;
int rate;
bool error_init;
bool debug;
};
StreamingWorker *create_worker(Callback *data, Callback *complete, Callback *error_callback, v8::Local<v8::Object> &options)
{
return new Capture(data, complete, error_callback, options);
}
DISABLE_WCAST_FUNCTION_TYPE
NODE_MODULE(capture, StreamWorkerWrapper::Init)
DISABLE_WCAST_FUNCTION_TYPE_END