Working audio on bot

This commit is contained in:
Logan Cusano
2022-12-11 18:24:24 -05:00
parent 4e1b82c557
commit 9ce846f1d9
3 changed files with 48 additions and 15 deletions

View File

@@ -2,6 +2,6 @@
"TOKEN": "OTQzNzQyMDQwMjU1MTE1MzA0.Yg3eRA.ZxEbRr55xahjfaUmPY8pmS-RHTY",
"ApplicationID": "943742040255115304",
"GuildID": "367396189529833472",
"DeviceID": "25",
"DeviceName": "VoiceMeeter Aux Output (VB-Audio VoiceMeeter AUX VAIO)"
"DeviceID": "5",
"DeviceName": "VoiceMeeter Aux Output (VB-Audi"
}

View File

@@ -12,7 +12,14 @@ export function getAudioDevice({deviceName = undefined, deviceId = undefined}){
}
export function getAudioDevices(){
const deviceList = portAudio.getDevices();
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;
}
@@ -26,11 +33,12 @@ export function createAudioInstance() {
inOptions: {
channelCount: 2,
sampleFormat: portAudio.SampleFormat16Bit,
sampleRate: 44100,
sampleRate: 48000,
deviceId: selectedDevice.id, // Use -1 or omit the deviceId to select the default device
closeOnError: false, // Close the stream if an audio error is detected, if set false then just log the error
framesPerBuffer: 0 // 44100 / 1000 * 120 / 2 // Get 120ms of audio
}
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;

View File

@@ -30,7 +30,7 @@ export function join(interaction){
replyToInteraction(interaction, `Ok, Joining ${voiceChannel.name}`);
// Declare the encoder
const encoder = new OpusEncoder(44100, 2);
const encoder = new OpusEncoder(48000, 2);
const player = createAudioPlayer({
behaviors: {
@@ -39,13 +39,14 @@ export function join(interaction){
});
const audioInstance = createAudioInstance();
const audioResource = createAudioResource(audioInstance, { inputType: StreamType.Raw });
audioInstance.on('data', buffer => {
buffer = Buffer.from(buffer);
const encoded = encoder.encode(buffer);
voiceConnection.playOpusPacket(encoded);
})
audioInstance.start();
//audioInstance.on('data', buffer => {
// Do on buffer event
//})
player.play(audioResource);
voiceConnection.subscribe(player);
}
/**
@@ -59,4 +60,28 @@ export function leave(interaction){
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
}
}
*/