forked from joeferner/node-portaudio
-
Notifications
You must be signed in to change notification settings - Fork 82
/
index.js
106 lines (89 loc) · 2.92 KB
/
index.js
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
/* Copyright 2019 Streampunk Media Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const { Readable, Writable, Duplex } = require('stream');
const portAudioBindings = require("bindings")("naudiodon.node");
var SegfaultHandler = require('segfault-handler');
SegfaultHandler.registerHandler("crash.log");
exports.SampleFormatFloat32 = 1;
exports.SampleFormat8Bit = 8;
exports.SampleFormat16Bit = 16;
exports.SampleFormat24Bit = 24;
exports.SampleFormat32Bit = 32;
exports.getDevices = portAudioBindings.getDevices;
exports.getHostAPIs = portAudioBindings.getHostAPIs;
function AudioIO(options) {
const audioIOAdon = portAudioBindings.create(options);
let ioStream;
const doRead = async size => {
const result = await audioIOAdon.read(size);
if (result.err)
ioStream.destroy(result.err);
else {
if (result.finished)
ioStream.push(null);
else
ioStream.push(result.buf);
};
};
const doWrite = async (chunk, encoding, cb) => {
const err = await audioIOAdon.write(chunk);
cb(err);
}
const readable = 'inOptions' in options;
const writable = 'outOptions' in options;
if (readable && writable) {
ioStream = new Duplex({
allowHalfOpen: false,
readableObjectMode: false,
writableObjectMode: false,
readableHighWaterMark: options.inOptions ? options.inOptions.highwaterMark || 16384 : 16384,
writableHighWaterMark: options.outOptions ? options.outOptions.highwaterMark || 16384 : 16384,
read: doRead,
write: doWrite
});
} else if (readable) {
ioStream = new Readable({
highWaterMark: options.inOptions.highwaterMark || 16384,
objectMode: false,
read: doRead
});
} else {
ioStream = new Writable({
highWaterMark: options.outOptions.highwaterMark || 16384,
decodeStrings: false,
objectMode: false,
write: doWrite
});
}
ioStream.start = () => audioIOAdon.start();
ioStream.quit = async cb => {
await audioIOAdon.quit('WAIT');
if (typeof cb === 'function')
cb();
}
ioStream.abort = cb => {
audioIOAdon.quit('ABORT', () => {
if (typeof cb === 'function')
cb();
});
}
ioStream.on('close', async () => {
ioStream.emit('closed');
});
ioStream.on('finish', async () => {
await ioStream.quit()
ioStream.emit('finished');
});
ioStream.on('error', err => console.error('AudioIO:', err));
return ioStream;
}
exports.AudioIO = AudioIO;