internal void TriggerVCSpeaking(DiscordVoiceClient client, IncomingVoiceStream stream) { if (OnUserSpeaking != null) { Task.Run(() => OnUserSpeaking.Invoke(this, new VoiceChannelSpeakingEventArgs(client, stream))); } }
internal void TriggerVCConnect(DiscordVoiceClient client) { if (OnJoinedVoiceChannel != null) { Task.Run(() => OnJoinedVoiceChannel.Invoke(this, new VoiceConnectEventArgs(client))); } }
// Adapted from a DiscordSharp sample project static Task DoVoiceMP3(DiscordVoiceClient vc, string file) { return(Task.Run(() => { var bytes = audio.GetBytesFromMP3(file); audio.EnqueueBytes(bytes); audio.PlayAudio(); })); }
public void JoinVoice(string guildId, string channelId) { if (userId == null) { throw new Exception("Client not ready"); } voiceClient = new DiscordVoiceClient(userId, gateway); voiceClient.OnVoiceReady += OnVoiceReadyInternal; voiceClient.JoinVoice(guildId, channelId); }
/// <summary> /// Joins a voice channel. /// </summary> /// <param name="guildId">ID of the guild</param> /// <param name="channelId">ID of the channel</param> /// <param name="muted">Whether the client will be muted or not</param> /// <param name="deafened">Whether the client will be deafened or not</param> public static DiscordVoiceClient JoinVoiceChannel(this DiscordSocketClient client, ulong guildId, ulong channelId, bool muted = false, bool deafened = false) { if (client.ConnectToVoiceChannels) { DiscordVoiceServer server = null; client.OnVoiceServer += (c, result) => { server = result; }; if (client.VoiceClients.ContainsKey(guildId)) { client.VoiceClients[guildId].Disconnect(); } client.ChangeVoiceState(guildId, channelId, muted, deafened); int attempts = 0; while (server == null) { if (attempts > 10 * 1000) { throw new TimeoutException("Gateway did not respond with a server"); } Thread.Sleep(1); attempts++; } if (client.VoiceClients.ContainsKey(guildId)) { client.VoiceClients[guildId].ChannelId = channelId; client.VoiceClients[guildId].Server = server; client.VoiceClients[guildId].RemoveHandlers(); return(client.VoiceClients[guildId]); } else { var vClient = new DiscordVoiceClient(client, server, channelId); client.VoiceClients.Add(guildId, vClient); return(vClient); } } else { client.ChangeVoiceState(guildId, channelId, muted, deafened); return(null); } }
private void VoiceClientStarted(DiscordClient client, DiscordVoiceClient vc, DiscordError error) { if (error.failed) { Debug.LogError("Start failed: " + error.message); return; } Debug.Log("Audio started."); voiceClient = vc; voiceClient.OnVoicePacketSend += PacketSend; voiceClient.OnVoicePacketReceived += PacketReceived; }
public void SendBytes(byte[] voiceBytes) { DiscordVoiceClient vc = client.GetVoiceClient(); try { if (vc.Connected) { vc.SendVoice(voiceBytes); } } catch (Exception ex) { client.GetTextClientLogger.Log(ex.Message, MessageLevel.Critical); } }
public static void TestRheaStream(DiscordMessageEventArgs e) { DiscordVoiceClient vc = client.GetVoiceClient(); if (vc == null) { SendBotMessage("!@Not in a channel!", e.Channel); } try { int ms = 20;//buffer 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("https://www.youtube.com/watch?v=2Vv-BfVoq4g")) { using (var resampler = new MediaFoundationResampler(mp3Reader, 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(); mp3Reader.Close(); } } } catch (Exception ex) { Log("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```", LogType.Console); } #endregion }
private void VoiceClientStopped(DiscordClient client, DiscordVoiceClient vc, DiscordError error) { if (error.failed) { Debug.LogError("Stop failed: " + error.message); return; } if (voiceClient == vc) { Debug.Log("VoiceClient stopped."); voiceClient = null; } }
void Start() { email = "email"; password = "******"; voiceClient = null; delayed = 0; sendinterval = 0; sending = false; buffer = new Buffer <float>(); source = GetComponent <AudioSource>(); receivedClip = AudioClip.Create("ReceivedAudio", 9600, 1, 48000, false); source.clip = receivedClip; ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback; client = new DiscordClient(); }
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); } })); }
private void TestRheaStream() { DiscordVoiceClient vc = client.GetVoiceClient(); try { int ms = voiceMsBuffer; 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("http://radiosidewinder.out.airtime.pro:8000/radiosidewinder_a")) { using (var resampler = new MediaFoundationResampler(mp3Reader, 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(); mp3Reader.Close(); } } } catch (Exception ex) { owner.SendMessage("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```"); } }
static Task DoVoiceURL(DiscordVoiceClient vc, string url, string name) { return(Task.Run(() => { Console.WriteLine("Beginning URL Play..."); IEnumerable <VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(url); VideoInfo video = videoInfos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 360); if (video.RequiresDecryption) { DownloadUrlResolver.DecryptDownloadUrl(video); } var videoDownloader = new VideoDownloader(video, Path.Combine("./downloads/", "temp" + name + video.VideoExtension)); videoDownloader.DownloadProgressChanged += (sender, args) => Console.WriteLine(args.ProgressPercentage); videoDownloader.DownloadFinished += (sender, args) => { Console.WriteLine("Beginning conversion..."); var inputFile = new MediaFile { Filename = @"./downloads/temp" + name + video.VideoExtension }; var outputFile = new MediaFile { Filename = @"./music/" + name + ".mp3" }; using (var engine = new Engine()) { Console.WriteLine("Converting..."); engine.Convert(inputFile, outputFile); System.Threading.Thread.Sleep(2000); Console.WriteLine("Done."); File.Delete("downloads/temp" + name + video.VideoExtension); } }; Console.WriteLine("Downloading youtube video..."); videoDownloader.Execute(); })); }
public override void Execute(DiscordSocketClient client, string[] args, Message message) { VoiceChannel channel; try { channel = client.GetChannel(Program.VoiceStates[message.Guild.Id].First(s => s.UserId == message.Author.User.Id).Channel.Id).ToVoiceChannel(); } catch { message.Channel.SendMessage("You must be connected to a voice channel to play music"); return; } if (!Program.Sessions.ContainsKey(message.Guild.Id) || Program.Sessions[message.Guild.Id].Channel.Id != channel.Id) { DiscordVoiceClient voiceClient = client.JoinVoiceChannel(message.Guild, channel, false, true); voiceClient.OnConnected += (c, e) => { var session = new MusicSession(message.Guild) { Client = voiceClient, Channel = channel, Queue = new Queue <Track>(), }; Program.Sessions.Add(message.Guild.Id, session); message.Channel.SendMessage("Connected to voice channel."); Task.Run(() => session.StartQueue()); }; voiceClient.Connect(); } }
public VoiceChannelSpeakingEventArgs(DiscordVoiceClient client, IncomingVoiceStream stream) { Client = client; Stream = stream; }
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 { } } }
public void SendVoice(string file) { DiscordVoiceClient vc = Concordia.voice; 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); if (file.EndsWith(".wav")) { using (var waveReader = new WaveFileReader(file)) { int byteCount; while ((byteCount = waveReader.Read(buffer, 0, blockSize)) > 0) { if (vc.Connected) { vc.SendVoice(buffer); } else { break; } } } } else if (file.EndsWith(".mp3")) { 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; resampler.Dispose(); mp3Reader.Close(); } } } } catch (Exception ex) { Console.WriteLine("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```"); } }
public void SendAudioFile(string file) { DiscordVoiceClient vc = client.GetVoiceClient(); try { int ms = vc.VoiceConfig.FrameLengthMs; int channels = vc.VoiceConfig.Channels; int sampleRate = 48000; var outFormat = new WaveFormat(sampleRate, 16, channels); //int blockSize = 48 * 2 * channels * ms; int blockSize = 48 * 2 * channels * ms; byte[] buffer = new byte[blockSize]; vc.SetSpeaking(true); string fileFormat = file.Split(new char[] { '.' }).Last(); if (fileFormat == "mp3") { using (var mp3Reader = new Mp3FileReader(file)) using (var resampler = new MediaFoundationResampler(mp3Reader, outFormat)) { resampler.ResamplerQuality = 60; int byteCount; while ((byteCount = resampler.Read(buffer, 0, buffer.Length)) > 0) { if (byteCount < blockSize) { for (int i = byteCount; i < blockSize; i++) { buffer[i] = 0; } } SendBytes(buffer); } resampler.Dispose(); mp3Reader.Close(); } } else if (fileFormat == "wav") { using (var wavReader = new WaveFileReader(file)) using (var resampler = new MediaFoundationResampler(wavReader, outFormat)) { resampler.ResamplerQuality = 60; int byteCount; while ((byteCount = resampler.Read(buffer, 0, buffer.Length)) > 0) { if (byteCount < blockSize) { for (int i = byteCount; i < blockSize; i++) { buffer[i] = 0; } } SendBytes(buffer); } resampler.Dispose(); wavReader.Close(); } } else { throw new NotSupportedException($"File format \"{fileFormat}\" is not supported."); } } catch (Exception ex) { client.GetTextClientLogger.Log(ex.Message, MessageLevel.Critical); } }
public VoiceConnectEventArgs(DiscordVoiceClient client) { Client = client; }