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?
Wall of sinusoid
Many thanks for your help
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 !