Merge #183: iOS timer sounds — the context resumes before every tone, unlocks from the tap, and a setting plays sounds on silent; #152 (kurktchiev)
# Conflicts: # frontend/src/lib/pt-br-locale.test.js # frontend/src/locales/de.js # frontend/src/locales/es.js # frontend/src/locales/fr.js # frontend/src/locales/hi.js # frontend/src/locales/hu.js # frontend/src/locales/it.js # frontend/src/locales/ko.js # frontend/src/locales/pl.js # frontend/src/locales/pt-BR.js # frontend/src/locales/pt.js # frontend/src/locales/ru.js # frontend/src/locales/th.js # frontend/src/locales/tr.js # frontend/src/locales/zh.js
This commit is contained in:
commit
33f4758a1c
23 files changed
+459
-9
No files matched your search
@@ -27,7 +27,7 @@ describe('Brazilian Portuguese locale', () => {
|
||||
.sort(byCodeUnit)
|
||||
const fingerprint = createHash('sha256').update(JSON.stringify(inherited)).digest('hex')
|
||||
|
||||
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(642)
|
||||
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(644)
|
||||
expect(inherited).toHaveLength(647)
|
||||
// If this fails, review the changed keys and wording before accepting a new hash. From
|
||||
// frontend/: node scripts/pt-br-inheritance-fingerprint.mjs --list
|
||||
|
||||
@@ -1,17 +1,90 @@
|
||||
// WebAudio beeps + haptics (ported from the vanilla app). `enabled` gates sound.
|
||||
//
|
||||
// iOS needs three things a desktop browser does not (#152, "flash but no beep"):
|
||||
// 1. The ring/silent switch mutes Web Audio. WebKit gives a page whose only audio is Web Audio
|
||||
// the Ambient category, and Ambient obeys the switch — at a gym the phone is usually on
|
||||
// silent, so the timer was mute exactly where it mattered. The only way out is the
|
||||
// 'playback' audio-session type (iOS 17+), which is exclusive: it pauses whatever else the
|
||||
// phone is playing, and WebKit never tells that app it may resume. Hence a setting
|
||||
// (setPlayOnSilent), off by default, rather than something done for everyone.
|
||||
// 2. Locking the screen or switching apps moves a running context to 'interrupted'. WebKit
|
||||
// only brings it back by itself if it was running when the interruption began; a context
|
||||
// that was suspended comes back suspended, and a suspended context makes no sound. So every
|
||||
// tone resumes the context first — allowed without a tap once the context has started
|
||||
// inside one.
|
||||
// 3. A context created outside a tap starts 'suspended' and no timer tick can start it.
|
||||
// unlock() runs from the taps that lead to a timer (set check, hold start, turning Sounds
|
||||
// on) so the context has started before any tick needs it.
|
||||
//
|
||||
// The context is suspended again a second after the last tone: an idle context otherwise keeps
|
||||
// rendering silence for the rest of the page, and under 'playback' keeps the phone's audio
|
||||
// session busy. (Suspending does NOT hand the phone back to a paused music app — see 1.)
|
||||
let audioCtx = null
|
||||
let idleTm = null
|
||||
let idleAt = 0
|
||||
|
||||
const ctxFor = () => {
|
||||
if (!audioCtx || audioCtx.state === 'closed') audioCtx = new (window.AudioContext || window.webkitAudioContext)()
|
||||
return audioCtx
|
||||
}
|
||||
|
||||
const wake = () => {
|
||||
const ctx = ctxFor()
|
||||
if (ctx.state !== 'running') { const p = ctx.resume(); if (p && p.catch) p.catch(() => {}) }
|
||||
return ctx
|
||||
}
|
||||
|
||||
// Suspend once every scheduled tone is over. A burst schedules several tones in one go; the
|
||||
// latest end wins, and a tone scheduled while the timer is pending pushes it out.
|
||||
const sleepAfter = endSec => {
|
||||
const at = Date.now() + endSec * 1000 + 1000
|
||||
if (at <= idleAt && idleTm) return
|
||||
idleAt = at
|
||||
clearTimeout(idleTm)
|
||||
idleTm = setTimeout(() => {
|
||||
idleTm = null
|
||||
try { if (audioCtx && audioCtx.state === 'running') { const p = audioCtx.suspend(); if (p && p.catch) p.catch(() => {}) } } catch (e) { /* */ }
|
||||
}, at - Date.now())
|
||||
}
|
||||
|
||||
export function beep(enabled, freq, dur, when) {
|
||||
if (!enabled) return
|
||||
try {
|
||||
audioCtx = audioCtx || new (window.AudioContext || window.webkitAudioContext)()
|
||||
const o = audioCtx.createOscillator(), g = audioCtx.createGain()
|
||||
o.connect(g); g.connect(audioCtx.destination)
|
||||
const ctx = wake()
|
||||
const o = ctx.createOscillator(), g = ctx.createGain()
|
||||
o.connect(g); g.connect(ctx.destination)
|
||||
o.frequency.value = freq || 880; o.type = 'sine'
|
||||
const t0 = audioCtx.currentTime + (when || 0)
|
||||
const t0 = ctx.currentTime + (when || 0)
|
||||
g.gain.setValueAtTime(0.001, t0)
|
||||
g.gain.exponentialRampToValueAtTime(0.35, t0 + 0.02)
|
||||
g.gain.exponentialRampToValueAtTime(0.001, t0 + (dur || 0.18))
|
||||
o.start(t0); o.stop(t0 + (dur || 0.18) + 0.05)
|
||||
sleepAfter((when || 0) + (dur || 0.18) + 0.05)
|
||||
} catch (e) { /* */ }
|
||||
}
|
||||
|
||||
// Call from inside a tap. Gets the context created and running while the browser still counts
|
||||
// this as a user gesture; it goes back to sleep on its own. Nothing audible.
|
||||
export function unlock(enabled) {
|
||||
if (!enabled) return
|
||||
try { wake(); sleepAfter(0) } catch (e) { /* */ }
|
||||
}
|
||||
|
||||
// Settings → "Play sounds when the phone is on silent". Offered only where it means something:
|
||||
// a WebKit with the audio-session API (iOS 17+) on a device that has a ring/silent switch or its
|
||||
// Control Centre equivalent — iPhone, or an iPad (which reports itself as a Mac with a touch
|
||||
// screen). macOS Safari has the API but no switch, and other browsers have neither.
|
||||
// 'playback' ignores the switch; 'auto' is the browser's own choice (Ambient for a page like
|
||||
// this one). Applied by App.jsx whenever the setting is loaded or changed.
|
||||
export const playOnSilentSupported = () => {
|
||||
if (typeof navigator === 'undefined' || !navigator.audioSession) return false
|
||||
const ua = navigator.userAgent || ''
|
||||
return /iPhone|iPad|iPod/.test(ua) || (/Macintosh/.test(ua) && navigator.maxTouchPoints > 1)
|
||||
}
|
||||
export function setPlayOnSilent(on) {
|
||||
if (!playOnSilentSupported()) return
|
||||
try { navigator.audioSession.type = on ? 'playback' : 'auto' } catch (e) { /* */ }
|
||||
}
|
||||
|
||||
|
||||
export function vibrate(p) { try { navigator.vibrate && navigator.vibrate(p) } catch (e) { /* */ } }
|
||||
@@ -0,0 +1,198 @@
|
||||
// @vitest-environment happy-dom
|
||||
// lib/sound.js keeps one AudioContext per page; each test gets a fresh module so that state
|
||||
// does not leak. The fake context records what the real one would be asked to do.
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
class FakeCtx {
|
||||
constructor() {
|
||||
this.state = 'suspended' // what every browser hands back outside a user gesture
|
||||
this.currentTime = 0
|
||||
this.destination = {}
|
||||
this.tones = []
|
||||
this.resumes = 0
|
||||
this.suspends = 0
|
||||
FakeCtx.instances.push(this)
|
||||
}
|
||||
resume() { this.resumes++; this.state = 'running'; return Promise.resolve() }
|
||||
suspend() { this.suspends++; this.state = 'suspended'; return Promise.resolve() }
|
||||
createGain() { return { connect() {}, gain: { setValueAtTime() {}, exponentialRampToValueAtTime() {} } } }
|
||||
createOscillator() {
|
||||
const ctx = this
|
||||
const o = { frequency: { value: 0 }, type: '', connect() {}, start(at) { ctx.tones.push({ freq: o.frequency.value, at }) }, stop() {} }
|
||||
return o
|
||||
}
|
||||
}
|
||||
FakeCtx.instances = []
|
||||
|
||||
let sound
|
||||
let session
|
||||
const ctx = () => FakeCtx.instances[0]
|
||||
const IPHONE = 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'
|
||||
const MAC = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15'
|
||||
const setDevice = (userAgent, maxTouchPoints = 0) => {
|
||||
Object.defineProperty(navigator, 'userAgent', { value: userAgent, configurable: true })
|
||||
Object.defineProperty(navigator, 'maxTouchPoints', { value: maxTouchPoints, configurable: true })
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.useFakeTimers()
|
||||
FakeCtx.instances = []
|
||||
window.AudioContext = FakeCtx
|
||||
session = { type: 'auto' }
|
||||
Object.defineProperty(navigator, 'audioSession', { value: session, configurable: true, writable: true })
|
||||
setDevice(IPHONE)
|
||||
vi.resetModules()
|
||||
sound = await import('./sound.js')
|
||||
})
|
||||
afterEach(() => { vi.useRealTimers() })
|
||||
|
||||
describe('sounds off', () => {
|
||||
it('creates no audio context and leaves the audio session alone', () => {
|
||||
sound.beep(false, 880, 0.15)
|
||||
sound.unlock(false)
|
||||
expect(FakeCtx.instances).toHaveLength(0)
|
||||
expect(session.type).toBe('auto')
|
||||
})
|
||||
})
|
||||
|
||||
describe('iOS: silent switch and interruptions (#152)', () => {
|
||||
it('the first tone gets the context running and leaves the audio session type alone', () => {
|
||||
sound.beep(true, 880, 0.15)
|
||||
expect(ctx().resumes).toBe(1)
|
||||
expect(ctx().state).toBe('running')
|
||||
expect(ctx().tones).toEqual([{ freq: 880, at: 0 }])
|
||||
expect(session.type).toBe('auto') // the silent-switch override is the setting's job
|
||||
})
|
||||
|
||||
it('resumes a context that a lock or app switch left suspended before scheduling the tone', () => {
|
||||
sound.beep(true, 880, 0.15)
|
||||
ctx().state = 'interrupted' // what iOS does on screen lock / app switch
|
||||
sound.beep(true, 660, 0.1)
|
||||
expect(ctx().resumes).toBe(2)
|
||||
expect(ctx().state).toBe('running')
|
||||
expect(ctx().tones).toHaveLength(2)
|
||||
})
|
||||
|
||||
// A real context reports 'suspended' until its resume() settles, so a burst can issue one
|
||||
// resume() per tone in a browser — harmless. What this pins is the guard itself.
|
||||
it('skips resume when the context already reports running', () => {
|
||||
sound.beep(true, 880, 0.15)
|
||||
sound.beep(true, 880, 0.15, 0.25)
|
||||
expect(ctx().resumes).toBe(1)
|
||||
})
|
||||
|
||||
it('replaces a context the browser has closed', () => {
|
||||
sound.beep(true, 880, 0.15)
|
||||
ctx().state = 'closed'
|
||||
sound.beep(true, 880, 0.15)
|
||||
expect(FakeCtx.instances).toHaveLength(2)
|
||||
expect(FakeCtx.instances[1].tones).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('works in a browser without navigator.audioSession', () => {
|
||||
Object.defineProperty(navigator, 'audioSession', { value: undefined, configurable: true, writable: true })
|
||||
sound.beep(true, 880, 0.15)
|
||||
expect(ctx().tones).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('the context sleeps between beeps', () => {
|
||||
it('suspends about a second after the last tone of a burst has ended', () => {
|
||||
sound.beep(true, 880, 0.25, 0); sound.beep(true, 1320, 0.5, 0.35) // last tone ends at 0.35 + 0.5 + 0.05 = 0.9s
|
||||
vi.advanceTimersByTime(1500)
|
||||
expect(ctx().state).toBe('running')
|
||||
vi.advanceTimersByTime(500)
|
||||
expect(ctx().state).toBe('suspended')
|
||||
expect(ctx().suspends).toBe(1)
|
||||
})
|
||||
|
||||
it('a later tone pushes the sleep out instead of cutting itself short', () => {
|
||||
sound.beep(true, 660, 0.1) // 3
|
||||
vi.advanceTimersByTime(1000)
|
||||
sound.beep(true, 660, 0.1) // 2
|
||||
vi.advanceTimersByTime(1000)
|
||||
sound.beep(true, 660, 0.1) // 1
|
||||
vi.advanceTimersByTime(1000)
|
||||
sound.beep(true, 880, 0.15, 0); sound.beep(true, 880, 0.15, 0.25) // 0: last tone ends at 0.25 + 0.15 + 0.05 = 0.45s
|
||||
expect(ctx().suspends).toBe(0)
|
||||
vi.advanceTimersByTime(1400)
|
||||
expect(ctx().state).toBe('running')
|
||||
vi.advanceTimersByTime(100)
|
||||
expect(ctx().state).toBe('suspended')
|
||||
expect(ctx().suspends).toBe(1)
|
||||
})
|
||||
|
||||
it('a short tone scheduled during a longer one does not shorten the longer one\'s sleep', () => {
|
||||
sound.beep(true, 880, 0.5) // ends 0.55s → sleep at 1.55s
|
||||
sound.beep(true, 660, 0.1) // ends 0.15s → must not pull the sleep to 1.15s
|
||||
vi.advanceTimersByTime(1200)
|
||||
expect(ctx().state).toBe('running')
|
||||
vi.advanceTimersByTime(400)
|
||||
expect(ctx().state).toBe('suspended')
|
||||
})
|
||||
})
|
||||
|
||||
describe('unlock from a tap', () => {
|
||||
it('gets the context created and running, without a tone', () => {
|
||||
sound.unlock(true)
|
||||
expect(FakeCtx.instances).toHaveLength(1)
|
||||
expect(ctx().state).toBe('running')
|
||||
expect(ctx().tones).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('lets the context sleep again on its own', () => {
|
||||
sound.unlock(true)
|
||||
vi.advanceTimersByTime(1000)
|
||||
expect(ctx().state).toBe('suspended')
|
||||
})
|
||||
|
||||
it('a tick after the tap finds a context it can resume rather than one it must create', () => {
|
||||
sound.unlock(true)
|
||||
vi.advanceTimersByTime(1000)
|
||||
sound.beep(true, 660, 0.1)
|
||||
expect(FakeCtx.instances).toHaveLength(1)
|
||||
expect(ctx().state).toBe('running')
|
||||
})
|
||||
})
|
||||
|
||||
describe('play on silent (Settings switch, WebKit only)', () => {
|
||||
it('is offered on an iPhone with the audio-session API', () => {
|
||||
expect(sound.playOnSilentSupported()).toBe(true)
|
||||
})
|
||||
|
||||
it('is offered on an iPad, which calls itself a Mac with a touch screen', () => {
|
||||
setDevice(MAC, 5)
|
||||
expect(sound.playOnSilentSupported()).toBe(true)
|
||||
})
|
||||
|
||||
it('is not offered on macOS Safari: it has the API but no ring/silent switch', () => {
|
||||
setDevice(MAC, 0)
|
||||
expect(sound.playOnSilentSupported()).toBe(false)
|
||||
})
|
||||
|
||||
it('is not offered where navigator.audioSession does not exist', () => {
|
||||
Object.defineProperty(navigator, 'audioSession', { value: undefined, configurable: true, writable: true })
|
||||
expect(sound.playOnSilentSupported()).toBe(false)
|
||||
})
|
||||
|
||||
it("on: the page's audio session becomes 'playback', which ignores the ring/silent switch", () => {
|
||||
sound.setPlayOnSilent(true)
|
||||
expect(session.type).toBe('playback')
|
||||
})
|
||||
|
||||
it("off: hands the choice back to the browser ('auto')", () => {
|
||||
sound.setPlayOnSilent(true)
|
||||
sound.setPlayOnSilent(false)
|
||||
expect(session.type).toBe('auto')
|
||||
})
|
||||
|
||||
it('is a no-op in a browser without navigator.audioSession', () => {
|
||||
Object.defineProperty(navigator, 'audioSession', { value: undefined, configurable: true, writable: true })
|
||||
expect(() => sound.setPlayOnSilent(true)).not.toThrow()
|
||||
})
|
||||
|
||||
it('survives a browser that rejects the type', () => {
|
||||
Object.defineProperty(navigator, 'audioSession', { value: Object.freeze({ type: 'auto' }), configurable: true, writable: true })
|
||||
expect(() => sound.setPlayOnSilent(true)).not.toThrow()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user