Skip to content

Sound effects 🔊 #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
"@astrojs/sitemap": "^3.3.0",
"@netlify/blobs": "^8.2.0",
"@netlify/edge-functions": "^2.11.1",
"@types/howler": "^2.2.12",
"astro": "^5.5.2",
"fathom-client": "^3.7.2",
"howler": "^2.2.4",
"three": "^0.174.0",
"typescript": "^5.8.2"
},
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/components/Controls.astro
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const { prev, next } = await getPagination(channelId);

<script>
import * as Fathom from "fathom-client";
import { playButtonClick } from "../sfx/button";

const powerButton = document.getElementById("power");
const screen = document.querySelector(".screen");
Expand All @@ -67,6 +68,9 @@ const { prev, next } = await getPagination(channelId);
const menuButton = document.getElementById("menu-button");
const channelButtons = document.querySelectorAll(".channel-button");

const buttons = document.querySelectorAll(".controls .button");
buttons.forEach(button => button.addEventListener('click', playButtonClick));

function updateButtonStates(powerState: string) {
if (powerState === "off") {
menuButton?.setAttribute("disabled", "true");
Expand Down
2 changes: 2 additions & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import SfxPreloader from "src/sfx/SfxPreloader.astro";
import "../styles/index.css";

interface Props {
Expand Down Expand Up @@ -63,6 +64,7 @@ const pageTitle =
type="font/woff2"
crossorigin
/>
<SfxPreloader />
</head>
<body>
<slot />
Expand Down
5 changes: 5 additions & 0 deletions src/sfx/SfxPreloader.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
import buttonSfxSprite from "./button-sprite.mp3";
---

<link rel="preload" href={buttonSfxSprite} as="fetch" crossorigin />
Binary file added src/sfx/button-sprite.mp3
Binary file not shown.
36 changes: 36 additions & 0 deletions src/sfx/button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Howl } from "howler";
import sprite from "./button-sprite.mp3";

/** The number of distinct button click samples stored in the sound sprite. */
const sampleCount = 11;

/** The duration of each sample in the sound sprite in milliseconds. */
const sampleDuration = 375;

/** The sprite that was most recently played. */
let lastSampleId: string | undefined;

/** Get a random ID for one of the sounds in the sound sprite. */
const getNextSampleId = (): string => {
const nextSampleId = String(Math.floor(Math.random() * sampleCount));
// Avoid repeating the same sample as the last click.
if (nextSampleId === lastSampleId) return getNextSampleId();
lastSampleId = nextSampleId;
return nextSampleId;
};

/** A Howler instance that plays back button clicks. */
const buttonSfx = new Howl({
src: sprite,
// This code tells Howler to break up the sprite file into 375ms chunks.
sprite: Object.fromEntries(
Array.from({ length: sampleCount }).map((_, index) => [
index,
[index * sampleDuration, sampleDuration],
]),
),
volume: 0.5,
});

/** Play a random plastic button click sound effect. */
export const playButtonClick = (): number => buttonSfx.play(getNextSampleId());