Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Sound Random_Sound_player  

Random_Sound_player

1. Put this script inside of the object you'd like to make a "random" greeter
2. Put a notecard called "SAYINGS" inside the script
* Each song should be on one line and <= 255 characters in length
* Each song must be in inventory and have the same name as given in the notecard (or bad things will happen)

Category: Sound
By : Jamethiel Wickentower
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for Random_Sound_player
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Random_Sound_player_1.lsl
1 // Copyright (c) 2009, David Lloyd (Jamethiel Wickentower)
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without modification,
5 // are permitted provided that the following conditions are met:
6 //
7 // Redistributions of source code must retain the above copyright notice, this list
8 // of conditions and the following disclaimer. Redistributions in binary form must
9 // reproduce the above copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided with the
11 // distribution.
12 //
13 //
14 // Neither the name of the the author nor the names of any contributor may be
15 // used to endorse or promote products derived from this software without specific
16 // prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // *********************************************************************************
30 // Instructions:
31 //
32 // 1. Put this script inside of the object you'd like to make a "random" greeter
33 // 2. Put a notecard called "SAYINGS" inside the script
34 // * Each song should be on one line and <= 255 characters in length
35 // * Each song must be in inventory and have the same name as given in the notecard (or bad
36 // things will happen)
37 // 3. The script will initialise and then announce to its owner what command
38 // channel it is listening on
39 // 4. Commands are listed below
40 //
41 //
42 // Commands:
43 //
44 // "RESET"
45 //
46 // This comman causes the script to reset.
47 //
48 // "REREAD"
49 //
50 // This command causes the list of songs to be read from the notecard again.
51 //
52 // "FIXED [number]"
53 //
54 // This command sets a fixed delay (i.e. the script will definitely delay this
55 // number of seconds). Note: you cannot set a fixed delay of less than 0.5
56 // seconds.
57 //
58 // "RANDOM [number]"
59 //
60 // This command sets a random delay; this is added to the fixed delay.
61 //
62 // "DELAYS"
63 //
64 // Reports what the current delays are.
65 //
66 // "ON" / "OFF"
67 //
68 // Turn the script on or off.
69 //
70 // "NOTECARD [name]"
71 //
72 // Sets the notecard to a notecard called "name". Make sure that notecard is in
73 // the object's inventory or the object might stop working.
74 //
75 // Known bugs:
76 //
77 // There are no known show stoppers. Do note that unless you stop and start the
78 // player manually, delay/random changes won't be picked up until the next timer()
79 // event.
80 //
81 // 1) User interface is a bit sparse - could do with a dialog
82 // 2) Notecard reading is quite slow - not much I can do about that though
83 //
84 // Feature Requests and Feedback:
85 //
86 // Please see my Google Code site:
87 //
88 // * http://code.google.com/p/linden-scripting-libraries/
89
90 key gOwner;
91 string gOwnerName;
92
93 integer CHANNEL = 0;
94 integer DEBUG = TRUE;
95
96 integer GOING = TRUE;
97
98 string gNotecard = "SOUNDS";
99 key gNotecardKey;
100
101 key gQueryID;
102 integer gLine = 0;
103
104 list gSongs;
105 float gVolume = 1.0;
106
107 float gFixedDelay = 5;
108 float gRandomDelay = 30;
109
110 // Trace
111 //
112 // If DEBUG is true, then trace it
113 trace(string msg) {
114 if(DEBUG == TRUE) {
115 llOwnerSay(msg);
116 }
117 }
118
119 // Clean Message
120 //
121 // Makes the message lower case and strips leading and trailing white space.
122 string clean_message(string message) {
123 return(llToUpper(llStringTrim(message, STRING_TRIM)));
124 }
125
126 // Discover Owner
127 //
128 // Gets detail about the item owner, such as KEY and NAME.
129 discover_owner() {
130 gOwner = llGetOwner();
131 gOwnerName = llKey2Name(gOwner);
132 llOwnerSay("Rezzed for " + gOwnerName);
133 }
134
135 // Setup the Public Listener
136 //
137 // This sets up the public listener.
138 setup_public_listener()
139 {
140 CHANNEL = llCeil(llFrand(99900)) + 100;
141 llOwnerSay("Listening on channel " + (string)CHANNEL);
142 llListen(CHANNEL, "", gOwner, "");
143 }
144
145 // Handle Public Message
146 //
147 // This handles public messages. This is a bit ham-fisted at the moment as it should
148 // start some type of IM with the avatar or open up a random chat channel. That will
149 // be implemented later.
150 handle_public_message(string message, string name, key id) {
151 string theMessage = clean_message(message);
152
153 if(id != gOwner) {
154 return;
155 }
156
157 // Here we start parsing commands that may have more than one word
158 list words = llParseString2List(theMessage, [" ", ","], []);
159 integer len = llGetListLength(words);
160 string command = clean_message(llList2String(words, 0));
161
162 if(command == "RESET") {
164 return;
165 }
166
167 if(command == "REREAD") {
168 gSongs = [];
169 gLine = 0;
170 read_songs();
171 return;
172 }
173
174 if(command == "SAYINGS") {
175 string message = llDumpList2String(gSongs, "\n");
176 llOwnerSay(message);
177 return;
178 }
179
180 if(command == "FIXED") {
181 llOwnerSay("FIXED");
182 float fixed_delay = llList2Float(words, 1);
183 if(fixed_delay < 0.5) {
184 llOwnerSay("You cannot have a fixed delay of less than 1/2 a second!");
185 fixed_delay = 0.5;
186 }
187
188 gFixedDelay = fixed_delay;
189 trace("Fixed delay: " + (string)gFixedDelay);
190 return;
191 }
192
193 if(command == "RANDOM") {
194 float random_delay = llList2Float(words, 1);
195 if(random_delay == 0) {
196 llOwnerSay("Possible incorrect value - double check.");
197 }
198
199 gRandomDelay = random_delay;
200 trace("Random delay: " + (string)gRandomDelay);
201 return;
202 }
203
204 if(command == "VOLUME") {
205 float volume = llList2Float(words, 1);
206 if(volume == 0) {
207 llOwnerSay("Setting the volume to zero...");
208 }
209
210 gVolume = volume;
211 trace("Volume: " + (string)gVolume);
212 return;
213 }
214
215 if(command == "DELAYS") {
216 llOwnerSay("Fixed delay: " + (string)gFixedDelay + "\nRandom delay: " + (string)gRandomDelay);
217 return;
218 }
219
220 if(command == "TOGGLE") {
221 if(GOING == TRUE) {
222 GOING = FALSE;
223 stop_player();
224 llOwnerSay("Going is now false.");
225 } else {
226 GOING = TRUE;
227 start_player();
228 llOwnerSay("Going is now true.");
229 }
230 return;
231 }
232
233 if(command == "ON") {
234 GOING = TRUE;
235 start_player();
236 return;
237 }
238
239 if(command == "OFF") {
240 GOING = FALSE;
241 stop_player();
242 return;
243 }
244
245 if(command == "NOTECARD" && len > 1) {
246 gNotecard = llList2String(words, 1);
247
248 trace("Setting notecard to " + (string)gNotecard);
249
250 stop_player();
251 read_songs();
252 return;
253 }
254
255 llOwnerSay("None implemented yet...");
256 }
257
258 // Stop Sayer
259 //
260 // Simply suspends the timer event.
261 stop_player() {
262 GOING = FALSE;
264 }
265
266 // Start Sayer
267 //
268 // Starts up the timer event
269 start_player() {
270 GOING = TRUE;
271 float delay = gFixedDelay + llFrand(gRandomDelay);
272 llSetTimerEvent(delay);
273 trace("Initial timer event delay: " + (string)delay);
274 }
275
276 // Read Sayings
277 //
278 // Reads the songs from the first note card contained in the inventory.
279 read_songs() {
280 gLine = 0;
281 gSongs = [];
282
283 if(gNotecard == "") {
285 }
286
287 gNotecardKey = llGetInventoryKey(gNotecard);
288
289 if(gNotecardKey == NULL_KEY) {
290 llOwnerSay("Can't find the notecard for " + gNotecard);
291 return;
292 }
293
294 llOwnerSay("Reading notecard " + gNotecard);
295 gQueryID = llGetNotecardLine((string)gNotecardKey, gLine);
296 }
297
298 // Handle Notecard Response
299 //
300 // Reads the list of songs from the notecard.
301 handle_notecard_response(string data) {
302 if(data != EOF) {
303 if(data != "") {
304 gSongs += data;
305 }
306 gLine++;
307 gQueryID = llGetNotecardLine((string)gNotecardKey, gLine);
308 stop_player();
309 } else {
310 llOwnerSay("Initiated " + (string)llGetListLength(gSongs) + " songs.");
311 start_player();
312 }
313 }
314
315 default {
316 state_entry() {
317 discover_owner();
318 setup_public_listener();
319 read_songs();
320 }
321
322 listen(integer channel, string name, key id, string message) {
323 if(channel == CHANNEL) {
324 handle_public_message(message, name, id);
325 return;
326 }
327 }
328
329 dataserver(key query_id, string data) {
330 if(gQueryID == query_id) {
331 handle_notecard_response(data);
332 }
333 }
334
335 touch_start(integer total_number) {
336 if(llDetectedKey(0) == gOwner) {
337 llOwnerSay("Listening on " + (string)CHANNEL);
338 }
339 }
340
341 timer() {
342 integer length = llGetListLength(gSongs);
343 string current_song = llList2String(gSongs, llCeil(llFrand(length - 1)));
344 llPlaySound(current_song, gVolume);
345
347 float delay = gFixedDelay + llFrand(gRandomDelay);
348 llSetTimerEvent(delay);
349 }
350 }

Back to the Best Free Tools in Second Life and OpenSim.