Sequential Sound Loader
Monkey Forums/Monkey Programming/Sequential Sound Loader
| ||
I wanted a module to load sounds sequentially based on a name and a number of sounds (eg "bob1.wav", "bob2.wav", etc) and to play a random sound from that list. Here is my solution. After importing this, create an instance of the class: foosoundlist:soundlist = new soundlist To load all wav files in the data directory starting with "foosound" you would do: foosoundlist.init("foosound",5) where 5 would be the number of sounds of that name that you have in the data directory. Each one of these gets its own sound object. Then, to play a random sound out of the list: foosoundlist.randsound() Code is as follows: Import main Class soundlist Field sounds:Sound[10] Field lastsound:Int Field chan:Int Field numsounds Field currentsoundchannel:Int Method addsound(soundname:String) sounds[numsounds]=LoadSound(soundname) numsounds = numsounds + 1 End Method Method init(name:String, number:Int) For Local i:Int = 1 To number addsound(name+i+".wav") Next End Method Method randsound() Local i:Int = Rand(numsounds) 'PlaySound sounds[i], currentsoundchannel currentsoundchannel=currentsoundchannel+1 If currentsoundchannel > 16 currentsoundchannel = 0 End Method End Class I'm interested in any improvements that anyone could suggest. One question: what do we have for error checking? Can we check if a file exists in a directory? Can we check if a sound or picture is actually loaded before we try to access it and get the dreaded memory violation? |