From d96b8ee05bd590826c188398596a3b9358835cba Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 26 Mar 2023 00:03:38 -0400 Subject: [PATCH] Initial update to ALSA --- .../controllers/audioController.js | 37 ++++++++----------- Client/discord-bot/package.json | 4 +- .../utilities/executeConsoleCommand.js | 22 +++++++++++ 3 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 Client/discord-bot/utilities/executeConsoleCommand.js diff --git a/Client/discord-bot/controllers/audioController.js b/Client/discord-bot/controllers/audioController.js index d6ab149..f1115aa 100644 --- a/Client/discord-bot/controllers/audioController.js +++ b/Client/discord-bot/controllers/audioController.js @@ -1,9 +1,11 @@ // Config -import {getDeviceID} from '../utilities/configHandler.js' +import {getDeviceID} from '../utilities/configHandler.js'; // Modules -import portAudio from 'naudiodon'; +import alsaInstance from 'alsa-capture'; +import executeConsoleCommand from "../utilities/executeConsoleCommand.js"; // Debug import ModuleDebugBuilder from "../utilities/moduleDebugBuilder.js"; +// Global Vars const log = new ModuleDebugBuilder("bot", "audioController"); /** @@ -27,16 +29,9 @@ export function confirmAudioDevice({deviceName = undefined, deviceId = undefined * @returns {unknown[]} */ export function getAudioDevices(){ - const deviceList = portAudio.getDevices().map((device) => { - if (device.defaultSampleRate === 48000 || device.maxInputChannels > 0) { - return device; - } - else { - return null; - } - }).filter(Boolean); - //log.DEBUG("Device List: ", deviceList); - return deviceList; + // Exec output contains both stderr and stdout outputs + const deviceList = executeConsoleCommand("arecord -L"); + log.DEBUG("Device list: ", deviceList); } /** @@ -49,15 +44,13 @@ export function createAudioInstance() { const selectedDevice = confirmAudioDevice({deviceId: getDeviceID()});//{deviceName: "VoiceMeeter VAIO3 Output (VB-Au"}); log.DEBUG("Device selected from config: ", selectedDevice); // Create an instance of AudioIO with outOptions (defaults are as below), which will return a WritableStream - return new portAudio.AudioIO({ - inOptions: { - channelCount: 2, - sampleFormat: portAudio.SampleFormat16Bit, - sampleRate: 48000, - deviceId: selectedDevice.id, // Use -1 or omit the deviceId to select the default device - closeOnError: true, // Close the stream if an audio error is detected, if set false then just log the error - framesPerBuffer: 100, //(48000 / 1000) * 20, //(48000 * 16 * 2) / 1000 * 20 // (48000 * (16 / 8) * 2) / 60 / 1000 * 20 //0.025 * 48000 / 2 - highwaterMark: 3840 - }, + return new alsaInstance({ + channels: 2, + format: "S16_BE", + rate: 48000, + device: selectedDevice.id ?? "default", // Use -1 or omit the deviceId to select the default device + periodSize: 100, //(48000 / 1000) * 20, //(48000 * 16 * 2) / 1000 * 20 // (48000 * (16 / 8) * 2) / 60 / 1000 * 20 //0.025 * 48000 / 2 + periodTime: undefined, +// highwaterMark: 3840 }); } \ No newline at end of file diff --git a/Client/discord-bot/package.json b/Client/discord-bot/package.json index 5f1a1f6..d7d193a 100644 --- a/Client/discord-bot/package.json +++ b/Client/discord-bot/package.json @@ -17,9 +17,9 @@ "@mapbox/node-pre-gyp": "^1.0.10", "debug": "^4.3.4", "discord.js": "^14.7.1", - "naudiodon": "^2.3.6", "node-gyp": "^9.3.0", - "libsodium-wrappers": "^0.7.10" + "libsodium-wrappers": "^0.7.10", + "alsa-capture": "0.3.0" }, "type": "module" } diff --git a/Client/discord-bot/utilities/executeConsoleCommand.js b/Client/discord-bot/utilities/executeConsoleCommand.js new file mode 100644 index 0000000..1904a82 --- /dev/null +++ b/Client/discord-bot/utilities/executeConsoleCommand.js @@ -0,0 +1,22 @@ +// Modules +import { promisify } from 'util'; +// Debug +import ModuleDebugBuilder from "../utilities/moduleDebugBuilder.js"; +// Global Vars +const log = new ModuleDebugBuilder("bot", "executeConsoleCommand"); +const exec = promisify(require('child_process').exec); + + +exports.executeAsyncConsoleCommand = (consoleCommand) => { + // Check to see if the command is a real command + // TODO needs to be improved + const acceptableCommands = [ "arecord -L" ]; + if (!acceptableCommands.includes(consoleCommand)) { + log.WARN("Console command is not acceptable: ", consoleCommand); + return undefined; + } + const tempOutput = exec(); + + // TODO add some error checking + return tempOutput.stdout.trim(); +} \ No newline at end of file