The problem you are encountering is basically the problem in using the same memory space for generating multiple sinusoids. With the looping constructs you have got going on, when you trace the code and see how it behaves,
oscili generates an output
aosc. But before it is used anywhere, the loop repeats itself, and the value of
aosc is overwritten by osili again. At the end of 30 iterations, only the last generation of oscili output is retained.
One solution to the problem is recursion instead of iteration.
Through recursion, you allocate separate blocks of memory for each of the
ocili. The best way to use recursion would be through a User Defined Opcode (UDO). The flow of execution in recursion is slightly harder to follow, but here is a sketch of additive sine waves used in a UDO.
-n -d -+rtmidi=NULL -M0 -m0d
; Initialize the global variables.
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
giSine ftgen 1, 0, 4096, 10, 1
opcode SynthGen, a, kiio
kAmpEnv, iSpread, iFreq, iNum xin
aMix init 0
iFreq += iSpread
aOsc oscili kAmpEnv/30, iFreq, giSine
if iNum < 30 then
aOut SynthGen kAmpEnv, iSpread, iFreq, iNum + 1
aOsc += aOut
kAmpEnv linseg 0, 0.1, 1, p3 -0.2, 1, 0.1, 0
iFreq = p4
aOut SynthGen kAmpEnv, iFreq/4, iFreq
outs aOut * 0.1, aOut * 0.1
;causes Csound to run for about 7000 years...
;starts instrument 1 and runs it for a week
i1 0 3 400
You should be able to modify this sketch to suit your requirements.