npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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 nodes
  • listen?: Name node(s) to take data from. Not available on input nodes
  • onNode?: AudioNode callback
  • onError?: 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: number
    • startTime: number
  • linearRampToValueAtTime:
    • type: 'linearRamp'
    • value: number
    • endTime: number
  • exponentialRampToValueAtTime:
    • type: 'exponentialRamp'
    • value: number
    • endTime: number
  • setTargetAtTime:
    • type: 'setTarget'
    • target: number
    • startTime: number
    • timeConstant: number
  • setValueCurveAtTime:
    • type: 'setValueCurve'
    • values: number[] | Float32Array
    • startTime: number
    • duration: number

AudioProvider

Creates an audio context for this module

  • latencyHint?: Context latency hint
  • sampleRate?: 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 Nodes
  • inputName? (default: input): input node name
  • outputName? (default: output): putput node name

Microphone Node

  • deviceId?: Media device id
  • echoCancellation?: Echo cancellation
  • noiseSuppression?: Noise suppression
  • autoGainControl?: Auto gain control
  • onStream?: MediaStream handler

StreamIn Node

  • stream: MediaProvider

Oscillator Node

  • type?: Oscillator type
  • frequency: Frequency
  • frequencySequence?: AudioParamSequence
  • detune?: detune
  • detuneSequence?: AudioParamSequence
  • start?: Start time
  • end?: End time
  • onEnded?: Ended handler

Speaker Node

  • deviceId?: Media device id

StreamOut Node

  • stream: MediaStream

Null Node

A node just for pivoting on.

Gain Node

  • gain: Gain value
  • gainSequence?: AudioParamSequence

Analyser Node

  • type: frequency or waveform
  • fftSize?: FFT size
  • interval?: Interval between updates
  • min?: Decibels minimum
  • max?: Decibels maximum
  • floatBuffer?: Update with Float32Array
  • onUpdate: Update data handler

HzAnalyser Node

  • limit?: Volume limit
  • padding?: Gap padding
  • fftSize?: FFT size
  • interval?: Interval between updates
  • min?: Decibels minimum
  • max?: Decibels maximum
  • onUpdate: Update data handler

NoteAnalyser Node

  • noteList?: Note list
  • limit?: Volume limit
  • padding?: Gap padding
  • fftSize?: FFT size
  • interval?: Interval between updates
  • min?: Decibels minimum
  • max?: Decibels maximum
  • onUpdate: Update data handler

Custom Node

  • type: node type
  • node: 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)