User Tools

Site Tools


courses:intro.prototyping.fall.2014.oct20th

Differences

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

Link to this comparison view

courses:intro.prototyping.fall.2014.oct20th [2014/10/17 05:19]
rickard created
courses:intro.prototyping.fall.2014.oct20th [2014/10/17 05:19] (current)
rickard created
Line 1: Line 1:
 +=== Audio session ===
  
 +== General tips and tricks == 
 +
 +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 the safest way to learn and get what you want. 
 +
 +Comments, when your code is starting to grow or looks weird it's time to put in comments to remind you or a friend what goes on.
 +
 +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. That way you always have partly working code to go back to.
 +
 +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 what you have it print.
 +
 +== For-loops: == 
 +For those times when you want stuff happen repeatedly and it would be just crazy to write one line per time, you use a for-loop.
 +
 +<code java>
 +     ​for(int i=0;​i<​5;​i++)
 +     {
 +        rect(60,​60,​50,​50); ​
 +     }
 +</​code> ​
 +
 +
 +
 +
 +== 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 [[http://​code.compartmental.net/​minim/​|library]]. The full documentation is [[http://​code.compartmental.net/​minim/​javadoc/​|here]].
 +
 +
 +Two of the classes we played around with was audioSample and audioPlayer.
 +
 +== Audiosample - short samples ==
 +<code java>
 +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();​
 +}
 +</​code>​
 +
 +
 +
 +== AudioPlayer - longer audio files ==
 +
 +<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, 
 +  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(); ​
 +}
 +  ​
 +</​code>​
 +
 +== AudioOutput - sound generation ==
 +
 +<code java>
 +import ddf.minim.*;​
 +import ddf.minim.ugens.*;​
 +
 +Minim minim;
 +AudioOutput out;
 +
 +void setup()
 +{
 +  size(512, 200);
 +  ​
 +  minim = new Minim(this);​
 +  ​
 +  // use the getLineOut method of the Minim object to get an AudioOutput object
 +  out = minim.getLineOut();​
 +  ​
 +  // set the tempo of the sequencer
 +  // this makes the first argument of playNote ​
 +  // specify the start time in quarter notes
 +  // and the duration becomes relative to the length of a quarter note
 +  // by default the tempo is 60 BPM (beats per minute).
 +  // at 60 BPM both start time and duration can be interpreted as seconds.
 +  // to retrieve the current tempo, use getTempo().
 +  out.setTempo( 80 );
 +  ​
 +  // pause the sequencer so our note play back will be rock solid
 +  // if you don't do this, then tiny bits of error can occur since 
 +  // the sequencer is running in parallel with you note queueing.
 +  out.pauseNotes();​
 +  ​
 +  // given start time, duration, and frequency
 +  out.playNote( 0.0, 0.9, 97.99 );
 +  out.playNote( 1.0, 0.9, 123.47 );
 +  ​
 +  // given start time, duration, and note name  ​
 +  out.playNote( 2.0, 2.9, "​C3"​ );
 +  out.playNote( 3.0, 1.9, "​E3"​ );
 +  out.playNote( 4.0, 0.9, "​G3"​ );
 +    ​
 +  // given start time and note name or frequency
 +  // (duration defaults to 1.0)
 +  out.playNote( 5.0, ""​ );
 +  out.playNote( 6.0, 329.63);
 +  out.playNote( 7.0, "​G4"​ );
 +  ​
 +  // the note offset is simply added into the start time of 
 +  // every subsequenct call to playNote. It's expressed in beats.
 +  // to get the current note offset, use getNoteOffset().
 +  out.setNoteOffset( 8.1 );
 +  ​
 +  // because only given a note name or frequency
 +  // starttime defaults to 0.0 and duration defaults to 1.0
 +  out.playNote( "​G5"​ );
 +  out.playNote( 987.77 );
 +  ​
 +  // now we can start the sequencer again to hear our sequence
 +  out.resumeNotes();​
 +}
 +
 +void draw()
 +{
 +  background(0);​
 +  stroke(255);​
 +  ​
 +  // draw the waveforms
 +  for(int i = 0; i < out.bufferSize() - 1; i++)
 +  {
 +    line( i, 50 + out.left.get(i)*50,​ i+1, 50 + out.left.get(i+1)*50 );
 +    line( i, 150 + out.right.get(i)*50,​ i+1, 150 + out.right.get(i+1)*50 );
 +  }
 +}
 +</​code>​
 +
 +
 +== Concepts we worked with ==
 +<code java>
 +println()
 +for
 +audioPlayer
 +play()
 +pause()
 +rewind()
 +isPlaying()
 +position()
 +skip()
 +audioSample
 +trigger()
 +AudioOutput
 +</​code>​
courses/intro.prototyping.fall.2014.oct20th.txt · Last modified: 2014/10/17 05:19 by rickard