From 62c69c48756daa43278d7304db1e1b7501b5e73d Mon Sep 17 00:00:00 2001 From: Andrew Gundersen Date: Thu, 19 Aug 2021 15:16:01 -0500 Subject: [PATCH] read write switch stream test, .npmrc for publishing --- .gitignore | 2 +- .npmrc | 2 ++ index.js | 25 +++++++++++++-- package-lock.json | 2 +- package.json | 10 +++--- test/rwSwitchStream.js | 72 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 .npmrc create mode 100644 test/rwSwitchStream.js diff --git a/.gitignore b/.gitignore index 512ecbb..62207ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /build /node_modules -.DS_Store \ No newline at end of file +.DS_Store diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..936e781 --- /dev/null +++ b/.npmrc @@ -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} \ No newline at end of file diff --git a/index.js b/index.js index 2144ae1..29bb371 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,23 @@ -const nodeAudio = require('bindings')('nodeaudio.node'), -exports.nodeAudio = nodeAudio; \ No newline at end of file +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; diff --git a/package-lock.json b/package-lock.json index 300f73d..b00d2b7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "NodeAudio", + "name": "@crimata/nodeaudio", "version": "0.0.0", "lockfileVersion": 1, "requires": true, diff --git a/package.json b/package.json index 92dce47..ba7f42d 100644 --- a/package.json +++ b/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/" + } } diff --git a/test/rwSwitchStream.js b/test/rwSwitchStream.js new file mode 100644 index 0000000..5a6e288 --- /dev/null +++ b/test/rwSwitchStream.js @@ -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); \ No newline at end of file -- 2.43.0