Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

script that plays sound file hourly?

Ingrid Ingersoll
Archived
Join date: 10 Aug 2004
Posts: 4,601
02-18-2005 12:56
I have a sound file that I'd like to have play hourly, on the hour, according to the SL clock. Does anyone know where I can find this?

tks
_____________________
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
02-18-2005 13:57
Untested, but this should (might) work:

CODE
string  SOUND_NAME      = "my sound";   // name of the sound to play (can also be a key)
float SOUND_VOLUME = 1.0; // volume to play the sound at (0.0 = quietest, 1.0 = loudest)
integer INTERVAL = 3600; // what second to play it on (60*60 = 3600 seconds = 1 hour)

default {
state_entry() {
while (TRUE) {
// I think llSleep() is more reliable than timer events, but I could be wrong.
llSleep( INTERVAL - ((integer)llGetWallclock() % INTERVAL) );
llPlaySound( SOUND_NAME, SOUND_VOLUME );
}
}
}


edit: changed because % doesnt work on floats :O
Ingrid Ingersoll
Archived
Join date: 10 Aug 2004
Posts: 4,601
02-18-2005 17:58
Thank You!
_____________________
Moleculor Satyr
Fireflies!
Join date: 5 Jan 2004
Posts: 2,650
02-19-2005 05:20
Ingrid, that code won't do what you want it to do. Instead it'll simply play a sound every hour, regardless of the actual time, give or take a few seconds.

Instead, try this:

CODE
integer hour;
string sound = "SoundNameHere";
float volume = 1.0;

default
{
state_entry()
{
llSetTimerEvent(5);
}
timer()
{
integer currenthour = llFloor(llGetWallclock()/3600);
if(currenthour != hour)
{
llSetTimerEvent(0);
llSetTimerEvent(5);
llPlaySound(sound,volume);
hour = currenthour;
}
}
}


I know it compiles, and I've done rudimentary testing, but I honestly don't know for SURE if it rings correctly every hour. I don't even know if it rings on the hour. Seems it should work though. If it doesn't come back and tell me what it's doing, and I'll see if I can make a workaround.
_____________________
</sarcasm>
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
02-19-2005 10:24
From: Moleculor Satyr
Ingrid, that code won't do what you want it to do. Instead it'll simply play a sound every hour, regardless of the actual time, give or take a few seconds.

llSleep( INTERVAL - ((integer)llGetWallclock() % INTERVAL) );

If llGetWallclock() is 60, it will sleep 3540 (3600 - 60) seconds and play when llGetWallclock() is 3600.
If llGetWallclock() is 3601, it will sleep 3599 (3600 - 1) seconds and play when llGetWallclock() is 7200.
If llGetWallclock() is 9614, it will sleep 1186 (3600 - 2414) seconds and play when llGetWallclock() is 10800.

How is that not what she wanted?
Moleculor Satyr
Fireflies!
Join date: 5 Jan 2004
Posts: 2,650
02-19-2005 11:21
From: Masakazu Kojima
How is that not what she wanted?


From: Ingrid Ingersoll
I have a sound file that I'd like to have play hourly, on the hour, according to the SL clock. Does anyone know where I can find this?


Note the bold part.
_____________________
</sarcasm>
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
02-19-2005 11:40
From: Moleculor Satyr
Note the bold part.
... which is exactly what I just demonstrated that it does. Unless you're saying llGetWallclock() is wrong, but your code uses it too.
Moleculor Satyr
Fireflies!
Join date: 5 Jan 2004
Posts: 2,650
02-19-2005 12:11
Right, but if the sim ever gets hit with a massive spike of time dialation, your script will be off by minutes or more. llSleep isn't good for "timing" things.

However, yes, your code would work perfectly in an empty sim that ran perfectly.
_____________________
</sarcasm>
Moleculor Satyr
Fireflies!
Join date: 5 Jan 2004
Posts: 2,650
02-19-2005 12:13
Right, but if the sim ever gets hit with a massive spike of time dialation, your script will be off by minutes or more. llSleep isn't good for "timing" things.

However, yes, your code would work perfectly in an empty sim that ran perfectly.
_____________________
</sarcasm>
Jimmy Loveless
Hello, avatar!
Join date: 1 Oct 2004
Posts: 35
02-19-2005 12:30
Its true. You can't rely on timers or llSleep to be time-accurate. They usually wind up pretty close but they are rarely accurate. And if you are using it in a loop again and again, the error will accumulate and the time at which the sound plays will drift. Better to use getwallclock or gettimestamp if you want your event to be synced up with the actual time.

