User Tools

Site Tools


courses:2009.10.29.intro_prototyping

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

courses:2009.10.29.intro_prototyping [2009/10/29 23:45] (current)
213.21.77.115 created
Line 1: Line 1:
 +====== oct 29: Intro Processing: text, key, mouse and audio ======
 +
 +
 +==== Text ====
 +
 +  * declare a [[http://​processing.org/​reference/​PFont.html|PFont]] variable
 +  * in setup, [[http://​processing.org/​reference/​loadFont_.html|loadFont]]
 +  * in draw, [[http://​processing.org/​reference/​textFont_.html|textFont]],​ [[http://​processing.org/​reference/​textAlign_.html|textAlign]],​ [[http://​processing.org/​reference/​text_.html|text]]
 +  * use [[http://​processing.org/​reference/​saveFrame_.html|saveFrame]] to save a rendered frame to your sketch folder, use carefully!
 +
 +File built during the class: {{:​courses:​font_fun.zip|}}
 +
 +<​code>​
 +PFont font;
 +PFont bigFont;
 +String myText = "type or click";​
 +
 +void setup(){
 +  size(800, 600);
 +  // load the font
 +  font = loadFont("​ArialMT-18.vlw"​); ​
 +  bigFont = loadFont("​BookAntiqua-Italic-70.vlw"​);​
 +  frameRate(10);​
 +}
 +
 +void draw(){
 +
 +  background(0);​
 +
 +  fill(random(255),​ random(255),​ random(255));​
 +  textFont(bigFont);​
 +  textAlign(LEFT); ​
 +  text("​x:"​ + mouseX, 15, 60);
 +  text("​y:"​ + mouseY, 15, 120);
 +
 +  text(myText,​ 15, 200);
 +  ​
 +  //​saveFrame("​mytestmovie-####​.jpg"​);​
 +}
 +
 +void mousePressed(){
 +  myText = "​clik";​
 +}
 +
 +void mouseReleased(){
 +  ​
 +  myText = ""; ​
 +}
 +
 +void keyPressed() {
 +  ​
 +  if(keyCode == UP){
 +    ​
 +    saveFrame("​mytestmovie-####​.jpg"​);​
 +    ​
 +  }else if(keyCode == DOWN){
 +
 +    exit();
 +    ​
 +  }else{
 +    ​
 +    myText = myText + str(key);
 +  } 
 +}
 +</​code>​
 +
 +
 +==== Audio ====
 +
 +  * using the [[http://​code.compartmental.net/​tools/​minim/​|Minim library]], now included in the Processing app
 +  * load and play different MP3s on key presses
 +  * remember to put the audio files in the //data// folder
 +
 +File built during the class: {{:​courses:​audio_player_fun.zip|}}
 +<​code>​
 +import ddf.minim.*;​
 +
 +AudioPlayer player;
 +Minim minim;
 +
 +void setup()
 +{
 +  size(512, 200, P2D);
 +
 +  minim = new Minim(this);​
 +
 +  // load a file, give the AudioPlayer buffers that are 1024 samples long
 +  // player = minim.loadFile("​groove.mp3"​);​
 +
 +  // load a file, give the AudioPlayer buffers that are 2048 samples long
 +  player = minim.loadFile("​groove.mp3",​ 2048);
 +  // play the file
 +  // player.pause();​
 +}
 +
 +void draw()
 +{
 +  background(0);​
 +  stroke(255);​
 +  // draw the waveforms
 +  // the values returned by left.get() and right.get() will be between -1 and 1,
 +  // so we need to scale them up to see the waveform
 +  // note that if the file is MONO, left.get() and right.get() will return the same value
 +  for(int i = 0; i < player.left.size()-1;​ i++)
 +  {
 +    line(i, 50 + player.left.get(i)*50,​ i+1, 50 + player.left.get(i+1)*50);​
 +    line(i, 150 + player.right.get(i)*50,​ i+1, 150 + player.right.get(i+1)*50);​
 +  }
 +}
 +
 +
 +void keyPressed(){
 +
 +  // load a new audio sample
 +  if(key == '​1'​){
 +    println("​Loading Audio 1");
 +    player.pause();​
 +    player = minim.loadFile("​1.mp3",​ 2048);
 +    player.play();​
 +
 +    // load a new audio sample
 +  }
 +  else if(key == '​2'​){
 +    println("​Loading Audio 2");
 +    player.pause();​
 +    player = minim.loadFile("​2.mp3",​ 2048);
 +    player.play();​
 +
 +  } 
 +
 +}
 +
 +void mousePressed(){
 +
 +  // if the audio is playing, we stop it
 +  if(player.isPlaying()){
 +    println("​Pausing Audio"​);​
 +    player.pause();​
 +  }
 +
 +  // if the audio is not playing, we start it
 +  else{
 +    println("​Playing Audio"​);​
 +    player.play(); ​
 +  }
 +
 +}
 +
 +void stop()
 +{
 +  // always close Minim audio classes when you are done with them
 +  player.close();​
 +  minim.stop();​
 +
 +  super.stop();​
 +}
 +</​code>​
  
courses/2009.10.29.intro_prototyping.txt · Last modified: 2009/10/29 23:45 by 213.21.77.115