Reference p5.SoundFile

p5.SoundFile

Load and play sound files.

Examples

Methods

isLoaded

Returns true if the sound file finished loading successfully.

play
Start the soundfile.
playMode

p5.SoundFile has two play modes: restart and sustain. Play Mode determines what happens to a p5.SoundFile if it is triggered while in the middle of playback. In sustain mode, playback will continue simultaneous to the new playback. In restart mode, play() will stop playback and start over. With untilDone, a sound will play only if it's not already playing. Sustain is the default mode.

pause
Pause the soundfile.
loop
Loop the soundfile.
setLoop
Set a loop region, and optionally a playback rate, and amplitude for the soundfile.
isLooping
Return the playback state of the soundfile.
isPlaying
Return the playback state of the soundfile.
isPaused

Returns true if a p5.SoundFile is paused, false if not (i.e. playing or stopped).

stop
Stop the soundfile.
pan

Set the stereo panning of a p5.sound object to a floating point number between -1.0 (left) and 1.0 (right). Default is 0.0 (center).

getPan

Returns the current stereo pan position (-1.0 to 1.0)

rate
Set the playback rate of the soundfile.
setVolume

Multiply the output volume (amplitude) of a sound file between 0.0 (silence) and 1.0 (full volume). 1.0 is the maximum amplitude of a digital sound, so multiplying by greater than 1.0 may cause digital distortion. To fade, provide a rampTime parameter. For more complex fades, see the Envelope class.

Alternately, you can pass in a signal source such as an oscillator to modulate the amplitude with an audio signal.

duration
Returns the duration of a sound file in seconds.
currentTime

Return the current position of the p5.SoundFile playhead, in seconds. Time is relative to the normal buffer direction, so if reverseBuffer has been called, currentTime will count backwards.

jump
Move the playhead of a soundfile that is currently playing to a new position.
channels
Gets the number of channels in the sound file.
sampleRate
Gets the number of channels in the sound file.
frames
Return the number of samples in a sound file.
getPeaks

Returns an array of amplitude peaks in a p5.SoundFile that can be used to draw a static waveform. Scans through the p5.SoundFile's audio buffer to find the greatest amplitudes. Accepts one parameter, 'length', which determines size of the array. Larger arrays result in more precise waveform visualizations.

Inspired by Wavesurfer.js.

reverseBuffer

Reverses the p5.SoundFile's buffer source. Playback must be handled separately (see example).

onended
Define a function to call when the soundfile is done playing.
connect

Connects the output of a p5sound object to input of another p5.sound object. For example, you may connect a p5.SoundFile to an FFT or an Effect. If no parameter is given, it will connect to the main output. Most p5sound objects connect to the master output when they are created.

disconnect

Disconnects the output of this p5sound object.

setPath
Change the path for the soundfile.
setBuffer

Replace the current Audio Buffer with a new Buffer.

addCue

Schedule events to trigger every time a MediaElement (audio/video) reaches a playback cue point.

Accepts a callback function, a time (in seconds) at which to trigger the callback, and an optional parameter for the callback.

Time will be passed as the first parameter to the callback function, and param will be the second parameter.

removeCue

Remove a callback based on its ID. The ID is returned by the addCue method.

clearCues

Remove all of the callbacks that had originally been scheduled via the addCue method.

save

Save a p5.SoundFile as a .wav file. The browser will prompt the user to download the file to their device. To upload a file to a server, see getBlob

getBlob

This method is useful for sending a SoundFile to a server. It returns the .wav-encoded audio data as a "Blob". A Blob is a file-like data object that can be uploaded to a server with an http request. We'll use the httpDo options object to send a POST request with some specific options: we encode the request as multipart/form-data, and attach the blob as one of the form values using FormData.

start
Start the soundfile.
amp
Adjust the amplitude of the soundfile.
Notice any errors or typos? Please let us know. Please feel free to edit src/SoundFile.js and open a pull request!

Related References