Arrays in Csound

I’m having trouble getting arrays to work in CSound. Is there any reason why ‘randi’ is not working in this situation?

-odac ; Realtime audio out

sr = 44100

ksmps = 32

nchnls = 2

0dbfs = 1

instr 1

knote[] init 12

knotes[] fillarray 261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440, 466.164

krhythm[] init 5

krhythms[] fillarray 0.5, 0.25, 0.75, 1

kamp = 0.6

knote randi 0, 7

printk .5, knote

kcps = knotes[int(knote)]

krhythm randi 0, 3

idur = krhythms[int(krhythm)] * .5

asig oscil kamp, kcps, 1

outs asig, asig

schedule 1, 0, idur ; Schedule the next note

endin

f 1 0 16384 10 1

; Play 10 random notes

i 1 0 2

i 1 3 2

i 1 6 2

i 1 9 2

i 1 12 2

i 1 15 2

i 1 18 2

i 1 21 2

i 1 24 2

i 1 27 2

e

Hi @Vonias welcome to the community. knote has been declared as an array. randi returns a scalar value. You’ll need to use knote[n] to assign it to an index within the array, where n is the index, or change it to a scalar.

Hi!

There are several problems

  • first (and this is very common, it is difficult to grasp at first), it seems that you are mixing initialisaztion time (i-rate) and performance time (k-rate), see

See https://flossmanual.csound.com/csound-language/initialization-and-performance-pass for a good explanation

Like:

idur = krhythms[int(krhythm)] * .5

idur is a constant and evaluated when the instrument is started, before the execution; krhythms[int(krhythm)] is evaluated in the first performance pass, ie in the init-pass, it has no value yet.

krhythm randi 0, 3

krhythm random 0, 3 ; value between 0 and 3

I guess your goals is to have a number of notes after one another with duration taken randomly from

krhythms[] and notes randomly from knotes[]

Since you don’t change the values in runtime, it makes much more sense to use i-array (logical if in globals space) and do everything in i-time.

I made an examlpe csd for you, I hope you can learn from it and get many things clearer.

Greetings,

tarmo

sr = 44100

nchnls = 2

0dbfs = 1

ksmps = 32

giNotes[] fillarray 261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440, 466.164

giRhythms[] fillarray 0.5, 0.25, 0.75, 1

giMaxNotes = 20 ; how many notes to play

seed 0 ; to make random different every time

instr 1

iamp = 0.6

icounter = p4 ; use this for counting how many notes have been played. 0 for the first note

inote random 0, 7 ; or lenarray(giNotes)

inote = int(inote)

; or shorter, using functional syntax with type :i to make sure the values is returned in i-time

;inote = int(random:i(0, 7))

icps = giNotes[inote]

irhythm = int(random:i(0, lenarray:i(giRhythms))) ; demo of the funtional synthax, everything in one line

idur = giRhythms[irhythm]

; time when the next note starts

inext = idur ; play right after the note. can by also different - with overlap or gap, up to you

p3 = idur ; you can override the duration of the note this way

print inote, irhythm, icps, inext, idur

asig oscil iamp, icps ; 1 ; - for sine wave you don’t need a table, Csound uses its internal one

asig *= adsr:a(0.05, 0.05, 0.6, idur/4) ; apply an envelope

outs asig, asig

if icounter<giMaxNotes then ; check if the limit is note over

schedule 1, inext, 1, icounter+1 ; Schedule the next note, set the start time AFter this note. Duration can be anything since it will be overridden anyway

endif

endin

;f 1 0 16384 10 1 ; no need for that

; Play random notes

i 1 0 1 ; call once, later it will call itself

f 0 30 ; keep Csound running for 30 seconds

K, 25. september 2024 11:53 Rory Walsh via The Csound Community <noreply@forum.csound.com> kirjutas:

csoundqt-temp.csd (1.88 KB)

1 Like

Thanks, Rory and Tarmo,

It works brilliantly now. I was so confused, probably using C++ logic to the program.

If you like, check out my CSound Album:

CSound Album

I’m getting close to creating an atonal music generator that is aesthetically pleasing. I wish there were some way to accept array inputs from the user; I’m exploring widgets now.

That’s really nice music. You can pass arrays from a user to Csound. You can pass them using the command line, or via a live coding environment, or through a text editor in CsoundQT or Cabbage. There are lots of ways to do this.

1 Like

rory, I can see how that would be super useful in having a list of pitch frequencies in atonal music.