Difference between revisions of "LlLoopSoundMaster"

From Second Life Wiki
Jump to navigation Jump to search
m (States should not be used for something so simple as a toggle. Modified the example.)
(Added new example which demonstrates how sound queueing affects playback order. Added llSetSoundQueueing as a related function.)
 
(One intermediate revision by one other user not shown)
Line 9: Line 9:
|return_text
|return_text
|spec
|spec
|caveats
|caveats=
* This function is affected by the [[llSetSoundQueueing|Sound Queueing]] property of the parent prim - this means it's possible to queue a slave sound prior to starting a master, without having to use more than one prim as an emitter. (See below).
|constants
|constants
|examples=
|examples=
Line 16: Line 17:
llLoopSoundMaster("MasterLoop", 1.0);
llLoopSoundMaster("MasterLoop", 1.0);
</source>
</source>
The script below can be added to an object, to toggle a SoundMaster on and off. Note that the script requires a sound in inventory called MasterLoop, or you need to change the argument in the function for it to work. This script is a slight modification to the script [[Toggle_States]], adding only the llLoopSoundMaster and [[llStopSound]] to make it work.
The script below is an example of how using [[llSetSoundQueueing]] affects the interaction between master and slave sounds. In the example, it is used to run a car engine.
<source lang="lsl2">
integer playState; //see LUT below.
 
list lut = ["Engine start, idle.","Accelerate, speed loop.","Decelerate, idle.", "Idle, stop"];
 
default
{
    state_entry()
    {
        llSetSoundQueueing(TRUE); //Set this so the we don't skip the 1st 'slave' sound.
    }
 
    touch_start(integer total_number)
    {
        llWhisper(0, "Sound: " + llList2String(lut,playState));
        if(!playState) //start engine.
        {
            llPlaySoundSlave("engine start",1); //This sound is skipped if Sound Queueing is not set.
            llLoopSoundMaster("idle",1);
        }
        else if(playState == 1) //accelerate, then play driving loop
        {
            llPlaySoundSlave("accelerate",1);
            llLoopSoundMaster("speed",1);
        }
        else if(playState == 2) //decelerate to idle.
        {
            llPlaySoundSlave("decelerate",1);
            llLoopSoundMaster("idle",1);
        }
        else //stop the engine.
        {
            llPlaySoundSlave("engine stop",1);
            llSleep(2); //Wait until the above sound completes.
            llStopSound(); //Call this to prevent the master loop from restarting randomly (e.g. upon script reset or region change).
            playState = -1; //This actually resets to 0, as it's incremented below.
        }
        ++playState; //increment playState.
    }
}
</source>
The script below can be added to an object, to toggle a SoundMaster on and off. Note that the script requires a sound in inventory called MasterLoop, or you need to change the argument in the function for it to work.
<source lang="lsl2">
<source lang="lsl2">
//This integer (actually a boolean) will be used to manage the toggle effect.
//This integer (actually a boolean) will be used to manage the toggle effect.
integer soundState = FALSE;
integer soundState = FALSE;
//Change MasterLoop to the sound clip you want to use.
string soundClip = "MasterLoop";
string soundClip = "MasterLoop";
default {
default {
     state_entry() {
     state_entry()
         // run this code when entering the default state
    {
        // displays red "OFF" as floating text above the prim
         //Displays red "OFF" as floating text above the prim
         llSetText("OFF", <1,0,0>, 1.0);
         llSetText("OFF", <1,0,0>, 1.0);
     }
     }
     touch_start(integer num_detected) {
     touch_start(integer num_detected)
         // when touched, soundState inverts its current boolean value.  
    {
         //When touched, soundState inverts its current boolean value. 1 becomes 0, 0 becomes 1.  
         soundState = !soundState;
         soundState = !soundState;
if(soundState)
        if(soundState)
{
        {
// run this code when entering soundState 'on'
            //Run this code when entering soundState 'on'
// displays green "ON" as floating text above the prim
            //Displays green "ON" as floating text above the prim
llSetText("ON", <0,1,0>, 1.0);
            llSetText("ON", <0,1,0>, 1.0);
llLoopSoundMaster(soundClip, 1.0);
            llLoopSoundMaster(soundClip, 1.0);
}else
        }else
{
        {   //Run this code when entering soundState 'off'
// when touched, stop sound & switch back to the default state
            //When touched, stop sound & display red "OFF" as floating text.
llStopSound();
            llSetText("OFF", <1,0,0>, 1.0);
}
            llStopSound();
        }
     }
     }
}
}
Line 47: Line 93:
|also_functions=
|also_functions=
{{LSL DefineRow||[[llLoopSound]]|3=Plays a sound {{HoverText|attached|The sound moves with the prim}} indefinitely.}}
{{LSL DefineRow||[[llLoopSound]]|3=Plays a sound {{HoverText|attached|The sound moves with the prim}} indefinitely.}}
{{LSL DefineRow||[[llLoopSoundSlave]]}}
{{LSL DefineRow||[[llLoopSoundSlave]]|3=Plays a sound {{HoverText|attached|The sound moves with the prim}} indefinitely, and declares it as a sync slave.}}
{{LSL DefineRow||[[llPlaySoundSlave]]|3=Plays a sound {{HoverText|attached|The sound moves with the prim}} once.}}
{{LSL DefineRow||[[llPlaySoundSlave]]|3=Plays a sound {{HoverText|attached|The sound moves with the prim}} once.}}
{{LSL DefineRow||[[llSetSoundQueueing]]|3=Enables the ability to queue sounds.}}
|also_events
|also_events
|also_tests
|also_tests

