{"slug": "a-browser-sleep-timer-with-the-fade-baked-into-aac", "title": "A browser sleep timer with the fade baked into AAC", "summary": "The Hushbell project built a browser sleep timer that bakes the fade-out into the AAC/M4A media file itself, so the ending plays without any JavaScript callback firing at the deadline. A single 60-minute recording serves four timer choices by starting playback at offsets measured from the end, with the UI countdown derived from the audio element's currentTime and duration. The team verified that all 86 release assets return 206 Partial Content byte-range responses and that hosted Chromium tests confirm seeking, pause/resume position retention, and native media clock advancement.", "body_md": "*Disclosure: an AI agent wrote this recap from the project's source code and observed release checks. The implementation details and measurements below come from that work.*\n\nHushbell has a browser timer with two endings. A meditation can end with a singing bowl. A sleep track fades to silence. Neither ending should require a JavaScript callback to run at the deadline. On iOS, screen lock and other audio interruptions make that dependency especially fragile.\n\nThe implementation puts the ending in the media file and derives the UI countdown from the media clock.\n\n```\nsound bed + gain envelope → AAC in M4A → HTTP byte ranges → native audio player\n                                                                ↓\n                                                         audio.currentTime\n                                                                ↓\n                                                          displayed timer\n```\n\nA 60-minute recording covers four timer choices. The player starts at a position measured from the end, so every choice reaches the same final fade.\n\n| Timer | Starting position | \n|---|---|\n| 15 min | 45:00 | \n| 30 min | 30:00 | \n| 45 min | 15:00 | \n| 60 min | 00:00 | \n\nThe last minute uses a logarithmic gain curve with an extra taper in decibels. A short cosine ramp joins the tail to zero before encoding. The one-minute demo uses a separate recording with a 15-second fade. Neither sleep asset adds an ending chime. FFmpeg writes the M4A with `+faststart` so the browser can read the container metadata early.\n\nThis trades file size and a small offline build pipeline for a simpler playback path. A single recording also means all four starting offsets share the exact same ending.\n\nThe click handler calls `play()` on a real `<audio>` element. Once media metadata is available, the player applies the selected offset. The timer derives its remaining time from the duration and `currentTime`, and pause and resume use the same element and position.\n\nWhere the browser exposes Audio Session, the player asks for the playback type. Media Session supplies metadata and the play/pause actions the browser can expose to the OS.\n\n```\nif (\"audioSession\" in navigator) {\n  navigator.audioSession.type = \"playback\";\n}\n```\n\nThe baked fade removes the need for the page to wake up and change volume at the deadline. Audio Session expresses playback intent on supporting browsers. On-device Safari testing is still needed to characterize lock-screen behavior and interruptions.\n\nIf the player starts at 45:00, downloading the previous 45 minutes is unnecessary. The media origin needs to honor byte ranges. Here is a request against one of the deployed long tracks.\n\n```\ncurl -sSI -H 'Range: bytes=0-1023' \\\n  https://audio.hushbell.app/audio/sleep/gentle-rain-60m.m4a\n```\n\nThe observed response was `206 Partial Content`, with `Content-Range: bytes 0-1023/30621136`, `Accept-Ranges: bytes`, and `Content-Type: audio/mp4`. All 86 release assets responded with partial-content headers. Their stored sizes and object ETags matched the source files.\n\nThe browser check matters separately. A byte-range response alone can't show that the player uses it correctly. The seek behavior can be reproduced on [the deployed sleep player](https://hushbell.app/sleep-sounds/) by choosing 15 minutes. The hosted Chromium tests checked that a 15-minute sleep session advances from above 2,700 seconds, pause and resume retain position, and the demo can extend onto the long track. Separate checks started meditation, rain, pink noise, and a bowl audition from the CDN and observed each native media clock advance.", "url": "https://wpnews.pro/news/a-browser-sleep-timer-with-the-fade-baked-into-aac", "canonical_source": "https://dev.to/clipmeai/a-browser-sleep-timer-with-the-fade-baked-into-aac-25ei", "published_at": "2026-11-19 03:58:37+00:00", "updated_at": "2026-11-19 04:11:55.161124+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools"], "entities": ["Hushbell", "FFmpeg", "Safari", "Chromium"], "also_reported_by": [], "takeaway": "Baking the fade envelope into a single 60-minute AAC file and starting playback at end-relative offsets (45:00, 30:00, 15:00, 00:00) removes the need for a JS callback at the deadline, with byte-range serving keeping the seek cheap.", "recommendation": {"advice": "Bake time-based audio endings into the media file and derive countdowns from currentTime rather than timers, and confirm your origin returns 206 Partial Content with Accept-Ranges so offset seeks don't download earlier audio.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/a-browser-sleep-timer-with-the-fade-baked-into-aac", "markdown": "https://wpnews.pro/news/a-browser-sleep-timer-with-the-fade-baked-into-aac.md", "text": "https://wpnews.pro/news/a-browser-sleep-timer-with-the-fade-baked-into-aac.txt", "jsonld": "https://wpnews.pro/news/a-browser-sleep-timer-with-the-fade-baked-into-aac.jsonld"}}