Open In App

p5.js | waveform() Function

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The waveform() function is an inbuilt function in p5.js library. This function is used to return the amplitude values, which will be the snapshot of amplitude readings in a single buffer. This can be used to draw the waveform of a sound. 

Syntax:

waveform(bins, precision)

Note: All the sound-related functions only work when the sound library is included in the head section of the index.html file. 

Parameter: This function accepts two parameters as mentioned above and described below:

  • bins: This parameter is used to hold an integer that will be represented like 2n. The range is 16 to 1024, the default value is 1024 and it is an optional parameter.
  • precision: This parameter is used to return Float32 Array which is more precise than a regular array if the previous parameter holds any value. It is also optional.

The below example illustrates the p5.waveform() function in JavaScript.

Example: 

javascript




var sound;
var wvfrm;
     
function preload() {
     
    // Initialize sound
    sound = loadSound("pfivesound.mp3");
}
     
function setup() {
     
    // Playing the preloaded sound
    sound.play();
  
    // will return no of frames
    wvfrm = sound.waveform();
    console.log(wvfrm);
}


Online editor: https://editor.p5js.org/ 

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/ 

Supported Browsers: The browsers supported by p5.js waveform() function are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads