This shows you the differences between two versions of the page.
courses:intro.prototyping.fall.2016.oct07 [2016/10/10 01:09] rickard created |
courses:intro.prototyping.fall.2016.oct07 [2016/10/10 01:09] (current) rickard created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===Sound in Processing === | ||
+ | |||
+ | Today we worked with a library for sound called Minim. | ||
+ | [[http:// | ||
+ | |||
+ | == Free sound for downloading== | ||
+ | |||
+ | http:// | ||
+ | |||
+ | http:// | ||
+ | |||
+ | There are also a whole heap of sounds on the server under the " | ||
+ | |||
+ | We moved on to a brief look into object oriented programming and on to play with sound using the Minim [[http:// | ||
+ | |||
+ | |||
+ | 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(" | ||
+ | gun = minim.loadSample(" | ||
+ | | ||
+ | } | ||
+ | |||
+ | void draw() | ||
+ | { | ||
+ | //nothing to actually draw | ||
+ | } | ||
+ | |||
+ | void keyPressed() | ||
+ | { | ||
+ | if ( key == ' | ||
+ | { | ||
+ | money.trigger(); | ||
+ | } | ||
+ | | ||
+ | if ( key == ' | ||
+ | { | ||
+ | gun.trigger(); | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | void stop() | ||
+ | { | ||
+ | // always close Minim audio classes when you are done with them | ||
+ | money.close(); | ||
+ | gun.close(); | ||
+ | minim.stop(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | == 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(" | ||
+ | } | ||
+ | |||
+ | void draw() | ||
+ | { | ||
+ | //nothing to actually draw | ||
+ | } | ||
+ | |||
+ | void keyPressed() | ||
+ | { | ||
+ | if ( key == ' | ||
+ | { | ||
+ | if(soundtrack.isPlaying()) //checking if the song is playing to choose play/pause | ||
+ | | ||
+ | else | ||
+ | | ||
+ | } | ||
+ | | ||
+ | if ( key == ' | ||
+ | { | ||
+ | soundtrack.skip(3000); | ||
+ | } | ||
+ | | ||
+ | if( key == ' | ||
+ | { | ||
+ | soundtrack.rewind(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void stop() | ||
+ | { | ||
+ | // always close Minim audio classes when you are done with them | ||
+ | soundtrack.close(); | ||
+ | minim.stop(); | ||
+ | } | ||
+ | | ||
+ | </ | ||
+ | |||
+ | == States/ | ||
+ | Some of you got caught in the situation where you wanted things to trigger on things with keyPressed, for example, but only got a very quick reaction and then back to the original behavior. That is because the trigger concepts we've worked with are just a brief interaction. If you want to make lasting changes it's better to use what is called a "state machine" | ||
+ | |||
+ | <code java> | ||
+ | int mode; | ||
+ | |||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | size(640, | ||
+ | mode=0; | ||
+ | } | ||
+ | |||
+ | void draw() | ||
+ | { | ||
+ | if(mode==0) | ||
+ | { | ||
+ | background(128, | ||
+ | } | ||
+ | | ||
+ | if(mode==1) | ||
+ | { | ||
+ | background(128, | ||
+ | } | ||
+ | | ||
+ | if(mode==2) | ||
+ | { | ||
+ | background(255, | ||
+ | } | ||
+ | | ||
+ | if(mode==3) | ||
+ | { | ||
+ | background(255, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void keyPressed() | ||
+ | { | ||
+ | | ||
+ | { | ||
+ | | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | == Concepts we worked with == | ||
+ | <code java> | ||
+ | |||
+ | audioPlayer | ||
+ | play() | ||
+ | pause() | ||
+ | rewind() | ||
+ | isPlaying() | ||
+ | position() | ||
+ | skip() | ||
+ | cue() | ||
+ | |||
+ | |||
+ | audioSample | ||
+ | trigger() | ||
+ | length() | ||
+ | stop() | ||
+ | </ | ||