Skip to content

Commit 683fc37

Browse files
authored
* Adresses microsoft#187837 * Fixes failed test
1 parent 40abe0e commit 683fc37

File tree

5 files changed

+39
-49
lines changed

5 files changed

+39
-49
lines changed

src/vs/editor/standalone/browser/standaloneServices.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
8787
import { IStorageService, InMemoryStorageService } from 'vs/platform/storage/common/storage';
8888
import { DefaultConfiguration } from 'vs/platform/configuration/common/configurations';
8989
import { WorkspaceEdit } from 'vs/editor/common/languages';
90-
import { AudioCue, AudioCueGroupId, IAudioCueService, Sound } from 'vs/platform/audioCues/browser/audioCueService';
90+
import { AudioCue, IAudioCueService, Sound } from 'vs/platform/audioCues/browser/audioCueService';
9191
import { LogService } from 'vs/platform/log/common/logService';
9292
import { getEditorFeatures } from 'vs/editor/common/editorFeatures';
9393
import { onUnexpectedError } from 'vs/base/common/errors';
@@ -1058,8 +1058,6 @@ class StandaloneAudioService implements IAudioCueService {
10581058
playAudioCueLoop(cue: AudioCue): IDisposable {
10591059
return toDisposable(() => { });
10601060
}
1061-
playRandomAudioCue(groupId: AudioCueGroupId, allowManyInParallel?: boolean): void {
1062-
}
10631061
}
10641062

10651063
export interface IEditorOverrideServices {

src/vs/platform/audioCues/browser/audioCueService.ts

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2928
export 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

235235
export 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
}

src/vs/workbench/contrib/audioCues/browser/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class ShowAudioCueHelp extends Action2 {
4949
{
5050
activeItem: items[0],
5151
onDidFocus: (item) => {
52-
audioCueService.playSound(item.audioCue.sound, true);
52+
audioCueService.playSound(item.audioCue.sound.getSound(true), true);
5353
},
5454
onDidTriggerItemButton: (context) => {
5555
preferencesService.openSettings({ query: context.item.audioCue.settingsKey });

src/vs/workbench/contrib/chat/browser/chatAccessibilityService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { status } from 'vs/base/browser/ui/aria/aria';
77
import { RunOnceScheduler } from 'vs/base/common/async';
88
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
9-
import { AudioCue, AudioCueGroupId, IAudioCueService } from 'vs/platform/audioCues/browser/audioCueService';
9+
import { AudioCue, IAudioCueService } from 'vs/platform/audioCues/browser/audioCueService';
1010
import { IChatAccessibilityService } from 'vs/workbench/contrib/chat/browser/chat';
1111
import { IChatResponseViewModel } from 'vs/workbench/contrib/chat/common/chatViewModel';
1212

@@ -37,7 +37,7 @@ export class ChatAccessibilityService extends Disposable implements IChatAccessi
3737
const isPanelChat = typeof response !== 'string';
3838
this._responsePendingAudioCue?.dispose();
3939
this._runOnceScheduler?.cancel();
40-
this._audioCueService.playRandomAudioCue(AudioCueGroupId.chatResponseReceived, true);
40+
this._audioCueService.playAudioCue(AudioCue.chatResponseReceived, true);
4141
this._hasReceivedRequest = false;
4242
if (!response) {
4343
return;

src/vs/workbench/contrib/terminal/browser/terminalInstance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
758758
icon: Codicon.bell,
759759
tooltip: nls.localize('bellStatus', "Bell")
760760
}, this._configHelper.config.bellDuration);
761-
this._audioCueService.playSound(AudioCue.terminalBell.sound);
761+
this._audioCueService.playSound(AudioCue.terminalBell.sound.getSound());
762762
}
763763
});
764764
}, 1000);

0 commit comments

Comments
 (0)