Submodule 18.3: Sound in p5.js
Site: | ΕΛ/ΛΑΚ Moodle |
Course: | Study / Web Design and Web Development |
Book: | Submodule 18.3: Sound in p5.js |
Printed by: | Guest user |
Date: | Tuesday, 3 December 2024, 8:51 PM |
Description
- load and play sound
- play and pause buttons
- capture sound
load and play sound
p5.js has a build-in p5.js sound library which enables us to manipulate sound.
In order to load sound, we use the loadSound()
function. Similar to other media, we put this function inside the preload()
.
To actually play the sound we should use the play()
function.
Moreover, we can adjust the sound level by using the setVolume()
function. This function takes values from 0.0 (minimum) to 1.0 (maximum).
Let's see an example of how the above should look like in the code.
Example
Here is an example code where we load and play a sound:
function preload() {
mySound = loadSound('sounds/Sg.mp3');
}
function setup() {
// define the sound level
mySound.setVolume(0.1);
// play the sound
mySound.play();
}
You can see the result of the above code here
Exercise
- Open your Visual Studio editor and the
p5yourName
folder. - Open the file
ex812.js
in your editor and save it asex1031.js
- Open the file
ex812.html
in your editor and save it asex1031.html
- In the
ex1031.html
file, update the link toex1031.js
from exersice812.js - Go to the
index.html
file and create, underModule 10
, alink
to theex1031.html
file with the title "load and play sound".
Modify the ex1031.js
file and use as a base the above example to load a sound. The sound volume should increase as long as the mouse is pressed and when released, should return to the original volume. You can see here an example.
function preload() {
mySound = loadSound('sounds/Sg.mp3');
}
function setup() {
// define the sound level
mySound.setVolume(0.1);
// play the sound
mySound.play();
}
function mousePressed() {
// increase sound level when mouse is pressed
mySound.setVolume(0.5);
}
function mouseReleased() {
// return to the original sound level
mySound.setVolume(0.1);
}
Do a Git commit with the message "load and play sound".
- See more about the loadSound() function
- See more about the play() function
- See more about the setVolume() function
play and pause buttons
In order to control whether our sound will play or pause we can create a button which will pause the sound if it is playing and make it play if it is not.
We have already see how we can make a sound play. In order to make it pause we use the pause()
function.
However, how we can detect whether a sound is playing?
p5.js has the isPlaying()
function. This function returns a boolean value which is true if the sound is playing and false if it is not.
Let's see how we can use it to play/pause our song.
Example
Here is an example code where we use a button to play/pause a sound:
// create a variable for the button
let myBtn;
function preload() {
mySound = loadSound('sounds/Sg.mp3');
}
function setup() {
createCanvas(200,200);
// create the button to control sound
myBtn = createButton('play');
// call the function playPause when the user presses the mouse
myBtn.mousePressed(playPause);
}
function playPause(){
//use the isPlaying to determine whether the sound is playing or not
// if the sound is not playing
if (!mySound.isPlaying()){
// make it play
mySound.play();
// define the sound level
mySound.setVolume(0.5);
}
// else pause it
else {
mySound.pause();
}
}
You can see the result of the above code here. This example works best in mozilla firefox.
Exercise
- Open your Visual Studio editor and the
p5yourName
folder. - Open the file
ex812.js
in your editor and save it asex1032.js
- Open the file
ex812.html
in your editor and save it asex1032.html
- In the
ex1032.html
file, update the link toex1032.js
from exersice812.js - Go to the
index.html
file and create, underModule 10
, alink
to theex1032.html
file with the title "play and pause buttons".
Modify the ex1032.js
file and use as a base the above example and load a sound which will start from the beginning each time the user presses the button. To do this you should use the stop()
function for which you can learn more here. You can see here an example.
// create a variable for the button
let myBtn;
function preload() {
mySound = loadSound('sounds/Sg.mp3');
}
function setup() {
createCanvas(200,200);
// create the button to control sound
myBtn = createButton('play');
// call the function playPause when the user presses the mouse
myBtn.mousePressed(playPause);
}
function playPause(){
//use the isPlaying to determine whether the sound is playing or not
// if the sound is not playing
if (!mySound.isPlaying()){
// make it play
mySound.play();
// define the sound level
mySound.setVolume(0.5);
}
// else stop it
else {
mySound.stop();
}
}
Do a Git commit with the message "play and pause buttons".
- See more about the isPlaying() function
- See more about the pause() function
capture sound
p5.js gives us the ability to capture microphone sound from our computers.
In order to do that we use the constructor new p5.AudioIn()
function.
To turn the microphone on we use the start()
function and to turn it off we use the stop()
function.
Moreover, we can get the volume levels of our microphone by using the getLevel()
function. This functions returns values from 0.0 to 1.0.
Example
Here is an example code where we capture microphone sound and draw an ellipse which will be changing size according to sound level:
// create variable to hold microphone inpute
let myMic;
function setup() {
createCanvas(windowWidth, windowHeight);
// capture microphone sound
myMic = new p5.AudioIn();
// turn on microphone
myMic.start();
}
function draw() {
background(123,23,45);
// get the audio level
let myMicLevel = myMic.getLevel();
// create a sketch which will be changing according to audio level
// we multiply the level by 1000 since the values it returns are very small
ellipse(width / 2, height / 2, myMicLevel * 1000, myMicLevel * 1000);
}
You can see the result of the above code here
Exercise
- Open your Visual Studio editor and the
p5yourName
folder. - Open the file
ex812.js
in your editor and save it asex1033.js
- Open the file
ex812.html
in your editor and save it asex1033.html
- In the
ex1033.html
file, update the link toex1033.js
from exersice812.js - Go to the
index.html
file and create, underModule 10
, alink
to theex1033.html
file with the title "capture sound".
Modify the ex1033.js
file and create an ellipse which will change color according to microphone volume. Moreover, if the volume is greater or equal to 100, keep its y position constant and change its x position according to volume. Else, change its y position. You can see here an example.
/// create variable to hold microphone inpute
let myMic;
// create variables for RGB
let r,g,b;
function setup() {
createCanvas(windowWidth, windowHeight);
background(123,23,45);
// capture microphone sound
myMic = new p5.AudioIn();
// turn on microphone
myMic.start();
}
function draw() {
// get the audio level
let myMicLevel = myMic.getLevel();
// change ellipse fill according to microphone volume
r = myMicLevel * 2000;
g = myMicLevel * 1000;
b = myMicLevel * 800;
fill (r,g,b,50);
// create a sketch which will be changing according to audio level
// if the volume is greater or equal to 100
if (myMicLevel * 8000>=100){
// change its x position
ellipse((200+(myMicLevel * 8000)), 200, 200);
}
else {
// else change its y position
ellipse(200, (200+(myMicLevel * 8000)), 200);
}
}
Do a Git commit with the message "capture sound".
- See more about the new p5.AudioIn() function
- See more about the getLevel() function