generated from nhcarrigan/template
feat: add notification sounds (#44)
### Explanation _No response_ ### Issue _No response_ ### Attestations - [ ] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [ ] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [ ] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [ ] I have pinned the dependencies to a specific patch version. ### Style - [ ] I have run the linter and resolved any errors. - [ ] My pull request uses an appropriate title, matching the conventional commit standards. - [ ] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Reviewed-on: #44 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #44.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { NOTIFICATION_SOUNDS, type NotificationType } from "./types";
|
||||
|
||||
class SoundPlayer {
|
||||
private audioCache: Map<NotificationType, HTMLAudioElement> = new Map();
|
||||
private enabled: boolean = true;
|
||||
private globalVolume: number = 1.0;
|
||||
|
||||
constructor() {
|
||||
// Preload all essential sounds
|
||||
this.preloadSounds();
|
||||
}
|
||||
|
||||
private preloadSounds(): void {
|
||||
Object.entries(NOTIFICATION_SOUNDS).forEach(([type, sound]) => {
|
||||
const audio = new Audio(`/sounds/${sound.filename}`);
|
||||
audio.preload = "auto";
|
||||
audio.volume = (sound.volume || 0.7) * this.globalVolume;
|
||||
this.audioCache.set(type as NotificationType, audio);
|
||||
});
|
||||
}
|
||||
|
||||
async play(type: NotificationType): Promise<void> {
|
||||
if (!this.enabled) return;
|
||||
|
||||
try {
|
||||
const audio = this.audioCache.get(type);
|
||||
if (!audio) {
|
||||
console.warn(`No audio found for notification type: ${type}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Clone the audio to allow overlapping sounds
|
||||
const audioClone = audio.cloneNode() as HTMLAudioElement;
|
||||
audioClone.volume = audio.volume;
|
||||
|
||||
await audioClone.play();
|
||||
} catch (error) {
|
||||
console.error("Failed to play notification sound:", error);
|
||||
}
|
||||
}
|
||||
|
||||
setEnabled(enabled: boolean): void {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
setGlobalVolume(volume: number): void {
|
||||
this.globalVolume = Math.max(0, Math.min(1, volume));
|
||||
// Update all cached audio volumes
|
||||
this.audioCache.forEach((audio, type) => {
|
||||
const sound = NOTIFICATION_SOUNDS[type];
|
||||
audio.volume = (sound.volume || 0.7) * this.globalVolume;
|
||||
});
|
||||
}
|
||||
|
||||
isEnabled(): boolean {
|
||||
return this.enabled;
|
||||
}
|
||||
}
|
||||
|
||||
// Export singleton instance
|
||||
export const soundPlayer = new SoundPlayer();
|
||||
Reference in New Issue
Block a user