Files
DRB-CnC/Client/discord-bot/controllers/audioController.js
2022-12-11 18:24:24 -05:00

45 lines
1.9 KiB
JavaScript

// Config
import { getDeviceID, getDeviceName } from '../utilities/configHandler.js'
// Modules
import portAudio from 'naudiodon';
import {createAudioResource} from "@discordjs/voice";
export function getAudioDevice({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);
}
export function getAudioDevices(){
const deviceList = portAudio.getDevices().map((device) => {
if (device.defaultSampleRate === 48000 || device.maxInputChannels > 0) {
return device;
}
else {
return null;
}
}).filter(Boolean);
console.log("Devices:", deviceList);
return deviceList;
}
export function createAudioInstance() {
//const resource = createAudioResource();
const selectedDevice = getAudioDevice({deviceId: getDeviceID()});//{deviceName: "VoiceMeeter VAIO3 Output (VB-Au"});
console.log(selectedDevice);
// Create an instance of AudioIO with outOptions (defaults are as below), which will return a WritableStream
const audioInstance = 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
},
});
//audioInstance.start();
return audioInstance;
}