]> Repos - nodeaudio/commitdiff
read write switch stream test, .npmrc for publishing
authorAndrew Gundersen <gundersena@xavier.edu>
Thu, 19 Aug 2021 20:16:01 +0000 (15:16 -0500)
committerAndrew Gundersen <gundersena@xavier.edu>
Thu, 19 Aug 2021 20:16:01 +0000 (15:16 -0500)
.gitignore
.npmrc [new file with mode: 0644]
index.js
package-lock.json
package.json
test/rwSwitchStream.js [new file with mode: 0644]

index 512ecbb5e8acebb9ac1ce1d77fa0e71d8c40174f..62207ba85874995181316014cbf96a2389c41c07 100644 (file)
@@ -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 (file)
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
index 2144ae19a73dde780e17ec184861f33619c7971d..29bb371822a2c9296e5c3131d4005ec4bfc471d9 100644 (file)
--- 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;
index 300f73de79e9a6a7137b833176ec503f61aab5a2..b00d2b7621ab17edf00591b42c0541a721d9f84b 100644 (file)
@@ -1,5 +1,5 @@
 {
-  "name": "NodeAudio",
+  "name": "@crimata/nodeaudio",
   "version": "0.0.0",
   "lockfileVersion": 1,
   "requires": true,
index 92dce475425d03dd2e4cdae2165095cbd4d4af4c..ba7f42d34bd7029a04ee8ba90bfb2b375fafd4d6 100644 (file)
@@ -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 (file)
index 0000000..5a6e288
--- /dev/null
@@ -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