summaryrefslogtreecommitdiff
path: root/DiscoBot/Audio/Voice.cs
blob: 2c8f75ecd4067f10de3c83ecc6e9a8cefe832097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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;
    using Discord.WebSocket;

    public class Voice : ModuleBase
    {
        public static IAudioClient Client { get; set; }

        public static void Send(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)
        {
//            Permissions.Test(this.Context, "Meister");

            if (Client != null)
            {
                SoundEffects.Play(Sound.Nooo);
                await Client.StopAsync();
                Client = null;
            }
        }

        [Command("volume")]
        public void SetVolume(int volume)
        {
            if (volume <= 100 && volume >= 0)
            {
                SoundEffects.Volume = volume;
            }
        }

        [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.Send(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);
        }
    }
}