Exemple #1
0
        private void SendVoice(string file, DiscordClient client, YouTubeVideo video)
        {
            DiscordVoiceClient vc = client.GetVoiceClient();

            try
            {
                int ms         = vc.VoiceConfig.FrameLengthMs;
                int channels   = vc.VoiceConfig.Channels;
                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);

                if (video.AudioFormat == AudioFormat.Mp3)
                {
                    using (var mp3Reader = new Mp3FileReader(video.Stream()))
                    {
                        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;
                            resampler.Dispose();
                            mp3Reader.Close();
                        }
                    }
                }
                else if (video.AudioFormat == AudioFormat.Vorbis)
                {
                    using (var vorbis = new NAudio.Vorbis.VorbisWaveReader(video.Stream()))
                    {
                        using (var resampler = new MediaFoundationResampler(vorbis, outFormat)
                        {
                            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;
                            resampler.Dispose();
                            vorbis.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                try
                {
                    MainEntry.owner.SendMessage("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```");
                }
                catch { }
            }
        }