summaryrefslogtreecommitdiff
path: root/DiscoBot/Audio
diff options
context:
space:
mode:
authorTrueDoctor <d-kobert@web.de>2018-04-11 14:41:01 +0200
committerTrueDoctor <d-kobert@web.de>2018-04-11 14:41:01 +0200
commite5e0c1c5c9fb6afd68ebe9dc075af23f46f11e25 (patch)
tree9caffddadea2851b929efa74531c801d0f8c4454 /DiscoBot/Audio
parent351067a5203307fc0c1a14ae2be84eae71246af9 (diff)
General Cleanup and retructuring
Diffstat (limited to 'DiscoBot/Audio')
-rw-r--r--DiscoBot/Audio/AudioModule.cs89
-rw-r--r--DiscoBot/Audio/Soundeffects.cs60
-rw-r--r--DiscoBot/Audio/Voice.cs112
3 files changed, 218 insertions, 43 deletions
diff --git a/DiscoBot/Audio/AudioModule.cs b/DiscoBot/Audio/AudioModule.cs
index 0c3814f..9eaf1f2 100644
--- a/DiscoBot/Audio/AudioModule.cs
+++ b/DiscoBot/Audio/AudioModule.cs
@@ -12,61 +12,64 @@ using DiscoBot.Commands;
using Discord;
using Discord.Commands;
-public class AudioModule : ModuleBase
+namespace DiscoBot.Audio
{
- // Scroll down further for the AudioService.
- // Like, way down
- private readonly AudioService _service;
-
- // Remember to add an instance of the AudioService
- // to your IServiceCollection when you initialize your bot
- public AudioModule(AudioService service)
- {
- _service = service;
- Dsa.Service = service;
- }
-
- // You *MUST* mark these commands with 'RunMode.Async'
- // otherwise the bot will not respond until the Task times out.
- [Command("_join", RunMode = RunMode.Async)]
- public async Task JoinCmd()
+ public class AudioModule : ModuleBase
{
- await _service.JoinAudio(Context.Guild, (Context.User as IVoiceState).VoiceChannel);
- }
+ // Scroll down further for the AudioService.
+ // Like, way down
+ private readonly AudioService _service;
- // Remember to add preconditions to your commands,
- // this is merely the minimal amount necessary.
- // Adding more commands of your own is also encouraged.
- [Command("_leave", RunMode = RunMode.Async)]
- public async Task LeaveCmd()
- {
- await _service.LeaveAudio(Context.Guild);
- }
+ // Remember to add an instance of the AudioService
+ // to your IServiceCollection when you initialize your bot
+ public AudioModule(AudioService service)
+ {
+ _service = service;
+ Dsa.Service = service;
+ }
- [Command("_play", RunMode = RunMode.Async)]
- public async Task PlayCmd([Remainder] string song)
- {
- if (Dsa.GeneralContext == null)
+ // You *MUST* mark these commands with 'RunMode.Async'
+ // otherwise the bot will not respond until the Task times out.
+ [Command("_join", RunMode = RunMode.Async)]
+ public async Task JoinCmd()
{
- Dsa.GeneralContext = this.Context;
+ await _service.JoinAudio(Context.Guild, (Context.User as IVoiceState).VoiceChannel);
}
- var sounds = Enum.GetValues(typeof(Sound));
- var soundList = new List<Sound>();
- foreach (var sound in sounds)
+ // Remember to add preconditions to your commands,
+ // this is merely the minimal amount necessary.
+ // Adding more commands of your own is also encouraged.
+ [Command("_leave", RunMode = RunMode.Async)]
+ public async Task LeaveCmd()
{
- soundList.Add((Sound)sound);
+ await _service.LeaveAudio(Context.Guild);
}
- var sc = new SpellCorrect();
+ [Command("_play", RunMode = RunMode.Async)]
+ public async Task PlayCmd([Remainder] string song)
+ {
+ if (Dsa.GeneralContext == null)
+ {
+ Dsa.GeneralContext = this.Context;
+ }
- var tSound = soundList.OrderBy(x => sc.Compare(song, x.ToString())).First();
+ var sounds = Enum.GetValues(typeof(Sound));
+ var soundList = new List<Sound>();
+ foreach (var sound in sounds)
+ {
+ soundList.Add((Sound)sound);
+ }
- if (sc.Compare(song, tSound.ToString()) > SpellCorrect.ErrorThreshold)
- {
- await _service.SendAudioAsync(Context.Guild, Context.Channel, song);
- }
+ var sc = new SpellCorrect();
+
+ var tSound = soundList.OrderBy(x => sc.Compare(song, x.ToString())).First();
- await SoundEffects.Play(tSound);
+ if (sc.Compare(song, tSound.ToString()) > SpellCorrect.ErrorThreshold)
+ {
+ await _service.SendAudioAsync(Context.Guild, Context.Channel, song);
+ }
+
+ await SoundEffects.Play(tSound);
+ }
}
} \ No newline at end of file
diff --git a/DiscoBot/Audio/Soundeffects.cs b/DiscoBot/Audio/Soundeffects.cs
new file mode 100644
index 0000000..555a79c
--- /dev/null
+++ b/DiscoBot/Audio/Soundeffects.cs
@@ -0,0 +1,60 @@
+namespace DiscoBot.Audio
+{
+ using System;
+ using System.Threading.Tasks;
+
+ using DiscoBot.Commands;
+
+ public enum Sound
+ {
+ Bell,
+ Ding,
+ Nooo,
+ Monterkill,
+ Finish,
+ Wrong,
+ Magic
+ }
+
+ public static class SoundEffects
+ {
+ public static async Task Play(Sound s)
+ {
+ string url = string.Empty;
+ int vol = 256;
+ switch (s)
+ {
+ case Sound.Bell:
+ case Sound.Ding:
+ url = "https://www.myinstants.com/media/sounds/boxing-bell.mp3";
+ break;
+ case Sound.Finish:
+ url = "https://www.myinstants.com/media/sounds/finishhim.swf.mp3";
+ break;
+ case Sound.Magic:
+ url = "https://www.myinstants.com/media/sounds/dream-harp-sound-effect.mp3";
+ break;
+ case Sound.Monterkill:
+ url = "https://www.myinstants.com/media/sounds/announcer_kill_monster_01.mp3";
+ break;
+ case Sound.Nooo:
+ url = "https://www.myinstants.com/media/sounds/nooo.swf.mp3";
+ break;
+ case Sound.Wrong:
+ // url = "https://www.myinstants.com/media/sounds/stupid_dum_03.mp3";
+ // vol = 10;
+ url = "https://www.myinstants.com/media/sounds/wrong-answer-sound-effect.mp3";
+ vol = 50;
+ break;
+ }
+
+ if (url != string.Empty)
+ {
+ // await Dsa.Service.SendAudioAsync(url, vol);
+ await Voice.SendAsync(url, vol);
+ }
+
+ throw new Exception("Ton Existiert nicht");
+ }
+ }
+}
diff --git a/DiscoBot/Audio/Voice.cs b/DiscoBot/Audio/Voice.cs
new file mode 100644
index 0000000..d1f7915
--- /dev/null
+++ b/DiscoBot/Audio/Voice.cs
@@ -0,0 +1,112 @@
+namespace DiscoBot.Audio
+{
+ using System;
+ using System.Collections.Generic;
+ using System.ComponentModel;
+ using System.Diagnostics;
+ using System.Linq;
+ using System.Threading.Tasks;
+
+ using DiscoBot.Auxiliary;
+
+ using Discord;
+ using Discord.Audio;
+ using Discord.Commands;
+
+ public class Voice : ModuleBase
+ {
+ public static IAudioClient Client { get; set; }
+
+ public static async Task SendAsync(string path, int volume = 256)
+ {
+ if (Client == null)
+ {
+ throw new NullReferenceException("Bot befindet sich nicht in einem Sprachchannel");
+ }
+
+ // Create FFmpeg using the previous example
+ var ffmpeg = CreateStream(path, volume);
+ var output = ffmpeg.StandardOutput.BaseStream;
+ var barInvoker = new BackgroundWorker();
+ barInvoker.DoWork += delegate
+ {
+ var discord = Client.CreatePCMStream(AudioApplication.Music);
+ output.CopyToAsync(discord);
+
+ discord.FlushAsync();
+ };
+
+ barInvoker.RunWorkerAsync();
+ }
+
+ [Command("join", RunMode = RunMode.Async)]
+ public async Task JoinChannelAsync(IVoiceChannel channel = null)
+ {
+ var msg = this.Context.Message;
+
+ // Get the audio channel
+ channel = channel ?? (msg.Author as IGuildUser)?.VoiceChannel;
+ if (channel == null)
+ {
+ await msg.Channel.SendMessageAsync(
+ "User must be in a voice channel, or a voice channel must be passed as an argument.");
+ return;
+ }
+
+ // For the next step with transmitting audio, you would want to pass this Audio Client in to a service.
+ var audioClient = await channel.ConnectAsync();
+ Client = audioClient;
+ }
+
+ [Command("leave", RunMode = RunMode.Async)]
+ public async Task LeaveChannelAsync(IVoiceChannel channel = null)
+ {
+ if (Client != null)
+ {
+ var wait = SoundEffects.Play(Sound.Nooo);
+ await Client.StopAsync();
+ Client = null;
+ wait.Wait();
+ }
+ }
+
+ [Command("play", RunMode = RunMode.Async)]
+ public async Task PlayAudioAsync(string path)
+ {
+ if (Client == null)
+ {
+ await this.Context.Channel.SendMessageAsync("Erst Joinen!");
+ }
+
+ var sounds = Enum.GetValues(typeof(Sound));
+ var soundList = new List<Sound>();
+ foreach (var sound in sounds)
+ {
+ soundList.Add((Sound)sound);
+ }
+
+ var sc = new SpellCorrect();
+
+ var tSound = soundList.OrderBy(x => sc.Compare(path, x.ToString())).First();
+
+ if (sc.Compare(path, tSound.ToString()) > SpellCorrect.ErrorThreshold)
+ {
+ await Task.Run(() => Voice.SendAsync(path));
+ }
+
+ await Task.Run(() => SoundEffects.Play(tSound));
+ }
+
+ private static Process CreateStream(string path, int vol = 256)
+ {
+ var ffmpeg = new ProcessStartInfo
+ {
+ FileName = "ffmpeg",
+ Arguments = $"-i {path} -ac 2 -f s16le -ar 48000 -ab 620000 -vol {vol} pipe:1",
+ UseShellExecute = false,
+ RedirectStandardOutput = true,
+ };
+ return Process.Start(ffmpeg);
+ }
+ }
+}