Exemple #1
0
        static Task DoVoice(DiscordVoiceClient vc, string file)
        {
            return Task.Run(() =>
            {
                try
                {
                    int ms = 20;
                    int channels = 2;
                    int sampleRate = 48000;

                    int blockSize = 48 * 2 * channels * ms; //sample rate * 2 * channels * milliseconds
                    byte[] buffer = new byte[blockSize];
                    var outFormat = new WaveFormat(sampleRate, 16, channels);

                    vc.SetSpeaking(true);
                    using (var mp3Reader = new Mp3FileReader(file))
                    {
                        using (var resampler = new WaveFormatConversionStream(outFormat, mp3Reader))
                        {
                            int byteCount;
                            while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0)
                            {
                                if (vc.Connected)
                                {
                                    //sequence = await vc.SendSmallOpusAudioPacket(buffer, sampleRate, byteCount, sequence).ConfigureAwait(false);
                                    vc.SendVoice(buffer);
                                    //sequence = vc.SendSmallOpusAudioPacket(buffer, 48000, buffer.Length, sequence);
                                    //Task.Delay(19).Wait();
                                }
                                else
                                    break;
                            }
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Voice finished enqueuing");
                            Console.ForegroundColor = ConsoleColor.White;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            });
        }
        public async void DisconnectFromVoice()
        {
            if (VoiceClient != null)
            {
                string msg = JsonConvert.SerializeObject(new
                {
                    op = 4,
                    d = new
                    {
                        guild_id = (string)null,
                        channel_id = (string)null,
                        self_mute = true,
                        self_deaf = false
                    }
                });

                ws.Send(msg);



                await Task.Run(() => VoiceClient.Dispose()).ConfigureAwait(false);
                VoiceClient = null;
            }
        }
        public async void ConnectToVoiceChannel(DiscordChannel channel, bool clientMuted = false, bool clientDeaf = false)
        {
            if (channel.type != "voice")
                throw new InvalidOperationException($"Channel '{channel.name}' is not a voice channel!");

            if (ConnectedToVoice())
                await Task.Run(()=>DisconnectFromVoice()).ConfigureAwait(false);

            if (VoiceClient == null)
                VoiceClient = new DiscordVoiceClient(this);
            VoiceClient.Disposed += (sender, e) => VoiceClient = null;
            VoiceClient.UserSpeaking += (sender, e) =>
            {
                if (UserSpeaking != null)
                    UserSpeaking(this, e);
            };

            string joinVoicePayload = JsonConvert.SerializeObject(new
            {
                op = 4,
                d = new
                {
                    guild_id = channel.parent.id,
                    channel_id = channel.id,
                    self_mute = clientMuted,
                    self_deaf = clientDeaf
                }
            });

            ws.Send(joinVoicePayload);
        }
Exemple #4
0
        private static void VoiceStuffs(DiscordVoiceClient vc, string file)
        {
            try
            {
                int ms = 60;
                int channels = 1;
                int sampleRate = 48000;

                int blockSize = 48 * 2 * channels * ms; //sample rate * 2 * channels * milliseconds
                byte[] buffer = new byte[blockSize];
                var outFormat = new WaveFormat(sampleRate, 16, channels);
                
                vc.SetSpeaking(true);
                using (var mp3Reader = new MediaFoundationReader(file))
                {
                    using (var resampler = new MediaFoundationResampler(mp3Reader, outFormat) { ResamplerQuality = 60 })
                    {
                        //resampler.ResamplerQuality = 60;
                        int byteCount;
                        while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0)
                        {
                            if (vc.Connected)
                            {
                                vc.SendVoice(buffer);
                            }
                            else
                                break;
                        }
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Voice finished enqueuing");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                }
            }
            catch (Exception ex)
            {
                owner.SendMessage("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```");
            }
        }
        /// <summary>
        /// Also disposes
        /// </summary>
        public void DisconnectFromVoice()
        {
            string disconnectMessage = JsonConvert.SerializeObject(new
            {
                op = 4,
                d = new
                {
                    guild_id = VoiceClient != null && VoiceClient.Channel != null ? VoiceClient.Channel.Parent.ID : (object)null,
                    channel_id = (object)null,
                    self_mute = true,
                    self_deaf = false
                }
            });
            if (VoiceClient != null)
            {
                try
                {
                    VoiceClient.Dispose();
                    VoiceClient = null;


                    ws.Send(disconnectMessage);
                }
                catch
                { }
            }
            if (ws != null)
                ws.Send(disconnectMessage);
            VoiceClient = null;
            if (VoiceThread != null)
                VoiceThread.Abort();
            DebugLogger.Log($"Disconnected from voice. VoiceClient null: {VoiceClient == null}");
        }
        /// <summary>
        /// Connects to a given voice channel.
        /// </summary>
        /// <param name="channel">The channel to connect to. </param>
        /// <param name="voiceConfig">The voice configuration to use. If null, default values will be used.</param>
        /// <param name="clientMuted">Whether or not the client will connect muted. Defaults to false.</param>
        /// <param name="clientDeaf">Whether or not the client will connect deaf. Defaults to false.</param>
        public void ConnectToVoiceChannel(DiscordChannel channel, DiscordVoiceConfig voiceConfig = null, bool clientMuted = false, bool clientDeaf = false)
        {
            if (channel.Type != ChannelType.Voice)
                throw new InvalidOperationException($"Channel '{channel.ID}' is not a voice channel!");

            if (ConnectedToVoice())
                DisconnectFromVoice();

            if (VoiceClient == null)
            {
                if (voiceConfig == null)
                {
                    VoiceClient = new DiscordVoiceClient(this, new DiscordVoiceConfig());
                }
                else
                    VoiceClient = new DiscordVoiceClient(this, voiceConfig);
            }
            VoiceClient.Channel = channel;
            VoiceClient.ErrorReceived += (sender, e) =>
            {
                if (GetLastVoiceClientLogger != null)
                {
                    GetLastVoiceClientLogger = VoiceClient.GetDebugLogger;
                    DisconnectFromVoice();
                }
            };
            VoiceClient.UserSpeaking += (sender, e) =>
            {
                if (UserSpeaking != null)
                    UserSpeaking(this, e);
            };
            VoiceClient.VoiceConnectionComplete += (sender, e) =>
            {
                if (VoiceClientConnected != null)
                    VoiceClientConnected(this, e);
            };
            VoiceClient.QueueEmpty += (sender, e) =>
            {
                VoiceQueueEmpty?.Invoke(this, e);
            };

            string joinVoicePayload = JsonConvert.SerializeObject(new
            {
                op = 4,
                d = new
                {
                    guild_id = channel.Parent.ID,
                    channel_id = channel.ID,
                    self_mute = clientMuted,
                    self_deaf = clientDeaf
                }
            });

            ws.Send(joinVoicePayload);
        }