@@ -23,7 +23,6 @@ export interface IAudioCueService {
23
23
24
24
playSound ( cue : Sound , allowManyInParallel ?: boolean ) : Promise < void > ;
25
25
playAudioCueLoop ( cue : AudioCue , milliseconds : number ) : IDisposable ;
26
- playRandomAudioCue ( groupId : AudioCueGroupId , allowManyInParallel ?: boolean ) : void ;
27
26
}
28
27
29
28
export class AudioCueService extends Disposable implements IAudioCueService {
@@ -43,25 +42,16 @@ export class AudioCueService extends Disposable implements IAudioCueService {
43
42
44
43
public async playAudioCue ( cue : AudioCue , allowManyInParallel = false ) : Promise < void > {
45
44
if ( this . isEnabled ( cue ) ) {
46
- await this . playSound ( cue . sound , allowManyInParallel ) ;
45
+ await this . playSound ( cue . sound . getSound ( ) , allowManyInParallel ) ;
47
46
}
48
47
}
49
48
50
49
public async playAudioCues ( cues : AudioCue [ ] ) : Promise < void > {
51
50
// Some audio cues might reuse sounds. Don't play the same sound twice.
52
- const sounds = new Set ( cues . filter ( cue => this . isEnabled ( cue ) ) . map ( cue => cue . sound ) ) ;
51
+ const sounds = new Set ( cues . filter ( cue => this . isEnabled ( cue ) ) . map ( cue => cue . sound . getSound ( ) ) ) ;
53
52
await Promise . all ( Array . from ( sounds ) . map ( sound => this . playSound ( sound , true ) ) ) ;
54
53
}
55
54
56
- /**
57
- * Gaming and other apps often play a sound variant when the same event happens again
58
- * for an improved experience. This function plays a random sound from the given group to accomplish that.
59
- */
60
- public playRandomAudioCue ( groupId : AudioCueGroupId , allowManyInParallel ?: boolean ) : void {
61
- const cues = AudioCue . allAudioCues . filter ( cue => cue . groupId === groupId ) ;
62
- const index = Math . floor ( Math . random ( ) * cues . length ) ;
63
- this . playAudioCue ( cues [ index ] , allowManyInParallel ) ;
64
- }
65
55
66
56
private getVolumeInPercent ( ) : number {
67
57
const volume = this . configurationService . getValue < number > ( 'audioCues.volume' ) ;
@@ -206,7 +196,6 @@ export class Sound {
206
196
return sound ;
207
197
}
208
198
209
-
210
199
public static readonly error = Sound . register ( { fileName : 'error.mp3' } ) ;
211
200
public static readonly warning = Sound . register ( { fileName : 'warning.mp3' } ) ;
212
201
public static readonly foldedArea = Sound . register ( { fileName : 'foldedAreas.mp3' } ) ;
@@ -228,19 +217,36 @@ export class Sound {
228
217
private constructor ( public readonly fileName : string ) { }
229
218
}
230
219
231
- export const enum AudioCueGroupId {
232
- chatResponseReceived = 'chatResponseReceived'
220
+ export class SoundSource {
221
+ constructor (
222
+ public readonly randomOneOf : Sound [ ]
223
+ ) { }
224
+
225
+ public getSound ( deterministic = false ) : Sound {
226
+ if ( deterministic || this . randomOneOf . length === 1 ) {
227
+ return this . randomOneOf [ 0 ] ;
228
+ } else {
229
+ const index = Math . floor ( Math . random ( ) * this . randomOneOf . length ) ;
230
+ return this . randomOneOf [ index ] ;
231
+ }
232
+ }
233
233
}
234
234
235
235
export class AudioCue {
236
236
private static _audioCues = new Set < AudioCue > ( ) ;
237
237
private static register ( options : {
238
238
name : string ;
239
- sound : Sound ;
239
+ sound : Sound | {
240
+ /**
241
+ * Gaming and other apps often play a sound variant when the same event happens again
242
+ * for an improved experience. This option enables audio cues to play a random sound.
243
+ */
244
+ randomOneOf : Sound [ ] ;
245
+ } ;
240
246
settingsKey : string ;
241
- groupId ?: AudioCueGroupId ;
242
247
} ) : AudioCue {
243
- const audioCue = new AudioCue ( options . sound , options . name , options . settingsKey , options . groupId ) ;
248
+ const soundSource = new SoundSource ( 'randomOneOf' in options . sound ? options . sound . randomOneOf : [ options . sound ] ) ;
249
+ const audioCue = new AudioCue ( soundSource , options . name , options . settingsKey ) ;
244
250
AudioCue . _audioCues . add ( audioCue ) ;
245
251
return audioCue ;
246
252
}
@@ -353,30 +359,17 @@ export class AudioCue {
353
359
settingsKey : 'audioCues.chatRequestSent'
354
360
} ) ;
355
361
356
- private static readonly chatResponseReceived = {
362
+ public static readonly chatResponseReceived = AudioCue . register ( {
357
363
name : localize ( 'audioCues.chatResponseReceived' , 'Chat Response Received' ) ,
358
364
settingsKey : 'audioCues.chatResponseReceived' ,
359
- groupId : AudioCueGroupId . chatResponseReceived
360
- } ;
361
-
362
- public static readonly chatResponseReceived1 = AudioCue . register ( {
363
- sound : Sound . chatResponseReceived1 ,
364
- ...this . chatResponseReceived
365
- } ) ;
366
-
367
- public static readonly chatResponseReceived2 = AudioCue . register ( {
368
- sound : Sound . chatResponseReceived2 ,
369
- ...this . chatResponseReceived
370
- } ) ;
371
-
372
- public static readonly chatResponseReceived3 = AudioCue . register ( {
373
- sound : Sound . chatResponseReceived3 ,
374
- ...this . chatResponseReceived
375
- } ) ;
376
-
377
- public static readonly chatResponseReceived4 = AudioCue . register ( {
378
- sound : Sound . chatResponseReceived4 ,
379
- ...this . chatResponseReceived
365
+ sound : {
366
+ randomOneOf : [
367
+ Sound . chatResponseReceived1 ,
368
+ Sound . chatResponseReceived2 ,
369
+ Sound . chatResponseReceived3 ,
370
+ Sound . chatResponseReceived4
371
+ ]
372
+ }
380
373
} ) ;
381
374
382
375
public static readonly chatResponsePending = AudioCue . register ( {
@@ -386,9 +379,8 @@ export class AudioCue {
386
379
} ) ;
387
380
388
381
private constructor (
389
- public readonly sound : Sound ,
382
+ public readonly sound : SoundSource ,
390
383
public readonly name : string ,
391
384
public readonly settingsKey : string ,
392
- public readonly groupId ?: string
393
385
) { }
394
386
}
0 commit comments