Making a random sound play out of 100 when you tap.
FlameDizasterX
Member Posts: 2
I'm making an app that's sorta like a magic 8 ball, I have 100 possible things it could say, and I was wondering, how do you make a random one of the sounds play when you tap?
Comments
Make an integer attribute, call it RandS, for instance.
Rules:
Change RandS to random(1,100)
When RandS = 100
Play Sound
Advice though: you'll be waiting a really long time for your sound.....
Maybe experiment with random(1,10) or random(1,5), etc. See which suits you best.
:-)
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
I'm going to start an app just like this. Is there a better piece of code to use then to replicate this 100 times?
If I call the sounds 1.ogg, 2.ogg 3.ogg etc is there a way to load in sounds randomly that way?
With random, to speed up your game, you might be able to pick each digit randomly with a random(0,9) behavior for the tens and ones place. Then you could stitch those together as game.tens*10+game.ones.
I'd try it a few different ways, and see what gives the best performance.
Asymptoteell
Ace
list your # and soundName in a Note behavior.
then in your controlActor:
changeAttribute game.SelectedSound To: random(1,100)
but, with Integer or Index, for 100 sounds you are going to need 100 rules!
and, manually scrollSelect the sound for each!
Rule: all when
game.SelectedSound is 1
playSound 1 (scroll to this)
etc. and etc.
alas, no string calls for playSound behavior ;(
MH
You can make a file with all your sounds in - spaced equitemporally - so let's imagine 100 sounds in one audio file, a 1 second long sound every 1 second (they can of course be longer or shorter and be spaced however far apart you like (once they are equitempora)).
Start this file playing - set it to loop so it plays continually and turn it's volume to 0 (all the way down).
Now duplicate this play sound behaviour - and offset this sound by 1 second (using the timer - after 1 second - play this sound)
Now duplicate this play sound behaviour - and offset this sound by 2 seconds (using the timer - after 2 seconds - play this sound)
Now duplicate this play sound behaviour - and offset this sound by 3 seconds (using the timer - after 3 seconds - play this sound)
Now duplicate this play sound behaviour - and offset this sound by 4 seconds (using the timer - after 4 seconds - play this sound)
(continued - I can't post longer than 200 words on this forum . . . )
Make a attribute called RAND (integer)
Make 5 attributes called X1, X2, X3, X4 and X5 (boolean)
Rule:
When clicked (let's imagine a big red button) > change RAND to a random number between 1 and 5.
If RAND is 1 then change X1 to 1 - otherwise X1 to 0
If RAND is 2 then change X2 to 1 - otherwise X2 to 0
If RAND is 3 then change X3 to 1 - otherwise X3 to 0
If RAND is 4 then change X4 to 1 - otherwise X4 to 0
If RAND is 5 then change X5 to 1 - otherwise X5 to 0
So the RAND value turns one of 5 'switches' on.
Ok, now we have 5 random sounds playing every 1 second - and if you press our big red button, one of the layers (chosen randomly) will be turned up.
The only thing you would need to work out (which I don't think should be that difficult) is to turn the sound down at the end of it's time slot - so in this 1 second sound example, you only want the volume to up for 1 second - and also you want to make sure the point at which the volume goes up is quantized to the timing of the space between the sounds, so again with our 1 second example you would need to calculate a delay for turning the sound up based on the time attribute (in relation to your known spacing of sounds) - so some sounds will play immediately when you hit the button, some will wait a fraction of second to 'play' (for the volume to go up).
So 100 or 1,000 sounds but you only need to code 5 rules.
Hope that all make sense !?
: s
To do it with a 100 sounds would only require me to open up Logic and drag my 100 sounds on to a single track all spaced equally - I wouldn't have to touch the GS coding at all, it doesn't care if the sound file is 8 sounds long or 10,000 sounds long, it just loops it.
. . . . . [EDIT - actually the sound being turned down issue is not an issue at all, as the next random number is generated - by a timer synced to the distance between our sounds (so in our example the random number will be generated once a second with a timer) . . . as it is generated it turns the previous sound down automatically due to the 'otherwise' clause).
Ace
Cheers big ears.
The idea works, I am sure to could be enormously refined.
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
Brilliant sketch, love that scene.
: )
Is there a reason you chose 5 layers, or was that just nice mix and easy to manage number? My sounds aren't the same length, but I can easily (time consuming though) set it up so that it is, to try it out. But before i do I had one other thing that I needed to sync to each sound, and this solution will probably be no good. For each sound can I accompany a display text field on screen or even an image, that is synced to a particular sound. I realise that Tynons clever solution won't twist to suit this, but there may be another way.
Maybe randomising the display text actor and have a play sound on it?
I can't see that being a problem, we would have to calculate a delay to sync the start of the 'next audio step' (if that makes sense) so your text or image would happen within this rule.
I agree this method would only work for short sounds of roughly the same length, so if you had a bunch of FX sounds (bangs, crashes, hits etc) or short phrases ("yes you can” "no you can't" "turn left” "turn right" "take your hand off my knee" "are these your trousers" etc etc) that would be fine as the delay to the next audio step would only be a second or so - but on longer audio clips you might spend the whole time waiting around for it to play . . . .
As others have said this process can be achieved by using 100 rules (if random number = x then play sound x) the idea was to make the process of selecting from and playing 100 sounds a little less labour intensive / manageable / flexible (set up right with this system you can swap a 100 sound file for a 40 or 200 sound file without having to touch the code) - but to be honest 10 or more layers would be easy to manage but I don't think you need 10 layers for 'randomness' when dealing with 100 sounds (for most people a 100 sound loop is effectively random!)
Also selecting 1 sound from 100 on a purely random basis would likey mean that you never hear sound 76 or 32, or you hear them once every 8 days in this system each member of each group of 5 gets a repeated and regular chance to be played.
(continued . .)
If you were to imagine this process as having 100 layers, it would be fully random.
If you imagine this process as having 1 layer it would be fully predictable.
5 layers makes it sufficiently random (or psedo random) but spreads evenly where it draws it's numbers from.
So on a scale of 1 to 100 (predictable to unpredictable - pattern to random) we are at 5!
Now that might sound like it's going to be very predictable, but one thing to bear in mind (and this is easily forgoten when dealing with numbers) when thinking about whether people might perceive a pattern if your system is not sufficently random is that sounds are not numbers, so whereas we might recognise and remember the following 'patterns' of numbers . . .
3 6 9 - 3 6 8 - 3 6 7 - 3 6 6
Or
25 35 45 55 65 75 85 95
Or even
9 8 7 6 5 4 3 2 1
They have no meaning or significance when used to drive audio clips.
So 5 is probably enough, but to be honest with 100 sounds I think as few as 3 layers woud be fine.