Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Need help with Sound Script

Alicya Undercroft
Registered User
Join date: 5 Oct 2008
Posts: 6
12-01-2008 02:43
Please let there be anyone who can help me.

I have found this script in the Forum that is actually working perfectly. But, I would like to add some more to this script. I have been trying this now for a couple of days without success (which is normal I guess since I have no experiences in scripting at all).

I have this script that creates a sound when my Avatar is Walking. What I would like to add is the choice to Turn ON/OFF the sound by a command in open chat and something that makes the sound not stop abruptly when my Avatar stops walking but fades out more slowly instead.

the script I found:

key owner_key;
integer owner_walking;

default
{
state_entry()
{
llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS);
llSetTimerEvent(0.25);
owner_key = llGetOwner();
}

changed(integer change)
{
if(change & CHANGED_OWNER)
{
owner_key = llGetOwner();
}
}

on_rez(integer param)
{
llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS);
}

run_time_permissions(integer perm)
{
llTakeControls(CONTROL_UP, FALSE, TRUE);
}

timer()
{
integer walking = (llGetAgentInfo(owner_key) & AGENT_WALKING) != 0;
if(walking)
{
if(!owner_walking)
{
llLoopSound("jingle-bells", 1.0);
}
}
else
{
if(owner_walking)
{
llStopSound();
}
}
owner_walking = walking;
}
}


I would be very thankfull for any help.
Yingzi Xue
Registered User
Join date: 11 Jun 2008
Posts: 144
12-01-2008 03:35
Here's what I came up with. It passes compile, but I don't have an animation to try it on.

From: someone

integer toggle; // toggle status of the walk sound
integer channel; // channel for chat command
float volume_decrease; // volume level

key owner_key; // owner key
integer owner_walking; // is the owner walking?

default
{
state_entry()
{
channel = 0; // set this to what you want to use; if not 0 you need to type /x command
llListen(channel,"",llGetOwner(),"";); // listen for chat commands from owner only

volume_decrease = 1.0; // volume at full
toggle = TRUE; // walking is on

llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS);
llSetTimerEvent(0.25);
owner_key = llGetOwner();
}

changed(integer change)
{
if(change & CHANGED_OWNER)
{
owner_key = llGetOwner();
}
}

on_rez(integer param)
{
llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS);
}

run_time_permissions(integer perm)
{
llTakeControls(CONTROL_UP, FALSE, TRUE);
}

timer()
{
integer walking = (llGetAgentInfo(owner_key) & AGENT_WALKING) != 0;
if(walking)
{
if(!owner_walking)
{
llLoopSound("jingle-bells", volume_decrease);
}
}
else
{
if(owner_walking)
{
llStopSound();
}
}
owner_walking = walking;

// if toggle is false (off) then decrease volume until 0.0 or less and stop walk sound
if (toggle == FALSE)
{
if (volume_decrease <= 0.0)
{
llSetTimerEvent(0);
}

volume_decrease = volume_decrease - .10;
}
}

listen(integer channel,string name,key id,string message)
{
// this is your toggle command
if (llStringTrim(llToLower(message),STRING_TRIM) == "toggle";)
{
// if toggle is true then make it false, which starts volume decrease, then off
// else if false then make true and turn on walk sound with full volume
if (toggle == TRUE) { toggle == FALSE;}
else if (toggle == FALSE) { toggle == TRUE; volume_decrease = 1.0; llSetTimerEvent(.25);}
}
}
}
Alicya Undercroft
Registered User
Join date: 5 Oct 2008
Posts: 6
Thank you Yingzi
12-01-2008 04:32
I have put the script in the object that is attached to my Avatar and the bells are still working :) so that is the good part. The bad part is that I am maybe to stuppid but the sound is not fading away slowly yet (Do I need to do some settings in the script?) and my other question is: where do I have set the chat-commands in the script to turn the sound on/off?

Thanx for your help :)
Innula Zenovka
Registered User
Join date: 20 Jun 2007
Posts: 1,825
12-01-2008 04:38
How to achieve a gentle fade-out rather than an abrupt stop depends rather on the sound clip you're using, but, assuming that your bells sound the way you want and gradually die away rather than come to an abrupt stop when you play them by opening the clip from your inventory, I think I would do it by changing what happens in the timer event to this, which comes from http://gwynethllewelyn.net/2007/12/03/heel-sounds-on-your-shoes/
CODE
  timer()
{
// Check if we’re walking; if yes, play sound
if (llGetAgentInfo(owner_key) & AGENT_WALKING)
{
llPlaySound ("jingle-bells",0.5);
walking = TRUE;
}
else
{
if (walking) // were we walking and just stopped?
{
llPlaySound("jingle-bells", 0.5); //typo in my original version -- corrected here to avoid confusion
walking = FALSE; // ok, transition finished for this loop.
}
}
}


