Skip to content

Commit 9612332

Browse files
committed
added description and examples for ftgen arrays
1 parent c9f7178 commit 9612332

File tree

1 file changed

+138
-9
lines changed

1 file changed

+138
-9
lines changed

book/03-d-function-tables.md

Lines changed: 138 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,59 @@ i 1 0 0 2; prints function table 2
215215
;example by joachim heintz
216216
```
217217

218+
Most of the GEN routines offer the possibility to insert the arguments as array rather than as single values. The manual page for ftgen shows this line:
219+
220+
```csound
221+
gir ftgen ifn, itime, isize, igen, iarray
222+
```
223+
224+
The `iarray` will contain the arguments of the GEN routine as array. Note that you cannot mix here numbers and strings as currently Csound can only have an array of one single type. Usually the array will contain numbers. This is a simple example in which we generate an array containing the numbers from 1 to 7, and then put the array in a GEN02 function table. As in the previsou example, we simply print the content of the table.
225+
226+
#### **_EXAMPLE 03D03_ftgen_array_arg.csd_**
227+
228+
```csound
229+
<CsoundSynthesizer>
230+
<CsOptions>
231+
-nm0
232+
</CsOptions>
233+
<CsInstruments>
234+
235+
// create i-array
236+
iArray[] = genarray(1,7)
237+
238+
// put this array into a function table via GEN02
239+
giFt ftgen 0, 0, -7, -2, iArray
240+
241+
instr 1; prints the values of giFt
242+
prints("%nFunction Table giFt:%n")
243+
indx = 0
244+
while (indx < 7) do
245+
prints(" Index %d = %f%n", indx, table:i(indx,giFt))
246+
indx += 1
247+
od
248+
endin
249+
250+
</CsInstruments>
251+
<CsScore>
252+
i 1 0 0
253+
</CsScore>
254+
</CsoundSynthesizer>
255+
;example by joachim heintz
256+
```
257+
258+
Prints:
259+
260+
Function Table giFt:
261+
Index 0 = 1.000000
262+
Index 1 = 2.000000
263+
Index 2 = 3.000000
264+
Index 3 = 4.000000
265+
Index 4 = 5.000000
266+
Index 5 = 6.000000
267+
Index 6 = 7.000000
268+
269+
This feature is very powerful, and can be much more than an abbreviation. Have a look at example 03D06 below, at the end of the GEN10 explanation.
270+
218271
### GEN01: Importing a Soundfile
219272

220273
[GEN01](http://www.csound.com/docs/manual/GEN01.html) is used for
@@ -265,7 +318,7 @@ and then plays it. Reading the function table here is done using
265318
the [poscil3](http://www.csound.com/docs/manual/poscil3.html) opcode,
266319
as one of many choices in Csound.
267320

268-
#### **_EXAMPLE 03D03_Sample_to_table.csd_**
321+
#### **_EXAMPLE 03D04_Sample_to_table.csd_**
269322

270323
```csound
271324
<CsoundSynthesizer>
@@ -317,7 +370,7 @@ sinusoids. This is done in the next example by instruments 1-5.
317370
Instrument 6 uses the sine wavetable twice: for generating both the
318371
sound and the envelope.
319372

320-
#### **_EXAMPLE 03D04_Standard_waveforms_with_GEN10.csd_**
373+
#### **_EXAMPLE 03D05_Standard_waveforms_with_GEN10.csd_**
321374

322375
```csound
323376
<CsoundSynthesizer>
@@ -387,6 +440,82 @@ i "Sine_with_env" 20 3
387440

388441
![](../resources/images/03-d-waveforms.png)
389442

443+
The last example for GEN10 continues what was explained above (example 03D03) about the usage of arrays. It is rather complex; if you are a beginner in Csound, just skip it.
444+
445+
We use four arrays here to create four random GEN10 tables, representing four different timbres. Each of these arrays are filled with 40 amplitude values for the first 40 harmonics. In a loop a random number between -1 and +1 is generated. If its absolut value is below 0.8, the value is set to zero. If its absolut value is above 0.8, it is kept with a standard attenuation for the higher partials.
446+
For the wavetable transformation we imagine a square with the four tables as corners. We imagine a two-dimensional area with a range of 0-1 for both, the horizontal and vertical axis. In the example we move from table 1 (bottom left of the square) to table to (bottom right), to table 3 (top right), to table 4 (top left), and back to table 1. Then we move in the diagonal to the middle in which all four tables mix to one sound.
447+
448+
#### **_EXAMPLE 03D06_random_GEN10_wavetable.csd_**
449+
450+
```csound
451+
<CsoundSynthesizer>
452+
<CsOptions>
453+
-odac
454+
</CsOptions>
455+
<CsInstruments>
456+
sr = 44100
457+
ksmps = 64
458+
0dbfs = 1
459+
nchnls = 2
460+
seed 13 ;try other values here
461+
462+
// create an array containing the number of four tables
463+
gifn[ ] init 4
464+
465+
// create an array for 40 harmonics
466+
ihar[ ] init 40
467+
468+
// for each table ...
469+
ii = 0
470+
while ii < 4 do
471+
472+
// ... put 40 random amplitudes for the harmonics in it
473+
ik = 1
474+
while ik < 40 do
475+
// generate random values between 0 and 1
476+
irnd = abs(random:i(-1,1))
477+
// use only the ones above 0.8 and scale
478+
ihar[ik-1] = irnd < 0.8 ? 0 : irnd/ik
479+
ik += 1
480+
od
481+
// here the array is inserted as argument to GEN10
482+
gifn[ii] = ftgen(0,0,8192,10,ihar)
483+
ii += 1
484+
od
485+
486+
instr Wavetable
487+
488+
// set volume and base frequency
489+
iAmp = 0.2
490+
iFreq = 133
491+
492+
// go table 1 -> 2 -> 3 -> 4 -> 1, then to a mix of all
493+
kh = linseg:k(0,10,1,10,1,10,0,10,0,10,0.5)
494+
kv = linseg:k(0,10,0,10,1,10,1,10,0,10,0.5)
495+
496+
// read the four tables ...
497+
a1 = poscil:a(iAmp,iFreq,gifn[0])
498+
a2 = poscil:a(iAmp,iFreq,gifn[1])
499+
a3 = poscil:a(iAmp,iFreq,gifn[2])
500+
a4 = poscil:a(iAmp,iFreq,gifn[3])
501+
502+
// ... and mix according to kh and kv
503+
aMix = (a1*(1-kh)+a2*kh)*(1-kv) + (a3*(1-kh)+a4*kh)*kv
504+
505+
// fades and output
506+
aOut = linen:a(aMix,1,p3,10)
507+
outall(aOut)
508+
509+
endin
510+
511+
</CsInstruments>
512+
<CsScore>
513+
i "Wavetable" 0 60
514+
</CsScore>
515+
</CsoundSynthesizer>
516+
;Example by Victor Lazzarini, adapted by joachim heintz
517+
```
518+
390519
## How to Write Values to a Function Table
391520

392521
As we have seen, GEN Routines generate function tables, and by doing
@@ -447,7 +576,7 @@ in the header (filled with zeros), then instrument 1 calculates the
447576
values in an i-time loop and writes them to the table using tableiw.
448577
Instrument 2 simply prints all the values in a list to the terminal.
449578

450-
#### **_EXAMPLE 03D05_Write_Fibo_to_table.csd_**
579+
#### **_EXAMPLE 03D07_Write_Fibo_to_table.csd_**
451580

452581
```csound
453582
<CsoundSynthesizer>
@@ -497,7 +626,7 @@ be used to record any kind of user input, for instance by MIDI or
497626
widgets. It can also be used to record random movements of k-signals,
498627
like here:
499628

500-
#### **_EXAMPLE 03D06_Record_ksig_to_table.csd_**
629+
#### **_EXAMPLE 03D08_Record_ksig_to_table.csd_**
501630

502631
```csound
503632
<CsoundSynthesizer>
@@ -589,7 +718,7 @@ index that changes at a-rate. The next example first records a randomly
589718
generated audio signal and then plays it back. It then records the live
590719
audio input for 5 seconds and subsequently plays it back.
591720

592-
#### **_EXAMPLE 03D07_Record_audio_to_table.csd_**
721+
#### **_EXAMPLE 03D09_Record_audio_to_table.csd_**
593722

594723
```csound
595724
<CsoundSynthesizer>
@@ -728,7 +857,7 @@ and an a-rate signal from a buffer
728857
with [poscil3](http://www.csound.com/docs/manual/html/poscil3.html) (an
729858
oscillator with a cubic interpolation):
730859

731-
#### **_EXAMPLE 03D08_RecPlay_ak_signals.csd_**
860+
#### **_EXAMPLE 03D10_RecPlay_ak_signals.csd_**
732861

733862
```csound
734863
<CsoundSynthesizer>
@@ -828,7 +957,7 @@ same folder as your .csd: "i-tim_save.txt" saves function table 1 (a
828957
sine wave) at i-time; "k-time_save.txt" saves function table 2 (a
829958
linear increment produced during the performance) at k-time.
830959

831-
#### **_EXAMPLE 03D09_ftsave.csd_**
960+
#### **_EXAMPLE 03D11_ftsave.csd_**
832961

833962
```csound
834963
<CsoundSynthesizer>
@@ -885,7 +1014,7 @@ values and writing them as an audio signal to disk. After this is done,
8851014
the instrument is turned off by executing
8861015
the [turnoff](http://www.csound.com/manual/html/turnoff.html) statement.
8871016

888-
#### **_EXAMPLE 03D10_Table_to_soundfile.csd_**
1017+
#### **_EXAMPLE 03D12_Table_to_soundfile.csd_**
8891018

8901019
```csound
8911020
<CsoundSynthesizer>
@@ -974,7 +1103,7 @@ and we use the function table with the drawing data in two ways in it:
9741103
To avoid very long grains at the end, we modify it to half of the reciprocal
9751104
(so that a grain density of 10 Hz results in grains of 1/20 seconds).
9761105

977-
#### **_EXAMPLE 03D11_textfile_to_table.csd_**
1106+
#### **_EXAMPLE 03D13_textfile_to_table.csd_**
9781107

9791108
```csound
9801109
<CsoundSynthesizer>

0 commit comments

Comments
 (0)