We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSound,  Music Libraries › MINIM and my state machine
Page Index Toggle Pages: 1
MINIM and my state machine (Read 1625 times)
MINIM and my state machine
Oct 24th, 2007, 5:09pm
 
I am using the minim library for sound in an arcade music game I am making.  I have a switch to change states and go to different parts of the game, and each state has various sound needs.  For example, the default case in my switch contains code for displaying the game's title screen and looping a piece of music.

My problem is that when I test each state, the music or triggered sound samples play once but if I return to the same state later the sound will not play a second time.  I have been getting an error saying "Minim IOerror: stream closed" or something like that.  The error message does not come up every time but the sound always cuts out after I leave the state it was played in.

Thank you in advance for your help!
Re: MINIM and my state machine
Reply #1 - Oct 25th, 2007, 4:09am
 
Seeing the code in question would be extremely helpful. Although it sounds like you are closing all your audio objects when you leave a state and then expecting to be able to use them again when you come back to that state. It won't even do to try to open them after closing them, it's not possible, you'd need to get a new object from Minim with loadFile or whatever you are using.
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();
}


Re: MINIM and my state machine
Reply #3 - Oct 27th, 2007, 6:59pm
 
Ok, so basically, since you want to be able to start and stop playback of theme and song, instead of calling stop() when you want them to pause playback, call pause(). You can then either resume playback from where you paused them by calling play() again or you can call rewind() and then play() to start them playing from the beginning. If you want them to loop, then call loop() instead of play().  If you call close() it closes the data stream that is reading audio from the file and, as you have done, in order to play the file again you have to reload it with Minim.loadFile. But if you do this a lot you may run into the heap space problem you mention. Basically, unless you don't plan on using a sound again, or at least for a long while, you should only call close() from with in the sketches stop() method as you've defined it. The only problem with your stop() method is that you call super.stop() twice. You should only call it once at the very end of the method because super.stop() calls Processing's stop method and that cleans up and closes the sketch.

Also, if you are going to use states in your program, you should read up on the State pattern (http://en.wikipedia.org/wiki/State_pattern), instead of using switch statements.
Re: MINIM and my state machine
Reply #4 - Oct 28th, 2007, 1:32am
 
Oh wow, I totally forgot there was a pause() function.  I feel silly about that one.

Ok, so close() is for after I am completely done with a sound object and super.stop() is for when I am completely done with the library and everything because it tells Processing to close up the sketch?  I think I understand.

Thank you very much for taking the time to look at my messy code.  And thanks also for the State Patterns article.  I had been Googling "state machine" and never would have found that.
Page Index Toggle Pages: 1