IV. Phasor, Table and Oscillator
The opcodes phasor, table, oscil, and their many variants share common principles. For example, oscil is merely an elaborate phasor that looks up values in a table, sets the phase, and adjusts the amplitude. Through exploring these three Csound mechanisms, you will hopefully come away with a greater understanding of some of the underlying fundamentals of digital oscillator design.
The Phasor
The phasor opcode is a special type of oscillator that outputs a sawtooth waveform with a range of 0 to 1, also known as a ramp. The phasor opcode is usually coupled with table for creating custom oscillators such as a sync oscillator or for cycling through a set list of pitches. A single master phasor can also be used to properly synchronize multiple lfos and envelopes.
Building an Oscillator with the Phasor
To build an oscillator that resembles oscil, opcodes phasor and table are used. First, the phasor signal is generated with a specified frequency, ifreq, using the phasor opcode. This signal will have a range of 0 to 1, and must be rescaled to match the size of the table used. When a table is specified with oscil, oscil automatically reads the length of the table internally. Here, the table length must be explicitly set. The table being used, f1, is a sine table with a resolution of 8192 points. The phasor signal is scaled by 8192. This rescaled signal is then used to read through the indexes of the stored sine wave using the table opcode. To simulate the amplitude parameter of oscil, the signal is then multiplied by the value stored in iamp.
The remaining oscil element to be emulated is the optional phase parameter. The phasor opcode's optional second parameter works identically to oscil's optional fourth parameter. Even though not demonstrated in this example, setting the phase as you would with oscil is possible with this code.

instr 1
iamp = p4
ifreq = p5
aphasor phasor ifreq
ascaled = aphasor * 8192
atable table ascaled, 1
aosc = atable * iamp
out aosc
endin
f1 0 8192 10 1 i1 0 2 10000 262
Simulating a Sync Oscillator
The analogue way of creating a sync oscillator uses two oscillators, a master and a slave. The master controls the primary pitch of the sync, and can be thought of as a control signal. The slave is generally tuned at a higher frequency than the master, and is the signal that is sent to the speakers. When a cycle of the master's oscillator is complete, the slave's oscillator is forced to reset its cycle position to the beginning. This creates a more complex wave shape for the slave oscillator and ensures the master and and slave have the same frequency, but with a different harmonic structure.
To simulate sync in Csound, a modified version of the phasor-table oscillator from the last example is used. The table opcode has an optional fifth argument that allows the index value to wrap around the table when the value exceeds the size of the table. Normally, a phasor signal is scaled to the size of the table being read. However, if the wrap argument is set to 1, and the phasor's range is greater than the size of the table, then the portion of the phasor's signal that falls outside the size of the table is wrapped to fit between 0 the table length.
For example, the length of the stored sawtooth wave in f2 is 8192. When the phasor's range is 0 to 12288, during one cycle of the phasor, a complete sawtooth is read from the table plus one half cycle. The phasor values between 8193 and 12288 are translated to values of 0 and 4096, corresponding with the first half of the stored table.

instr 1
idur = p3
iamp = p4
ipch = cpspch(p5)
isync = p6
aenv linseg 1, idur * .5, isync, idur * .5, 1
aphasor phasor ipch
atable table3 aphasor * 8192 * aenv, 1, 0, 0, 1
out atable * iamp
endin
f1 0 8192 7 -1 8192 1 i1 0 4 10000 6.00 8 i1 4 4 10000 6.05 16
This creates a very dirty sounding sync. Aliasing is to blame.
Quick and Dirty Complex Rhythms
Here's an easy way of creating complex rhythms in Csound with phasor and table. Table f2 stores a normalized envelope-shaped waveform with a sharp attack and a long decay. The frequency of the phasor pulls its value from p6 in the score, and is stored in irate. The phasor signal is used by table to read through f2, thus creating a lfo. The signal is then used to modulate the amplitude of an audio oscillator. The result is a score-friendly rhythmic instrument.

instr 1
idur = p3
iamp = p4
ipch = cpspch(p5)
irate = p6
aphasor phasor irate
atable table aphasor * 8192, 2
aosc oscil iamp, ipch, 1, -1
aosc = aosc * atable
out aosc
endin
f1 0 8192 10 1 0 0 0 0 0 .5 0 0 .3 0 0 0 .2 0 0 .1 f2 0 8192 -7 0 192 1 200 .6 7800 0 i1 0 2 10000 7.00 4 i1 + . . 7.05 3 i1 + . . 7.02 8 i1 + . . 7.07 6 i1 + . . 7.00 .5 i1 12 2 10000 7.07 4 i1 . . . 7.072 2 i1 14 . . 7.07 4 i1 . . . 7.072 3 i1 16 . . 7.07 4 i1 . . . 7.072 8 i1 18 . . 7.07 4 i1 . . . 7.072 6 i1 20 . . 7.07 3 i1 . . . 7.072 6 i1 22 . . 7.07 2 i1 . . . 7.072 3 i1 24 . . 7.07 2 i1 . . . 7.072 6 i1 26 . . 7.07 8 i1 . . . 7.072 3 i1 28 . . 7.07 3 i1 . . . 7.072 12 i1 30 . . 7.07 12 i1 . . . 7.072 4 e
Building a Phasor with oscil
Building a custom phasor unit that mimics the phasor opcode using oscil is quite easy. Create an f-table using GEN07 that rises from 0 to 44099 divided by 44100, or (sr - 1 / sr) for other sample rates. Why not from 0 to 1? The phasor opcode never actually reaches one. The reason being, 0 and 1 are identical in phase, much like 0 and 2π refers to the first value of a sine wave.
f1 0 8192 -7 0 8192 0.999977324263
Now use an oscil to cycle through this stored function table.
aphasor oscil 1, 440, 1
This is now nearly identical to the phasor opcode.
Quasi-Phasor
Building a phasor in the last example might have seemed somewhat redundant. However, that technique can be modified to create custom phasor weirdness. There is no real reason why the phasor waveshape can't be altered to read through a table in more interesting ways. This is why I've decided to demonstrate what happens if we use a normalized triangle wave in place of a rising sawtooth. During a single cycle, the quasi-phasor reads through the sine table forwards and then backwards, generating a reflected sine wave.

instr 1
idur = p3
iamp = p4
ipch = cpspch(p5)
aphasor oscil 1, ipch, 2
aphasor = aphasor * 8192
atable table aphasor, 1
out atable * iamp
endin
f1 0 8192 10 1 f2 0 8192 -7 0 4096 1 4096 0 i1 0 2 10000 7.00