Cabbage Logo
Back to Cabbage Site

Wall of sinusoid

Dear all,
Am struggeling with creating a bunch of sinusoids. Please see the code below.
Although i have a loop (trying to create some 30 oscillatore it seems to output only one oscili at a time.
What am i doing wrong here?

Many thanks for your help
Dominik



-odac -d; -+rtmidi=portmidi


sr = 44100
ksmps = 64
nchnls = 2
0dbfs = 1
; layered sound…
instr 6; strings
idur = p3; Duration
iamp = p4; Amplitude
ifqc = cpspch(p5); pitch to frequency
kampenv linseg 0.0, 0.1, 1.0, (idur-0.1-0.1), 0.8, 0.1, 0 ; env
ispread = ifqc/100
i_ind = 0
abank = 0
loop_no:
irand birnd ispread; Returns a random number in a bi-polar range
irand += ifqc
aosc oscili kampenv, irand, 1
abank += aosc
loop_lt i_ind, 1, 30, loop_no
outs abankiamp, abankiamp
endin

; --------------------------------------------------------------------
; Score…

f1 0 4096 10 1; GEN10 sinusoidal table
t 0 120; tempo
; Sta Dur Amp Pitch
i6 0 4 0.2 9.00; C4
i6 4 . . 9.02; D4
i6 8 . . 9.07; G4
i6 12 . . 9.00; C4
i6 16 . 0.2 9.02; D2

Hey Dominik,

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.

<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d 
</CsOptions>
<CsInstruments>
; 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
    endif
    aOsc        +=          aOut
    xout        aOsc
endop

instr 1

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

endin

</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...
f0 z
;starts instrument 1 and runs it for a week
i1 0 3 400

</CsScore>
</CsoundSynthesizer>

You should be able to modify this sketch to suit your requirements.

Many thanks, It works a treat !