The rationale for this is that llLoopSound plays the sound over and over again until you turn it off with llStopSound(). You often use it for a background sound effect like a crackling fire or running water, for example. In contrast, llPlaySound plays the sound clip once and then stops. My version of the code keeps on checking to see if it should play the sound again and, if it you're not walking (or running, which I think is detected by AGENT_WALKING), just doesn't play it any more, thus allowing the clip to end naturally rather than by flicking an off switch, which is what llStopSound() does.
Alicya Undercroft
Registered User
Join date: 5 Oct 2008
Posts: 6
syntax error
12-01-2008 05:15
Hi Innula,

I have tried to change the script the way you proposed but now I am getting a error in this line:

llPlaySound((("jingle-bells", 0.5);

The error shows up directly before the comma
Innula Zenovka
Registered User
Join date: 20 Jun 2007
Posts: 1,825
12-01-2008 05:50
oops... typo. Should be
CODE
 llPlaySound("jingle-bells", 0.5);
Sorry
Alicya Undercroft
Registered User
Join date: 5 Oct 2008
Posts: 6
Still doesnt work
12-01-2008 06:07
To bad, I still can't make it run. Now other Errors show up.
I guess I have to live with the fact that the sound doesnt fade out slowly when stop walking.
But maybe you can still help me with a command to swich of the sound with a simple ”bells off” and ”bells on” in chat.

I would be sooo thankful :)
Yingzi Xue
Registered User
Join date: 11 Jun 2008
Posts: 144
12-01-2008 07:48
It's been my experience that sounds are very unpredictable and are almost always affected by latency, so it's very hard to get a series of sounds to play in a synchronized manner. Volume may act the same way. It may not be feasible. The script I wrote was something I slapped together before getting ready for work, so I didn't have time to test it.
Innula Zenovka
Registered User
Join date: 20 Jun 2007
Posts: 1,825
12-01-2008 07:52
Try this. I've set it to listen on channel 5, because I really don't like using listeners set to the chat channel (both laggy and irritating for anyone else within 20 metres) but you can change myChannel to 0, at the start of the script, yourself if you really, really have to.

Not had a chance to test it in world but it compiles ok on the lsl editor.
CODE
key owner_key;
integer walking;
string mySound;
integer myChannel = 5;


default
{
state_entry()
{
llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS);
llSetTimerEvent(0.25);
mySound = llGetInventoryName(INVENTORY_SOUND,0);//grab the name of the first sound you find in the prim's inventory
owner_key = llGetOwner();
llListen(myChannel, "", owner_key, "");

}

changed(integer change)
{
if(change & CHANGED_OWNER)
{
owner_key = llGetOwner();
}
}

on_rez(integer param)
{
llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS);
}

run_time_permissions(integer perm)
{
llTakeControls(CONTROL_UP, FALSE, TRUE);
}

listen(integer channel, string name, key id, string message)
{
if(llToLower(message) != "bells on" && llToLower(message) != "bells off")//don't worry about capitalisation
return; // because the message isn't for me
if (llToLower(message) == "bells on")
{
llSetTimerEvent(0.25); //turn the timer on, so I'm checking whether or not we're walking
}
else if (llToLower(message) == "bells off")
{
llSetTimerEvent(0.0); //turn the timer off
}
}


timer()
{
// Check if we’re walking; if yes, play sound
if (llGetAgentInfo(owner_key) & AGENT_WALKING)
{
llPlaySound (mySound,0.5);
walking = TRUE;
}
else
{
if (walking) // were we walking and just stopped?
{
llPlaySound(mySound, 0.5);
walking = FALSE; // ok, transition finished for this loop.
}
}
}
}
Alicya Undercroft
Registered User
Join date: 5 Oct 2008
Posts: 6
name not defined within scope
12-01-2008 09:30
that is the error that shows up after I insert the new script.

the error seems to be in the following line before the =


mySound = llGetInventoryName(INVENTORY_SOUND,0);//grab the name of the first sound you find in the prim's inventory
Innula Zenovka
Registered User
Join date: 20 Jun 2007
Posts: 1,825
12-01-2008 09:56
Which is most peculiar, both because "string mySound;" up at the top defines mySound as a global variable, and, in consequence, available throughout the script, and because I've just gone in world, copied and pasted my effort from here into a script and it compiles (and works) fine.

I've dropped you a copy on you -- take a look and see what differences you can see between mine and yours (I suspect you may somehow have missed off the declarations at the top when you copied it from here).
Alicya Undercroft
Registered User
Join date: 5 Oct 2008
Posts: 6
It Works!!!!!!!
12-01-2008 11:24
Thank you so much!!!!!
I think.... I think I love you!!!! :)