You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sending-voice.md 3.9 kB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ---
  2. title: Sending Voice
  3. ---
  4. **Information on this page is subject to change!**
  5. >[!WARNING]
  6. >This article is out of date, and has not been rewritten yet.
  7. Information is not guaranteed to be accurate.
  8. ## Installing
  9. Audio requires two native libraries, `libsodium` and `opus`.
  10. Both of these libraries must be placed in the runtime directory of your
  11. bot. (When developing on .NET Framework, this would be `bin/debug`,
  12. when developing on .NET Core, this is where you execute `dotnet run`
  13. from; typically the same directory as your csproj).
  14. For Windows Users, precompiled binaries are available for your
  15. convienence [here](https://discord.foxbot.me/binaries/)
  16. For Linux Users, you will need to compile [Sodium] and [Opus] from
  17. source, or install them from your package manager.
  18. [Sodium]: https://download.libsodium.org/libsodium/releases/
  19. [Opus]: http://downloads.xiph.org/releases/opus/
  20. ## Joining a Channel
  21. Joining a channel is the first step to sending audio, and will return
  22. an [IAudioClient] to send data with.
  23. To join a channel, simply await [ConnectAsync] on any instance of an
  24. @Discord.IVoiceChannel.
  25. [!code-csharp[Joining a Channel](samples/joining_audio.cs)]
  26. The client will sustain a connection to this channel until it is
  27. kicked, disconnected from Discord, or told to disconnect.
  28. It should be noted that voice connections are created on a per-guild
  29. basis; only one audio connection may be open by the bot in a single
  30. guild. To switch channels within a guild, invoke [ConnectAsync] on
  31. another voice channel in the guild.
  32. [IAudioClient]: xref:Discord.Audio.IAudioClient
  33. [ConnectAsync]: xref:Discord.IVoiceChannel#Discord_IVoiceChannel_ConnectAsync
  34. ## Transmitting Audio
  35. ### With FFmpeg
  36. [FFmpeg] is an open source, highly versatile AV-muxing tool. This is
  37. the recommended method of transmitting audio.
  38. Before you begin, you will need to have a version of FFmpeg downloaded
  39. and placed somewhere in your PATH (or alongside the bot, in the same
  40. location as libsodium and opus). Windows binaries are available on
  41. [FFmpeg's download page].
  42. [FFmpeg]: https://ffmpeg.org/
  43. [FFmpeg's download page]: https://ffmpeg.org/download.html
  44. First, you will need to create a Process that starts FFmpeg. An
  45. example of how to do this is included below, though it is important
  46. that you return PCM at 48000hz.
  47. >[!NOTE]
  48. >As of the time of this writing, Discord.Audio struggles significantly
  49. >with processing audio that is already opus-encoded; you will need to
  50. >use the PCM write streams.
  51. [!code-csharp[Creating FFmpeg](samples/audio_create_ffmpeg.cs)]
  52. Next, to transmit audio from FFmpeg to Discord, you will need to
  53. pull an [AudioOutStream] from your [IAudioClient]. Since we're using
  54. PCM audio, use [IAudioClient.CreatePCMStream].
  55. The sample rate argument doesn't particularly matter, so long as it is
  56. a valid rate (120, 240, 480, 960, 1920, or 2880). For the sake of
  57. simplicity, I recommend using 1920.
  58. Channels should be left at `2`, unless you specified a different value
  59. for `-ac 2` when creating FFmpeg.
  60. [AudioOutStream]: xref:Discord.Audio.AudioOutStream
  61. [IAudioClient.CreatePCMStream]: xref:Discord.Audio.IAudioClient#Discord_Audio_IAudioClient_CreatePCMStream_System_Int32_System_Int32_System_Nullable_System_Int32__System_Int32_
  62. Finally, audio will need to be piped from FFmpeg's stdout into your
  63. AudioOutStream. This step can be as complex as you'd like it to be, but
  64. for the majority of cases, you can just use [Stream.CopyToAsync], as
  65. shown below.
  66. [Stream.CopyToAsync]: https://msdn.microsoft.com/en-us/library/hh159084(v=vs.110).aspx
  67. If you are implementing a queue for sending songs, it's likely that
  68. you will want to wait for audio to stop playing before continuing on
  69. to the next song. You can await `AudioOutStream.FlushAsync` to wait for
  70. the audio client's internal buffer to clear out.
  71. [!code-csharp[Sending Audio](samples/audio_ffmpeg.cs)]