@@ -23,7 +23,6 @@ export interface IAudioCueService {
2323
2424 playSound ( cue : Sound , allowManyInParallel ?: boolean ) : Promise < void > ;
2525 playAudioCueLoop ( cue : AudioCue , milliseconds : number ) : IDisposable ;
26- playRandomAudioCue ( groupId : AudioCueGroupId , allowManyInParallel ?: boolean ) : void ;
2726}
2827
2928export class AudioCueService extends Disposable implements IAudioCueService {
@@ -43,25 +42,16 @@ export class AudioCueService extends Disposable implements IAudioCueService {
4342
4443 public async playAudioCue ( cue : AudioCue , allowManyInParallel = false ) : Promise < void > {
4544 if ( this . isEnabled ( cue ) ) {
46- await this . playSound ( cue . sound , allowManyInParallel ) ;
45+ await this . playSound ( cue . sound . getSound ( ) , allowManyInParallel ) ;
4746 }
4847 }
4948
5049 public async playAudioCues ( cues : AudioCue [ ] ) : Promise < void > {
5150 // 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 ( ) ) ) ;
5352 await Promise . all ( Array . from ( sounds ) . map ( sound => this . playSound ( sound , true ) ) ) ;
5453 }
5554
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- }
6555
6656 private getVolumeInPercent ( ) : number {
6757 const volume = this . configurationService . getValue < number > ( 'audioCues.volume' ) ;
@@ -206,7 +196,6 @@ export class Sound {
206196 return sound ;
207197 }
208198
209-
210199 public static readonly error = Sound . register ( { fileName : 'error.mp3' } ) ;
211200 public static readonly warning = Sound . register ( { fileName : 'warning.mp3' } ) ;
212201 public static readonly foldedArea = Sound . register ( { fileName : 'foldedAreas.mp3' } ) ;
@@ -228,19 +217,36 @@ export class Sound {
228217 private constructor ( public readonly fileName : string ) { }
229218}
230219
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+ }
233233}
234234
235235export class AudioCue {
236236 private static _audioCues = new Set < AudioCue > ( ) ;
237237 private static register ( options : {
238238 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+ } ;
240246 settingsKey : string ;
241- groupId ?: AudioCueGroupId ;
242247 } ) : 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 ) ;
244250 AudioCue . _audioCues . add ( audioCue ) ;
245251 return audioCue ;
246252 }
@@ -353,30 +359,17 @@ export class AudioCue {
353359 settingsKey : 'audioCues.chatRequestSent'
354360 } ) ;
355361
356- private static readonly chatResponseReceived = {
362+ public static readonly chatResponseReceived = AudioCue . register ( {
357363 name : localize ( 'audioCues.chatResponseReceived' , 'Chat Response Received' ) ,
358364 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+ }
380373 } ) ;
381374
382375 public static readonly chatResponsePending = AudioCue . register ( {
@@ -386,9 +379,8 @@ export class AudioCue {
386379 } ) ;
387380
388381 private constructor (
389- public readonly sound : Sound ,
382+ public readonly sound : SoundSource ,
390383 public readonly name : string ,
391384 public readonly settingsKey : string ,
392- public readonly groupId ?: string
393385 ) { }
394386}
0 commit comments