Phase modulation with feedback

Source code and result:

In this example, an oscillator is modulating its own phase through a feedback loop. The fondamental frequency of the oscillator is not changed by the modulation, thus the note pitch is stable. The modulation index of the last notes in the example has high values to produce some noise in the timbre.

Csound orchestra

         sr = 44100
         kr = 4410
      ksmps = 10
     nchnls = 1

          opcode fdbkPM, a, kk
            setksmps  1
kcps, kmod  xin
asig0       init      0
aphi        phasor    kcps
;in Phase Modulation, modulation index is independant from modulator frequency
aphi        =         frac(aphi + kmod*asig0)
asig        tablei    aphi, 1, 1, 0, 1
asig0       =         asig
            xout      asig
          endop

          instr 3
idur        = p3
iamp        = p4
icarcps     = cpspch(p5)
indxmul     = p6 / 6.2831853      ;p6/(2*pi)

kndx        adsr      0.4, 0.5, 0.1, 0.05
kmod        =         kndx * indxmul
asig        fdbkPM    icarcps, kmod
kenv        adsr      0.1, 0.2, 0.8, 0.2
            out       iamp * kenv * asig
          endin
   

To get efficient feedback, the control rate has to be the same as the sample rate (ksmps = 1). So the algorithm is implemented in a user defined opcode (UDO) named fdbkPM. This opcode takes two k-rate input parameters, oscillator frequency and modulation index, and returns an a-rate signal. The setksmps 1 instruction set the control rate to the same value than the sample rate inside the opcode. The a-rate variable asig0 is used for the one sample delay of the feedback loop.