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:
aemino
2017-07-26 01:06:40 -07:00
committed by Crawl
parent 7eb9e65c41
commit 4342ed29a8
9 changed files with 97 additions and 58 deletions
+22 -5
View File
@@ -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);
+5 -1
View File
@@ -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);
}
+1 -7
View File
@@ -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');
};
+5 -1
View File
@@ -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);
}