First timer trying to learn simple stuff, selecting a random index that changes along time from an array

Hi, I’m trying to play a SineOsc. I want the frequency to be randomly selected from an array. I also want it to change along time as defined by Dust.kr(1) with a TIRand.
I’m constantly getting an “Index not an Integer” error.
Help says TIRand is supposed to create an Integer value. Ive tried index.asInteger, index.round. No luck.
Can someone help me?

(
{
	var trig = Dust.kr(1);
	var index;
	x = [440, 880, 1760, 3520, 7040, 14080];
	index = TIRand.kr(0, 5, trig);
	SinOsc.ar(x[index],0, 0.1);
}.play;
)

That should be

var x = [440, 880, 1760, 3520, 7040, 14080];

or better yet…

var freqs = [440, 880, 1760, 3520, 7040, 14080];

Use Select to index into an array on the server.

{
	var trig = Dust.kr(1);
	var freqs = [440, 880, 1760, 3520, 7040, 14080];
	var index = TIRand.kr(0, 5, trig);
	var freq = Select.kr(index, freqs);
	SinOsc.ar(freq, 0, 0.1);
}.play;
1 Like

Or how about this?

(
{
	var trig = Dust.kr(1);
	var freqs = 2 ** TIRand.kr(0, 5, trig) * 440;
	SinOsc.ar(freqs.poll, 0, 0.1);
}.play;
)

Thanks that solved my problem but i don’t understand why i had to do that middle step of using Select.kr

Array[2] works and returns the index 2 of the array.

I thought that TIRand(0, 5, trig) should return an integer between 0 and 5 that should work as the first arg for a SinOsc.

That’s because 2 is an integer. TIRand is a placeholder that indicates the (possible) existence of a functor on the audio server, what should the [ ] do with that?

2.class
TIRand.kr(...).class

All manipulation of sound (or anything on the server in general) must be done with one of these functors, called ugens. Now when you write a + in audio code, this automatically expands to something like Add.ar(...). Something similar could be done with [ ]… but it hasn’t (and for the best really). So you must use the underlying ugen yourself. Look at all the various versions of Select and you will see why it’s not a simple task of replacing [ ] with a Ugen automatically.

Edit: I may or may not be using the word functor wrong… I basically mean a ‘callable’ takes in a signal and returns a signal (so endofunction?).

2 Likes

Thanks, I understand it a bit more now

1 Like