not-ilinked / Anarchy

The superior Discord API wrapper
https://anarchyteam.dev
208 stars 45 forks source link

Distorted audio #3371

Open Suiranoil opened 1 year ago

Suiranoil commented 1 year ago

I dont know whether this is NAudio or Anarchy problem, so im gonna post this on both repos. I have a discord bot that record audio from users in some voice channel and plays it on my speakers. When only one person is talking audio is clear, but when there are 2+ people speaking at the same time audio is distorted. I have no idea what to do, can somebody help me please? There is my code:

using Discord;
using Discord.Gateway;
using Discord.Media;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;

namespace DiscordVoiceRecorderConsole.Handlers
{
    internal static class VoiceHandler
    {
        private class UserPlayback
        {
            private readonly BufferedWaveProvider _stream;
            private readonly WaveOutEvent _waveOut;

            public UserPlayback()
            {
                _stream = new BufferedWaveProvider(new WaveFormat(48000, 16, 2))
                {
                    BufferLength = 3840 * 16,
                    DiscardOnBufferOverflow = true
                };
                _waveOut = new WaveOutEvent();
                _waveOut.Init(_stream);
                _waveOut.Play();
            }

            public void Write(byte[] data)
            {
                _stream.AddSamples(data, 0, data.Length);
            }
        }

        private static readonly Dictionary<ulong, UserPlayback> _userPlayback = new();

        internal static void OnVoiceJoin(DiscordSocketClient client, VoiceConnectEventArgs args)
        {
            Console.WriteLine("Joined voice channel!");
        }

        internal static void OnVoiceLeft(DiscordSocketClient client, VoiceDisconnectEventArgs args)
        {
            Console.WriteLine("Left voice channel!");
            _userPlayback.Clear();
        }

        internal static void OnSpeaking(DiscordSocketClient client, VoiceChannelSpeakingEventArgs args)
        {
            if (!_userPlayback.ContainsKey(args.Stream.UserId))
                _userPlayback.Add(args.Stream.UserId, new UserPlayback());

            var playback = _userPlayback[args.Stream.UserId];

            WriteStreamToPlayback(playback, args.Stream);
        }

        private static void WriteStreamToPlayback(UserPlayback playback, IncomingVoiceStream stream)
        {
            try
            {
                while (true)
                {
                    var packet = stream.Read();
                    var data = packet.Data;

                    if (data == null)
                        break;

                    playback.Write(data);
                    Thread.Sleep(1);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
Suiranoil commented 1 year ago

The issue seems to be with Anarchy, somehow when it decodes multiple voice streams the audio breaks???

verticalsync commented 1 year ago

The issue seems to be with Anarchy, somehow when it decodes multiple voice streams the audio breaks???

Anarchy is pretty much abandoned, it's only worked on by contributors which even they don't wanna put the work into this that is needed.

Sighyu commented 1 year ago

The issue seems to be with Anarchy, somehow when it decodes multiple voice streams the audio breaks???

Anarchy is pretty much abandoned, it's only worked on by contributors which even they don't wanna put the work into this that is needed.

do you happend to know a more updated selfbot project other than anarchy

verticalsync commented 1 year ago

The issue seems to be with Anarchy, somehow when it decodes multiple voice streams the audio breaks???

Anarchy is pretty much abandoned, it's only worked on by contributors which even they don't wanna put the work into this that is needed.

do you happend to know a more updated selfbot project other than anarchy

No, I don't.

VendorAttestation commented 1 year ago

The issue seems to be with Anarchy, somehow when it decodes multiple voice streams the audio breaks???

Anarchy is pretty much abandoned, it's only worked on by contributors which even they don't wanna put the work into this that is needed.

do you happend to know a more updated selfbot project other than anarchy

https://github.com/aiko-chan-ai/discord.js-selfbot-v13

verticalsync commented 1 year ago

The issue seems to be with Anarchy, somehow when it decodes multiple voice streams the audio breaks???

Anarchy is pretty much abandoned, it's only worked on by contributors which even they don't wanna put the work into this that is needed.

do you happend to know a more updated selfbot project other than anarchy

https://github.com/aiko-chan-ai/discord.js-selfbot-v13

I think he meant for C#, but maybe not.

Sighyu commented 8 months ago

The issue seems to be with Anarchy, somehow when it decodes multiple voice streams the audio breaks???

Anarchy is pretty much abandoned, it's only worked on by contributors which even they don't wanna put the work into this that is needed.

do you happend to know a more updated selfbot project other than anarchy

https://github.com/aiko-chan-ai/discord.js-selfbot-v13

I think he meant for C#, but maybe not.

a bit late reply but yea c#