// Config const { getDeviceID } = require('../utilities/configHandler.js'); // Modules const portAudio = require('naudiodon'); const { returnAlsaDeviceObject } = require("../utilities/executeConsoleCommands.js"); // Debug const { DebugBuilder } = require("../utilities/debugBuilder.js"); // Global Vars const log = new DebugBuilder("client", "audioController"); /** * Checks to make sure the selected audio device is available and returns the device object (PortAudio Device Info) * At least one option must be supplied, it will prefer ID to device name * * @param deviceName The name of the device being queried * @param deviceId The ID of the device being queried * @returns {unknown} */ async function confirmAudioDevice({deviceName = undefined, deviceId = undefined}){ const deviceList = await getAudioDevices(); if (!deviceName && !deviceId) throw new Error("No device given"); let confirmedDevice; if (deviceId) confirmedDevice = deviceList.find(device => device.id === deviceId); if (deviceName) confirmedDevice = deviceList.find(device => device.name === deviceName); log.DEBUG("Confirmed Audio Device: ", confirmedDevice); return confirmedDevice; } exports.confirmAudioDevice = confirmAudioDevice; /** * Return a list of the audio devices connected with input channels * * @returns {unknown[]} */ async function getAudioDevices(){ // Exec output contains both stderr and stdout outputs //const deviceList = await returnAlsaDeviceObject(); const deviceList = portAudio.getDevices().map((device) => { if (device.maxInputChannels > 2) { return device; } else { return null; } }).filter(Boolean); log.VERBOSE("Device List: ", deviceList); return deviceList; } exports.getAudioDevices = getAudioDevices; /** * Create and return the audio instance from the saved settings * TODO Allow the client to save and load these settings dynamically * * @returns new portAudio.AudioIO */ async function createAudioInstance() { const selectedDevice = await 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: 20, //(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: "U8", rate: 48000, device: selectedDevice.name ?? "default", // 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 }); */ } exports.createAudioInstance = createAudioInstance;