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

View File

@@ -30,11 +30,6 @@ class AudioPlayer extends EventEmitter {
* @type {Prism}
*/
this.prism = new Prism();
/**
* The opus encoder that the player uses
* @type {NodeOpusEngine|OpusScriptEngine}
*/
this.opusEncoder = OpusEncoders.fetch();
this.streams = new Collection();
this.currentStream = {};
this.streamingData = {
@@ -67,6 +62,7 @@ class AudioPlayer extends EventEmitter {
destroy() {
if (this.opusEncoder) this.opusEncoder.destroy();
this.opusEncoder = null;
}
destroyCurrentStream() {
@@ -83,13 +79,25 @@ class AudioPlayer extends EventEmitter {
this.currentStream = {};
}
playUnknownStream(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
OpusEncoders.guaranteeOpusEngine();
const options = { seek, volume, passes };
/**
* Set the bitrate of the current Opus encoder.
* @param {number} value New bitrate, in kbps.
* If set to 'auto', the voice channel's bitrate will be used
*/
setBitrate(value) {
if (!value) return;
if (!this.opusEncoder) return;
const bitrate = value === 'auto' ? this.voiceConnection.channel.bitrate : value;
this.opusEncoder.setBitrate(bitrate);
}
playUnknownStream(stream, options = {}) {
this.destroy();
this.opusEncoder = OpusEncoders.fetch(options);
const transcoder = this.prism.transcode({
type: 'ffmpeg',
media: stream,
ffmpegArguments: ffmpegArguments.concat(['-ss', String(seek)]),
ffmpegArguments: ffmpegArguments.concat(['-ss', String(options.seek || 0)]),
});
this.destroyCurrentStream();
this.currentStream = {
@@ -105,9 +113,10 @@ class AudioPlayer extends EventEmitter {
return this.playPCMStream(transcoder.output, options, true);
}
playPCMStream(stream, { seek = 0, volume = 1, passes = 1 } = {}, fromUnknown = false) {
OpusEncoders.guaranteeOpusEngine();
const options = { seek, volume, passes };
playPCMStream(stream, options = {}, fromUnknown = false) {
this.destroy();
this.opusEncoder = OpusEncoders.fetch(options);
this.setBitrate(options.bitrate);
const dispatcher = this.createDispatcher(stream, options);
if (fromUnknown) {
this.currentStream.dispatcher = dispatcher;
@@ -122,8 +131,8 @@ class AudioPlayer extends EventEmitter {
return dispatcher;
}
playOpusStream(stream, { seek = 0, passes = 1 } = {}) {
const options = { seek, passes, opus: true };
playOpusStream(stream, options = {}) {
options.opus = true;
this.destroyCurrentStream();
const dispatcher = this.createDispatcher(stream, options);
this.currentStream = {
@@ -134,8 +143,7 @@ class AudioPlayer extends EventEmitter {
return dispatcher;
}
playBroadcast(broadcast, { volume = 1, passes = 1 } = {}) {
const options = { volume, passes };
playBroadcast(broadcast, options) {
this.destroyCurrentStream();
const dispatcher = this.createDispatcher(broadcast, options);
this.currentStream = {
@@ -148,7 +156,9 @@ class AudioPlayer extends EventEmitter {
return dispatcher;
}
createDispatcher(stream, options) {
createDispatcher(stream, { seek = 0, volume = 1, passes = 1 } = {}) {
const options = { seek, volume, passes };
const dispatcher = new StreamDispatcher(this, stream, options);
dispatcher.on('end', () => this.destroyCurrentStream());
dispatcher.on('error', () => this.destroyCurrentStream());