1- import cuid from 'cuid' ;
21import React , { useEffect , useRef , useState } from 'react' ;
32import { Portal } from 'App/shared/Portal' ;
43import { getGrid } from 'utils/getGrid' ;
54import { useTouchAndMouse } from 'hooks/useTouchAndMouse' ;
65import { Knob } from './Knob' ;
76import { Kit , FX } from 'App/Tone' ;
87import { ArrowUpDownIcon , ChevronDownIcon } from 'assets/icons' ;
8+ import { useRotaryKnob } from 'hooks/useRotaryKnob' ;
99
10- export const Mixer = ( ) => {
10+ export const SampleMixer = ( ) => {
1111 const grid = getGrid ( 9 ) ;
1212 return (
1313 < Portal targetId = 'overGridPortal' >
1414 < div id = 'mixer' className = 'mixer' >
15- < div className = 'mixSamples' >
15+ < div className = 'mixItemWrapper mixSamples' >
1616 { grid . map ( ( i ) => {
17- return < MixSample key = { `mixSample ${ i } ` } i = { i } /> ;
17+ return < MixSample key = { `mixItem ${ i } ` } i = { i } /> ;
1818 } ) }
1919 </ div >
2020 </ div >
@@ -27,25 +27,21 @@ const MixSample = ({ i }) => {
2727 const sample = Kit . samples [ i ] ;
2828
2929 const [ property , setProperty ] = useState ( 'volume' ) ;
30- const { value, startFunc, moveFunc, endFunc } = useRotaryKnob ( sample , property ) ;
31- const touchAndMouse = useTouchAndMouse ( startFunc , moveFunc , endFunc ) ;
32- useApplyValue ( sample , property , value ) ;
30+ const initialVal = getInitialValue ( sample , property ) ;
3331
3432 const onChange = ( { target : { value } } ) => setProperty ( value ) ;
3533
36- const id = `mixSample ${ i } ` ;
34+ const id = `mixItem ${ i } ` ;
3735 const knobId = id + property ;
3836 return (
39- < div id = { id } className = 'mixSample' >
40- < p className = 'sampleName' > { sample . name } </ p >
41- < div className = 'knobWrapper' >
42- < div className = 'knob' id = { knobId } { ...touchAndMouse } >
43- < label htmlFor = { knobId } >
44- < ArrowUpDownIcon />
45- </ label >
46- < Knob value = { value } />
47- </ div >
48- </ div >
37+ < div id = { id } className = 'mixItem' >
38+ < p className = 'mixItemName' > { sample . name } </ p >
39+ < RotaryKnob
40+ property = { property }
41+ sample = { sample }
42+ initialVal = { initialVal }
43+ knobId = { knobId }
44+ />
4945 < div className = 'customSelectWrapper' >
5046 < select
5147 id = 'mixerSelect'
@@ -65,67 +61,40 @@ const MixSample = ({ i }) => {
6561 </ select >
6662 < ChevronDownIcon />
6763 </ div >
68- < div className = { `mixSampleBorder border${ i } ` } />
64+ < div className = { `mixItemBorder border${ i } ` } />
6965 </ div >
7066 ) ;
7167} ;
7268
73- const useApplyValue = ( sample , property , value ) => {
69+ const RotaryKnob = ( { property, sample, initialVal, knobId } ) => {
70+ const { value, startFunc, moveFunc, endFunc } = useRotaryKnob ( initialVal ) ;
71+ const touchAndMouse = useTouchAndMouse ( startFunc , moveFunc , endFunc ) ;
72+
7473 const prevPropertyRef = useRef ( null ) ;
7574 useEffect ( ( ) => {
7675 if ( prevPropertyRef . current !== property ) return ( prevPropertyRef . current = property ) ;
7776 if ( property === 'volume' ) {
7877 let newVal = ( value - 100 ) * 0.25 ;
79- console . log ( newVal ) ;
8078 return ( sample . channel . volume . value = newVal ) ;
8179 }
8280 if ( property === 'pan' ) return ( sample . channel . pan . value = ( value - 50 ) / 100 ) ;
8381 sample [ property ] . set ( { gain : value / 100 } ) ;
8482 } , [ property , sample , value ] ) ;
85- } ;
86-
87- const useRotaryKnob = ( sample , property ) => {
88- const [ value , setValue ] = useState ( getValue ( sample , property ) ) ;
89- useEffect ( ( ) => {
90- setValue ( getValue ( sample , property ) ) ;
91- } , [ property , sample ] ) ;
92-
93- const prevYRef = useRef ( null ) ;
9483
95- const startFunc = ( e ) => {
96- prevYRef . current = getY ( e ) ;
97- } ;
98- const moveFunc = ( e ) => {
99- const newY = getY ( e ) ;
100- let amount = getKnobAmount ( newY , prevYRef . current ) ;
101- prevYRef . current = newY ;
102- setValue ( ( value ) => {
103- let newVal = value + amount ;
104- if ( newVal < 0 ) newVal = 0 ;
105- if ( newVal > 100 ) newVal = 100 ;
106- return newVal ;
107- } ) ;
108- } ;
109- const endFunc = ( ) => {
110- prevYRef . current = null ;
111- } ;
112-
113- return { value, startFunc, moveFunc, endFunc } ;
114- } ;
115-
116- const getY = ( e ) => {
117- let y ;
118- if ( e . touches ) y = e . touches [ 0 ] . clientY ;
119- else y = e . clientY ;
120- return y ;
121- } ;
122-
123- const getKnobAmount = ( newY , prevY ) => {
124- let amount = prevY - newY ;
125- return amount ;
84+ return (
85+ < div className = 'knobWrapper' >
86+ < div className = 'knob' id = { knobId } { ...touchAndMouse } >
87+ < label htmlFor = { knobId } >
88+ < ArrowUpDownIcon />
89+ </ label >
90+ < Knob value = { value } />
91+ </ div >
92+ </ div >
93+ ) ;
12694} ;
12795
128- const getValue = ( sample , property ) => {
96+ const getInitialValue = ( sample , property ) => {
97+ console . log ( sample , property ) ;
12998 if ( property === 'volume' ) {
13099 let value = sample . channel . volume . value * 4 + 100 ;
131100 if ( value > 100 ) value = 100 ;
0 commit comments