Ejemplo n.º 1
0
        private async Task PlayAsync(VoiceNextConnection connection, AudioInfo audioInfo)
        {
            if (audioInfo.CommandContext.Command.Module.GetInstance(audioInfo.CommandContext.Services) is SkiCommandModule skiCommandModule)
            {
                skiCommandModule.LogMessage(audioInfo.CommandContext, $"Playing: {audioInfo.FullPath}");
                try
                {
                    ProcessStartInfo processStartInfo = new ProcessStartInfo("ffmpeg", $@"-loglevel panic -i ""{audioInfo.FullPath}"" -ac 2 -f s16le -ar 48000 pipe:1")
                    {
                        RedirectStandardOutput = true,
                        UseShellExecute        = false
                    };
                    Process             ffmpeg         = Process.Start(processStartInfo);
                    Stream              audioStream    = ffmpeg.StandardOutput.BaseStream;
                    VoiceTransmitStream transmitStream = connection.GetTransmitStream();
                    await audioStream.CopyToAsync(transmitStream, Program.CancellationTokenSource.Token);

                    await transmitStream.FlushAsync(Program.CancellationTokenSource.Token);

                    await connection.WaitForPlaybackFinishAsync();
                }

                catch (System.Exception e)
                {
                    skiCommandModule.LogMessage(audioInfo.CommandContext, "Failed to play", e);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Plays the audio.
        /// </summary>
        /// <param name="voiceNextCon">The voice next con.</param>
        /// <param name="filename">The filename.</param>
        /// <returns>Completed Task once the audio finishes playing</returns>
        public async Task PlayAudio(VoiceNextConnection voiceNextCon, string filename)
        {
            // wait for current playback to finish
            while (voiceNextCon.IsPlaying)
            {
                Program.Client.DebugLogger.Info($"Waiting for current audio to finish");
                await voiceNextCon.WaitForPlaybackFinishAsync();
            }

            // play
            await voiceNextCon.SendSpeakingAsync(true);

            try
            {
                var ffmpeg_inf = new ProcessStartInfo
                {
                    FileName               = "ffmpeg",
                    Arguments              = $"-i \"{filename}\" -ac 2 -f s16le -ar 48000 pipe:1",
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true
                };

                var ffmpeg = Process.Start(ffmpeg_inf);
                var ffout  = ffmpeg.StandardOutput.BaseStream;

                // let's buffer ffmpeg output
                using (var ms = new MemoryStream())
                {
                    await ffout.CopyToAsync(ms);

                    ms.Position = 0;

                    var buff = new byte[3840]; // buffer to hold the PCM data
                    var br   = 0;
                    while ((br = ms.Read(buff, 0, buff.Length)) > 0)
                    {
                        if (br < buff.Length) // it's possible we got less than expected, let's null the remaining part of the buffer
                        {
                            for (var i = br; i < buff.Length; i++)
                            {
                                buff[i] = 0;
                            }
                        }
                        await voiceNextCon.SendAsync(buff, 20); // we're sending 20ms of data
                    }
                }
            }
            catch (Exception ex)
            {
                Program.Client.DebugLogger.Info($"Exception playing audio {ex}");
                throw ex;
            }
            finally
            {
                await voiceNextCon.SendSpeakingAsync(false);

                Program.Client.DebugLogger.Info($"Finished playing audio");
            }
        }
Ejemplo n.º 3
0
        public async Task PlayAudio(CommandContext ctx, VoiceNextConnection VoiceConnection, string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("This file could not be found", filePath);
            }

            if (VoiceConnection.IsPlaying)
            {
                await VoiceConnection.WaitForPlaybackFinishAsync();
            }

            Exception exc = null;
            await VoiceConnection.SendSpeakingAsync(true);

            try
            {
                var psi = new ProcessStartInfo
                {
                    FileName  = "ffmpeg",
                    Arguments = $@"-i ""{filePath}"" -ac 2 -f s16le -ar 48000 pipe:1",
                    RedirectStandardOutput = true,
                    UseShellExecute        = false
                };

                var ffmpeg = Process.Start(psi);
                var ffout  = ffmpeg.StandardOutput.BaseStream;

                ffout.CopyTo(VoiceConnection.GetTransmitStream());
            }
            catch (Exception ex) { exc = ex; }
            finally
            {
                await VoiceConnection.SendSpeakingAsync(false);
            }

            if (exc != null)
            {
                await ctx.RespondAsync($"An exception occurred during playback: `{exc.GetType()}: {exc.Message}`");
            }
        }
Ejemplo n.º 4
0
        public async Task Play(CommandContext ctx,
                               [Description("full path to the file to play."), RemainingText] string filename)
        {
            VoiceNextConnection vnc = await GetVNextConnection(ctx);

            if (vnc == null)
            {
                return;
            }
            if (filename == null)
            {
                await ctx.RespondAsync("No file specified.");

                return;
            }
            if (!File.Exists(filename))
            {
                await ctx.RespondAsync($"File `{filename}` does not exist.");

                return;
            }
            while (vnc.IsPlaying)
            {
                await ctx.Message.RespondAsync("Waiting for audio to end.");

                await vnc.WaitForPlaybackFinishAsync();
            }
            await ctx.Message.RespondAsync($"Playing `{filename}`");

            await vnc.SendSpeakingAsync();

            try
            {
                /* borrowed from
                 * https://github.com/RogueException/Discord.Net/blob/5ade1e387bb8ea808a9d858328e2d3db23fe0663/docs/guides/voice/samples/audio_create_ffmpeg.cs
                 */
                var ffmpegInf = new ProcessStartInfo
                {
                    FileName               = "ffmpeg",
                    Arguments              = $"-i \"{filename}\" -ac 2 -f s16le -ar 48000 pipe:1",
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true
                };
                Process ffmpeg = Process.Start(ffmpegInf);
                if (ffmpeg != null)
                {
                    Stream ffout = ffmpeg.StandardOutput.BaseStream;

                    VoiceTransmitSink transmit = vnc.GetTransmitSink();
                    await ffout.CopyToAsync(transmit);

                    await transmit.FlushAsync();

                    await vnc.WaitForPlaybackFinishAsync();

                    ffout.Close();
                }
            }
            catch (Exception ex)
            {
                await ctx.RespondAsync($"An exception occured during playback: `{ex.GetType()}: {ex.Message}`");
            }
        }
        public async Task Play(CommandContext ctx, [RemainingText, Description("Full path to the file to play.")] string path)
        {
            string filename = Path.Combine(Directory.GetCurrentDirectory(), "Audios", path);

            VoiceNextExtension vnext = ctx.Client.GetVoiceNext();

            if (vnext == null)
            {
                await ctx.RespondAsync("VNext is not enabled or configured.");

                return;
            }

            VoiceNextConnection vnc = vnext.GetConnection(ctx.Guild);

            if (vnc == null)
            {
                await ctx.RespondAsync("Not connected in this guild.");

                return;
            }

            if (!File.Exists(filename))
            {
                await ctx.RespondAsync($"File `{path}` does not exist.");

                return;
            }

            while (vnc.IsPlaying)
            {
                await vnc.WaitForPlaybackFinishAsync();
            }

            Exception exc = null;
            await ctx.Message.RespondAsync($"Playing `{path}`");

            try
            {
                await vnc.SendSpeakingAsync();

                Stream ffout = new FfmpegUtils().GetffmpegStream(filename);

                VoiceTransmitSink txStream = vnc.GetTransmitSink();
                await ffout.CopyToAsync(txStream);

                await txStream.FlushAsync();

                await vnc.WaitForPlaybackFinishAsync();
            }
            catch (Exception ex)
            {
                exc = ex;
            }
            finally
            {
                await vnc.SendSpeakingAsync(false);

                await ctx.Message.RespondAsync($"Finished playing `{path}`");
            }

            if (exc != null)
            {
                await ctx.RespondAsync($"An exception occured during playback: `{exc.GetType()}: {exc.Message}`");
            }
        }