# useTapTempo

> Tap tempo with a rolling average and idle reset.

`useTapTempo` powers the [tempo control](/docs/components/tempo-control). Use it with your own input, sound pad or keyboard shortcut.

```npm
npx shadcn-svelte@latest add @audiocn-svelte/use-tap-tempo
```

## Usage

```svelte
<script lang="ts">
	import { useTapTempo } from "#lib/hooks/use-tap-tempo.svelte.js";

	const tempo = useTapTempo({ defaultValue: 120 });
</script>

<button type="button" onclick={(event) => tempo.tap(event)}>Tap</button>
<span>{tempo.value} BPM</span>
```

Call `tempo.tap()` once per beat. The second tap produces a BPM value; later taps average the last four intervals. A gap longer than two seconds starts a fresh sequence without changing the current value on its first tap. Taps less than 80 ms apart are ignored.

BPM values are always whole numbers. Tapped tempos, `value`, `defaultValue` and `setValue()` round to the nearest integer.

Call `tap()` from your app's keyboard handler or hotkey library.

Call it during component setup. The options can be an object or a getter. Read `value` and `tapCount` from the returned object: they are getters, so destructuring them loses updates.

## Options

<PropsTable
	rows={[
		["value / defaultValue", "number", "120 (uncontrolled)", "BPM."],
		["min / max", "number", "30 / 600", "Positive integer BPM bounds."],
		[
			"tapTimeout",
			"number",
			"2000",
			"Idle milliseconds before a new sequence. Set at least 60000 / min for slower tempos.",
		],
		["disabled / readOnly", "boolean", "false", "Prevent taps and value changes."],
		["onValueChange", "(value, details) => void", null, "details.reason: tap, input or keyboard."],
		[
			"onValueCommitted",
			"(value) => void",
			null,
			"Called after a measured tap, including an unchanged value.",
		],
	]}
/>

## Returns

<PropsTable
	rows={[
		["value", "number", null, "Current integer BPM, clamped to the configured range."],
		[
			"tap",
			"(event?: Event) => void",
			null,
			"Record a tap. Pass its native event to include it in change details.",
		],
		[
			"setValue",
			"(value, details?) => void",
			null,
			"Change BPM and clear tap history. The default reason is input.",
		],
		["resetTaps", "() => void", null, "Clear tap history without changing BPM."],
		[
			"tapCount",
			"number",
			"0",
			"Accepted taps, including the first. Use it to restart visual feedback.",
		],
	]}
/>
