Skip to content

Commit a3e35a1

Browse files
committed
ui(LFO): update EchoLFO example
1 parent a5d5e2b commit a3e35a1

1 file changed

Lines changed: 41 additions & 2 deletions

File tree

example/src/components/visualizaion/EchoLFO.tsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
11
import React from 'react'
22
import * as Tone from 'tone'
33
import { LFO, Knob, Button, LFOProps, SineIcon, SquareIcon, TriangleIcon } from '@echo-ui'
4+
import { Play, StopCircle } from 'lucide-react'
45

56
export const EchoLFO = () => {
67
const [type, setType] = React.useState<LFOProps['type']>('sine')
78
const [frequency, setFrequency] = React.useState(0)
89
const [amplitude, setAmplitude] = React.useState(0)
910
const [delay, setDelay] = React.useState(0)
11+
const [isPlaying, setIsPlaying] = React.useState(false)
1012

11-
const lfo = React.useRef<Tone.LFO | null>(null)
13+
const panner = React.useRef<Tone.AutoPanner | null>(null)
14+
const osc = React.useRef<Tone.Oscillator | null>(null)
1215

1316
React.useEffect(() => {
14-
lfo.current = new Tone.LFO('4n', 400, 4000)
17+
panner.current = new Tone.AutoPanner({
18+
frequency: 4,
19+
depth: 1,
20+
})
21+
.toDestination()
22+
.start()
23+
osc.current = new Tone.Oscillator({
24+
volume: -12,
25+
type: 'square6',
26+
frequency: 'C4',
27+
}).connect(panner.current)
1528
}, [])
1629

30+
React.useEffect(() => {
31+
panner.current?.set({
32+
frequency: frequency * 10,
33+
})
34+
35+
osc.current?.set({
36+
type,
37+
frequency: 440,
38+
volume: amplitude,
39+
})
40+
}, [type, frequency, amplitude, delay])
41+
42+
const triggerPlay = () => {
43+
if (isPlaying) {
44+
osc.current?.stop()
45+
setIsPlaying(false)
46+
} else {
47+
osc.current?.start()
48+
setIsPlaying(true)
49+
}
50+
}
51+
1752
return (
1853
<section className="h-32 w-2/3 mb-32">
1954
<Button.Group className="mb-2" radius="sm">
@@ -28,6 +63,10 @@ export const EchoLFO = () => {
2863
</Button>
2964
</Button.Group>
3065

66+
<Button onClick={triggerPlay}>
67+
{isPlaying ? <StopCircle size={24} /> : <Play size={24} />}
68+
</Button>
69+
3170
<LFO amplitude={amplitude} frequency={frequency} delay={delay} type={type} />
3271

3372
<Knob.Group

0 commit comments

Comments
 (0)