Tutorial 4

F-tables and oscillators

In this tutorial we'll start moving the objects we create on the openGL window. We will do this by means of an oscillator. An oscillator is a 'device' that repeats a pattern repeatedly. In synths oscillators repeat some kind of wave shape (sine, square, saw, etc.). In Csound, oscillators can repeat any kind of shape, whether a mathematical function, a sound file, or curves connecting points. This shape an oscillator uses, is stored within Csound in an f-table (or function table). As you'll see below, an oscillator will use the f-table to 'oscillate' at a given frequency, but let's start to make it more clear...

<CsoundSynthesizer>
<CsOptions>
-+Y
</CsOptions>
<CsInstruments>

#include "OpenGL.h"
sr=100
kr =100
ksmps=1
nchnls=1

GLfps 30
GLpanel "OpenGL panel", 512, 512
GLpanel_end
FLrun

glMatrixMode $GL_PROJECTION
glLoadIdentity
gluPerspective 60,0.1,100
glMatrixMode $GL_MODELVIEW
GLinsert_i $GL_NOT_VALID
glClear $GL_COLOR_BUFFER_BIT + $GL_DEPTH_BUFFER_BIT
GLinsert_i 1.1

All the usual headers and openGL initialisation instructions. No change here.

gisine ftgen 1,0,1024,10,1
The instruction ftgen creates an f-table. gisine is a global variable, which is a variable that is shared and can be modified by all instruments. A global variable always begins with the character 'g' followed by the charater of the type of variable, in this case 'i' which as we saw in the last tutorial stands for i-rate or instrument rate, which for now we will consider as constant. All f-tables are referenced by a number which is stored in the variable preceding the ftgen instruction, in this case gisine. The values to the right of ftgen correspond to the following:
First the number to reference the table, in this case we are creating f-table 1. Second, the time at which the table should be created, here we create the table at time 0, Third comes the size of the table. This can be thought of as the 'resolution' of the table, or how many points the table will have. Most tables must have a size that is a power of two, 1024 in this case. The next value of 10, refers to what in Csound-speak is called a GEN routine. This defines what process will be used to create the table. These processes can be mathematical, can be literal values, or can be files from the disk. GEN routine 10 generates a sum of harmonic partials, or a sum of sine waves with f, 2f, 3f, 4f, etc. frequencies. After this value of 10, we can place as many values as we like stating the relative strength of each partial. In this case we only use the first partial with a strength of 1, which generates a pure sine wave.
So to recap, we just created a sine wave made up of 1024 points into f-table 1.

instr 1
iframes = p4
irotate = p5
We will be using p-fields again, but this time to control different parameters of the instrument. We assign the p-fields to the local i-rate variables iframes and irotate.
glLoadIdentity
Clears all transformations.
trot GLoscil 1,iframes,1
Here we set up our oscillator. The instruction for an oscillator within the graphical engine is GLoscil. The oscillator value is put into the variable trot. This variable, because it starts with a t, is called a t-rate variable. T-rate variables are the ones which are updated only every frame (frame rate is defined in the instruction GLfps). All openGL instructions within CsoundAV that can use variables must use either i-rate or t-rate variables (we haven't covered other types of variables in these tutorials but it's still important to know this). GLoscil must have at least 3 parameters, which are amplitude, number of frames in a cycle and f-table used by the oscillator. In this case we create a variable trot which will be the result of oscillating f-table 1 (a sine wave), with a period of iframes (which is dictated by p-field 4 with an amplitude of 1). Note that the amplitude refers to the maximum positive value, and that the variable trot will then have values between 1 and -1.
glRotate irotate,0,0,1
Now we rotate our reference axis around the z axis irotate degrees given by p-field 5. Remember that rotation around the z axis is like rotating a propeller, or a steering wheel.
glTranslate 0,trot+1,-5
This glTranslate instruction performs a variable translation, simultaneously moving 5 units into the screen so the objects are visible, and doing a translation on the y axis dictated by the oscillator used above, through the use of the variable trot. We add 1 to trot to keep all values positive. The values will oscillate between 0 and 2.
Now that the position has been set, we place our object which will be a square, with different coloured edges, like we've done previously.

glBegin $GL_QUADS
glColor 0,0,1,1
glVertex3 0,0,0
glVertex3 0,1,0
glColor 1,0,1,1
glVertex3 1,1,0
glColor 0,1,1,1
glVertex3 1,0,0
glEnd
GLinsert 1.5
endin
The instrument ends, and now we call it in the score.

</CsInstruments>
<CsScore>
i 1 2 14 60 0
i 1 4 12 90 90
i 1 6 10 120 180
i 1 8 8 150 270
First we call the instrument four times starting at different times, but ending at the same time. The frequency of oscillation will be fps/p4, or for the first four instances 0.5, 0.333, 0.25, 0.2, i.e. The first one will be the fastest one. And each one will rotate 90 degrees counterclockwise with respect to the next.
i 1 18 12 60 45
i 1 18 12 90 135
i 1 18 12 120 225
i 1 18 12 150 315
Here we've just called another 4 instances of instrument 1, this time simultaneously and now slanted 45 degrees.
</CsScore>
</CsoundSynthesizer>

This is tutorial 4. Experiment with different colours, shapes and rotation values. Read about f-tables and GEN routines on the csound manual.

NOTES:
Apart from the instruction ftgen, you can also create f-tables from the score using the 'f' command. The latter is the more traditional one, but I've chosen ftgen for the tutorial because it seems more logical to see the creation of the f-table above the use of the oscillator.
As in GLoscil and ftgen, most csound opcodes use parameters on the right and results on the left.

Back to OpenGL Tutorials Index