Skip to content

Commit cbf6b63

Browse files
committed
feat(LFO): rewrite props
1 parent 3ffa034 commit cbf6b63

4 files changed

Lines changed: 44 additions & 33 deletions

File tree

example/src/components/visualizaion/EchoLFO.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import React from 'react'
2+
import * as Tone from 'tone'
23
import { LFO, Knob, Button, LFOProps, SineIcon, SquareIcon, TriangleIcon } from '@echo-ui'
34

45
export const EchoLFO = () => {
56
const [type, setType] = React.useState<LFOProps['type']>('sine')
67
const [frequency, setFrequency] = React.useState(0)
7-
const [speed, setSpeed] = React.useState(0)
8+
const [amplitude, setAmplitude] = React.useState(0)
89
const [delay, setDelay] = React.useState(0)
910

11+
const lfo = React.useRef<Tone.LFO | null>(null)
12+
13+
React.useEffect(() => {
14+
lfo.current = new Tone.LFO('4n', 400, 4000)
15+
}, [])
16+
1017
return (
1118
<section className="h-32 w-2/3 mb-32">
1219
<Button.Group className="mb-2" radius="sm">
@@ -21,7 +28,7 @@ export const EchoLFO = () => {
2128
</Button>
2229
</Button.Group>
2330

24-
<LFO speed={speed} frequency={frequency} delay={delay} type={type} />
31+
<LFO amplitude={amplitude} frequency={frequency} delay={delay} type={type} />
2532

2633
<Knob.Group
2734
className="mt-2"
@@ -41,10 +48,10 @@ export const EchoLFO = () => {
4148

4249
<Knob
4350
className="mr-3"
44-
value={speed}
45-
onChange={setSpeed}
46-
topLabel="Speed"
47-
bottomLabel={speed * 100 + '%'}
51+
value={amplitude}
52+
onChange={setAmplitude}
53+
topLabel="Amplitude"
54+
bottomLabel={amplitude * 100 + '%'}
4855
/>
4956

5057
<Knob

packages/components/visualization/LFO/LFO.tsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,28 @@ import { useStyle } from './styles'
77
import {
88
TYPE,
99
DELAY,
10-
SPEED,
1110
WIDTH,
1211
HEIGHT,
1312
LINE_WIDTH,
1413
LINE_COLOR,
15-
MIN,
16-
MAX,
14+
AMPLITUDE,
1715
FREQUENCY,
1816
} from './constants'
1917

2018
export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
2119
const {
2220
type = TYPE,
2321
frequency: _frequency = FREQUENCY,
22+
amplitude: _anplitude = AMPLITUDE,
2423
delay: _delay = DELAY,
25-
speed: _speed = SPEED,
26-
min = MIN,
27-
max = MAX,
2824
lineWidth = LINE_WIDTH,
2925
lineColor = LINE_COLOR,
3026
...restProps
3127
} = props
3228

3329
const frequency = validValue(_frequency, 0, 1)
34-
const speed = validValue(_speed, 0.1, 1)
35-
const delay = validValue(_delay, 1, 100)
30+
const amplitude = validValue(_anplitude, 0, 1)
31+
const delay = validValue(_delay, 0, 100)
3632

3733
const LFORef = useRef<LFORef | null>(null)
3834
const xScale = useRef<d3.ScaleLinear<number, number> | null>(null)
@@ -43,7 +39,7 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
4339
useEffect(() => {
4440
generateScales()
4541
generateWave()
46-
}, [speed, frequency, delay, type, lineWidth, lineColor])
42+
}, [frequency, amplitude, delay, type, lineWidth, lineColor])
4743

4844
const dimensions = useResizeObserver(
4945
LFORef,
@@ -56,7 +52,7 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
5652
true,
5753
{
5854
frequency,
59-
speed,
55+
amplitude,
6056
delay,
6157
},
6258
)
@@ -66,9 +62,9 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
6662

6763
xScale.current = d3
6864
.scaleLinear()
69-
.domain([0, 4 * Math.PI * (speed * 10)])
65+
.domain([0, 4 * Math.PI * (frequency * 10)])
7066
.range([0, width])
71-
yScale.current = d3.scaleLinear().domain([min, max]).range([height, 0])
67+
yScale.current = d3.scaleLinear().domain([0, 1]).range([height, 0])
7268
}
7369

7470
const generateWave = () => {
@@ -79,6 +75,17 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
7975

8076
svg.selectAll('*').remove()
8177

78+
if (frequency === 0) {
79+
svg
80+
.append('line')
81+
.attr('x1', 0)
82+
.attr('y1', height / 2)
83+
.attr('x2', width)
84+
.attr('y2', height / 2)
85+
.attr('stroke', lineColor)
86+
.attr('stroke-width', lineWidth)
87+
}
88+
8289
if (delay > 0) {
8390
svg
8491
.append('circle')
@@ -104,8 +111,8 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
104111
const generateSineWave = (svg: d3.Selection<d3.BaseType, unknown, null, undefined>) => {
105112
if (type !== 'sine' || !svg) return
106113

107-
const data = d3.range(0, 4 * Math.PI * (speed * 10), 0.01).map((x) => {
108-
const y = Math.sin(x) * frequency * (max - min) * 0.5 + (max + min) / 2
114+
const data = d3.range(0, 4 * Math.PI * (frequency * 10), 0.01).map((x) => {
115+
const y = Math.sin(x) * amplitude * 0.5 + 0.5
109116
return { x, y }
110117
})
111118

@@ -127,12 +134,10 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
127134
const generateSquareWave = (svg: d3.Selection<d3.BaseType, unknown, null, undefined>) => {
128135
if (type !== 'square' || !svg) return
129136

130-
const center = (max + min) / 2
131-
const halfHeight = ((max - min) * frequency) / 2
132-
133-
const data = [{ x: 0, y: center }].concat(
134-
d3.range(0, 4 * Math.PI * (speed * 10), 0.01).map((x) => {
135-
const y = Math.floor(x / Math.PI) % 2 === 0 ? center + halfHeight : center - halfHeight
137+
const halfHeight = 0.5 * amplitude
138+
const data = [{ x: 0, y: 0.5 }].concat(
139+
d3.range(0, 4 * Math.PI * (frequency * 10), 0.01).map((x) => {
140+
const y = Math.floor(x / Math.PI) % 2 === 0 ? 0.5 + halfHeight : 0.5 - halfHeight
136141
return { x, y }
137142
}),
138143
)
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
export const WIDTH = 360
2-
export const HEIGHT = 100
3-
41
export const TYPE = 'sine'
2+
export const MIN = 1
3+
export const MAX = 15
54
export const FREQUENCY = 0.5
5+
export const AMPLITUDE = 0.5
66
export const DELAY = 0
7-
export const SPEED = 10
8-
export const MIN = -10
9-
export const MAX = 10
107

118
export const LINE_COLOR = 'var(--echo-primary)'
129
export const LINE_WIDTH = 3
10+
export const WIDTH = 360
11+
export const HEIGHT = 100

packages/components/visualization/LFO/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export interface LFOProps extends React.HTMLAttributes<HTMLDivElement> {
22
frequency?: number
3+
amplitude?: number
34
min?: number
45
max?: number
56
delay?: number
6-
speed?: number
77
type?: 'sine' | 'square' | 'triangle'
88
lineColor?: string
99
lineWidth?: number

0 commit comments

Comments
 (0)