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.

voice.md 4.3 kB

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