-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: big refactor of
InteractiveOscillator
(#9)
## Features * FEAT: create `SetupOscillator` comp * FEAT: create `SetupStates` comp * FEAT: improve docstrings and comments * FEAT: remove legacy instantiation, use new methods ## Docs * DOCS: Adds prop-types
- Loading branch information
1 parent
ac71565
commit 3333730
Showing
8 changed files
with
192 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { useRef, useEffect } from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
/** | ||
* Sets up webAudioAPI oscillator object. | ||
* New audio context, oscillator and gain node connected. | ||
* Rendered paused on startup. | ||
* @component | ||
* @example | ||
* const id = "my-osc" | ||
* const [isPlaying, setPlaying] = useState() | ||
* const { gainNodeRef, oscRef } = SetupOscillator({id:id, isPlaying:isPlaying,setPlaying:setPlaying}); | ||
*/ | ||
export default function SetupOscillator(props) { | ||
const audioContextRef = useRef(); | ||
const gainNodeRef = useRef(); | ||
const oscRef = useRef(); | ||
const playingRef = useRef(false); | ||
|
||
// initial osc starting | ||
useEffect(() => { | ||
const audioContext = new AudioContext(); | ||
const osc = audioContext.createOscillator(); | ||
const gainNode = audioContext.createGain(); | ||
// Connect and start | ||
osc.connect(gainNode); | ||
gainNode.connect(audioContext.destination); | ||
osc.start(); | ||
|
||
// Create refs to updatable params, start suspended | ||
gainNodeRef.current = gainNode; | ||
oscRef.current = osc; | ||
audioContextRef.current = audioContext; | ||
audioContext.suspend(); | ||
// Disconnect osc | ||
return () => { | ||
osc.disconnect(gainNode); | ||
gainNode.disconnect(audioContext.destination); | ||
audioContext.close(); | ||
}; | ||
}, []); | ||
// updates play/pause state | ||
useEffect(() => { | ||
if (playingRef.current !== props.isPlaying) { | ||
console.log( | ||
`${props.id} oscillator ` + (playingRef.current ? "stopped" : "started") | ||
); | ||
playingRef.current | ||
? audioContextRef.current.suspend() | ||
: audioContextRef.current.resume(); | ||
playingRef.current = !playingRef.current; | ||
} | ||
}, [props.isPlaying, props.id, props.setPlaying]); | ||
|
||
return { | ||
audioContextRef: audioContextRef, | ||
gainNodeRef: gainNodeRef, | ||
oscRef: oscRef, | ||
playingRef: playingRef, | ||
}; | ||
} | ||
SetupOscillator.propTypes = { | ||
/** playing useState */ | ||
isPlaying: PropTypes.bool, | ||
/** sets playing useState */ | ||
setPlaying: PropTypes.func, | ||
/** id */ | ||
id: PropTypes.string, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useState } from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
/** | ||
* Sets up react states for oscillator. | ||
* @component | ||
* @example | ||
* const freq = 440 | ||
* const oscType = "sine" | ||
* | ||
* const oscRef = useRef() // Typically defined with `SetupOscillator` | ||
* const { freqState, oscType, gainState } = SetupStates({initFreq:freq, initOscType:oscType}); | ||
*/ | ||
export default function SetupStates(props) { | ||
const [freq, setFreq] = useState(props.initFreq); | ||
const [oscType, setOscType] = useState(props.initOscType); | ||
const [gain, setGain] = useState(1); | ||
|
||
return { | ||
freqState: { get: freq, set: setFreq }, | ||
oscType: { get: oscType, set: setOscType }, | ||
gainState: { get: gain, set: setGain }, | ||
}; | ||
} | ||
|
||
SetupStates.propTypes = { | ||
/** Initial frequency of oscillator [Hz] */ | ||
initFreq: PropTypes.number, | ||
/** Initial oscillator waveshape */ | ||
// TODO: set this from the oscillatorTypes struct | ||
initOscType: PropTypes.oneOf(["sine", "square", "sawtooth", "triangle"]), | ||
}; |
Oops, something went wrong.