Latest revision as of 15:51, 22 April 2022

Summary

Function: llLoopSoundMaster( string sound, float volume );

Plays attached sound looping at volume, declares it a sync master.

• string sound a sound in the inventory of the prim this script is in or a UUID of a sound
• float volume between 0.0 (silent) and 1.0 (loud) (0.0 <= volume <= 1.0)

Caveats

  • If sound is missing from the prim's inventory and it is not a UUID or it is not a sound then an error is shouted on DEBUG_CHANNEL.
  • If sound is a UUID then there are no new asset permissions consequences for the object.
    • The resulting object develops no new usage restrictions that might have occurred if the asset had been placed in the prims inventory.
  • This function is affected by the Sound Queueing property of the parent prim - this means it's possible to queue a slave sound prior to starting a master, without having to use more than one prim as an emitter. (See below).
All Issues ~ Search JIRA for related Bugs

Examples

// for an uploaded .wav file called "MasterLoop" in inventory
llLoopSoundMaster("MasterLoop", 1.0);

The script below is an example of how using llSetSoundQueueing affects the interaction between master and slave sounds. In the example, it is used to run a car engine.

integer playState; //see LUT below.

list lut = ["Engine start, idle.","Accelerate, speed loop.","Decelerate, idle.", "Idle, stop"];

default
{
    state_entry()
    {
        llSetSoundQueueing(TRUE); //Set this so the we don't skip the 1st 'slave' sound.
    }

    touch_start(integer total_number)
    {
        llWhisper(0, "Sound: " + llList2String(lut,playState));
        if(!playState) //start engine.
        {
            llPlaySoundSlave("engine start",1); //This sound is skipped if Sound Queueing is not set.
            llLoopSoundMaster("idle",1);
        }
        else if(playState == 1) //accelerate, then play driving loop
        {
            llPlaySoundSlave("accelerate",1);
            llLoopSoundMaster("speed",1);
        }
        else if(playState == 2) //decelerate to idle.
        {
            llPlaySoundSlave("decelerate",1);
            llLoopSoundMaster("idle",1);
        }
        else //stop the engine.
        {
            llPlaySoundSlave("engine stop",1);
            llSleep(2); //Wait until the above sound completes.
            llStopSound(); //Call this to prevent the master loop from restarting randomly (e.g. upon script reset or region change).
            playState = -1; //This actually resets to 0, as it's incremented below.
        }
        ++playState; //increment playState.
    }
}

The script below can be added to an object, to toggle a SoundMaster on and off. Note that the script requires a sound in inventory called MasterLoop, or you need to change the argument in the function for it to work.

//This integer (actually a boolean) will be used to manage the toggle effect.
integer soundState = FALSE;
//Change MasterLoop to the sound clip you want to use.
string soundClip = "MasterLoop";
default {
    state_entry()
    {
        //Displays red "OFF" as floating text above the prim
        llSetText("OFF", <1,0,0>, 1.0);
    }
    touch_start(integer num_detected)
    {
        //When touched, soundState inverts its current boolean value. 1 becomes 0, 0 becomes 1. 
        soundState = !soundState;
        if(soundState)
        {
            //Run this code when entering soundState 'on'
            //Displays green "ON" as floating text above the prim
            llSetText("ON", <0,1,0>, 1.0);
            llLoopSoundMaster(soundClip, 1.0);
        }else
        {   //Run this code when entering soundState 'off'
            //When touched, stop sound & display red "OFF" as floating text.
            llSetText("OFF", <1,0,0>, 1.0);
            llStopSound();
        }
    }
}

See Also

Functions

•  llLoopSound Plays a sound attached indefinitely.
•  llLoopSoundSlave Plays a sound attached indefinitely, and declares it as a sync slave.
•  llPlaySoundSlave Plays a sound attached once.
•  llSetSoundQueueing Enables the ability to queue sounds.

Deep Notes

Search JIRA for related Issues

Signature

function void llLoopSoundMaster( string sound, float volume );