read write switch stream test, .npmrc for publishing
This commit is contained in:
parent
b5cb7139a3
commit
62c69c4875
6 changed files with 105 additions and 8 deletions
2
.npmrc
Normal file
2
.npmrc
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
@crimata:registry=https://gitlab.com/api/v4/projects/28849281/packages/npm/
|
||||
//gitlab.com/api/v4/projects/28849281/packages/npm/:_authToken=${NPM_TOKEN}
|
||||
25
index.js
25
index.js
|
|
@ -1,2 +1,23 @@
|
|||
const nodeAudio = require('bindings')('nodeaudio.node'),
|
||||
exports.nodeAudio = nodeAudio;
|
||||
const nodeAudio = require('bindings')('nodeaudio.node');
|
||||
|
||||
/* Utility function that merges Int16Arrays */
|
||||
const mergeChunks = (chunks) => {
|
||||
const chunkLengths = chunks.map((b) => b.length),
|
||||
totalChunkLength = chunkLengths.reduce((p, c) => p+c, 0),
|
||||
int16Arr = new Int16Array(totalChunkLength);
|
||||
|
||||
console.log(`${totalChunkLength} frames captured in total.`);
|
||||
|
||||
let offset = 0;
|
||||
for (var i = 0; i < chunks.length; i++) {
|
||||
int16Arr.set(chunks[i], offset);
|
||||
offset += chunkLengths[i];
|
||||
}
|
||||
|
||||
return int16Arr;
|
||||
}
|
||||
|
||||
const utils = { mergeChunks: mergeChunks };
|
||||
|
||||
exports.core = nodeAudio;
|
||||
exports.utils = utils;
|
||||
|
|
|
|||
2
package-lock.json
generated
2
package-lock.json
generated
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"name": "NodeAudio",
|
||||
"name": "@crimata/nodeaudio",
|
||||
"version": "0.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
|
|
|
|||
10
package.json
10
package.json
|
|
@ -1,9 +1,8 @@
|
|||
{
|
||||
"name": "NodeAudio",
|
||||
"name": "@crimata/nodeaudio",
|
||||
"version": "0.0.0",
|
||||
"description": "NodeJS bindings for PortAudio",
|
||||
"main": "index.js",
|
||||
"private": true,
|
||||
"main": "./index.js",
|
||||
"dependencies": {
|
||||
"bindings": "~1.2.1",
|
||||
"sleep": "^6.3.0"
|
||||
|
|
@ -11,5 +10,8 @@
|
|||
"scripts": {
|
||||
"test": "node test.js"
|
||||
},
|
||||
"gypfile": true
|
||||
"gypfile": true,
|
||||
"publishConfig": {
|
||||
"registry": "https://gitlab.com/api/v4/projects/28849281/packages/npm/"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
72
test/rwSwitchStream.js
Normal file
72
test/rwSwitchStream.js
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
const nodeAudio = require('../index.js');
|
||||
EventEmitter = require('events'),
|
||||
emitter = new EventEmitter();
|
||||
|
||||
/* Set up keyboard input for recording */
|
||||
var stdin = process.stdin;
|
||||
stdin.setRawMode( true );
|
||||
stdin.resume();
|
||||
stdin.setEncoding( 'utf8' );
|
||||
|
||||
let recording = false;
|
||||
|
||||
let inputDevice;
|
||||
let outputDevice;
|
||||
|
||||
const chunks = [];
|
||||
emitter.on("data", (int16Arr) => {
|
||||
if (recording) {
|
||||
console.log(`Captured ${int16Arr.length} frames`);
|
||||
chunks.push(int16Arr);
|
||||
}
|
||||
});
|
||||
|
||||
const setStreams = () => {
|
||||
|
||||
const defaultInput = nodeAudio.core.GetDefaultInputDevice();
|
||||
const defaultOutput = nodeAudio.core.GetDefaultOutputDevice();
|
||||
console.log(`Input: ${defaultInput}, Output: ${defaultOutput}`);
|
||||
|
||||
if (inputDevice !== defaultInput) {
|
||||
inputDevice = defaultInput;
|
||||
nodeAudio.core.CloseInputStream(inputDevice);
|
||||
nodeAudio.core.OpenInputStream(inputDevice);
|
||||
}
|
||||
|
||||
if (outputDevice !== defaultOutput) {
|
||||
outputDevice = defaultOutput;
|
||||
nodeAudio.core.CloseOutputStream(outputDevice);
|
||||
nodeAudio.core.OpenOutputStream(outputDevice);
|
||||
}
|
||||
}
|
||||
|
||||
const stopRecording = () => {
|
||||
recording = false;
|
||||
pcmAudio = nodeAudio.utils.mergeChunks(chunks);
|
||||
chunks.length = 0;
|
||||
|
||||
/* Play recorded audio */
|
||||
console.log("Playing recorded audio...");
|
||||
nodeAudio.core.WriteToOutputStream(pcmAudio.buffer);
|
||||
}
|
||||
|
||||
nodeAudio.core.Initialize(emitter.emit.bind(emitter));
|
||||
|
||||
/* Check every 2 seconds for new audio device */
|
||||
setInterval(setStreams, 2000);
|
||||
|
||||
/* Toggle recorder with 'r' */
|
||||
stdin.on( 'data', function( key ) {
|
||||
if (key == "r") {
|
||||
if (recording) {
|
||||
stopRecording();
|
||||
} else {
|
||||
recording = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
nodeAudio.core.Terminate();
|
||||
process.exit();
|
||||
}, 30000);
|
||||
Loading…
Reference in a new issue