Functioning audio bot

This commit is contained in:
Logan Cusano
2022-12-11 20:10:36 -05:00
parent 9ce846f1d9
commit ca9463856b
10 changed files with 142 additions and 122 deletions

View File

@@ -1,16 +1,31 @@
// Config
import { getDeviceID, getDeviceName } from '../utilities/configHandler.js'
import {getDeviceID} from '../utilities/configHandler.js'
// Modules
import portAudio from 'naudiodon';
import {createAudioResource} from "@discordjs/voice";
// Debug
import debugBuilder from "../utilities/debugBuilder.js";
const log = new debugBuilder("bot", "audioController");
export function getAudioDevice({deviceName = undefined, deviceId = undefined}){
/**
* 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 (deviceName) return deviceList.find(device => device.name === deviceName);
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) {
@@ -20,16 +35,21 @@ export function getAudioDevices(){
return null;
}
}).filter(Boolean);
console.log("Devices:", deviceList);
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 {IoStreamRead}
*/
export function createAudioInstance() {
//const resource = createAudioResource();
const selectedDevice = getAudioDevice({deviceId: getDeviceID()});//{deviceName: "VoiceMeeter VAIO3 Output (VB-Au"});
console.log(selectedDevice);
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
const audioInstance = new portAudio.AudioIO({
return new portAudio.AudioIO({
inOptions: {
channelCount: 2,
sampleFormat: portAudio.SampleFormat16Bit,
@@ -40,6 +60,4 @@ export function createAudioInstance() {
highwaterMark: 3840
},
});
//audioInstance.start();
return audioInstance;
}