Skip to content

Commit

Permalink
feat(Queue): add option to disable inline volume
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Nov 28, 2021
1 parent 3185557 commit 87d49c6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,11 @@ class Player extends EventEmitter<PlayerEvents> {
return `${depsReport}\n${line}\nLoaded Extractors:\n${extractorReport || "None"}`;
}

emit<U extends keyof PlayerEvents>(eventName: U, ...args: Parameters<PlayerEvents[U]>): boolean {
emit<U extends keyof PlayerEvents>(eventName: U, ...args: Parameters<PlayerEvents[U]>): boolean {
if (this.requiredEvents.includes(eventName) && !super.eventNames().includes(eventName)) {
// eslint-disable-next-line no-console
console.error(...args);
process.emitWarning(`[DiscordPlayerWarning] Unhandled "${eventName}" event! Events ${this.requiredEvents.map(m => `"${m}"`).join(", ")} must have event listeners!`);
process.emitWarning(`[DiscordPlayerWarning] Unhandled "${eventName}" event! Events ${this.requiredEvents.map((m) => `"${m}"`).join(", ")} must have event listeners!`);
return false;
} else {
return super.emit(eventName, ...args);
Expand Down
6 changes: 4 additions & 2 deletions src/Structures/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class Queue<T = unknown> {
},
initialVolume: 100,
bufferingTimeout: 3000,
spotifyBridge: true
spotifyBridge: true,
disableVolume: false
} as PlayerOptions,
options
);
Expand Down Expand Up @@ -695,7 +696,8 @@ class Queue<T = unknown> {

const resource: AudioResource<Track> = this.connection.createStream(stream, {
type: StreamType.Raw,
data: track
data: track,
disableVolume: Boolean(this.options.disableVolume)
});

if (options.seek) this._streamTime = options.seek;
Expand Down
10 changes: 5 additions & 5 deletions src/VoiceInterface/StreamDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ class StreamDispatcher extends EventEmitter<VoiceEvents> {
* @returns {AudioResource}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createStream(src: Readable | Duplex | string, ops?: { type?: StreamType; data?: any }) {
createStream(src: Readable | Duplex | string, ops?: { type?: StreamType; data?: any; disableVolume?: boolean }) {
this.audioResource = createAudioResource(src, {
inputType: ops?.type ?? StreamType.Arbitrary,
metadata: ops?.data,
inlineVolume: true // we definitely need volume controls, right?
// eslint-disable-next-line no-extra-boolean-cast
inlineVolume: !Boolean(ops?.disableVolume)
});

return this.audioResource;
Expand Down Expand Up @@ -223,9 +224,8 @@ class StreamDispatcher extends EventEmitter<VoiceEvents> {
* @returns {boolean}
*/
setVolume(value: number) {
if (!this.audioResource || isNaN(value) || value < 0 || value > Infinity) return false;
if (!this.audioResource?.volume || isNaN(value) || value < 0 || value > Infinity) return false;

// ye boi logarithmic ✌
this.audioResource.volume.setVolumeLogarithmic(value / 100);
return true;
}
Expand All @@ -235,7 +235,7 @@ class StreamDispatcher extends EventEmitter<VoiceEvents> {
* @type {number}
*/
get volume() {
if (!this.audioResource || !this.audioResource.volume) return 100;
if (!this.audioResource?.volume) return 100;
const currentVol = this.audioResource.volume.volume;
return Math.round(Math.pow(currentVol, 1 / 1.660964) * 100);
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export interface PlayerProgressbarOptions {
* @property {number} [initialVolume=100] The initial player volume
* @property {number} [bufferingTimeout=3000] Buffering timeout for the stream
* @property {boolean} [spotifyBridge=true] If player should bridge spotify source to youtube
* @property {boolean} [disableVolume=false] If player should disable inline volume
* @property {Function} [onBeforeCreateStream] Runs before creating stream
*/
export interface PlayerOptions {
Expand All @@ -146,6 +147,7 @@ export interface PlayerOptions {
initialVolume?: number;
bufferingTimeout?: number;
spotifyBridge?: boolean;
disableVolume?: boolean;
onBeforeCreateStream?: (track: Track, source: TrackSource, queue: Queue) => Promise<Readable>;
}

Expand Down

0 comments on commit 87d49c6

Please sign in to comment.