Fixed audio stopping bug

- Added basic noisegate
- Handling more voice connection statuses
This commit is contained in:
Logan Cusano
2023-05-06 14:09:09 -04:00
parent d1a8059cb9
commit 4fbed169ab
3 changed files with 81 additions and 15 deletions

View File

@@ -67,7 +67,7 @@ async function createAudioInstance() {
sampleFormat: portAudio.SampleFormat16Bit, sampleFormat: portAudio.SampleFormat16Bit,
sampleRate: 48000, sampleRate: 48000,
deviceId: selectedDevice.id, // Use -1 or omit the deviceId to select the default device 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 closeOnError: false, // Close the stream if an audio error is detected, if set false then just log the error
framesPerBuffer: 20, //(48000 / 1000) * 20, //(48000 * 16 * 2) / 1000 * 20 // (48000 * (16 / 8) * 2) / 60 / 1000 * 20 //0.025 * 48000 / 2 framesPerBuffer: 20, //(48000 / 1000) * 20, //(48000 * 16 * 2) / 1000 * 20 // (48000 * (16 / 8) * 2) / 60 / 1000 * 20 //0.025 * 48000 / 2
highwaterMark: 3840, highwaterMark: 3840,
}, },

View File

@@ -1,15 +1,19 @@
require('dotenv').config();
// Debug // Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js"); const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "commandController"); const log = new DebugBuilder("client", "commandController");
// Modules // Modules
const { joinVoiceChannel, VoiceConnectionStatus, getVoiceConnection } = require("@discordjs/voice"); const { joinVoiceChannel, VoiceConnectionStatus, getVoiceConnection, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require("@discordjs/voice");
const { OpusEncoder } = require("@discordjs/opus"); const { OpusEncoder } = require("@discordjs/opus");
const { calcRmsSync } = require("../utilities/rmsCalculator.js");
// Utilities // Utilities
const {replyToInteraction} = require("../utilities/messageHandler.js"); const {replyToInteraction} = require("../utilities/messageHandler.js");
const {createAudioInstance} = require("../controllers/audioController.js"); const {createAudioInstance} = require("../controllers/audioController.js");
const { all } = require('../routes/index.js');
// Declare the encoder // Declare the encoder
const encoder = new OpusEncoder(48000, 2); const encoder = new OpusEncoder(48000, 2);
const noiseGateOpen = process.env.AUDIO_NOISE_GATE_OPEN ?? 100;
/** /**
* Join the specified voice channel * Join the specified voice channel
@@ -31,33 +35,74 @@ exports.join = async function join({interaction= undefined, guildID= undefined,
log.DEBUG("Channel ID: ", channelID) log.DEBUG("Channel ID: ", channelID)
log.DEBUG("Guild ID: ", guildID) log.DEBUG("Guild ID: ", guildID)
const voiceConnection = await joinVoiceChannel({ const vcConnectionObj = {
channelId: channelID, channelId: channelID,
guildId: guildID, guildId: guildID,
adapterCreator: guildObj.voiceAdapterCreator, adapterCreator: guildObj.voiceAdapterCreator,
selfMute: false, selfMute: false,
selfDeaf: false, selfDeaf: false,
}); };
// Join the voice channel
const voiceConnection = await joinVoiceChannel(vcConnectionObj);
// Create the audio stream instance
const audioInstance = await createAudioInstance(); const audioInstance = await createAudioInstance();
log.VERBOSE("Audio Instance: ", audioInstance);
log.VERBOSE("Audio Instance: ", audioInstance); // Play audio data when it's received from the stream
audioInstance.on('data', buffer => { audioInstance.on('data', buffer => {
buffer = Buffer.from(buffer); buffer = Buffer.from(buffer);
log.VERBOSE("Audio buffer: ", buffer); //log.VERBOSE("Audio buffer: ", buffer);
const encodedBuffer = encoder.encode(buffer); // Check intensity of audio and only play when audio is present (no white noise/static)
// TODO Add a function here to check the volume of either buffer and only play audio to discord when there is audio to be played volume = Math.trunc(calcRmsSync(buffer, buffer.length));
voiceConnection.playOpusPacket(encodedBuffer);
})
// Exit the audio handler when the bot disconnects if (parseInt(volume) > parseInt(noiseGateOpen)) {
voiceConnection.on(VoiceConnectionStatus.Destroyed, () => { //log.VERBOSE("Noisegate and buffer volume: ", (parseInt(volume) > parseInt(noiseGateOpen)), noiseGateOpen, volume);
audioInstance.quit(); const encodedBuffer = encoder.encode(buffer);
}) voiceConnection.playOpusPacket(encodedBuffer);
}
});
audioInstance.start(); audioInstance.start();
// Handle state changes in the voice connection
voiceConnection.on('stateChange', async (oldState, newState) => {
//log.VERBOSE("VoiceConnection state Changed from state: ", oldState, "\n\nto state: ", newState);
log.DEBUG("VoiceConnection state Changed from: ", oldState.status, "to: ", newState.status);
// Ready -> Connecting
if (oldState.status === VoiceConnectionStatus.Ready && newState.status === VoiceConnectionStatus.Connecting) {
log.DEBUG("Configuring Network");
voiceConnection.configureNetworking();
return;
}
// Ready -> Disconnected
if (oldState.status === VoiceConnectionStatus.Ready && newState.status === VoiceConnectionStatus.Disconnected) {
log.DEBUG("Attempting to reconnect the voice connection");
voiceConnection = joinVoiceChannel(vcConnectionObj);
return;
}
// Ready
if (newState.status === VoiceConnectionStatus.Ready){
log.DEBUG("Bot connected to voice channel");
return;
}
// Destroyed
if (newState.status === VoiceConnectionStatus.Destroyed){
log.DEBUG("Quitting audio instance");
audioInstance.quit();
return;
}
});
voiceConnection.on('error', async (error) => {
log.ERROR("Voice Connection Error: ", error);
})
if (guildID && callback) return callback(); if (guildID && callback) return callback();
else return; else return;
} }

View File

@@ -0,0 +1,21 @@
exports.calcRmsSync = (arr , n) => {
var square = 0;
var mean = 0;
var root = 0;
// Calculate square.
for (i = 0; i < n; i++) {
square += Math.pow(arr[i], 2);
}
// Calculate Mean.
mean = (square / (n));
// Calculate Root.
root = Math.sqrt(mean);
// Normalize the output
root = root / 10
return root;
}