-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;
--- /dev/null
+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