Skip to content

Commit cb6bfea

Browse files
committed
feat(LFO): changing the units of frequency and related effects
1 parent ed81cea commit cb6bfea

4 files changed

Lines changed: 26 additions & 24 deletions

File tree

example/src/components/visualizaion/EchoLFO.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Play, StopCircle } from 'lucide-react'
55

66
export const EchoLFO = () => {
77
const [type, setType] = React.useState<LFOProps['type']>('sine')
8-
const [frequency, setFrequency] = React.useState(0)
8+
const [frequency, setFrequency] = React.useState(1)
99
const [amplitude, setAmplitude] = React.useState(0)
1010
const [delay, setDelay] = React.useState(0)
1111
const [isPlaying, setIsPlaying] = React.useState(false)
@@ -69,25 +69,23 @@ export const EchoLFO = () => {
6969

7070
<LFO amplitude={amplitude} frequency={frequency} delay={delay} type={type} />
7171

72-
<Knob.Group
73-
className="mt-2"
74-
size={40}
75-
trackWidth={3}
76-
min={0}
77-
max={1}
78-
step={0.1}
79-
pointerHeight={6}
80-
>
72+
<Knob.Group className="mt-2" size={40} trackWidth={3} pointerHeight={6}>
8173
<Knob
8274
value={frequency}
8375
onChange={setFrequency}
8476
topLabel="Frequency"
85-
bottomLabel={frequency * 100 + '%'}
77+
min={1}
78+
max={15}
79+
step={1}
80+
bottomLabel={frequency + 'Hz'}
8681
/>
8782

8883
<Knob
8984
className="mr-3"
9085
value={amplitude}
86+
min={0}
87+
max={1}
88+
step={0.1}
9189
onChange={setAmplitude}
9290
topLabel="Amplitude"
9391
bottomLabel={amplitude * 100 + '%'}

packages/components/visualization/LFO/LFO.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ type DataItem = { x: number; y: number }
2020
export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
2121
const {
2222
type = TYPE,
23-
frequency: _frequency = FREQUENCY,
23+
frequency = FREQUENCY,
2424
amplitude: _anplitude = AMPLITUDE,
2525
delay: _delay = DELAY,
2626
lineWidth = LINE_WIDTH,
2727
lineColor = LINE_COLOR,
2828
...restProps
2929
} = props
3030

31-
const frequency = validValue(_frequency, 0, 1)
3231
const amplitude = validValue(_anplitude, 0, 1)
3332
const delay = validValue(_delay, 0, 100)
3433

@@ -62,10 +61,7 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
6261
const generateScales = () => {
6362
const { width, height } = dimensions.current
6463

65-
xScale.current = d3
66-
.scaleLinear()
67-
.domain([0, 4 * Math.PI * (frequency * 10)])
68-
.range([0, width])
64+
xScale.current = d3.scaleLinear().domain([0, 1]).range([0, width])
6965
yScale.current = d3.scaleLinear().domain([0, 1]).range([height, 0])
7066
}
7167

@@ -142,8 +138,9 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
142138
): { x: number; y: number }[] => {
143139
if (type !== 'sine' || !svg) return []
144140

145-
const data = d3.range(0, 4 * Math.PI * (frequency * 10), 0.01).map((x) => {
146-
const y = Math.sin(x) * amplitude * 0.5 + 0.5
141+
const data = d3.range(0, 1, 0.001).map((x) => {
142+
const adjustedX = x * 2 * Math.PI * frequency
143+
const y = Math.sin(adjustedX) * amplitude * 0.5 + 0.5
147144
return { x, y }
148145
})
149146

@@ -158,8 +155,10 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
158155
const centerY = 0.5
159156
const halfHeight = centerY * amplitude
160157
const data = [{ x: 0, y: centerY }].concat(
161-
d3.range(0, 4 * Math.PI * (frequency * 10), 0.01).map((x) => {
162-
const y = Math.floor(x / Math.PI) % 2 === 0 ? centerY + halfHeight : centerY - halfHeight
158+
d3.range(0, 1, 0.001).map((x) => {
159+
const adjustedX = x * 2 * Math.PI * frequency
160+
const y =
161+
Math.floor(adjustedX / Math.PI) % 2 === 0 ? centerY + halfHeight : centerY - halfHeight
163162
return { x, y }
164163
}),
165164
)
@@ -173,12 +172,13 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
173172
if (type !== 'triangle' || !svg) return []
174173

175174
const centerY = 0.5
176-
const data = d3.range(0, 4 * Math.PI * (frequency * 10), 0.01).map((x) => {
175+
const data = d3.range(0, 1, 0.001).map((x) => {
177176
// Adjust the phase so that the waveform starts in the middle of the rising segment
178177
// and ensure that the starting point is at the center of the Y-axis
179178
// By mapping the x value to the corresponding position in a cycle,
180179
// the waveform is in the middle of the rising segment when it starts
181-
const phase = ((x + Math.PI / 2) % (2 * Math.PI)) / Math.PI
180+
const adjustedX = x * 2 * Math.PI * frequency
181+
const phase = ((adjustedX + Math.PI / 2) % (2 * Math.PI)) / Math.PI
182182

183183
let y
184184
if (phase < 1) y = phase * amplitude

packages/components/visualization/LFO/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
export const TYPE = 'sine'
22
export const MIN = 1
33
export const MAX = 15
4-
export const FREQUENCY = 0.5
4+
export const MIN_FREQUENCY = 1
5+
export const MAX_FREQUENCY = 15
6+
export const FREQUENCY = 1
57
export const AMPLITUDE = 0.5
68
export const DELAY = 0
79

packages/components/visualization/LFO/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export interface LFOProps extends React.HTMLAttributes<HTMLDivElement> {
22
frequency?: number
33
amplitude?: number
4+
minFrequency?: number
5+
maxFrequency?: number
46
min?: number
57
max?: number
68
delay?: number

0 commit comments

Comments
 (0)