Skip to content

Instantly share code, notes, and snippets.

@AlexRatmansky
Last active November 5, 2020 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexRatmansky/92544afb8133b223cc97d8b0ec6a8fe0 to your computer and use it in GitHub Desktop.
Save AlexRatmansky/92544afb8133b223cc97d8b0ec6a8fe0 to your computer and use it in GitHub Desktop.
useSountToneJs
import { useEffect, useRef, useState } from "react";
import { MembraneSynth, Offline, Player } from "tone";
interface Synthhh {
kick?: Player;
}
const sound: Synthhh = {};
Offline(() => {
new MembraneSynth().toDestination().triggerAttackRelease("C2", "32n");
}, 5).then((buffer) => {
sound.kick = new Player(buffer).toDestination();
});
export const useSound = () => {
const [counter, setCounter] = useState(0);
const synthRef = useRef(sound);
useEffect(() => {
synthRef.current.kick?.start();
}, [counter]);
const setCounterHandler = () => {
setCounter((v) => v + 1);
};
return [setCounterHandler];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment