Initial update to ALSA

This commit is contained in:
Logan Cusano
2023-03-26 00:03:38 -04:00
parent ce072d9287
commit d96b8ee05b
3 changed files with 39 additions and 24 deletions

View File

@@ -1,9 +1,11 @@
// Config // Config
import {getDeviceID} from '../utilities/configHandler.js' import {getDeviceID} from '../utilities/configHandler.js';
// Modules // Modules
import portAudio from 'naudiodon'; import alsaInstance from 'alsa-capture';
import executeConsoleCommand from "../utilities/executeConsoleCommand.js";
// Debug // Debug
import ModuleDebugBuilder from "../utilities/moduleDebugBuilder.js"; import ModuleDebugBuilder from "../utilities/moduleDebugBuilder.js";
// Global Vars
const log = new ModuleDebugBuilder("bot", "audioController"); const log = new ModuleDebugBuilder("bot", "audioController");
/** /**
@@ -27,16 +29,9 @@ export function confirmAudioDevice({deviceName = undefined, deviceId = undefined
* @returns {unknown[]} * @returns {unknown[]}
*/ */
export function getAudioDevices(){ export function getAudioDevices(){
const deviceList = portAudio.getDevices().map((device) => { // Exec output contains both stderr and stdout outputs
if (device.defaultSampleRate === 48000 || device.maxInputChannels > 0) { const deviceList = executeConsoleCommand("arecord -L");
return device; log.DEBUG("Device list: ", deviceList);
}
else {
return null;
}
}).filter(Boolean);
//log.DEBUG("Device List: ", deviceList);
return deviceList;
} }
/** /**
@@ -49,15 +44,13 @@ export function createAudioInstance() {
const selectedDevice = confirmAudioDevice({deviceId: getDeviceID()});//{deviceName: "VoiceMeeter VAIO3 Output (VB-Au"}); const selectedDevice = confirmAudioDevice({deviceId: getDeviceID()});//{deviceName: "VoiceMeeter VAIO3 Output (VB-Au"});
log.DEBUG("Device selected from config: ", selectedDevice); log.DEBUG("Device selected from config: ", selectedDevice);
// Create an instance of AudioIO with outOptions (defaults are as below), which will return a WritableStream // Create an instance of AudioIO with outOptions (defaults are as below), which will return a WritableStream
return new portAudio.AudioIO({ return new alsaInstance({
inOptions: { channels: 2,
channelCount: 2, format: "S16_BE",
sampleFormat: portAudio.SampleFormat16Bit, rate: 48000,
sampleRate: 48000, device: selectedDevice.id ?? "default", // Use -1 or omit the deviceId to select the default device
deviceId: selectedDevice.id, // 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
closeOnError: true, // Close the stream if an audio error is detected, if set false then just log the error periodTime: undefined,
framesPerBuffer: 100, //(48000 / 1000) * 20, //(48000 * 16 * 2) / 1000 * 20 // (48000 * (16 / 8) * 2) / 60 / 1000 * 20 //0.025 * 48000 / 2 // highwaterMark: 3840
highwaterMark: 3840
},
}); });
} }

View File

@@ -17,9 +17,9 @@
"@mapbox/node-pre-gyp": "^1.0.10", "@mapbox/node-pre-gyp": "^1.0.10",
"debug": "^4.3.4", "debug": "^4.3.4",
"discord.js": "^14.7.1", "discord.js": "^14.7.1",
"naudiodon": "^2.3.6",
"node-gyp": "^9.3.0", "node-gyp": "^9.3.0",
"libsodium-wrappers": "^0.7.10" "libsodium-wrappers": "^0.7.10",
"alsa-capture": "0.3.0"
}, },
"type": "module" "type": "module"
} }

View File

@@ -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();
}