Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Processing is not distributed with sound features built in, but you can use sound by installing a library. The Sound libraries need to be downloaded through the Library Manager. go to the "Sketch" menu and select "Add Library..." from the "Import Library..." submenu. 

Use the search feature to find and install the "minim" sound library. 

http://code.compartmental.net/tools/minim/quickstart/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 hereThe p5.SoundFile may not be available immediately because it loads the file information asynchronously.


Code Block
languagejava
titlesound code
linenumberstrue
import ddf.minim.*;

Minim minim;
AudioSample let sound1;
//audio samples are kept in a buffer. They are suitable for shorter sounds
AudioPlayer player; //Audio player loads the sound on the fly. This is suitable for background music or longer audio clips

void setuplet sound2;

function preload() {
  sizesoundFormats(600'mp3', 300'ogg');
  // sound init
  minim = new Minim(thissound1 = loadSound('doorbell.mp3');
 	sound2 sound1 = minim.loadSample("soundFile1loadSound('cowbell.mp3"');
}

player = minim.loadFile("soundFile2.mp3");function setup() {
  createCanvas(600, 300);

}

voidfunction draw() {
  //circle 1
  background(100);
  noStroke();
 	fill fill(255);
	ellipse(400, height/2, 40, 40);
	fill (125);
	ellipse(200, height/2, 40, 40);
	
}

function mousePressed() {
  //circle 21
  if (player.isPlaying(overCircle(200, height/2, 40)) {
    fillprint(255, 0, 0"circle 1");
		doorBell();
  }
else  {//circle 2
  if fill(255);
  }(overCircle(400, height/2, 40)) {
    ellipseprint(400,"circle height/2, 40, 40);
}; 

void mousePressed(2");
    cowBell();
  }
}

function overCircle( x, y,  diameter) {
  //check if mouse is over the circle 1
  if (overCircle(200dist(x, y, height/2mouseX, 40mouseY) <= diameter) {
		 return true;
  } else {
    return  println("circle 1");
false;
  }
}

function doorBell() {
  //toggle sound on and off
  if (sound1.triggerisPlaying() );
  {
 fill(255, 0, 0   sound1.stop();
		
  } ellipse(200, height/2, 40, 40);else {
      fill(255sound1.play();
		sound2.stop();
  }
}

function cowBell() {
  //circle 2toggle sound on and off
  if (overCircle(400, height/2, 40))sound2.isPlaying() )
  {
		
   println("circle 2");
 sound2.stop();
  } else {
   jukeBox 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 ( playersound1.isPlaying() )
  {
    playersound1.pause();
  } else  {
    playersound1.play();
  }
  // if the player reaches the end of the file,   // it must be rewound to play the startfile again
before being replayed.
  if ( player.position() == player.lengthsound1.onEnded() )
  {
    player.rewind();
    player.sound1.play();
  } 
}

Download the source files: 


sound.zipLink to online demo