[BUG]: Lags on Windows compared to djs/voice
Hydro001 opened this issue · comments
Version
1.0.5
Description
Using this on Windows makes the bot sound lag while there's no issue doing the same thing in djs/voice
Steps to reproduce
Try the code provided below and these packages :
- @discordjs/voice: latest
- @performanc/voice: latest
- discord.js: latest
- libsodium-wrappers: latest
- opusscript: latest
- yt-stream: latest
Example code
Using this package :
import { Client, GatewayIntentBits } from 'discord.js';
import perfcVoice from '@performanc/voice';
import prism from 'prism-media';
import ytstream from 'yt-stream';
import config from './config.js';
const ffmpeg = new prism.FFmpeg({
args: [
'-loglevel', '0',
'-analyzeduration', '0',
'-hwaccel', 'auto',
'-threads', 2,
'-filter_threads', 2,
'-filter_complex_threads', 2,
'-i', '-',
'-f', 's16le',
'-ar', '48000',
'-ac', '2',
'-crf', '0'
]
});
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });
const connection = perfcVoice.joinVoiceChannel({ guildId: config.guild_id, userId: config.bot_user_id, encryption: 'xsalsa20_poly1305_lite' });
client.on('raw', (data) => {
switch (data.t) {
case 'VOICE_STATE_UPDATE': {
if (data.d.guild_id == config.guild_id && data.d.member?.user.id == config.bot_user_id) {
connection.voiceStateUpdate({
session_id: data.d.session_id
});
}
break;
}
case 'VOICE_SERVER_UPDATE': {
if (data.d.guild_id == config.guild_id) {
connection.voiceServerUpdate({
token: data.d.token,
endpoint: data.d.endpoint
});
connection.connect();
}
break;
}
}
});
connection.on('stateChange', async (oldState, newState) => {
if (newState.status == 'ready') {
//console.log('Voice connection ready');
const stream = await ytstream.stream(`https://www.youtube.com/watch?v=dQw4w9WgXcQ`, {
quality: 'high',
type: 'audio',
highWaterMark: 1048576 * 32,
download: true
});
const result = stream.stream.pipe(ffmpeg).pipe(
new prism.opus.Encoder({
rate: 48000,
channels: 2,
frameSize: 960
}
));
connection.play(result);
}
});
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
const channel = client.channels.cache.get(config.channel_id);
client.guilds.cache.get(config.guild_id).shard.send(
{
op: 4,
d: {
guild_id: channel.guildId,
channel_id: channel.id,
self_mute: false,
self_deaf: false
}
});
});
client.login(config.token);
Using djs/voice :
import { Client, GatewayIntentBits } from 'discord.js';
import { joinVoiceChannel, createAudioPlayer, createAudioResource, NoSubscriberBehavior } from '@discordjs/voice';
import config from './config.js';
import ytstream from 'yt-stream';
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
const channel = client.channels.cache.get(config.channel_id);
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
const player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Play,
},
});
const stream = await ytstream.stream(`https://www.youtube.com/watch?v=dQw4w9WgXcQ`, {
quality: 'high',
type: 'audio',
highWaterMark: 1048576 * 32,
download: true
});
const resource = createAudioResource(stream.stream);
connection.subscribe(player);
player.play(resource);
});
client.login(config.token);
Confirmations
- My environment meets the minimum requirements.
- I have verified that this is not a duplicate issue.
Code of Conduct
- I agree to follow this project's Code of Conduct
This seems to be related on the resolution/precision of setInterval
on Windows, which seems to fluctuate more than non-Windows (e.g Linux distros).
A solution would be make setInterval
to be aware of the time, reducing this imprecision. However Windows isn't recommended for hosting and tends to be really bad at real-time IO (aside from numerous other things it isn't optimized for).
I'll see about implement time aware setInterval
s for /voice
only for Windows, but no promises for that. Be aware that it will be more CPU-hungry.