Sound

Sound class simplifies common tasks like audio inputs and visualizations using a subset of Web Audio API. It can be used with other audio libraries like tone.js, and extended to support additional web audio functions. See the guide to get started.

Class in src/Play.ts

Constructor

new Sound ( type )

Construct a Sound instance. Usually, it's more convenient to use one of the static methods like Sound.load or Sound.from.

parameters
type: SoundType

a SoundType string: "file", "input", or "gen"

returns
An instance of Sound
source

Accessors

GET binSize : number

If an analyzer is added (see analyze function), get the number of frequency bins in the analyzer.

GET SET buffer : AudioBuffer

Get this Sound's AudioBuffer (if any) instance for advanced use-cases. See Sound.loadAsBuffer.

GET ctx : AudioContext

Get this Sound's AudioContext instance for advanced use-cases.

GET SET frequency : number

If the sound is generated, this sets and gets the frequency of the tone.

GET node : AudioNode

Get this Sound's AudioNode subclass instance for advanced use-cases.

GET outputNode : AudioNode

Get this Sound's Output node AudioNode instance for advanced use-cases.

GET playable : boolean

Indicate whether the sound is ready to play. When loading from a file, this corresponds to a "canplaythrough" event. You can also use this.source.addEventListener( 'canplaythrough', ...) if needed. See also MDN documentation.

GET playing : boolean

Indicate whether the sound is currently playing.

GET progress : number

A value between 0 to 1 to indicate playback progress.

GET sampleRate : number

Get the sample rate of the audio, for example, at 44100 hz.

GET source : HTMLMediaElement

Get this Sound's Audio element (if used) instance for advanced use-cases. See Sound.load.

GET stream : MediaStream

Get this Sound's MediaStream (eg, from microphone, if in use) instance for advanced use-cases. See Sound.input

GET type : SoundType

Get the type of input for this Sound instance. Either "file", "input", or "gen"

Methods

analyze ( size, minDb, maxDb, smooth )

Add an analyzer to this Sound. Call this once only.

parameters
size: number (default value: 256)

the number of frequency bins

minDb: number (default value: -100)

Optional minimum decibels (corresponds to AnalyserNode.minDecibels)

maxDb: number (default value: -30)

Optional maximum decibels (corresponds to AnalyserNode.maxDecibels)

smooth: number (default value: 0.8)

Optional smoothing value (corresponds to AnalyserNode.smoothingTimeConstant)

returns
this

source

connect ( node )

Connect another AudioNode to this Sound instance's AudioNode. Using this function, you can extend the capabilities of this Sound instance for advanced use cases such as filtering.

parameters
node: AudioNode

another AudioNode

returns
this

source

createBuffer ( buf )

Create or re-use an AudioBuffer. Only needed if you are using Sound.loadAsBuffer.

parameters
buf: AudioBuffer

an AudioBuffer. Optionally, you can call this without parameters to re-use existing buffer.

returns
this

source

freqDomain ( )

Get the raw frequency-domain data from analyzer as unsigned 8-bit integers. An analyzer must be added before calling this function (See analyze function).

returns
Uint8Array

source

freqDomainTo ( size, position, trim )

Map the frequency-domain data from analyzer to a range. An analyzer must be added before calling this function (See analyze function).

parameters
size: PtLike

map each data point [index, value] to [width, height]

position: PtLike (default value: [0,0])

Optionally, set a starting [x, y] position. Default is [0, 0]

trim: number[] (default value: [0,0])

Optionally, trim the start and end values by [startTrim, data.length-endTrim]

returns
Group

a Group containing the mapped values

example

form.point( s.freqDomainTo( space.size ) )

source

STATIC from ( node, ctx, type, stream )

Create a Sound given an AudioNode and an AudioContext from Web Audio API. See also this example using tone.js in the guide.

parameters
node: AudioNode

an AudioNode instance

ctx: AudioContext

an AudioContext instance

type: SoundType (default value: "gen")

a string representing a type of input source: either "file", "input", or "gen".

stream: MediaStream

Optionally include a MediaStream, if the type is "input"

returns
Sound

a Sound instance

source

STATIC generate ( type, val )

Create a Sound by generating a waveform using OscillatorNode.

parameters
type: OscillatorType

a string representing the waveform type: "sine", "square", "sawtooth", "triangle", "custom"

val: number | PeriodicWave

the frequency value in Hz to play, or a PeriodicWave instance if type is "custom".

returns
Sound

a Sound instance

example

Sound.generate( 'sine', 120 )

source

STATIC input ( constraint )

Create a Sound by streaming from an input device like microphone. Note that this function returns a Promise which resolves to a Sound instance.

parameters
constraint: MediaStreamConstraints

@param constraint Optional constraints which can be used to select a specific input device. For example, you may use enumerateDevices to find a specific deviceId;

returns
Promise

a Promise which resolves to Sound instance

example

Sound.input().then( s => sound = s );

source

STATIC load ( source, crossOrigin )

Create a Sound by loading from a sound file or an audio element.

parameters
source: HTMLMediaElement | string

either an url string to load a sound file, or an audio element.

crossOrigin: string (default value: "anonymous")

whether to support loading cross-origin. Default is "anonymous".

returns
Promise

a Sound instance

example

Sound.load( '/path/to/file.mp3' )

source

STATIC loadAsBuffer ( url )

Create a Sound by loading from a sound file url as AudioBufferSourceNode. This method is cumbersome since it can only be played once. Use this method for now if you need to visualize sound in Safari and iOS. Once Apple has full support for FFT with streaming HTMLMediaElement, this method will likely be deprecated.

parameters
url: string

an url to the sound file

returns
Promise

source

removeOutputNode ( )

Removes the 'output' node added from setOuputNode Note: if you start the Sound after calling this, it will play via the default node

returns
this

source

reset ( )

Stop playing and disconnect the AudioNode.

returns
this

source

setOutputNode ( outputNode )

Sets the 'output' node for this Sound This would typically be used after Sound.connect, if you are adding nodes in your chain for filtering purposes.

parameters
outputNode: AudioNode

The AudioNode that should connect to the AudioContext

returns
this

source

start ( timeAt )

Start playing. Internally this connects the AudioNode to AudioContext's destination.

parameters
timeAt: number (default value: 0)

optional parameter to play from a specific time

returns
this

source

stop ( )

Stop playing. Internally this also disconnects the AudioNode from AudioContext's destination.

returns
this

source

timeDomain ( )

Get the raw time-domain data from analyzer as unsigned 8-bit integers. An analyzer must be added before calling this function (See analyze function).

returns
Uint8Array

source

timeDomainTo ( size, position, trim )

Map the time-domain data from analyzer to a range. An analyzer must be added before calling this function (See analyze function).

parameters
size: PtLike

map each data point [index, value] to [width, height]

position: PtLike (default value: [0,0])

Optionally, set a starting [x, y] position. Default is [0, 0]

trim: number[] (default value: [0,0])

Optionally, trim the start and end values by [startTrim, data.length-endTrim]

returns
Group

a Group containing the mapped values

example

form.point( s.timeDomainTo( space.size ) )

source

toggle ( )

Toggle between start and stop. This won't work if using Sound.loadAsBuffer, since AudioBuffer can only be played once. (See Sound.createBuffer to reset buffer for replay).

returns
this

source