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 3.9 kB

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