|
| 1 | +import React from 'react' |
| 2 | +import * as Tone from 'tone' |
| 3 | +import { LFO, Knob, Button, LFOProps, SineIcon, SquareIcon, TriangleIcon } from 'echo-ui' |
| 4 | +import { Play, Pause } from 'lucide-react' |
| 5 | + |
| 6 | +export const LFODefault = () => { |
| 7 | + const [type, setType] = React.useState<LFOProps['type']>('sine') |
| 8 | + const [frequency, setFrequency] = React.useState(1) |
| 9 | + const [amplitude, setAmplitude] = React.useState(0.7) |
| 10 | + const [isPlaying, setIsPlaying] = React.useState(false) |
| 11 | + |
| 12 | + const autoFilter = React.useRef<Tone.AutoFilter | null>(null) |
| 13 | + const osc = React.useRef<Tone.Oscillator | null>(null) |
| 14 | + |
| 15 | + React.useEffect(() => { |
| 16 | + autoFilter.current = new Tone.AutoFilter({ |
| 17 | + frequency: frequency, |
| 18 | + depth: 1, |
| 19 | + }) |
| 20 | + .toDestination() |
| 21 | + .start() |
| 22 | + |
| 23 | + osc.current = new Tone.Oscillator({ |
| 24 | + volume: -(1 - amplitude) * 100, |
| 25 | + frequency, |
| 26 | + }).connect(autoFilter.current) |
| 27 | + }, []) |
| 28 | + |
| 29 | + React.useEffect(() => { |
| 30 | + autoFilter.current?.set({ |
| 31 | + frequency: frequency, |
| 32 | + }) |
| 33 | + |
| 34 | + osc.current?.set({ |
| 35 | + type, |
| 36 | + frequency: 440, |
| 37 | + volume: -(1 - amplitude) * 60, |
| 38 | + }) |
| 39 | + }, [type, frequency, amplitude]) |
| 40 | + |
| 41 | + const triggerPlay = () => { |
| 42 | + if (isPlaying) { |
| 43 | + osc.current?.stop() |
| 44 | + setIsPlaying(false) |
| 45 | + } else { |
| 46 | + osc.current?.start() |
| 47 | + setIsPlaying(true) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return ( |
| 52 | + <section className="h-32 mb-32 mx-auto"> |
| 53 | + <Button.Group className="mb-2" radius="sm"> |
| 54 | + <Button toggled={type === 'sine'} onClick={() => setType('sine')}> |
| 55 | + <SineIcon /> |
| 56 | + </Button> |
| 57 | + <Button toggled={type === 'square'} onClick={() => setType('square')}> |
| 58 | + <SquareIcon /> |
| 59 | + </Button> |
| 60 | + <Button toggled={type === 'triangle'} onClick={() => setType('triangle')}> |
| 61 | + <TriangleIcon /> |
| 62 | + </Button> |
| 63 | + <Button |
| 64 | + onClick={triggerPlay} |
| 65 | + toggled={isPlaying} |
| 66 | + className="data-[toggled='true']:bg-green-500" |
| 67 | + > |
| 68 | + {isPlaying ? ( |
| 69 | + <Pause className="fill-current" size={15} /> |
| 70 | + ) : ( |
| 71 | + <Play className="fill-current" size={15} /> |
| 72 | + )} |
| 73 | + </Button> |
| 74 | + </Button.Group> |
| 75 | + |
| 76 | + <LFO amplitude={amplitude} frequency={frequency} type={type} /> |
| 77 | + |
| 78 | + <Knob.Group className="mt-2 flex justify-center" size={40} trackWidth={3} pointerHeight={6}> |
| 79 | + <Knob |
| 80 | + value={frequency} |
| 81 | + onChange={setFrequency} |
| 82 | + topLabel="Frequency" |
| 83 | + min={1} |
| 84 | + max={15} |
| 85 | + step={1} |
| 86 | + bottomLabel={frequency + 'Hz'} |
| 87 | + /> |
| 88 | + |
| 89 | + <Knob |
| 90 | + className="mr-3" |
| 91 | + value={amplitude} |
| 92 | + min={0} |
| 93 | + max={1} |
| 94 | + step={0.1} |
| 95 | + onChange={setAmplitude} |
| 96 | + topLabel="Amplitude" |
| 97 | + bottomLabel={amplitude * 100 + '%'} |
| 98 | + /> |
| 99 | + </Knob.Group> |
| 100 | + </section> |
| 101 | + ) |
| 102 | +} |
0 commit comments