User Tools

Site Tools


courses:intro.prototyping.fall.2011.oct27

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
courses:intro.prototyping.fall.2011.oct27 [2011/10/27 03:05]
130.239.153.147
courses:intro.prototyping.fall.2011.oct27 [2011/10/27 07:17] (current)
130.239.149.116
Line 1: Line 1:
 +=== Sound in Processing ===
  
 +We got introduced to a sound library called Minim and played around with how to play sound effects and full mp3-files. Some more info on the library can be found [[http://​code.compartmental.net/​tools/​minim/​manual-minim/​|here]] ​
 +
 +This is an example of using a shorter sound effect
 +<code java>
 +import ddf.minim.*;​
 + 
 +Minim minim;
 +AudioSample kick;
 + 
 +void setup()
 +{
 +  size(640, 480);
 +  // always start Minim before you do anything with it
 +  minim = new Minim(this);​
 +  // load Balloon.wav from the data folder, with a 512 sample buffer
 +  kick = minim.loadSample("​Balloon.wav"​);​
 +}
 + 
 +void draw()
 +{
 +  //nothing to actually draw
 +}
 + 
 +void keyPressed()
 +{
 +  if ( key == '​k'​ ) 
 +  {
 +    kick.trigger();​
 +  }
 +}
 + 
 +void stop()
 +{
 +  // always close Minim audio classes when you are done with them
 +  kick.close();​
 +  minim.stop();​
 +  //​super.stop();​
 +}
 +</​code>​
 +
 +This is an example of playing an entire mp3
 +<code java>
 +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("​song.mp3"​);​
 +  //​soundtrack.play();​
 +}
 +
 +void draw()
 +{
 +  //nothing to actually draw
 +}
 +
 +void keyPressed()
 +{
 +  if ( key == '​k'​ ) 
 +  {
 +    soundtrack.play();​
 +  }
 +  ​
 +  if ( key == '​o'​)
 +  {
 +    soundtrack.pause();​
 +    soundtrack.rewind();​
 +  }
 +}
 +
 +void stop()
 +{
 +  // always close Minim audio classes when you are done with them
 +  soundtrack.close();​
 +  minim.stop(); ​
 +  //​super.stop();​
 +}
 +  ​
 +</​code>​
 +
 +My example for the game with keyboard input:
 +<code java>
 +int X = 100;
 +int Y = 100;
 +char direction = '​E';​
 +
 +void setup()
 +{
 +  size(640, 480);
 +}
 +
 +void draw()
 +{
 +  background(100);​
 +  ​
 +  //series of if-statements to check if boundaries are hit and if so change background
 +  if(X>​580)
 +  {
 +    background(255,​0,​0);​
 +    X = 580;
 +  }
 +  else if(X < 10)
 +  {
 +    background(255,​0,​0);​
 +    X = 5;
 +  }
 +  else if(Y < 10)
 +  {
 +    background(255,​0,​0);​
 +    Y = 5;
 +  }  ​
 +  else if(Y > 430)
 +  {
 +    background(255,​0,​0);​
 +    Y = 430;
 +  }  ​
 +  ​
 +  //series of if-statements to control direction of movement ​
 +  if(direction == '​E'​)
 +  {
 +    X = X + 5;
 +  }
 +  else if(direction == '​W'​)
 +  {
 +     X = X - 5; 
 +  }
 +  else if(direction == '​N'​)
 +  {
 +    Y = Y - 5;
 +  }
 +  else if(direction == '​S'​)
 +  {
 +    Y = Y + 5;
 +  }
 +  rect(X,​Y,​50,​50); ​
 +  delay(50);
 +
 +}
 +
 +//​keyPressed is run independently from draw, here changing direction by key value
 +void keyPressed()
 +{
 +  if (key == '​d'​)
 +  {
 +    direction = '​E';​
 +  }
 +  else if(key == '​a'​)
 +  {
 +    direction = '​W';​
 +  }
 +  else if(key == '​w'​)
 +  {
 +    direction = '​N';​
 +  }
 +  else if(key == '​s'​)
 +  {
 +    direction = '​S';​
 +  }
 +}
 +
 +</​code>​
courses/intro.prototyping.fall.2011.oct27.txt · Last modified: 2011/10/27 07:17 by 130.239.149.116