Cheers, JL
Moleculor Satyr
Fireflies!
Join date: 5 Jan 2004
Posts: 2,650
02-19-2005 13:21
Well, to be honest, Masakazu's code would only encounter a single offset per time-dialation problem, and would correct itself after every chime, but time-dialation is always fluctuating in sims, so llSleep is still not a good thing to use. If time-dialation were perfect, however, the code would be flawless, and probably better than mine.
_____________________
</sarcasm>
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
02-19-2005 14:34
From: Moleculor Satyr
Right, but if the sim ever gets hit with a massive spike of time dialation, your script will be off by minutes or more. llSleep isn't good for "timing" things.
Why didn't you say that in the first place? You made it sound like my script was just doing llSleep(3600); ("...it'll simply play a sound every hour, regardless of the actual time...";). That's why I put the note about thinking llSleep() was more reliable: in my experience llSleep() has never been wrong, and llSetTimerEvent() ... well, I'm not sure it's ever been right.

I tested llSleep() in the most crowded sim I could find (The Forest) and it got up to 640 seconds before it was a single second off, but the average time dilation was about 0.97. I have a tester running in my home sim (Baku) which is generally pretty bad and has a fair number of people around, and it's up to 1280 seconds (edit3: 9600 now) without being off by a single second. A major spike would probably make it wildly off, but I don't think it takes "an empty sim that runs perfectly" for it to be reasonably accurate most of the time.

If you want it to be just about perfect almost all the time I would do it like this maybe:

CODE
string  SOUND_NAME      = "my sound";   // name of the sound to play (can also be a key)
float SOUND_VOLUME = 1.0; // volume to play the sound at (0.0 = quietest, 1.0 = loudest)
integer INTERVAL = 3600; // what second to play it on (60*60 = 3600 seconds = 1 hour)

integer THRESHOLD = 5; // how many seconds before trigger to go into hyper mode

default {
state_entry() {
integer pause;
while (TRUE) {
pause = (INTERVAL - ((integer)llGetWallclock() % INTERVAL)) / 2;
while (pause > THRESHOLD) {
llSleep(pause);
pause = (INTERVAL - ((integer)llGetWallclock() % INTERVAL)) / 2;
}
while ( ((integer)llGetWallclock() % INTERVAL) != 0 ) {
// do nothing.
}

llPlaySound( SOUND_NAME, SOUND_VOLUME );
}
}
}
Ingrid Ingersoll
Archived
Join date: 10 Aug 2004
Posts: 4,601
02-19-2005 18:33
Thanks guys! it works!
_____________________
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
02-20-2005 15:36
From: Moleculor Satyr
Right, but if the sim ever gets hit with a massive spike of time dialation, your script will be off by minutes or more. llSleep isn't good for "timing" things.

However, yes, your code would work perfectly in an empty sim that ran perfectly.

From: Jimmy Loveless
Its true. You can't rely on timers or llSleep to be time-accurate. They usually wind up pretty close but they are rarely accurate.

Are you sure about this?

I've had a tester running in Baku, doing llSleep(x) and then reporting the difference in llGetWallclock() from before and after, then doubling x and doing it again:

Object: 160 -- 160.000000
Object: 320 -- 320.000000
Object: 640 -- 640.000000
Object: 1280 -- 1280.000000
Object: 2560 -- 2560.000000
(I changed it here since it was set not to go past 3600 and started over at 1200)
Object: 1200 -- 1200.000000
Object: 2400 -- 2400.000000
Object: 4800 -- 4800.000000
Object: 9600 -- 9600.000000
Object: 19200 -- -67200.000000 (the wall clock crossed midnight here, 60*60*24 - 67200 is 19200)
Object: 38400 -- 38400.000000

You can see graphs of Baku's time dilation here: http://geekmafia.net/~gabespin/simstats/

I think when I was getting 1 second off it was because of lag between llSleep() and llGetWallclock(), not time dilation affecting llSleep().

I can't get into SL to copy the script exactly right now but it's something like this:
CODE
default {
state_entry() {
integer x = 1200;
float start;
float end;
while (TRUE) {
start = llGetWallclock();
llSleep(x);
end = llGetWallclock();
llInstantMessage( llGetOwner(), (string)x + " -- " + (string)(end - start) );
x *= 2;
}
}
}