跳到正文
hackernews100(buttons.joshwcomeau.com)· gaurang_tandon·· 5 小時前品質評分57

Button Sounds 收錄 50 多種按鈕音效,並提供網頁開發套件

Button Sounds – collection of professionally-recorded button clicks and presses

重點導讀

同一個按鈕音效每次播放都一模一樣,重複聽起來容易顯得生硬。

  • Button Sounds 收錄 50 多種按鈕聲音,每種都有多個按下和放開的錄音,播放時會隨機挑一段。
  • 開發者也能用 NPM 套件把音效加到網頁按鈕上,或選擇音高變化和復古低音質效果。這讓反覆播放的按鈕聲聽起來更有變化。

AI 伴隨問答

自備 Key 深度分析

針對這篇報導有疑問?可直接點選快捷問題或向 AI 追問背景脈絡與技術細節:

正文

Sound Playground

Select Sound

About This Sound

If you fall deep enough into the mechanical keyboard rabbit hole, you’ll start hearing terms like “creamy” and “thocky”. This keyboard from Leobog with custom “Reaper” switches is supposedly one of the thockiest.

Press Sample

Release Sample

How it works: Click the big red button to play the currently-selected sound. Browse the selection using the left-hand sidebar (_protip:_ after clicking the button, you can use the up/down arrow keys, ↑ / ↓, to quickly move between sounds!).

Each sound has a handful of individually-recorded samples that are randomly selected on every click. The highlights the currently-playing sample, and you can click the numbers to lock it.

How it works: Click the big red button to play the currently-selected sound. There are more than 50 sounds to choose from in the “Select Sound” list; you can tap the up/down arrows to move between them.

Each sound has a handful of individually-recorded samples that are randomly selected on every tap and release. You can lock it to a specific sample by tapping the numbers towards the bottom of the demo.

Not hearing anything? Frustratingly, browsers can decide to block a website from playing any sound. iOS Safari seems particularly strict about this. So, if you’re not hearing a click sound when tapping the big red button, please check out this website on a desktop computer.

About _Button Sounds_

There’s two pieces to this project:

1. A library of 50+ high-quality button sounds, with up to 16 recorded samples per sound.
2. An NPM package that makes it as easy as possible to start using these sounds in your web projects.

On this page, we’re going to look at both of these pieces. But first, let’s talk about _why_ I created this project.

Multi-sampled sounds

The vast majority of existing SFX are “one-shots”; you get a single recorded instance of the sound. If that sound gets triggered often (like, clicking a button repeatedly), it winds up feeling very monotonous and artificial. In real life, button presses don’t sound _identical_ every time.

One of my favourite lil’ sound effect tricks is to record the same sound multiple times. Whenever it gets triggered, a random instance is selected and played. It’s subtle, but it winds up feeling a lot more realistic and organic.

Here's a little demo. Try spamming the “Trigger” button while locked to the first sample, and then click the “Unlock” button and try again to hear the difference:

Image 1

down1

up1

down2

up2

down3

up3

down4

up4

Like I said, it can be a bit subtle, especially if you’re listening on laptop or phone speakers. But I hope you’ll agree that the random sample selection just _feels_ better.

This is the core idea with this library. I scoured my home looking for things with buttons and made a whole bunch of recordings. Each of the 50+ sounds has at least 8 samples (4 pressing, 4 releasing). In reality, I recorded many more than that, but I only kept the most interesting ones.

A super-convenient library

The other half of this project is the NPM package(opens in new tab) that makes it as easy as possible to start using these sounds in your own projects.

Here’s a quick “Hello World” example:

import { wireUp } from 'button-sounds';

// Grab a reference to the button you want to add sounds to:
const button = document.querySelector('.my-button');

// Attach pointer event listeners, and specify the sound to play:
wireUp(button, 'hi75-soft');
```

In this example, I’m loading the hi75-soft sound, but you can pick any of the sounds listed in the playground above!

The wireUp function is doing _a bunch_ of stuff under the hood, including:

1. Preloading the specified audio file so that it’s available when the first click occurs.
2. Adding event handlers to the button so that the correct sounds trigger at the right times. There are some subtle gotchas(opens in new tab) here that the library takes care of automatically.
3. Picking random samples on every press/release, and optionally adding a small amount of pitch variation for even more variety.
4. Optionally reduce the bit/sample rate for crunchy NES-style sounds.

By default, the audio files will be loaded from the JSDelivr CDN, so you don’t even need to host them yourself (though you can if you’d prefer). The files are only loaded when you call .wireUp, which means only the selected sound(s) will be downloaded.

On the JS side, the button-sounds package is tiny, only adding ~3kB gzip(opens in new tab) to your bundle (though it does asynchronously load Howler, a ~10kB JS library for playing sounds).

The lower-level API

I consider the wireUp function to be the high-level API. Call one function and you’re good to go.

In some cases, however, you might want more control over when the press/release sounds trigger. In that case, you can use the lower-level API:

import { load, press, release } from 'button-sounds';

// Preload the audio file ASAP:
load('hi75-soft');

const btn = document.querySelector('my-button');

// Trigger the sounds whenever you’d like:
btn.addEventListener('pointerdown', () => {
if (someCondition) {
press('hi75-soft');
}
});
```

This lower-level API is intended to be used for less-basic use cases, of course. If the goal is to trigger the sounds on press/release, the higher-level API is both easier to use and provides better UX (specifically around making sure the 'release' sound always triggers correctly, regardless of where the cursor is).

Additional configuration

As shown in the playground above, the library also includes options for pitch variation and lower fidelity:

import { wireUp } from 'button-sounds';

const button = document.querySelector('.my-button');

wireUp(
button,
'hi75-soft',
{
pitchVariation: 0.1,
lofi: true,
lofiOptions: {
bits: 4,
downsample: 0.25,
},
}
);
```

The lofi parameter is a boolean that can be turned on for a typical NES-style bitcrusher effect, but you can also supply LofiOptions to control the specific bit depth and downsampling rate.

React bindings

Finally, if you use React, I’ve also included a couple of React hooks that tackle all of this stuff. Here’s the main high-level API:

import { useButtonSounds } from 'button-sounds/react';

function SomeComponent() {
const buttonRef = React.useRef(null);

useButtonSounds(buttonRef, 'hi75-soft');

return (
<button ref={buttonRef}>
Click me
</button>
);
}
```

And here’s the lower-level API that exposes press and release functions directly:

import { useImperativeButtonSounds } from 'button-sounds/react';

function SomeComponent() {
const { press, release } = useImperativeButtonSounds('hi75-soft');

return (
<button
onPointerDown={() => {
if (someCondition) {
press();
}
}}
>
Click me
</button>
);
}
```

Going deeper

This has been a quick introduction to the library. If you want to learn more, there’s extensive documentation on Github:

I don’t have a docs page specifically for agents, but the links above should inform both humans and robots alike, so feel free to point your agent at the most relevant doc for your use case!

來源: hackernews100 (buttons.joshwcomeau.com)
開啟原文

讀者討論

理性交流 · 即時討論