mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 09:33:32 +01:00
Added Opus stream support, added volume interface (#1102)
* Added opus stream support, added volume interface * Remove setImmediate * Fix weird syntax error * Most useless commit ever You're welcome, @PgBiel * Fix potential memory leak with OpusScript Emscripten has the tendency to not free resources even when the Opus engine instance has been garbage collected. Thanks to @abalabahaha for pointing this out. * Typo * VoiceReceiver.destroy: destroy opus encoder
This commit is contained in:
57
src/client/voice/util/VolumeInterface.js
Normal file
57
src/client/voice/util/VolumeInterface.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const EventEmitter = require('events');
|
||||
|
||||
class VolumeInterface extends EventEmitter {
|
||||
constructor({ volume = 0 } = {}) {
|
||||
super();
|
||||
this.setVolume(volume || 1);
|
||||
}
|
||||
|
||||
applyVolume(buffer, volume) {
|
||||
volume = volume || this._volume;
|
||||
if (volume === 1) return buffer;
|
||||
|
||||
const out = new Buffer(buffer.length);
|
||||
for (let i = 0; i < buffer.length; i += 2) {
|
||||
if (i >= buffer.length - 1) break;
|
||||
const uint = Math.min(32767, Math.max(-32767, Math.floor(volume * buffer.readInt16LE(i))));
|
||||
out.writeInt16LE(uint, i);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the volume relative to the input stream - i.e. 1 is normal, 0.5 is half, 2 is double.
|
||||
* @param {number} volume The volume that you want to set
|
||||
*/
|
||||
setVolume(volume) {
|
||||
this._volume = volume;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the volume in decibels
|
||||
* @param {number} db The decibels
|
||||
*/
|
||||
setVolumeDecibels(db) {
|
||||
this.setVolume(Math.pow(10, db / 20));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the volume so that a perceived value of 0.5 is half the perceived volume etc.
|
||||
* @param {number} value The value for the volume
|
||||
*/
|
||||
setVolumeLogarithmic(value) {
|
||||
this.setVolume(Math.pow(value, 1.660964));
|
||||
}
|
||||
|
||||
/**
|
||||
* The current volume of the broadcast
|
||||
* @readonly
|
||||
* @type {number}
|
||||
*/
|
||||
get volume() {
|
||||
return this._volume;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = VolumeInterface;
|
||||
Reference in New Issue
Block a user