PerformanC / voice

PerformanC's Discord Voice API client.
BSD 2-Clause "Simplified" License
2 stars 1 forks source link

[BUG]: Lags on Windows compared to djs/voice #1

Closed Hydro001 closed 6 months ago

Hydro001 commented 7 months ago

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 :

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

Code of Conduct

ThePedroo commented 7 months ago

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 setIntervals for /voice only for Windows, but no promises for that. Be aware that it will be more CPU-hungry.

ThePedroo commented 6 months ago

This issue was addressed by https://github.com/PerformanC/voice/commit/bc88b62d733dc3e6463726254d24d33649a55ed3.