-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from gemini-hlsw/alarms-louder
Add sound and more noticeable elements to active guide alarms
- Loading branch information
Showing
13 changed files
with
168 additions
and
32 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useEffect, useMemo } from 'react'; | ||
import { useAlarmValue } from '../atoms/alarm'; | ||
import alarmSoundMp3 from '@assets/sounds/alarm.mp3'; | ||
import alarmSoundWebm from '@assets/sounds/alarm.webm'; | ||
|
||
export function AlarmAudio() { | ||
const alarm = useAlarmValue(); | ||
|
||
const alarmAudio = useMemo(() => { | ||
const audio = selectPlayableAlarm(); | ||
audio.loop = true; | ||
|
||
// Be nicer to developers' ears | ||
if (import.meta.env.DEV) audio.volume = 0.3; | ||
|
||
return audio; | ||
}, []); | ||
|
||
useEffect(() => { | ||
const checkAlarm = () => { | ||
if (alarm) | ||
alarmAudio | ||
.play() | ||
.then(() => clearInterval(AudioRetryInterval)) | ||
.catch((err) => { | ||
console.log('waiting for user interaction to play first notification', err); | ||
}); | ||
else { | ||
clearInterval(AudioRetryInterval); | ||
alarmAudio.pause(); | ||
} | ||
}; | ||
|
||
// Retry playing the alarm every 500ms until it plays, and start immediately | ||
const AudioRetryInterval = setInterval(checkAlarm, 500); | ||
checkAlarm(); | ||
|
||
// Stop the alarm when the component is unmounted | ||
return () => { | ||
clearInterval(AudioRetryInterval); | ||
alarmAudio.pause(); | ||
alarmAudio.remove; | ||
}; | ||
}, [alarm, alarmAudio]); | ||
|
||
return <></>; | ||
} | ||
|
||
/** | ||
* use mp3 if possible, otherwise use webm | ||
*/ | ||
function selectPlayableAlarm(): HTMLAudioElement { | ||
const mp3 = new Audio(alarmSoundMp3); | ||
const webm = new Audio(alarmSoundWebm); | ||
|
||
if (mp3.canPlayType('audio/mpeg')) return mp3; | ||
else if (webm.canPlayType('audio/webm')) return webm; | ||
// If neither can play, at least we'll try mp3 | ||
else return mp3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { atom, useAtom, useAtomValue, useSetAtom } from 'jotai'; | ||
import { atomWithReset, RESET } from 'jotai/utils'; | ||
|
||
// More types can be added as needed | ||
export type AlarmType = 'Guide'; | ||
|
||
const alarmAtom = atomWithReset<AlarmType | null>(null); | ||
export const useAlarm = () => useAtom(alarmAtom); | ||
export const useAlarmValue = () => useAtomValue(alarmAtom); | ||
|
||
export const guideAlarmAtom = atom( | ||
(get) => get(alarmAtom) === 'Guide', | ||
(_get, set, value: boolean) => set(alarmAtom, value ? 'Guide' : RESET), | ||
); | ||
export const useGuideAlarm = () => useAtom(guideAlarmAtom); | ||
export const useSetGuideAlarm = () => useSetAtom(guideAlarmAtom); | ||
export const useGuideAlarmValue = () => useAtomValue(guideAlarmAtom); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters