User Tools

Site Tools


courses:intro.prototyping.fall.2013.oct1

Audio and wrap up

The homework is to make something new with sound, and I'd love it if you could combine it with images or 2D-shapes. I'll give you until the next time we meet as entire class. So NOT next time, when we do the lecture to get access to the workshop.

I started this weeks class with some boring but valuable tips about planning any larger programming ideas in english before starting to put it into code. While exploring ideas of course is a whole other matter. Expanding on ideas step by step is also fun.

Which brings me to the other valuable tip. Saving versions. If you have something working that you like, save it, and continue on a copy in case you slip up.

Which brings me to println(). A valuable function to track how your program is working. It puts text in the black window at the bottom of the processing programming window, and you can use it either to see what value a variable has, or check is you're reaching that part of an if-statement that you just put in the code. All depending on where you place it, or have it print.

Webcam

Last time was all about loading images and manipulating them. I wanted to finish that subject by showing how easy it is to connect to a webcam in Processing, and once you have that going, each frame can be handled as a picture. Which means that you can do filters and graphics with the video feed.

import processing.video.*;
 
Capture cam;
 
void setup() {
  size(640, 480);
 
  String[] cameras = Capture.list();
 
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }
 
    // The camera can be initialized directly using an 
    // element from the array returned by list():
    cam = new Capture(this, cameras[1]);
    cam.start();     
  }      
 
}
 
void draw() {
  if (cam.available() == true) {
    cam.read();
  }
  image(cam, 0, 0);
}
Sound

http://www.mediacollege.com/downloads/sound-effects/

http://www.freesoundeffects.com/

We moved on to a brief look into object oriented programming and on to play with sound using the Minim library. As Madya pointed out, there is more information on the same website under “tools”, and the full documentation is here.

Two of the classes we played around with was audioSample and audioPlayer.

Audiosample
import ddf.minim.*;
 
Minim minim;
AudioSample money, gun;
 
void setup()
{
  size(640, 480);
  // always start Minim before you do anything with it
  minim = new Minim(this);
  // load soundfile from the data folder
  money = minim.loadSample("CASH2.WAV");
  gun = minim.loadSample("SHOTGUN-X3.WAV");
 
}
 
void draw()
{
  //nothing to actually draw
}
 
void keyPressed()
{  
  if ( key == 'y' ) 
  {
    money.trigger();
  }
 
  if ( key == 'p' ) 
  {
    gun.trigger();
  }
}
 
void stop()
{
  // always close Minim audio classes when you are done with them
  money.close();
  gun.close();
  minim.stop();
}
AudioPlayer
import ddf.minim.*;
 
Minim minim;
AudioPlayer soundtrack;
 
void setup()
{
  size(640, 480);
  // always start Minim before you do anything with it
  minim = new Minim(this);
  //load a file, give the AudioPlayer buffers that are 2048 samples long
  soundtrack = minim.loadFile("song2.mp3");
}
 
void draw()
{
  //nothing to actually draw
}
 
void keyPressed()
{
  if ( key == 'q' ) 
  {
    if(soundtrack.isPlaying()) //checking if the song is playing to choose play/pause
     soundtrack.pause();
    else
     soundtrack.play();
  }
 
  if ( key == 'w')
  {
    soundtrack.skip(3000); //skipping three seconds
  }
 
  if( key == 'e')
  {
    soundtrack.rewind(); //well...yeah, guess. :-)
  }
}
 
void stop()
{
  // always close Minim audio classes when you are done with them
  soundtrack.close();
  minim.stop(); 
}
 
Concepts we worked with
println()
audioPlayer
play()
pause()
rewind()
isPlaying()
position()
skip()
audioSample
trigger()
courses/intro.prototyping.fall.2013.oct1.txt · Last modified: 2013/10/03 01:54 by rickard