voice: allow for changing volume with webm/ogg streams

This commit is contained in:
Amish Shah
2018-04-10 21:19:29 +01:00
parent e845758037
commit cb69102e5e
2 changed files with 12 additions and 3 deletions

View File

@@ -69,8 +69,17 @@ class BasePlayer extends EventEmitter {
playOpusStream(stream, options, streams = {}) {
this.destroyDispatcher();
streams.opus = stream;
if (options.volume !== false && !streams.input) {
streams.input = stream;
const decoder = new prism.opus.Decoder({ channels: 2, rate: 48000, frameSize: 960 });
const volume = streams.volume = new prism.VolumeTransformer16LE(null, { volume: options ? options.volume : 1 });
streams.opus = stream
.pipe(decoder)
.pipe(volume)
.pipe(new prism.opus.Encoder({ channels: 2, rate: 48000, frameSize: 960 }));
}
const dispatcher = this.createDispatcher(options, streams);
stream.pipe(dispatcher);
streams.opus.pipe(dispatcher);
return dispatcher;
}

View File

@@ -72,10 +72,10 @@ class PlayInterface {
return this.player.playOpusStream(resource, options);
} else if (type === 'ogg/opus') {
if (!(resource instanceof Readable)) throw new Error('VOICE_PRISM_DEMUXERS_NEED_STREAM');
return this.player.playOpusStream(resource.pipe(new prism.OggOpusDemuxer()));
return this.player.playOpusStream(resource.pipe(new prism.OggOpusDemuxer()), options);
} else if (type === 'webm/opus') {
if (!(resource instanceof Readable)) throw new Error('VOICE_PRISM_DEMUXERS_NEED_STREAM');
return this.player.playOpusStream(resource.pipe(new prism.WebmOpusDemuxer()));
return this.player.playOpusStream(resource.pipe(new prism.WebmOpusDemuxer()), options);
}
throw new Error('VOICE_PLAY_INTERFACE_BAD_TYPE');
}