// Config import {getDeviceID} from '../utilities/configHandler.js' // Modules import portAudio from 'naudiodon'; // Debug import ModuleDebugBuilder from "../utilities/moduleDebugBuilder.js"; const log = new ModuleDebugBuilder("bot", "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} */ export function confirmAudioDevice({deviceName = undefined, deviceId = undefined}){ const deviceList = getAudioDevices(); if (!deviceName && !deviceId) throw new Error("No device given"); if (deviceId) return deviceList.find(device => device.id === deviceId); if (deviceName) return deviceList.find(device => device.name === deviceName); } /** * Return a list of the audio devices connected with input channels * * @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; } /** * 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 */ 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 }, }); }