Script:Synch Sound

From Second Life Wiki
Jump to navigation Jump to search

<lsl> /*

   Every PULSE seconds, ACCORDING TO THE CLOCK,
   this script plays the first sound in the object.
   
   So if you have a dozen things out with the same PULSE value
   they will all play at approximately the same time.
  • /

integer PULSE = 60000; // 60 seconds;

integer time() { // count milliseconds since the day began

   string stamp = llGetTimestamp(); // "YYYY-MM-DDThh:mm:ss.ff..fZ"
   return (integer) llGetSubString(stamp, 11, 12) * 3600000 + // hh
          (integer) llGetSubString(stamp, 14, 15) * 60000 +  // mm
          llRound((float)llGetSubString(stamp, 17, -2) * 1000000.0)/1000; // ss.ff..f

}

integer howclose; string mySound;

default {

   state_entry()
   {
       howclose = -1;
       llSetTimerEvent( 0.1 );
       mySound = llGetInventoryName( INVENTORY_SOUND, 0 );
   }
   timer()
   {
       integer current = time() % PULSE;
       if ( howclose < 0 ) howclose = current;
       
       if ( current < howclose ) // bing!
       {
           llTriggerSound( mySound, 1.0 );
           llSetTimerEvent( 10.0 );
       }
       else if ( current > PULSE - 1000 ) // within a second
       {
           llSetTimerEvent( 0.05 ); // 0.05 is the min delay possible.
       }
       else if ( current > PULSE - 10000 ) // within 10 seconds
       {
           llSetTimerEvent( 1.0 );
       }
       else // far away
       {
           llSetTimerEvent( 10.0 );
       }
       howclose = current;
   }
   
   on_rez( integer p )
   {
       llResetScript();
   }

} </lsl>