Topics in this Session
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.loop();
}
}
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? Here are some resources for finding sounds:
<aside> 💡 Are you looking for sounds to play with? Here are some resources for finding sounds:
Dirt Samples which is part of the Super-Dirt project. Dave has bucket on Amazon which you can access here: https://publicbucket2share.s3.amazonaws.com/Dirt-Samples/index.html
Free Music Archive (FMA) offers free access to open licensed, original music by independent artists around the world: https://freemusicarchive.org/genres
Freesound is a web site with free sounds you can use: https://freesound.org/
London Philharmonia Sound Samples: https://philharmonia.co.uk/resources/sound-samples/
Archive.org has a large amount of freely available music and sounds. Note that you want to find “mp3” files, much of their archive is streamable only, but there are many collections that have mp3 files to download
</aside>
let mySound;
mySound=loadSound("anymp3file.mp3");
// Play a sound once
mySound.play()
// Check if a sound is playing (returns true or false)
mySound.isPlaying()
// Play a sound and loop continuously
mySound.loop()
// Check if a sound is looping
mySound.isLooping()
// Stops the sound
mySound.stop()
// Stops the sound but leaves the play at the current point
mySound.pause()
// Adjust the volume (0 to 1)
mySound.amp(0.5);
// Number of seconds within the sound file
mySound.duration()
// Current time within the song
mySound.currentTime()
// Jumps to a part of the sample (in this example, 4 seconds into the sound)
mySound.jump(4)
// Set the playback rate. 1 is default. 2 is twice as fast. 0.5 is half slower
mySound.rate(0.5);
// Call the function myFunction() when the sound ends (end of play, pause or stop)
mySound.onended(myFunction)