Search code examples
javascriptaudiop5.jsvolume

p5 can't find variable masterVolume


I added p5.sound.js as instructed, and assured it was in the correct place. I try to call:

masterVolume(1.0, 0.05, 0)

And it gives me the error. What is the deal?

Here is the complete page of the code that fails:

<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width"/>
    <title>test</title>
    <style>
    body { 
    margin: 0; 
    -webkit-user-select: none;
    }
    canvas { display: block; }
    .green {
        color:#219900;
    }
    </style>
</head>
<body>

<script src="js/p5.min.js"></script>
<script src="js/p5.sound.min.js"></script>


  <script>
    masterVolume(1.0, 0.05, 0)
  </script>

</body>
</html>

Solution

  • In order to call masterVolumep5js setup must be called first.

    Try putting the call to masterVolume in a function that will run in or after setup.

    function setup() {
       // try the call here
       masterVolume(1.0, 0.05, 0);
    }
    function draw(){
       // or try the call here or in any p5js method
       masterVolume(1.0, 0.05, 0);
    }