The setInput() function is an inbuilt function in p5.js library. This function is used to connects to the p5sound instance that is the master output by default. By using this function you can also pass in a specific source.
Syntax:
setInput(snd, smoothing)
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:
- snd: This parameter is use to set the sound source and it is optional.
- smoothing: This parameter is use to set the smooth amplitude reading and the range is between 0.0 to 1.0, this parameter is also optional.
Below example illustrate the p5.setInput() function in JavaScript:
function preload(){ sound1 = loadSound( 'song.mp3' ); sound2 = loadSound( 'pfivesound.mp3' ); } function setup(){ amplitude = new p5.Amplitude(); sound1.play(); sound2.play(); amplitude.setInput(sound2); } function draw() { background(255); fill(200); let gfg = amplitude.getLevel(); let size = map(gfg, 0, 1, 0, 400); ellipse(width/1, height/1, size*2, size*2); } function mousePressed(){ sound2.pause(); } function mouseReleased(){ sound2.play(); } |
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 setInput() function are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Safari
- Opera