-
Notifications
You must be signed in to change notification settings - Fork 3
/
AudioManager.ts
209 lines (178 loc) · 5.63 KB
/
AudioManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import type { ErrorCode } from "../util/ErrorCode"
import type { CacheManager } from "../cache/CacheManager"
import type { VoiceConnection } from "@discordjs/voice"
import type { downloadOptions } from "ytdl-core"
import type { AudioPlayer } from "./AudioPlayer"
import SCDL from "soundcloud-downloader"
import { AudioManagerValidation as validation } from "../validation"
import { mkdirSync, mkdtempSync, existsSync } from "fs"
import { AudioPlayerImpl } from "./AudioPlayerImpl"
import { TypedEmitter } from "tiny-typed-emitter"
import { join as pathJoin } from "path"
import { tmpdir } from "os"
function initCache(dir: string): void {
if (!existsSync(dir)) mkdirSync(dir)
}
type createAudioPlayerType = () => AudioPlayer
function defaultCreateAudioPlayerType(): AudioPlayer {
return new AudioPlayerImpl()
}
/**
* The options for AudioManager
*/
export interface AudioManagerOptions {
/**
* The audio cache manager
*/
cache?: CacheManager
/**
* The directory where the audio cache is saved
*/
cacheDir?: string
/**
* The timeout for cache deletion (in ms)
*/
cacheTimeout?: number
/**
* Abort the player after reaching timeout on buffering (in ms), default to 7 seconds
*/
bufferTimeout?: number
/**
* The downloadOptions (ytdl-core) when getting audio source from youtube
*/
youtubeOptions?: downloadOptions
/**
* The soundcloud client (soundcloud-downloader) when getting audio source from soundcloud
*/
soundcloudClient?: typeof SCDL
/**
* Custom method for creating audio player implementation
*/
createAudioPlayer?: createAudioPlayerType
}
export interface AudioManagerEvents {
/**
* @param guildID The guildID of the linked connection in player
* @param urlOrLocation The audio url or location
*/
audioStart(guildID: string, urlOrLocation: string): void
/**
* @param guildID The guildID of the linked connection in player
* @param urlOrLocation The audio url or location
*/
audioEnd(guildID: string, urlOrLocation: string): void
/**
* @param guildID The guildID of the linked connection in player
* @param urlOrLocation The audio url or location
* @param errorCode The error code to identify error
*/
audioError(guildID: string, urlOrLocation: string, errorCode: ErrorCode): void
}
/**
* The manager of the audio players
*/
export class AudioManager extends TypedEmitter<AudioManagerEvents> {
/**
* Emitted whenever an audio is started playing
*
* Listener must implement {@link AudioManagerEvents.audioStart | AudioStartCallback}
* @event
*/
static AUDIO_START = "audioStart"
/**
* Emitted whenever an audio is ended after playing
*
* Listener must implement {@link AudioManagerEvents.audioEnd | AudioEndCallback}
* @event
*/
static AUDIO_END = "audioEnd"
/**
* Emitted whenever an error is thrown while getting audio source before playing
*
* Listener must implement {@link AudioManagerEvents.audioError | AudioErrorCallback}
* @event
*/
static AUDIO_ERROR = "audioError"
/**
* The audio cache manager
*/
public readonly cache?: CacheManager
/**
* Abort the player after reaching timeout on buffering (in ms), default to 7 seconds
*/
public readonly bufferTimeout?: number
/**
* The soundcloud client (soundcloud-downloader) when getting audio source
*/
public readonly soundcloud: typeof SCDL
/**
* The downloadOptions (ytdl-core) when getting audio source
*/
public readonly youtube: downloadOptions
/**
* @internal
*/
private readonly _players = new Map<string, AudioPlayer>()
/**
* @internal
*/
private readonly _createAudioPlayer: createAudioPlayerType
/**
* @param options The options to create new audio player manager
*/
constructor(options: AudioManagerOptions) {
super()
validation.validateOptions(options)
const { cache, cacheDir, cacheTimeout, bufferTimeout, youtubeOptions, soundcloudClient, createAudioPlayer } = options
this.cache = cache
this.bufferTimeout = bufferTimeout ?? 7 * 1000
this.youtube = youtubeOptions ?? {}
this.soundcloud = soundcloudClient ?? SCDL
this._createAudioPlayer = createAudioPlayer || defaultCreateAudioPlayerType
if (cache) {
const path = cacheDir ?? tmpdir()
const timeout = cacheTimeout ?? 1000 * 60 * 10
initCache(path)
cache.setTimeout(timeout)
cache.setPath(mkdtempSync(pathJoin(path, "node-discord-media-player-")))
}
}
/**
* Get player from list if exist, otherwise create new
* @param connection The voice connection
* @returns The audio player
*/
getPlayer(connection: VoiceConnection): AudioPlayer {
validation.validateConnection(connection)
const guildID = connection.joinConfig.guildId
let player = this._players.get(guildID)
if (!player) {
player = this._createAudioPlayer()
validation.validatePlayer(player)
player.setManager(this)
player.link(connection)
this._players.set(guildID, player)
}
return player
}
/**
* Delete player from list and unlink it
* @param connection The voice connection
* @returns false if failed or doesn't exist, true if deleted
*/
deletePlayer(connection: VoiceConnection): boolean {
validation.validateConnection(connection)
const guildID = connection.joinConfig.guildId
if (!this._players.has(guildID)) return false
const player = this._players.get(guildID)
const success = this._players.delete(guildID)
if (success) player.unlink()
return success
}
/**
* @internal
*/
_deletePlayerIfExist(guildID: string): void {
if (this._players.has(guildID)) this._players.delete(guildID)
}
}