mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-16 11:28:18 +00:00
Audio bitrate support (#1439)
* Audio bitrate support Note: not implemented for VoiceBroadcasts * Fix default args, auto bitrate * Late night typos are the best * Changes bitrate to kbps for VoiceChannel stuff * Add methods to manipulate bitrate while encoding
This commit is contained in:
@@ -4,21 +4,38 @@
|
||||
*/
|
||||
class BaseOpus {
|
||||
/**
|
||||
* @param {Object} [options] The options to apply to the Opus engine
|
||||
* @param {boolean} [options.fec] Whether to enable forward error correction (defaults to false)
|
||||
* @param {number} [options.plp] The expected packet loss percentage (0-1 inclusive, defaults to 0)
|
||||
* @param {Object} [options] The options to apply to the Opus engine.
|
||||
* @param {number} [options.bitrate=48] The desired bitrate (kbps).
|
||||
* @param {boolean} [options.fec=false] Whether to enable forward error correction.
|
||||
* @param {number} [options.plp=0] The expected packet loss percentage.
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
constructor({ bitrate = 48, fec = false, plp = 0 } = {}) {
|
||||
this.ctl = {
|
||||
BITRATE: 4002,
|
||||
FEC: 4012,
|
||||
PLP: 4014,
|
||||
};
|
||||
|
||||
this.options = options;
|
||||
this.samplingRate = 48000;
|
||||
this.channels = 2;
|
||||
|
||||
/**
|
||||
* The desired bitrate (kbps)
|
||||
* @type {number}
|
||||
*/
|
||||
this.bitrate = bitrate;
|
||||
|
||||
/**
|
||||
* Miscellaneous Opus options
|
||||
* @type {Object}
|
||||
*/
|
||||
this.options = { fec, plp };
|
||||
}
|
||||
|
||||
init() {
|
||||
try {
|
||||
this.setBitrate(this.bitrate);
|
||||
|
||||
// Set FEC (forward error correction)
|
||||
if (this.options.fec) this.setFEC(this.options.fec);
|
||||
|
||||
|
||||
@@ -10,10 +10,14 @@ class NodeOpusEngine extends OpusEngine {
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
this.encoder = new opus.OpusEncoder(48000, 2);
|
||||
this.encoder = new opus.OpusEncoder(this.samplingRate, this.channels);
|
||||
super.init();
|
||||
}
|
||||
|
||||
setBitrate(bitrate) {
|
||||
this.encoder.applyEncoderCTL(this.ctl.BITRATE, Math.min(128, Math.max(16, bitrate)) * 1000);
|
||||
}
|
||||
|
||||
setFEC(enabled) {
|
||||
this.encoder.applyEncoderCTL(this.ctl.FEC, enabled ? 1 : 0);
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@ const list = [
|
||||
require('./OpusScriptEngine'),
|
||||
];
|
||||
|
||||
let opusEngineFound;
|
||||
|
||||
function fetch(Encoder, engineOptions) {
|
||||
try {
|
||||
return new Encoder(engineOptions);
|
||||
@@ -27,10 +25,6 @@ exports.fetch = engineOptions => {
|
||||
const fetched = fetch(encoder, engineOptions);
|
||||
if (fetched) return fetched;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
exports.guaranteeOpusEngine = () => {
|
||||
if (typeof opusEngineFound === 'undefined') opusEngineFound = Boolean(exports.fetch());
|
||||
if (!opusEngineFound) throw new Error('OPUS_ENGINE_MISSING');
|
||||
throw new Error('OPUS_ENGINE_MISSING');
|
||||
};
|
||||
|
||||
@@ -10,10 +10,14 @@ class OpusScriptEngine extends OpusEngine {
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
this.encoder = new OpusScript(48000, 2);
|
||||
this.encoder = new OpusScript(this.samplingRate, this.channels);
|
||||
super.init();
|
||||
}
|
||||
|
||||
setBitrate(bitrate) {
|
||||
this.encoder.encoderCTL(this.ctl.BITRATE, Math.min(128, Math.max(16, bitrate)) * 1000);
|
||||
}
|
||||
|
||||
setFEC(enabled) {
|
||||
this.encoder.encoderCTL(this.ctl.FEC, enabled ? 1 : 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user