Sunday, March 3, 2013

Breathing Duration

As part of the ~isThereaBreath routine, I later incorporated code to determine the length of each breathing component (inspiration/expiration/hold/pause).  Rather than post all of that below, I'll post simply the code that checks for the duration of one specific element: an inspiration.



/*
SECTION 2: HOW LONG DO THESE DIFFERENT BREATH ELEMENTS LAST?
In this section you can find out:
4. The time that each breath component began and ended
5. The duration of each breath component as it is completed
6. The time since the last breath (to get a sense for the frequency of breaths)
7. The overall breath cycle - two corresponding arrays that together list the duration and breath component of each completed breath cycle
*/


//-------------------Poll start time for inspiration---------------------------//

//this routine polls thisThread to find a current time
~inspirationBegins = Routine({ 
     (
     ~postMessage = 1;
     ~curTime1 = thisThread.seconds - ~pieceBegins;
     "^^^^^^^^^^^^^^^^^^^^Inhalation began at ".post;
     ~curTime1.post;
     "^^^^^^^^^^^^^^^^^^^^".postln).yield;
      });



//-----------------------find the end time and total duration of inspiration----------------------------------//

  //-------------------inhalation routine---------------------------//
  //This routine is called only after the corresponding routine from above is called.  
  //It is called when the sensors indicate that this part of the breath component (in this case, the inhalation) has ended. 

~inhalationLength = Routine({ 
        (
        ~postMessage = 5;//inform the GUI the Inhalation has ended
        ~newcurTime = thisThread.seconds - ~pieceBegins; //get a new current time
        ~totalInhalationTime = ~newcurTime - ~curTime1; //figure out how long the inhalation lasted 

         "@@@@@@@@@@@@@ inhalation ended at ".post; 
          ~newcurTime.post;
          "@@@@@@@@@@@@@".postln; //post the time that the inhalation ended

          "total length of inhalation = ".post;
          ~totalInhalationTime.postln; //post the duration of the inhalation

//The purpose of this code is to store duration values in an array so I have a sense of an overall
//breath level

if(~totalInhalationTime != 0, { //sometimes the durations are basically nothing...assuming dur !=0
      ~durationsList.add(~totalInhalationTime); //add the total duration time to the duration list
      // "durationsList is ".post;
      //  ~durationsList.postln; //post the list
      ~correspondingComponentsList.add(1); // add a 1 to the corresponding components list
      // "correspondingComponentsList is ".post;
      // ~correspondingComponentsList.postln; //and post the corresponding components list     
       ~newComponent = 1;   
      //NOTE: it doesn't tell you what the new component is UNTIL THE BREATH IS OVER   
                              });
                               
).yield;
                        });







//------------------------------Look for beginning and ending of new inspiration---------------------------//
/*
The routines listed below control the routines listed above.  They basically check for the beginning of a new breath element (an inspiration, expiration, hold or pause.  After sensing a new breath element, they call the routine that polls the start time, turn off any other routines for other breath components that happen to be currently running, and then wait for the current breath component to end.
*/ 

if((~breathArray[9].notNil)
    .and(~breathArray[8]).notNil
    .and(~breathArray[7].notNil), { //check to make sure values have come in to avoid error messages

//-------------------------------BEGINNING OF INSPIRATION-----------------------//
if((~breathArray[9] == 1)
    .and(~breathArray[8] <= 0), { //when we see the beginning of a new inhalation
        ~inspirationBegins.play; //note when it begins. (See routine above.)
          t.play; //send message to routine to start checking for when the breath ends. (See routine below.)
    });

  //this routine (t) tells routine 'a' to start.
  //It needs to be this way, instead of trying to directly start 'a' 
  //...because otherwise it creates many instances of a and a becomes unstoppable
  t = Routine({
      w = a.play;
                        }); 
                       
 
//-------------------------------ENDING OF INSPIRATION-----------------------//                      

//starts routine to check inspiration length, and (supposedly*) stops itself when the inhalation is over. 
//It then notes when it ends and post how long it lasted
//*for some reason some of these were not stopping themselves...so I added additional backup code so that when one of these starts
//it turns off the other routines if they happen to be on

a = Routine({ 
      inf.do({
            if((~breathArray[9] <= 0)
                 .and(~breathArray[8] == 1), { //when we come to the end of a new inhalation 
                         ~inhalationLength.play; //find the inhalation length 
                         w.stop; //stop checking for the end of the inhalation because it happened already.
                                           
                                        }, {
                                         
                                         
 //sometimes the routines aren't stopping themselves...the code below reminds other routines to stop 
                                        //turn off any other routines that happen to still be on
                                          if(~exhalationRoutineD.notNil, {
                                              if(~exhalationRoutineD == 1, {//if expiration is running
                                                  ~exhalationLength.play; //find the exhalation length 
                                                    c.stop;//and stop the exhalation routine
                                                     ~exhalationRoutineD = 0;
                                              });
                                          });
                                             
                                             
                                              if(~heldRoutineE.notNil, {
                                                  if(~heldRoutineE == 1, {//if held breath is running
                                                      ~holdBreath.play; //find the held breath length 
                                                        f.stop;//and stop the held breath routine
                                                       ~heldRoutineE = 0;
                                                 });
                                              });
                                             
                                              if(~betweenBreathsH.notNil, {
                                                  if(~betweenBreathsH == 1, {//if between breaths was running
                                                      ~betweenBreaths.play; //find between breaths length
                                                        j.stop;//and stop the between breaths routine
                                                       ~betweenBreathsH = 0;
                                                  });
                                              });
                                             
 "WWWWWWWWWWWWWWWWW waiting for the INHALATION to be over WWWWWWWWWWWWWWWWWWWW".postln; 

                                        ~breathRoutineA = 1;
                                        ~postMessage = 9;//tell GUI waiting for inhalation to be over
                                         
                                          });
                                          0.1.wait; //avoid crash
                                              });
                                        });

No comments:

Post a Comment