Last active
November 5, 2020 19:37
-
-
Save AlexRatmansky/92544afb8133b223cc97d8b0ec6a8fe0 to your computer and use it in GitHub Desktop.
useSountToneJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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