react-audio-mixer
v0.4.0
Published
A collection of audio and music hooks
Downloads
52
Readme
React Audio Mixer
Do you need audio options in React without the fuss of dealing with contexts? Are you trying to mix a bunch of AudioNode objects together but it's hard? Do you have lots of componants trying to work together with audioNodes?
Use react-audio-mixer
to make music in your React apps!
Usage
function App() {
return (
<AudioProvider>
<MicrophoneNode
name="mic"
echoCancellation
noiseSuppression
/>
<GainNode
name="gain"
listen="mic"
gain={0.5}
/>
<SpeakerNode
listen="gain"
/>
</AudioProvider>
);
}
All nodes can be provided the following:
name?
: Name of the node or group of nodeslisten?
: Name node(s) to take data from. Not available on input nodesonNode?
: AudioNode callbackonError?
: Error handler
Some node attributes can take sequence values that correspond with [AudioParam][mdn-audioparam].
These are provided as an Array AudioParamSequence
in any comination of the following tuples:
setValueAtTime
:type
: 'setValue'value
: numberstartTime
: number
linearRampToValueAtTime
:type
: 'linearRamp'value
: numberendTime
: number
exponentialRampToValueAtTime
:type
: 'exponentialRamp'value
: numberendTime
: number
setTargetAtTime
:type
: 'setTarget'target
: numberstartTime
: numbertimeConstant
: number
setValueCurveAtTime
:type
: 'setValueCurve'values
: number[] | Float32ArraystartTime
: numberduration
: number
AudioProvider
Creates an audio context for this module
latencyHint?
: Context latency hintsampleRate?
: Context sample rate
useAudio default
Returns the context and status of the AudioProvider
useAudioDevices
Requests and gathers available audio media devices.
const [devices, ready] = useAudioDevices();
useAudioInputDevices
Requests and gathers available input audio media devices.
const [devices, ready] = useAudioInputDevices();
useAudioOutputDevices
Requests and gathers available output audio media devices.
const [devices, ready] = useAudioOutputDevices();
useStream
Requests a stream via MediaStreamConstraints
const constraints = useMemo(() => ({ audio: true, video: false }))
const stream = useStream(constraints);
Group Node
Scopes children in a new group
children?
: Audio NodesinputName?
(default:input
): input node nameoutputName?
(default:output
): putput node name
Microphone Node
deviceId?
: Media device idechoCancellation?
: Echo cancellationnoiseSuppression?
: Noise suppressionautoGainControl?
: Auto gain controlonStream?
: MediaStream handler
StreamIn Node
stream
: MediaProvider
Oscillator Node
type?
: Oscillator typefrequency
: FrequencyfrequencySequence?
: AudioParamSequencedetune?
: detunedetuneSequence?
: AudioParamSequencestart?
: Start timeend?
: End timeonEnded?
: Ended handler
Speaker Node
deviceId?
: Media device id
StreamOut Node
stream
: MediaStream
Null Node
A node just for pivoting on.
Gain Node
gain
: Gain valuegainSequence?
: AudioParamSequence
Analyser Node
type
:frequency
orwaveform
fftSize?
: FFT sizeinterval?
: Interval between updatesmin?
: Decibels minimummax?
: Decibels maximumfloatBuffer?
: Update with Float32ArrayonUpdate
: Update data handler
HzAnalyser Node
limit?
: Volume limitpadding?
: Gap paddingfftSize?
: FFT sizeinterval?
: Interval between updatesmin?
: Decibels minimummax?
: Decibels maximumonUpdate
: Update data handler
NoteAnalyser Node
noteList?
: Note listlimit?
: Volume limitpadding?
: Gap paddingfftSize?
: FFT sizeinterval?
: Interval between updatesmin?
: Decibels minimummax?
: Decibels maximumonUpdate
: Update data handler
Custom Node
type
: node typenode
: AudioNode
import useAudio, { CustomNode } from 'react-audio-mixer';
function SomeNode(props) {
const { name, listen, onError } = props;
const { context, ready } = useAudio();
const node = useMemo(() => {
try {
context.createDynamicsCompressor();
catch(e) {}
}, [context]);
// Do something with your node
return (
<CustomNode
name={name}
listen={listen}
type="node"
node={node}
onError={onError}
/>
);
}
Examples
PulseGain Node
function PulseGain(props) {
const { name, listen, min = 0, max = 1, interval = 2000 } = props;
const { context } = useAudio();
const [gainSequence, setGainSequence] = useState();
useEffect(() => {
const update = () => {
const now = context.currentTime;
setGainSequence([
['linearRamp', max, interval / 2000 + now],
['linearRamp', min, interval / 1000 + now],
]);
};
update();
const timer = setInterval(update, interval);
return () => {
clearInterval(timer);
};
}, [context, min, max, interval]);
return (
<GainNode
name={name}
listen={listen}
gain={min}
gainSequence={gainSequence}
/>
);
}
RandomBeeps Node
function RandomBeeps(props) {
const { name, min = 256, max = 512, length = 500, margin = 500 } = props;
const { context } = useAudio();
const [[frequency, start, end], setState] = useState([0, 0, 0]);
const updateState = useCallback((offset = 0) => {
const now = context.currentTime + offset;
setState([
min + (max - min) * Math.random(),
margin / 1000 + now,
(margin + length) / 1000 + now,
]);
}, [context, min, max, length, margin]);
useEffect(() => {
updateState(-margin / 1000);
}, [margin, updateState]);
return frequency && (
<OscillatorNode
name={name}
frequency={frequency}
start={start}
end={end}
onEnded={updateState}
/>
);
}
License
Copyright (c) 2021, Michael Szmadzinski. (MIT License)