Looping Two Sounds

From Second Life Wiki
Jump to: navigation, search

Created by Kira Komarov and User:FailedNoob Resident based on a script by lovelustdesire Resident.

Shortnote

This script loops two sounds in the same primitive. It is an example of what the Using llSensorRepeat as a timer. FUSS-code is about on the Wizardry and Steamworks page.

The way this is designed is that in the playing state, it uses llSensorRepeat as a second timer in order to preload both sounds alternately. The problem with llPreloadSound is that it can only preload the sounds for agents that are present in the area. That is why we need a second timer: with one timer we play the sound clips and with the other we continuously preload them.

Additionally, we use: <lsl> o = ~o </lsl> to switch over an if-clause which is also mentioned on the Wizardry and Steamworks main page.

Testing

Drop two sounds, named P_1 and P_2 in a primitive and paste the code below inside the same primitive. Touch the primitive to start the initial buffering and after 20 seconds the sound clips should play continuously.

Code

<lsl> ////////////////////////////////////////////////////////// // [K] Kira Komarov - FailedNoob Resident and based on // // some code originally made by lovelustdesire Resident // // of the group: College of Scripting Music Science // //////////////////////////////////////////////////////////

//1st soundfile length = 9.820 //2nd soundfile length = 9.658

integer running = FALSE; float duration = 19.478 ; // duration of both files down to the tenth of a second if needed

integer o1 = -1; integer o2 = -1;

default {

   state_entry() {
       llSetSoundQueueing(TRUE);
   }
   
   touch_start(integer detected) {
       running = FALSE;
       llPreloadSound("P_1");
       llPreloadSound("P_2");
       llSetTimerEvent(1);
       llWhisper(0, "Starting...");
   }
   timer() {
       if(!running) {
           llSetTimerEvent(duration);
           running = TRUE;
           return;
       }
       llSetTimerEvent(0);
       state playing;
   }

}

state playing {

   state_entry() {
       llSay(0, "Playing...");
       llSetTimerEvent(1);
       llSensorRepeat("", NULL_KEY, 1, 0.1, 0.1, duration);
   }
   no_sensor() {
       if(o1 = ~o1) {
           llPreloadSound("P_1");
           return;
       }
       llPreloadSound("P_2");
   }
   
   timer() {
       llSetTimerEvent(0);
       if(o2 = ~o2) {
           llPlaySound("P_2", 1.0);
           llSetTimerEvent(9.658);
           return;
       }
       llPlaySound("P_1", 1.0);
       llSetTimerEvent(9.820);
   }
   touch_start(integer detected) {
       llSetTimerEvent(0);
       llWhisper(0, "Stopping...");
       running = FALSE;
       llStopSound();
       state default;
   }

} </lsl>