First time here? Check out the FAQ!

How do I get nextRandom to evaluate just once in my encapsulated Sound?

0 votes

I have a Sound that has 5 slightly detuned oscillators in it:


My encapsulated Sound has a single Frequency parameter which sets the frequencies of the five oscillators. If I put a "nextRandom" expression in the Frequency parameter of my encapsulated Sound instead of all of the oscillators changing together it is as if nextRandom is evaluated five times and the oscillators all end up with different frequencies. How would I make them all have the same frequency?



Thanks to Charlie Norton for this question which came up during our Kyma Kata.

asked Dec 21, 2018 in Capytalk & Smalltalk by alan-jackson (Virtuoso) (15,220 points)
edited Dec 22, 2018 by alan-jackson

1 Answer

0 votes

If you do not give a seed to the random number generator, it will be different in each copy. You can add it to the end of your Frequency expression, something like:

(!Gate nextRandom into: #({-1 @ 0} {1 @ 1})) seededWith: 0.16661

answered Dec 22, 2018 by ssc (Savant) (115,770 points)
Using "seededWith:" would give me the same sequence every time I compile the Sound, which I might not want to do.

I can't bounce the Frequency parameter through a SoundToGlobalController inside my encapsulated Sound because wouldn't I have to expose the STGC on the Sounds interface?

When it comes to expressions like "nextRandom" does an encapsulated Sound work differently from a prototype? Are there prototypes where if I use nextRandom without seeding it the Sound would de-cohere because it would be evaluated in multiple places internally?
To avoid having the same sequence each time you play, you could try starting with a different seed each time, for example you could generate a single random number in Smalltalk (at compile time) and use that as the seed for the nextRandom stream (that continues to generate random numbers while your Sound is running):

(!Gate nextRandom into: #({-1 @ 0} {1 @ 1})) seededWith: Random new next