Check out the Code! 2 Sound Module Instructions Here

The following are my notes on Sound which I covered on Mar 29, 2023. In this review session we cover three topics:

Here is the video from my weekly review session on March 29, 2023 :

GMT20230329-180501_Recording_1920x1080.mp4

Playing Sound Files - P5 Reference

let drums;

function preload() {
	// Note in order to play sound files, you need to upload them
	//   to the P5 editor. In this case, I have uploaded drums.mp3
	//   into my code folder.
  drums=loadSound("drums.mp3");  
}

function setup() {
  createCanvas(400, 400);
  drums.loop();
}

function mousePressed() {
  if (drums.isPlaying()) {
    drums.pause();
  } else {
    drums.play();
  }
}

function draw() {
  background(220);
}

Here is the code on playing clips of sounds from our review session (this code is more advanced than the code above as it has three sounds and how to work with sliders)

Are you looking for sounds to play with. Check out the website freesound.org

Visualizing Sound

Amplitude - See P5 Reference. The following code is the minimum code needed to do an amplitude visualization. Note that this code will do nothing as the Sketch is not playing any sound. Add this code to a sketch that is making sound.

let amp;

function setup() {
  // you may have other code here in the setup
	// make sure you start something with sound
	// as the Amplitude only works if sound is playing in your sketch
	amp = new p5.Amplitude();
}

function draw() {
	background(170);
	rect(200,200,amp.getLevel()*50,amp.getLevel()*50);
}

In our review session, we combined the above Amplitude visualization with our playing sounds sketch. Check it out at the P5 Editor.