Your browser does not support the canvas tag.

Week 6a
sound library thread

The new Sound library *almost* works!
The best way to experiment with sound is to 1. look at the API and 2. look at the examples. It's time we started pulling apart other people's code a little bit! Remember the goal is to learn - when you fins something you don't understand, pull it apart and reasearch until you do. sound library api
Make sure you look at all the commands in soundFile.

One thing that's broken is looping on stereo audio files. Here a way to fix it. Get the duration of the clip, and then use a millis() timer to check to see if the propper amount of time has passed. It's not elegant, and they need to fix it, but it works.

import processing.sound.*;
SoundFile file;
float dur; 
long timer; 
void setup() {
  size(640, 360);
  file = new SoundFile(this, "beep.mp3");;
  dur = file.duration(); 
  timer = millis();

void draw() {
  if (millis() - timer > dur* 1000) { // in draw
    timer = millis();;
Another things that's broken is that on some computers it's not finding the right output channel. But who cares ... wait are you one of those people who likes to "hear" the sound? That's so uncool, but whatever.


Minim lets you add sound a number of ways. In this case lets use it to add a sound effect to our "game". The best way to have a short sound that needs to load quickly is called a "sample" and the way we play it is called "trigger". I believe that minim now works with 24 bit sound (it used to be limited to 8 or 16 bit) - worth checking out.
For some reason Minim buries the API on their site here's a direct link. The only reason people find minim confusing is that the API is a nightmare. Here's a sound that we can use to test, but there are plenty online. I particularly like, although looking stuff up there is a little hit or miss. Make sure you put the sound in your sketch's data folder just like you would a font or image.

This program uses sounds from freesound:
BB hi hat tight.aif by Mrfit ( )
import ddf.minim.*;
Minim minim;
AudioSample pfft;
int circle1x, circle1y; 
int circle1Size = 20; 
int circle2x = 200; 
int circle2y = 170;
int circle2Size = 60; 
boolean hitflag; 

void setup() {
  size(400, 400);
  minim = new Minim(this);
  pfft = minim.loadSample( "hit.aif", 128);
void draw() {
  circle1x = mouseX; 
  circle1y = mouseY; 
  float d = dist(circle2x, circle2y, circle1x, circle1y); // get distance between circles
  if (d < circle2Size/2 + circle1Size/2) { // compare to the combined radiuses
    fill(0, 0, 200);
    if (hitflag == false) {
      hitflag = true;
  else {
    hitflag = false;
  ellipse(circle1x, circle1y, circle1Size, circle1Size);
  ellipse(circle2x, circle2y, circle2Size, circle2Size);

NASA puts sound online!
SoundCloud Downloader


movie link
The video library is there to explore as well! It's similar to the sound library. You can stop start pause, speed up, slow down, play backwards ...
One thing that's different is that video is just images one after another, and images we can put into the pixel array and muck with. This means you can glitch video too!
Movie movie;
boolean flip;
PImage destination; 
void setup() {
  size(640, 360);
  movie = new Movie(this, "");
  destination = createImage(640, 360, RGB);

void draw() {
  if (movie.available() == true) {;
  movie.loadPixels(); //load the frame into an array
  destination.loadPixels(); // load destination into an array
  for (int i = 0; i < movie.width; i++) { // For each pixel in the video frame...
    for (int j = 0; j < movie.height; j++) { // For each pixel in the video frame...
      color currColor = movie.get(i, j); // get current color
      color newcolor = color (green(currColor), blue(currColor), red(currColor));  // swap colors
      if (flip == true) { 
        destination.set(movie.width - i, j, newcolor); // put the new color into destination
      else {
        destination.set(i, j, currColor); // put the old colors into destination
  image(destination, 0, 0); //put destination array on the screen
void mouseReleased() {
  flip = !flip;
Using Collision detection we did last week can you make round buttons that make the video pause, speed up, and reverse direction?