Skip to content
Related Articles

Related Articles

p5.js | SoundFile Object Installation and Methods
  • Difficulty Level : Medium
  • Last Updated : 30 Dec, 2019
GeeksforGeeks - Summer Carnival Banner

The SoundFile object is a part of the p5.sound which is a library of JavaScript. This object contains so many functions nearly 30+ functions are there. Each one of them is useful to design a sounded website. By using this object you can easily add audio into your website and play, pause, stop anything you want. To do so you just need to attach the sound library in your index file. To do that we will follow some procedure that is mentioned below one by one.

  • Step 1: First, you have to download the p5.Sound library from this link, after opening this link go for p5.js complete section like below.
  • Step 2: After downloading the file you have to unzip the folder then you will some files inside the p5.js folder like index.html, sketch.js by opening the index.html file you will see p5.js is already linked to it you can change that for faster performance by replacing that script with the below script.

    <script src=”../p5.min.js”></script>

    The Sample HTML page may look like this:




    <!DOCTYPE html>
    <html>
        <head>
            <script src=”../p5.min.js”></script>
            <script src="sketch.js"></script>
        </head>
          
        <body>
        </body>
    </html>
  • Step 3:In this step, we will set up the code editor for p5.js. The best code editor, in my opinion, is Brackets, Atom. But you can use others also like Notepad++, Sublime, etc.
  • Step 4: Now open your editor and trace down to your p5.js folder location and open the sketch.js file. In this file we will perform the task, here we will write our script to test this library and use them on our website.
  • Step 5: In this step, you will get to know how to use it.




    function setup() {
      
    }
      
    function draw() {
      ellipse(50, 50, 80, 80);
    }

    You will get somthing like this:

List of all the function p5.SoundFile:

FunctionDescription
loadSound()This function is used to load the sound.
isLoaded()This function is used to check the sound is loaded or not.
play()This function is use to play the loaded sound.
playMode()This function is used to play the sound in two playing mode restart and sustain.
pause()This function is used to pause the played sound.
loop()This function is used to create the loop of that sound.
islooping()This function is used to check the loop function called or not.
setLoop()This function is used to create loop define number of times.
isPlaying()This function is used to check audio is playing or not.
isPaused()This function is used to check audio is paused or not.
stop()This function is used to stop the played audio.
setVolume()This function is used to set the volume of the audio range is 0 to 1.
pan()This function is used to set the left right pan of the audio range is -1(left) to 1(right).
getPan()This function is used to check the pan side.
rate()This function is used to set the playing speed of the audio.
duration()This function is used to get the duration of the audio.
currentTime()This function is used to get the current time of the playing audio.
jump()This function is used to jump at some specific point of the audio.
channels()This function is used to get the channels Mono means 1 Stereo means 2.
sampleRate()This function is used to get the samplerate per second of the audio.
frames()This function is used to get the frames of the audio, where frames= samplesrate * duration.
getPeaks()This function is used to get the array of amplitude peaksof the audio.
reverseBuffer()This function is used to play the audio in reverse way.
onended()This function is used to call event after ending the audio.
connect()This function is used to connects the output of a p5sound object to input of another p5.sound object.
disconnect()This function is used to disconnects the output of this p5sound object.
setPath()This function is used to set the pathe for the audio.
setBuffer()This function is used to set the buffer for the audio.
processPeaks()This function is used to get the beats of the audio.
addCue()This function is used to add events after specified time.
removeCue()This function is used to remove specific event that was shedule by the addCue function.
clearCues()This function is to clear all the event that was schedule by the addCue function.
save()This function is used to save the audio in localy.
getBlob()This function is used to upload the audio in the server.

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :