diff --git a/NadekoBot.sln b/NadekoBot.sln index d80a8b3b..af29b679 100644 --- a/NadekoBot.sln +++ b/NadekoBot.sln @@ -11,7 +11,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.Net.Modules", "E:\O EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.Net.Commands", "E:\Ostalo\Discord.Net\src\Discord.Net.Commands.Net45\Discord.Net.Commands.csproj", "{1B5603B4-6F8F-4289-B945-7BAAE523D740}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.Net.Audio", "E:\Ostalo\Discord.Net\src\Discord.Net.Audio.Net5\Discord.Net.Audio.csproj", "{7BFEF748-B934-4621-9B11-6302E3A9F6B3}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.Net.Audio", "E:\Ostalo\Discord.Net\src\Discord.Net.Audio.Net45\Discord.Net.Audio.csproj", "{7BFEF748-B934-4621-9B11-6302E3A9F6B3}" EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index f41c640e..7da39505 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -21,7 +21,7 @@ namespace NadekoBot.Modules private static bool exit = true; public static bool NextSong = false; - public static DiscordAudioClient Voice; + public static IAudioClient Voice; public static Channel VoiceChannel; public static bool Pause = false; public static List SongQueue = new List(); @@ -96,6 +96,21 @@ namespace NadekoBot.Modules } }); + cgb.CreateCommand("testq") + .Description("Queue a song using a multi/single word name.\nUsage: `!m q Dream Of Venice`") + .Parameter("Query", ParameterType.Unparsed) + .Do(async e => { + var youtube = YouTube.Default; + var video = youtube.GetAllVideos(e.GetArg("Query")) + .Where(v => v.AdaptiveKind == AdaptiveKind.Audio) + .OrderByDescending(v => v.AudioBitrate).FirstOrDefault(); + + if (video?.Uri != "" && video.Uri != null) { + SongQueue.Add(video); + await e.Send("**Queued** " + video.FullName); + } + }); + cgb.CreateCommand("q") .Alias("yq") .Description("Queue a song using a multi/single word name.\nUsage: `!m q Dream Of Venice`") @@ -145,7 +160,7 @@ namespace NadekoBot.Modules { if (Voice != null) return; VoiceChannel = e.Server.FindChannels(e.GetArg("ChannelName").Trim(), ChannelType.Voice).FirstOrDefault(); - //Voice = await client.JoinVoiceServer(VoiceChannel); + Voice = await client.Audio().Join(VoiceChannel); Exit = false; NextSong = false; Pause = false; @@ -166,18 +181,28 @@ namespace NadekoBot.Modules await e.Send( "Exiting..."); return; } - int blockSize = 1920; - byte[] buffer = new byte[1920]; - //float multiplier = 1.0f / 48000 / 2; + int blockSize = 3840; + byte[] buffer = new byte[3840]; var msg = await e.Send( "Playing " + Music.CurrentSong.FullName + " [00:00]"); int counter = 0; int byteCount; using (var stream = GetAudioFileStream(Music.CurrentSong.Uri)) { - while ((byteCount = stream.Read(buffer, 0, blockSize)) > 0) + var m = await e.Send("Downloading song..."); + var memStream = new MemoryStream(); + while (true) { + byte[] buff = new byte[0x4000 * 10]; + int read = stream.Read(buff, 0, buff.Length); + if (read <= 0) break; + memStream.Write(buff, 0, read); + } + + e.Send("Song downloaded"); + memStream.Position = 0; + while ((byteCount = memStream.Read(buffer, 0, blockSize)) > 0) { - // Voice.SendVoicePCM(buffer, byteCount); + Voice.Send(buffer, byteCount); counter += blockSize; if (NextSong) { @@ -194,10 +219,10 @@ namespace NadekoBot.Modules } }); } - // await Voice.WaitVoice(); + Voice.Wait(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } - // await client.LeaveVoiceServer(VoiceChannel.Server); + await Voice.Disconnect(); Voice = null; VoiceChannel = null; }); @@ -209,7 +234,7 @@ namespace NadekoBot.Modules Process p = Process.Start(new ProcessStartInfo() { FileName = "ffmpeg", - Arguments = "-i \"" + Uri.EscapeUriString(file) + "\" -f s16le -ar 48000 -af volume=1 -ac 1 pipe:1 ", + Arguments = "-i \"" + Uri.EscapeUriString(file) + "\" -f s16le -ar 48000 -af volume=1 -ac 2 pipe:1 ", UseShellExecute = false, RedirectStandardOutput = true }); diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index cdb8cded..7cff4077 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -7,6 +7,7 @@ using Discord.Commands; using NadekoBot.Modules; using Discord.Modules; using Discord.Legacy; +using Discord.Audio; namespace NadekoBot { @@ -63,12 +64,17 @@ namespace NadekoBot //create module service var modules = client.Services.Add(new ModuleService()); + //add audio service + var audio = client.Services.Add(new AudioService(new AudioServiceConfig() { + Channels = 2 + })); + //install modules modules.Install(new Administration(), "Administration", FilterType.Unrestricted); modules.Install(new Conversations(), "Conversations", FilterType.Unrestricted); modules.Install(new Gambling(), "Gambling", FilterType.Unrestricted); modules.Install(new Games(), "Games", FilterType.Unrestricted); - //modules.Install(new Music(), "Music", FilterType.Unrestricted); + modules.Install(new Music(), "Music", FilterType.Unrestricted); modules.Install(new Searches(), "Searches", FilterType.Unrestricted); //run the bot diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index f8552f42..f5b9574b 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -127,7 +127,7 @@ - + {7bfef748-b934-4621-9b11-6302e3a9f6b3} Discord.Net.Audio