SoundFile object with a path to a file.
The p5.SoundFile may not be available immediately because it loads the file information asynchronously.
To do something with the sound as soon as it loads pass the name of a function as the second parameter.
Only one file path is required. However, audio file formats (i.e. mp3, ogg, wav and m4a/aac) are not supported by all web browsers. If you want to ensure compatability, instead of a single file path, you may include an Array of filepaths, and the browser will choose a format that works.
Examples
Syntax
p5.SoundFile(path, [successCallback], [errorCallback], [whileLoadingCallback])
Parameters
path to a sound file (String). Optionally, you may include multiple file formats in an array. Alternately, accepts an object from the HTML5 File API, or a p5.File.
Name of a function to call once file loads
Name of a function to call if file fails to load. This function will receive an error or XMLHttpRequest object with information about what went wrong.
Name of a function to call while file is loading. That function will receive progress of the request to load the sound file (between 0 and 1) as its first parameter. This progress does not account for the additional time needed to decode the audio data.
Methods
Returns true if the sound file finished loading successfully.
Play the p5.SoundFile
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.
Pauses a file that is currently playing. If the file is not playing, then nothing will happen.
After pausing, .play() will resume from the paused position. If p5.SoundFile had been set to loop before it was paused, it will continue to loop after it is unpaused with .play().
Loop the p5.SoundFile. Accepts optional parameters to set the playback rate, playback volume, loopStart, loopEnd.
Set a p5.SoundFile's looping flag to true or false. If the sound is currently playing, this change will take effect when it reaches the end of the current playback.
Returns 'true' if a p5.SoundFile is currently looping and playing, 'false' if not.
Returns true if a p5.SoundFile is playing, false if not (i.e. paused or stopped).
Returns true if a p5.SoundFile is paused, false if not (i.e. playing or stopped).
Stop soundfile playback.
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).
Returns the current stereo pan position (-1.0 to 1.0)
Set the playback rate of a sound file. Will change the speed and the pitch. Values less than zero will reverse the audio buffer.
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.
Returns the duration of a sound file in seconds.
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.
Move the playhead of a soundfile that is currently playing to a new position and a new duration, in seconds. If none are given, will reset the file to play entire duration from start to finish. To set the position of a soundfile that is not currently playing, use the play
or loop
methods.
Return the number of channels in a sound file. For example, Mono = 1, Stereo = 2.
Return the sample rate of the sound file.
Return the number of samples in a sound file. Equal to sampleRate * duration.
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.
Reverses the p5.SoundFile's buffer source. Playback must be handled separately (see example).
Schedule an event to be called when the soundfile reaches the end of a buffer. If the soundfile is playing through once, this will be called when it ends. If it is looping, it will be called when stop is called.
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.
Disconnects the output of this p5sound object.
Reset the source for this SoundFile to a new path (URL).
Replace the current Audio Buffer with a new Buffer.
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.
Remove a callback based on its ID. The ID is returned by the addCue method.
Remove all of the callbacks that had originally been scheduled via the addCue method.
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
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
.