diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 9f84b10739f3..fd6de7242f0b 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -11,7 +11,7 @@ To get ready to work on the codebase, please do the following: 1. Fork & clone the repository, and make sure you're on the **master** branch 2. Run `npm install` -3. If you're working on voice, also run `npm install node-opus` or `npm install opusscript` +3. If you're working on voice, also run `npm install @discordjs/opus` or `npm install opusscript` 4. Code your heart out! 5. Run `npm test` to run ESLint and ensure any JSDoc changes are valid 6. [Submit a pull request](https://github.com/discordjs/discord.js/compare) diff --git a/README.md b/README.md index e75f670632cf..ba982df528c1 100644 --- a/README.md +++ b/README.md @@ -30,13 +30,13 @@ discord.js is a powerful [node.js](https://nodejs.org) module that allows you to Ignore any warnings about unmet peer dependencies, as they're all optional. Without voice support: `npm install discord.js` -With voice support ([node-opus](https://www.npmjs.com/package/node-opus)): `npm install discord.js node-opus` +With voice support ([@discordjs/opus](https://www.npmjs.com/package/@discordjs/opus)): `npm install discord.js @discordjs/opus` With voice support ([opusscript](https://www.npmjs.com/package/opusscript)): `npm install discord.js opusscript` ### Audio engines -The preferred audio engine is node-opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose node-opus. -Using opusscript is only recommended for development environments where node-opus is tough to get working. -For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers. +The preferred audio engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus. +Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working. +For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers. ### Optional packages - [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the WebSocket when *not* using uws (`npm install bufferutil`) diff --git a/docs/general/faq.md b/docs/general/faq.md index 2bbb90bfbb30..69864395b1cb 100644 --- a/docs/general/faq.md +++ b/docs/general/faq.md @@ -8,8 +8,8 @@ Update to Node.js 6.0.0 or newer. ## How do I get voice working? - Install FFMPEG. -- Install either the `node-opus` package or the `opusscript` package. - node-opus is greatly preferred, due to it having significantly better performance. +- Install either the `@discordjs/opus` package or the `opusscript` package. + @discordjs/opus is greatly preferred, due to it having significantly better performance. ## How do I install FFMPEG? - **npm:** `npm install ffmpeg-binaries` @@ -17,7 +17,7 @@ Update to Node.js 6.0.0 or newer. - **Ubuntu 14.04:** `sudo apt-get install libav-tools` - **Windows:** `npm install ffmpeg-binaries` or see the [FFMPEG section of AoDude's guide](https://github.com/bdistin/OhGodMusicBot/blob/master/README.md#download-ffmpeg). -## How do I set up node-opus? -- **Ubuntu:** Simply run `npm install node-opus`, and it's done. Congrats! +## How do I set up @discordjs/opus? +- **Ubuntu:** Simply run `npm install @discordjs/opus`, and it's done. Congrats! - **Windows:** Run `npm install --global --production windows-build-tools` in an admin command prompt or PowerShell. - Then, running `npm install node-opus` in your bot's directory should successfully build it. Woo! + Then, running `npm install @discordjs/opus` in your bot's directory should successfully build it. Woo! diff --git a/docs/general/welcome.md b/docs/general/welcome.md index 5c458b953aa3..8ba3f71eca68 100644 --- a/docs/general/welcome.md +++ b/docs/general/welcome.md @@ -38,13 +38,13 @@ discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to Ignore any warnings about unmet peer dependencies, as they're all optional. Without voice support: `npm install discord.js` -With voice support ([node-opus](https://www.npmjs.com/package/node-opus)): `npm install discord.js node-opus` +With voice support ([@discordjs/opus](https://www.npmjs.com/package/@discordjs/opus)): `npm install discord.js @discordjs/opus` With voice support ([opusscript](https://www.npmjs.com/package/opusscript)): `npm install discord.js opusscript` ### Audio engines -The preferred audio engine is node-opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose node-opus. -Using opusscript is only recommended for development environments where node-opus is tough to get working. -For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers. +The preferred audio engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus. +Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working. +For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers. ### Optional packages - [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the WebSocket when *not* using uws (`npm install bufferutil`) diff --git a/docs/topics/voice.md b/docs/topics/voice.md index 9db041aeb5f5..7e9faf056dd9 100644 --- a/docs/topics/voice.md +++ b/docs/topics/voice.md @@ -7,12 +7,12 @@ To get started, make sure you have: * FFmpeg - `npm install ffmpeg-binaries` * an opus encoder, choose one from below: * `npm install opusscript` - * `npm install node-opus` + * `npm install @discordjs/opus` * a good network connection -The preferred opus engine is node-opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose node-opus. -Using opusscript is only recommended for development environments where node-opus is tough to get working. -For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers. +The preferred opus engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus. +Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working. +For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers. ## Joining a voice channel The example below reacts to a message and joins the sender's voice channel, catching any errors. This is important diff --git a/package.json b/package.json index b5964c7fbe47..ea11916f5b44 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "runkitExampleFilename": "./docs/examples/ping.js", "dependencies": { "long": "^4.0.0", - "prism-media": "^0.0.3", + "prism-media": "^0.0.4", "snekfetch": "^3.6.4", "tweetnacl": "^1.0.0", "ws": "^6.0.0" @@ -44,6 +44,7 @@ "bufferutil": "^4.0.0", "erlpack": "discordapp/erlpack", "libsodium-wrappers": "^0.7.3", + "@discordjs/opus": "^0.1.0", "node-opus": "^0.2.7", "opusscript": "^0.0.6", "sodium": "^2.0.3" @@ -55,6 +56,9 @@ "erlpack": { "optional": true }, + "@discordjs/opus": { + "optional": true + }, "node-opus": { "optional": true }, @@ -93,6 +97,7 @@ "prism-media": false, "opusscript": false, "node-opus": false, + "@discordjs/opus": false, "tweetnacl": false, "sodium": false, "src/sharding/Shard.js": false, @@ -101,6 +106,7 @@ "src/client/voice/dispatcher/StreamDispatcher.js": false, "src/client/voice/opus/BaseOpusEngine.js": false, "src/client/voice/opus/NodeOpusEngine.js": false, + "src/client/voice/opus/DiscordJsOpusEngine.js": false, "src/client/voice/opus/OpusEngineList.js": false, "src/client/voice/opus/OpusScriptEngine.js": false, "src/client/voice/pcm/ConverterEngine.js": false, diff --git a/src/client/voice/opus/DiscordJsOpusEngine.js b/src/client/voice/opus/DiscordJsOpusEngine.js new file mode 100644 index 000000000000..a3759c2932f7 --- /dev/null +++ b/src/client/voice/opus/DiscordJsOpusEngine.js @@ -0,0 +1,34 @@ +const OpusEngine = require('./BaseOpusEngine'); + +class DiscordJsOpusEngine extends OpusEngine { + constructor(player) { + super(player); + const opus = require('@discordjs/opus'); + this.encoder = new opus.OpusEncoder(this.samplingRate, this.channels); + super.init(); + } + + setBitrate(bitrate) { + this.encoder.setBitrate(Math.min(128, Math.max(16, bitrate)) * 1000); + } + + setFEC(enabled) { + this.encoder.applyEncoderCTL(this.ctl.FEC, enabled ? 1 : 0); + } + + setPLP(percent) { + this.encoder.applyEncoderCTL(this.ctl.PLP, Math.min(100, Math.max(0, percent * 100))); + } + + encode(buffer) { + super.encode(buffer); + return this.encoder.encode(buffer, 1920); + } + + decode(buffer) { + super.decode(buffer); + return this.encoder.decode(buffer, 1920); + } +} + +module.exports = DiscordJsOpusEngine; diff --git a/src/client/voice/opus/NodeOpusEngine.js b/src/client/voice/opus/NodeOpusEngine.js index 02e8806374cf..a9ffc551fa0d 100644 --- a/src/client/voice/opus/NodeOpusEngine.js +++ b/src/client/voice/opus/NodeOpusEngine.js @@ -1,15 +1,9 @@ const OpusEngine = require('./BaseOpusEngine'); -let opus; - class NodeOpusEngine extends OpusEngine { constructor(player) { super(player); - try { - opus = require('node-opus'); - } catch (err) { - throw err; - } + const opus = require('node-opus'); this.encoder = new opus.OpusEncoder(this.samplingRate, this.channels); super.init(); } diff --git a/src/client/voice/opus/OpusEngineList.js b/src/client/voice/opus/OpusEngineList.js index 7fbec9ea4136..cf63fbce8e95 100644 --- a/src/client/voice/opus/OpusEngineList.js +++ b/src/client/voice/opus/OpusEngineList.js @@ -1,4 +1,5 @@ const list = [ + require('./DiscordJsOpusEngine'), require('./NodeOpusEngine'), require('./OpusScriptEngine'), ]; diff --git a/src/client/voice/opus/OpusScriptEngine.js b/src/client/voice/opus/OpusScriptEngine.js index a5e046d40bfa..271a068705b1 100644 --- a/src/client/voice/opus/OpusScriptEngine.js +++ b/src/client/voice/opus/OpusScriptEngine.js @@ -1,15 +1,9 @@ const OpusEngine = require('./BaseOpusEngine'); -let OpusScript; - class OpusScriptEngine extends OpusEngine { constructor(player) { super(player); - try { - OpusScript = require('opusscript'); - } catch (err) { - throw err; - } + const OpusScript = require('opusscript'); this.encoder = new OpusScript(this.samplingRate, this.channels); super.init(); }