mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-31 03:49:11 +00:00
feat(timer): playing sound on time end
This commit is contained in:
committed by
Carlos Valente
parent
265b167600
commit
4c08ca2908
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -195,6 +195,24 @@
|
|||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.sound-prompt {
|
||||||
|
position: absolute;
|
||||||
|
bottom: $view-block-padding;
|
||||||
|
left: $view-inline-padding;
|
||||||
|
padding: 0.5em 0.75em;
|
||||||
|
border-radius: $element-border-radius;
|
||||||
|
background-color: $viewer-card-bg-color;
|
||||||
|
color: $viewer-secondary-color;
|
||||||
|
font-size: $timer-label-size;
|
||||||
|
text-transform: uppercase;
|
||||||
|
pointer-events: none;
|
||||||
|
transition: opacity $viewer-transition-time;
|
||||||
|
|
||||||
|
&--hidden {
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* =================== LOGO ===================*/
|
/* =================== LOGO ===================*/
|
||||||
.logo {
|
.logo {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import TitleCard from '../../common/components/title-card/TitleCard';
|
|||||||
import ViewLogo from '../../common/components/view-logo/ViewLogo';
|
import ViewLogo from '../../common/components/view-logo/ViewLogo';
|
||||||
import ViewParamsEditor from '../../common/components/view-params-editor/ViewParamsEditor';
|
import ViewParamsEditor from '../../common/components/view-params-editor/ViewParamsEditor';
|
||||||
import { useAutoTickingClock } from '../../common/hooks/useAutoTickingClock';
|
import { useAutoTickingClock } from '../../common/hooks/useAutoTickingClock';
|
||||||
|
import { useFadeOutOnInactivity } from '../../common/hooks/useFadeOutOnInactivity';
|
||||||
import { useTimerSocket } from '../../common/hooks/useSocket';
|
import { useTimerSocket } from '../../common/hooks/useSocket';
|
||||||
import { useWindowTitle } from '../../common/hooks/useWindowTitle';
|
import { useWindowTitle } from '../../common/hooks/useWindowTitle';
|
||||||
import { cx } from '../../common/utils/styleUtils';
|
import { cx } from '../../common/utils/styleUtils';
|
||||||
@@ -30,6 +31,7 @@ import {
|
|||||||
getTotalTime,
|
getTotalTime,
|
||||||
} from './timer.utils';
|
} from './timer.utils';
|
||||||
import { TimerData, useTimerData } from './useTimerData';
|
import { TimerData, useTimerData } from './useTimerData';
|
||||||
|
import { useTimerSound } from './useTimerSound';
|
||||||
|
|
||||||
import './Timer.scss';
|
import './Timer.scss';
|
||||||
|
|
||||||
@@ -66,6 +68,7 @@ function Timer({ customFields, projectData, isMirrored, settings, viewSettings,
|
|||||||
freezeOvertime,
|
freezeOvertime,
|
||||||
freezeMessage,
|
freezeMessage,
|
||||||
hidePhase,
|
hidePhase,
|
||||||
|
endSound,
|
||||||
font,
|
font,
|
||||||
keyColour,
|
keyColour,
|
||||||
timerColour,
|
timerColour,
|
||||||
@@ -75,6 +78,8 @@ function Timer({ customFields, projectData, isMirrored, settings, viewSettings,
|
|||||||
const { getLocalizedString } = useTranslation();
|
const { getLocalizedString } = useTranslation();
|
||||||
const localisedMinutes = getLocalizedString('common.minutes');
|
const localisedMinutes = getLocalizedString('common.minutes');
|
||||||
|
|
||||||
|
const showSoundPrompt = useTimerSound(time.phase, endSound);
|
||||||
|
|
||||||
// gather modifiers
|
// gather modifiers
|
||||||
const viewTimerType = timerType ?? timerTypeNow;
|
const viewTimerType = timerType ?? timerTypeNow;
|
||||||
const showOverlay = getShowMessage(message.timer);
|
const showOverlay = getShowMessage(message.timer);
|
||||||
@@ -156,6 +161,8 @@ function Timer({ customFields, projectData, isMirrored, settings, viewSettings,
|
|||||||
|
|
||||||
<ViewParamsEditor target={OntimeView.Timer} viewOptions={timerOptions} />
|
<ViewParamsEditor target={OntimeView.Timer} viewOptions={timerOptions} />
|
||||||
|
|
||||||
|
{showSoundPrompt && <SoundPermissionPrompt />}
|
||||||
|
|
||||||
<div className={cx(['blackout', message.timer.blackout && 'blackout--active'])} />
|
<div className={cx(['blackout', message.timer.blackout && 'blackout--active'])} />
|
||||||
|
|
||||||
{!hideMessage && (
|
{!hideMessage && (
|
||||||
@@ -227,3 +234,13 @@ function TimerAutoTickingClock({ clockFormat }: { clockFormat: MaybeString }) {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function SoundPermissionPrompt() {
|
||||||
|
const isUserActive = useFadeOutOnInactivity(true);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={cx(['sound-prompt', !isUserActive && 'sound-prompt--hidden'])} aria-live='polite'>
|
||||||
|
Interact with the page (click/tap or press any key) to enable sound
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { TimerPhase } from 'ontime-types';
|
||||||
|
|
||||||
|
import { shouldPlayEndSound } from '../timer.utils';
|
||||||
|
|
||||||
|
describe('shouldPlayEndSound()', () => {
|
||||||
|
test.each([TimerPhase.Default, TimerPhase.Warning, TimerPhase.Danger])(
|
||||||
|
'sounds when a running timer goes into overtime from %s',
|
||||||
|
(previousPhase) => {
|
||||||
|
expect(shouldPlayEndSound(previousPhase, TimerPhase.Overtime)).toBe(true);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
it('stays silent on the first phase we see, a client could be joining mid-overtime', () => {
|
||||||
|
expect(shouldPlayEndSound(null, TimerPhase.Overtime)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('stays silent when the phase was reset, a reload during overtime starts from none', () => {
|
||||||
|
expect(shouldPlayEndSound(TimerPhase.None, TimerPhase.Overtime)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('stays silent for a roll timer waiting to start', () => {
|
||||||
|
expect(shouldPlayEndSound(TimerPhase.Pending, TimerPhase.Overtime)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sounds once, not on every update while in overtime', () => {
|
||||||
|
expect(shouldPlayEndSound(TimerPhase.Overtime, TimerPhase.Overtime)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('stays silent on phases which are not the end of the timer', () => {
|
||||||
|
expect(shouldPlayEndSound(TimerPhase.Default, TimerPhase.Warning)).toBe(false);
|
||||||
|
expect(shouldPlayEndSound(TimerPhase.Warning, TimerPhase.Danger)).toBe(false);
|
||||||
|
expect(shouldPlayEndSound(TimerPhase.Overtime, TimerPhase.None)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -16,6 +16,7 @@ import { ViewOption } from '../../common/components/view-params-editor/viewParam
|
|||||||
import { makeOptionsFromCustomFields } from '../../common/components/view-params-editor/viewParams.utils';
|
import { makeOptionsFromCustomFields } from '../../common/components/view-params-editor/viewParams.utils';
|
||||||
import { PresetContext } from '../../common/context/PresetContext';
|
import { PresetContext } from '../../common/context/PresetContext';
|
||||||
import { isStringBoolean, makeColourString } from '../common/viewUtils';
|
import { isStringBoolean, makeColourString } from '../common/viewUtils';
|
||||||
|
import { endSoundOptions, isEndSound, type EndSound } from './timer.sound';
|
||||||
|
|
||||||
// manually match the properties of TimerType excluding the None
|
// manually match the properties of TimerType excluding the None
|
||||||
const timerDisplayOptions: SelectOption[] = [
|
const timerDisplayOptions: SelectOption[] = [
|
||||||
@@ -76,6 +77,15 @@ export const getTimerOptions = (timeFormat: string, customFields: CustomFields):
|
|||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: 'endSound',
|
||||||
|
title: 'Play sound on timer end',
|
||||||
|
description:
|
||||||
|
'Plays a sound in this screen when the timer reaches zero. The screen must be interacted with once before it can play',
|
||||||
|
type: 'option',
|
||||||
|
values: endSoundOptions,
|
||||||
|
defaultValue: 'none',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -193,6 +203,7 @@ type TimerOptions = {
|
|||||||
freezeOvertime: boolean;
|
freezeOvertime: boolean;
|
||||||
freezeMessage: string;
|
freezeMessage: string;
|
||||||
hidePhase: boolean;
|
hidePhase: boolean;
|
||||||
|
endSound: EndSound;
|
||||||
font?: string;
|
font?: string;
|
||||||
keyColour?: string;
|
keyColour?: string;
|
||||||
timerColour?: string;
|
timerColour?: string;
|
||||||
@@ -208,6 +219,7 @@ function getOptionsFromParams(searchParams: URLSearchParams, defaultValues?: URL
|
|||||||
|
|
||||||
// Get timerType from either source
|
// Get timerType from either source
|
||||||
const timerType = validateTimerType(getValue('timerType'), TimerType.None);
|
const timerType = validateTimerType(getValue('timerType'), TimerType.None);
|
||||||
|
const endSoundValue = getValue('endSound');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
hideClock: isStringBoolean(getValue('hideClock')),
|
hideClock: isStringBoolean(getValue('hideClock')),
|
||||||
@@ -227,6 +239,7 @@ function getOptionsFromParams(searchParams: URLSearchParams, defaultValues?: URL
|
|||||||
freezeOvertime: isStringBoolean(getValue('freezeOvertime')),
|
freezeOvertime: isStringBoolean(getValue('freezeOvertime')),
|
||||||
freezeMessage: getValue('freezeMessage') ?? '',
|
freezeMessage: getValue('freezeMessage') ?? '',
|
||||||
hidePhase: isStringBoolean(getValue('hidePhase')),
|
hidePhase: isStringBoolean(getValue('hidePhase')),
|
||||||
|
endSound: isEndSound(endSoundValue) ? endSoundValue : 'none',
|
||||||
|
|
||||||
font: getValue('font') ?? undefined,
|
font: getValue('font') ?? undefined,
|
||||||
keyColour: makeColourString(getValue('keyColour')),
|
keyColour: makeColourString(getValue('keyColour')),
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import arpeggio from '../../assets/sounds/arpeggio.wav';
|
||||||
|
import bell from '../../assets/sounds/bell.wav';
|
||||||
|
import chime from '../../assets/sounds/chime.wav';
|
||||||
|
import type { SelectOption } from '../../common/components/select/Select';
|
||||||
|
|
||||||
|
// synthesised tones bundled with the app, no external source or licence to track
|
||||||
|
export const endSoundSources = { chime, bell, arpeggio } as const;
|
||||||
|
|
||||||
|
export type EndSound = 'none' | keyof typeof endSoundSources;
|
||||||
|
|
||||||
|
export const endSoundOptions: SelectOption[] = [
|
||||||
|
{ value: 'none', label: 'None' },
|
||||||
|
{ value: 'chime', label: 'Chime' },
|
||||||
|
{ value: 'bell', label: 'Bell' },
|
||||||
|
{ value: 'arpeggio', label: 'Arpeggio' },
|
||||||
|
];
|
||||||
|
|
||||||
|
export function isEndSound(value: string | null): value is EndSound {
|
||||||
|
return value === 'none' || value === 'chime' || value === 'bell' || value === 'arpeggio';
|
||||||
|
}
|
||||||
@@ -189,3 +189,18 @@ export function getCardData(
|
|||||||
nextSecondary,
|
nextSecondary,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the end of timer sound should play for a given phase transition
|
||||||
|
* We only sound the transition into overtime from a phase that was already counting,
|
||||||
|
* which keeps a client that connects or reloads mid-overtime silent
|
||||||
|
*/
|
||||||
|
export function shouldPlayEndSound(previousPhase: TimerPhase | null, phase: TimerPhase): boolean {
|
||||||
|
if (phase !== TimerPhase.Overtime) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
previousPhase === TimerPhase.Default || previousPhase === TimerPhase.Warning || previousPhase === TimerPhase.Danger
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,99 @@
|
|||||||
|
import { TimerPhase } from 'ontime-types';
|
||||||
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
|
import { endSoundSources, type EndSound } from './timer.sound';
|
||||||
|
import { shouldPlayEndSound } from './timer.utils';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plays a sound when the timer reaches its end
|
||||||
|
*
|
||||||
|
* Browsers reject playback until the document has been interacted with, and that permission
|
||||||
|
* is lost on every page load. Since a timer screen is typically left unattended, we prime the
|
||||||
|
* audio element on the first interaction and let the view prompt for one if it never comes.
|
||||||
|
* Safari grants the permission per element, so priming has to call play() on this element from
|
||||||
|
* inside the event handler, it is not enough to know that an interaction happened.
|
||||||
|
*/
|
||||||
|
export function useTimerSound(phase: TimerPhase, sound: EndSound): boolean {
|
||||||
|
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||||
|
const previousPhaseRef = useRef<TimerPhase | null>(null);
|
||||||
|
const [isArmed, setIsArmed] = useState(false);
|
||||||
|
|
||||||
|
const enabled = sound !== 'none';
|
||||||
|
|
||||||
|
// Create and clean up the audio element; changing sounds requires re-arming it in Safari.
|
||||||
|
useEffect(() => {
|
||||||
|
setIsArmed(false);
|
||||||
|
|
||||||
|
if (sound === 'none') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const audio = new Audio(endSoundSources[sound]);
|
||||||
|
audioRef.current = audio;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
audio.pause();
|
||||||
|
audioRef.current = null;
|
||||||
|
};
|
||||||
|
}, [sound]);
|
||||||
|
|
||||||
|
// Listen for user interaction until muted playback succeeds and arms the selected audio element.
|
||||||
|
useEffect(() => {
|
||||||
|
if (!enabled || isArmed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
const prime = () => {
|
||||||
|
const audio = audioRef.current;
|
||||||
|
if (!audio) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const wasMuted = audio.muted;
|
||||||
|
audio.muted = true;
|
||||||
|
audio
|
||||||
|
.play()
|
||||||
|
.then(() => {
|
||||||
|
if (audioRef.current !== audio) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
audio.pause();
|
||||||
|
audio.currentTime = 0;
|
||||||
|
setIsArmed(true);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
// playback is still blocked, a later interaction will try again
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
audio.muted = wasMuted;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener('pointerdown', prime, { capture: true, signal: controller.signal });
|
||||||
|
document.addEventListener('keydown', prime, { capture: true, signal: controller.signal });
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
controller.abort();
|
||||||
|
};
|
||||||
|
}, [enabled, isArmed]);
|
||||||
|
|
||||||
|
// Track phase transitions and play only when a running timer enters overtime.
|
||||||
|
useEffect(() => {
|
||||||
|
const previousPhase = previousPhaseRef.current;
|
||||||
|
previousPhaseRef.current = phase;
|
||||||
|
|
||||||
|
if (!enabled || !shouldPlayEndSound(previousPhase, phase)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const audio = audioRef.current;
|
||||||
|
if (!audio) return;
|
||||||
|
|
||||||
|
audio.currentTime = 0;
|
||||||
|
audio.play().catch(() => {
|
||||||
|
// the screen has not been interacted with, the view shows a prompt for it
|
||||||
|
});
|
||||||
|
}, [enabled, phase]);
|
||||||
|
|
||||||
|
return enabled && !isArmed;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user