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;
}

View File

@@ -1,6 +0,0 @@
// Utilities
import { replyToInteraction } from '../utilities/messageHandler.js';
export default function ping(interaction) {
return replyToInteraction(interaction, "Pong! I have Aids and now you do too!");
}

View File

@@ -1,87 +0,0 @@
// Modules
import {
joinVoiceChannel,
getVoiceConnection,
createAudioResource,
createAudioPlayer,
NoSubscriberBehavior, StreamType
} from '@discordjs/voice';
import OpusEncoderPkg from "@discordjs/opus";
const { OpusEncoder } = OpusEncoderPkg;
// Utilities
import { replyToInteraction } from '../utilities/messageHandler.js';
import { createAudioInstance } from "./audioController.js";
/**
* Join the specified voice channel
*
* @param interaction Message interaction from discord
*/
export function join(interaction){
const voiceChannel = interaction.options.getChannel('voicechannel');
const voiceConnection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: interaction.guildId,
adapterCreator: interaction.guild.voiceAdapterCreator,
selfMute: false,
selfDeaf: false,
});
replyToInteraction(interaction, `Ok, Joining ${voiceChannel.name}`);
// Declare the encoder
const encoder = new OpusEncoder(48000, 2);
const player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Play,
},
});
const audioInstance = createAudioInstance();
audioInstance.on('data', buffer => {
buffer = Buffer.from(buffer);
const encoded = encoder.encode(buffer);
voiceConnection.playOpusPacket(encoded);
})
audioInstance.start();
}
/**
* If in a voice channel for the specified guild, leave
*
* @param interaction Message interaction from discord
*/
export function leave(interaction){
const guildId = interaction.guild.id;
const voiceConnection = getVoiceConnection(guildId);
if (!voiceConnection) return replyToInteraction(interaction, "Not in a voice channel.");
voiceConnection.destroy();
return replyToInteraction(interaction, `Goodbye`);
}
/*
* Brute forcing the required buffer size, 16 bit, 48KHz, 2ch = 480 Bytes (need to get frames still)
let notWorking = true;
let splitIndexInverse = 0
while (notWorking) {
try {
const newBuffer = buffer.slice(buffer.length - splitIndexInverse);
console.log(timestamp ,newBuffer, "end")
const encoded = encoder.encode(newBuffer);
console.log("Working Buffer Length" ,newBuffer.length)
console.log("Working Buffer" ,newBuffer)
notWorking = false
break;
} catch (err){
console.log(err)
if (splitIndexInverse >= buffer.length) {
notWorking = false
throw new Error("Shit fucked")
}
splitIndexInverse += 1
}
}
*/