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)