This is an old revision of the document!


the basics of sound in p5js

This one plays two sound files simultaneously. There are a few other things it does, which you should try to disable so that you can understand how it works. Try disabling one of the sounds. It also uses a randomized start time for the second of the sound files. Also notice that it pauses the loop when the mouse is released; that means that the loop starts from where it left off when the button is re-pressed. A different possible behaviour is for the loop to restart. Can you look at the documentation and figure out how to do that?

https://editor.p5js.org/renick/sketches/PC5p6LAGl

Notice the terrible clicking that happens when you stop pressing the mouse button. There's not proper envelope for the sound. That sudden drop from some amplitude to 0 makes the click. One way to fix that might be this:

After your add your sound in the editor, be sure to save the drawing. My sound didn't load until I did so.

Be sure to add the file extension in the file name. I didn't and got a difficult-to-interpret error.

  • p5js-sound1.1710913946.txt.gz
  • Last modified: 13 months ago
  • by renick