diff --git a/src/client/voice/VoiceBroadcast.js b/src/client/voice/VoiceBroadcast.js index 14cf2a186..b9a6f5117 100644 --- a/src/client/voice/VoiceBroadcast.js +++ b/src/client/voice/VoiceBroadcast.js @@ -1,4 +1,4 @@ -const VolumeInterface = require('./util/VolumeInterface'); +class VolumeInterface {} const OpusEncoders = require('./opus/OpusEngineList'); const Collection = require('../../util/Collection'); diff --git a/src/client/voice/dispatcher/StreamDispatcher.js b/src/client/voice/dispatcher/StreamDispatcher.js index e5de206bf..b4a8ab4b2 100644 --- a/src/client/voice/dispatcher/StreamDispatcher.js +++ b/src/client/voice/dispatcher/StreamDispatcher.js @@ -1,4 +1,5 @@ const { VoiceStatus } = require('../../../util/Constants'); +const VolumeInterface = require('../util/VolumeInterface'); const { Writable } = require('stream'); const secretbox = require('../util/Secretbox'); @@ -159,6 +160,18 @@ class StreamDispatcher extends Writable { */ this.emit('speaking', value); } + + get volume() { + return this.player.streams.volume ? this.player.streams.volume.volume : 1; + } + + setVolume(value) { + if (!this.player.streams.volume) return false; + this.player.streams.volume.setVolume(value); + return true; + } } +VolumeInterface.applyToClass(StreamDispatcher); + module.exports = StreamDispatcher; diff --git a/src/client/voice/player/AudioPlayer.js b/src/client/voice/player/AudioPlayer.js index 6d60f22d9..eeb578a64 100644 --- a/src/client/voice/player/AudioPlayer.js +++ b/src/client/voice/player/AudioPlayer.js @@ -69,8 +69,9 @@ class AudioPlayer extends EventEmitter { playPCMStream(stream, options = {}) { this.destroyDispatcher(); + const volume = this.streams.volume = new prism.VolumeTransformer16LE(null, { volume: 0.2 }); const opus = this.streams.opus = new prism.opus.Encoder({ channels: 2, rate: 48000, frameSize: 960 }); - stream.pipe(opus); + stream.pipe(volume).pipe(opus); return this.playOpusStream(opus, options); } @@ -85,7 +86,6 @@ class AudioPlayer extends EventEmitter { this.destroyDispatcher(); const options = { seek, volume, passes }; const dispatcher = new StreamDispatcher(this, options); - this.streamingData.count = 0; dispatcher.on('speaking', value => this.voiceConnection.setSpeaking(value)); return dispatcher; } diff --git a/src/client/voice/util/VolumeInterface.js b/src/client/voice/util/VolumeInterface.js index 7ecd28e64..8146bb913 100644 --- a/src/client/voice/util/VolumeInterface.js +++ b/src/client/voice/util/VolumeInterface.js @@ -25,7 +25,7 @@ class VolumeInterface extends EventEmitter { * @type {number} */ get volumeDecibels() { - return Math.log10(this._volume) * 20; + return Math.log10(this.volume) * 20; } /** @@ -34,7 +34,7 @@ class VolumeInterface extends EventEmitter { * @type {number} */ get volumeLogarithmic() { - return Math.pow(this._volume, 1 / 1.660964); + return Math.pow(this.volume, 1 / 1.660964); } applyVolume(buffer, volume) { @@ -83,4 +83,19 @@ class VolumeInterface extends EventEmitter { } } -module.exports = VolumeInterface; +const props = [ + 'volumeDecibels', + 'volumeLogarithmic', + 'setVolumeDecibels', + 'setVolumeLogarithmic', +]; + +exports.applyToClass = function applyToClass(structure) { + for (const prop of props) { + Object.defineProperty( + structure.prototype, + prop, + Object.getOwnPropertyDescriptor(VolumeInterface.prototype, prop) + ); + } +};