Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
/
p5.js Sound
Updated Sep 23, 2021

p5.js Sound

Analytics

Loading data...

p5.sound extends p5 with Web Audio functionality including audio input, playback, analysis and synthesis. In order to work with audio you need to download a javascript library here. The p5.SoundFile may not be available immediately because it loads the file information asynchronously.
Link to online demo

let sound1; let sound2; function preload() { soundFormats('mp3', 'ogg'); sound1 = loadSound('doorbell.mp3'); sound2 = loadSound('cowbell.mp3'); } function setup() { createCanvas(600, 300); } function draw() { background(100); noStroke(); fill (255); ellipse(400, height/2, 40, 40); fill (125); ellipse(200, height/2, 40, 40); } function mousePressed() { //circle 1 if (overCircle(200, height/2, 40)) { print("circle 1"); doorBell(); } //circle 2 if (overCircle(400, height/2, 40)) { print("circle 2"); cowBell(); } } function overCircle( x, y, diameter) { //check if mouse is over the circle if (dist(x, y, mouseX, mouseY) <= diameter) { return true; } else { return false; } } function doorBell() { //toggle sound on and off if (sound1.isPlaying() ) { sound1.stop(); } else { sound1.play(); sound2.stop(); } } function cowBell() { //toggle sound on and off if (sound2.isPlaying() ) { sound2.stop(); } else { sound2.play(); sound1.stop(); } } boolean overCircle(int x, int y, int diameter) { //check if mouse is over the circle if (dist(x, y, mouseX, mouseY) <= diameter) { return true; } else { return false; } } void jukeBox() { //toggle sound on and off if ( sound1.isPlaying() ) { sound1.pause(); } else { sound1.play(); } // if the player reaches the end of the file, play the file again if ( sound1.onEnded() ) { sound1.play(); } }

 

For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
    • p5.js Introduction
    • p5.js Variables
    • p5.js Random Numbers
    • p5.js SVG + Images
    • p5.js WebGL
    • p5.js Classes and Objects
    • p5.js Events and Functions
    • p5.js Loops
    • p5.js Coordinates
    • P5js Nested Loops
    • p5.js Animation Exercise 1
    • p5.js Animation Exercise 2
    • p5.js Conditionals
    • p5.js Arrays and Lists
    • p5.js Simple Collision Detection
    • p5.js Reactors
    • p5.js Tiling and Repetition
    • p5.js Vectors
    • p5.js Animation Solution with Objects
    • p5.js Easing
    • p5.js Perlin Noise
    • p5.js Particle System
    • p5.js Sound
    • p5j.s Typography
    • P5js Archive
  • Programming in Processing (java)

{"serverDuration": 57, "requestCorrelationId": "ad7df5220c4b42ada80a2189505bd08b"}