From 9ce846f1d94ca7417073e15633abe6a038f0886b Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 11 Dec 2022 18:24:24 -0500 Subject: [PATCH] Working audio on bot --- Client/discord-bot/config/botConfig.json | 4 +- .../controllers/audioController.js | 18 +++++--- .../controllers/voiceController.js | 41 +++++++++++++++---- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/Client/discord-bot/config/botConfig.json b/Client/discord-bot/config/botConfig.json index cb44d5e..ff3e722 100644 --- a/Client/discord-bot/config/botConfig.json +++ b/Client/discord-bot/config/botConfig.json @@ -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" } \ No newline at end of file diff --git a/Client/discord-bot/controllers/audioController.js b/Client/discord-bot/controllers/audioController.js index 158bdc2..e9fd66a 100644 --- a/Client/discord-bot/controllers/audioController.js +++ b/Client/discord-bot/controllers/audioController.js @@ -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; diff --git a/Client/discord-bot/controllers/voiceController.js b/Client/discord-bot/controllers/voiceController.js index 2be2f9e..f492865 100644 --- a/Client/discord-bot/controllers/voiceController.js +++ b/Client/discord-bot/controllers/voiceController.js @@ -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`); -} \ No newline at end of file +} + +/* +* 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 + } + } + */ \ No newline at end of file