euphoriafish
YaBB Newbies
Offline
Posts: 9
Re: MINIM and my state machine
Reply #2 - Oct 26th , 2007, 3:00am
Thank you so much for responding, ddf. I am definitely confused about when and where to close my audio objects, as well as what the difference is between object.close() and super.stop(). Here are the parts of my code having to do with sound and the state machine: void setup() { //multiple states using Minim Minim.start(this); //start Minim before doing anything with it //start menu setup theme = Minim.loadFile("wowiezowie.mp3",512); //store the mp3 in an AudioPlayer object //demo setup song = Minim.loadFile("joke.wav"); //performance mode setup kick=Minim.loadSample("kick.wav",512); hat=Minim.loadSample("hat.wav",512); ride=Minim.loadSample("ride.wav",512); snare=Minim.loadSample("snare.wav",512); tom=Minim.loadSample("tom.wav",512); /* tom2=Minim.loadSample("tomtest.wav",512); tom3=Minim.loadSample("tomtest2.wav",512); tom4=Minim.loadSample("tom4.wav",512);*/ } void draw() { background(0); if (keyPressed) { if(key=='1') { //state= "State_DEMO"; test=1; } else if(key=='2') { //state= "State_PERFORM"; test=2; } else if(key=='3') { //state="State_CHECK_INSTRUCT"; test=3; } else if(key=='4') { //state="State_AUDIENCE"; test=4; } else if(key=='5') { //state="State_CONTINUE"; test=5; } } switch ( test ) { //case "State_DEMO" : case 1: // Process for test = 1 theme.close(); song.play(); beat.setSensitivity(300); kickSize = snareSize = hatSize = 16; // taken from beat detect example beat.detect(song.mix); fill(255); if ( beat.isKick() ) kickSize = 32; if ( beat.isSnare() ) snareSize = 32; if ( beat.isHat() ) hatSize = 32; textSize(kickSize); text("KICK", width/4, height/2); textSize(snareSize); text("SNARE", width/2, height/2); textSize(hatSize); text("HAT", 3*width/4, height/2); kickSize = constrain(kickSize * 0.95, 16, 32); snareSize = constrain(snareSize * 0.95, 16, 32); hatSize = constrain(hatSize * 0.95, 16, 32); break; //case "State_PERFORM" : case 2: theme.close(); //stop the title song rhythms = loadStrings("rhythms.txt"); if(keyPressed) { if(key=='k') { kick.trigger(); delay(150); } if(key=='h') { hat.trigger(); delay(150); } if(key=='r') { ride.trigger(); delay(150); } if(key=='s') { snare.trigger(); delay(150); } if(key=='t') { tom.trigger(); delay(150); } //if(key=='u')snare.trigger(); //if(key=='i')tom2.trigger(); //if(key=='o')tom2.trigger(); //don't need these last two really and they were crashing i think? } // super.stop(); how to stop the sound within a state? break; //case "State_CHECK_INSTRUCT": case 3: text("in data check mode--not yet implemented", width/3,(height/4)); text("developer transitions", width/3,(height/4)+200); break; // case "State_AUDIENCE" : case 4: text("in audience mode--not yet implemented", width/3,(height/4)); text("developer transitions", width/3,(height/4)+200); break; // case "State_CONTINUE" : case 5: // Process for test = 5 text("Perform again? Press 1 for Yes, 6 for No", width/3,(height/4)); if(keyPressed) { if(key=='1') { test=1; } else if (key=='6') { test=6; } else { test=5; } } break; default : // Process for all other cases is to go to the title state. background(0); image(title,(width/4)-130,height/4); //if I load the sound file inside the state, the audio is distorted and I get an error message about java heap space in the thread. do i need a new object every time i enter the state, and how do i do that correctly? // theme = Minim.loadFile("wowiezowie.mp3",512); //store the mp3 in an AudioPlayer object theme.loop(); textFont(font,30);//declare font and font size text("Press 1 to start", width/3,(height/4)+200+a); a=(a+1); if(a>100) { a=0; } if(keyPressed) { if(key=='1') { test=1; } else test=6; } break; } } void stop() { //from title state theme.close(); super.stop(); //from demo state song.close(); //from performance state //always close Minim audio classes when you are done with them kick.close(); hat.close(); snare.close(); tom.close(); tom2.close(); tom3.close(); tom4.close(); super.stop(); }