Introduction to Csound
Orchestra File
- header for global parameters
- instrument definitions (tell the computer how to synthesize sound)
Score File
- function table definitions (used by instruments during synthesis)
- note lists (tell the computer what notes the instruments should "play")
The Orchestra File
An instrument is a collection of modular statements which either generate or modify an audio signal.
There can be multiple instrument definitions in a single orchestra file.
An Example Instrument
instr 1
a1 oscil 10000, 440, 1
out a1
endin
The Complete Orchestra File
sr = 20000 ; audio sampling rate kr = 400 ; control rate ksmps = 50 ; samples/control period nchnls = 1 ; number of audio channels
instr 1
a1 oscil 10000, 440, 1
out a1
endin
The Score File
A function table contains samples for one period of a periodic function.
A note statement contains information that indicates which instrument plays the note, along with note duration, start time, and other optional parameters.
The Function Table
The function table statement (f) has the following required parameters:
p1 - function table number
p2 - creation time
p3 - table size (number of samples)
p4 - generating subroutine
Csound Function GEN10: Sum of Sine Waves
GEN10 - sum of sine waves
p1 - function table number
p2 - initialization time
p3 - number of samples in table
p4 - 10
p5 - amplitude of 1st harmonic
p6 - amplitude of 2nd harmonic (optional)
p7 - amplitude of 3rd harmonic (optional) .........etc.
Function Table Examples
f1 0 256 10 1 f2 0 256 10 1 0 0.33 0 0.2 f3 0 512 10 1 0.25 0.111 0.06 0.04
Tempo and Beats
By default, one beat = one second (thus, 60 beats per minute)
The tempo statement (t) can modify the beat duration:
t 0 120
(From beat 0, there are 120 beats/minute.)
The Note Statement
The note statement (i) has the following required parameters:
p1 - instrument number
p2 - start time for synthesis (in beats)
p3 - duration of note (in beats)
Example Note Statements
(The period "." indicates the carry feature, where the value is the same value as the same parameter in
the previous statement.)
t 0 120 i1 0 0.5 i1 0.5 . i1 1.0 . i1 1.5 . i1 2.0 1.0
The Complete Score File
; a sine wave function table f1 0 256 10 1 ; five notes played by instrument 1 i1 0 0.5 i1 0.5 . i1 1.0 . i1 1.5 . i1 2.0 1.0 e
Optional Note Parameters
Can be used to specify additional information for use in the instrument.
Common use to specify:
- note pitch (fundamental frequency)
- note amplitude
Second Example (Use of Optional Parameters)
; Orchestra File
sr = 20000
kr = 400
ksmps = 50
nchnls = 1
instr 1
a1 oscil p4, p5, 1
out a1
endin
====================================
; Score File f1 0 256 10 1 i1 0 0.5 10000 440 i1 0.5 . 5000 660 i1 1.0 . 10000 440 i1 1.5 . 20000 200 i1 2.0 1.0 15000 440 e
Introduction to Control (k) Orchestra Variables
Audio variables (e.g. a1) are computed once for each audio sample.
Control variables (e.g. k1) are computed once for every ksmps audio samples.
Third Example (Use of Control Variable)
; Orchestra File
sr = 20000
kr = 400
ksmps = 50
nchnls = 1
instr 1
k1 line 0, p3, p4
a1 oscil k1, p5, 1
out a1
endin
====================================
; Score File f1 0 256 10 1 i1 0 0.5 10000 440 i1 0.5 . 5000 660 i1 1.0 . 10000 440 i1 1.5 . 20000 200 i1 2.0 1.0 15000 440 e
Oscillator Algorithm
Given:
- Wave table with L samples representing one complete period of a periodic waveform
- Sampling rate for output R
- Fundamental frequency for output f0
Question: What samples should be stored in the output .wav audio file?
Let i be an index (pointer or subscript) of the next sample to the output buffer (initially 0)
Algorithm:
- Output sample at index i.
- Update i to point to the next sample to output by adding the sampling increment I.
- Repeat until enough samples are generated for duration of note.
Formulas
Given the wave table size L, the sampling rate R and the required fundamental frequency f0, the sampling increment for the oscillator algorithm is I = f0L/R.
Given the duration D of the output file (in seconds), the number of samples S that must be generated is S = DR.
Sampling Increment Examples
L = 16, R = 16000 Hz, f0 = 1000 Hz
I = 1
L = 16, R = 16000 Hz, f0 = 2000 Hz
I = 2
L = 32, R = 16000 Hz, f0 = 2000 Hz
I = 4
L = 16, R = 32000 Hz, f0 = 3000 Hz
I = 1.5
Non-integral Increments
Truncation
output = table[int(i)]
Rounding
output = table[int(i + 0.5)]
Interpolation
Let iL = int(i) and iH = int(i + 1). Let table[iL] = A and table[iH] = B.
output = (i - iL)(B-A) + A
General Oscillator Algorithm
Initialize table[] with periodic waveform
index = 0, j = 0, incr = f0*L/R
buf[j] = table[index]
S = D * R
for j = 1 to (S-1)
begin
index = (index + incr) mod L
buf[j] = table[index]
end
Csound Signal Generators
kr line ia, idur1, ib ; kr = control var. ar line ia, idur1, ib ; ar = audio var. kr linseg ia, idur1, ib [, idur2, ic [, ...]] ar linseg ia, idur1, ib [, idur2, ic [, ...]]
examples: k4 line p4, p3, 0 a3 linseg 0, p3/3, p4, 2*p3/3, 0
kr expon ia, idur1, ib ar expon ia, idur1, ib kr expseg ia, idur1, ib [, idur2, ic [, ...]] ar expseg ia, idur1, ib [, idur2, ic [, ...]]
examples: k4 expon p4, p3, 0.0001 a4 expseg 0.0001, p3/3, p4, 2*p3/3, 0.0001
kr oscil xamp, xcps, ifn[, iphs] ar oscil xamp, xcps, ifn[, iphs] kr oscili xamp, xcps, ifn[, iphs] ar oscili xamp, xcps, ifn[, iphs]
ifn: function table number (stored in score) iphs: initial phase (fractional value between 0 and 1)
Csound Signal Modifiers
kr linen xamp, irise, idur, idec ar linen xamp, irise, idur, idec
irise = rise time in seconds idec = decay time in seconds idur = overall duration of envelope in seconds
example: k5 linen p4, p3/10, p3, p3/4 a1 oscil k5, p5, 1
Csound Example: Amplitude Control
sr = 22000 ; ORCHESTRA
kr = 1100
ksmps = 20
nchnls = 1
; p3 = duration, p4 = amplitude, p5 = pitch
instr 1
k1 linen p4, p3/8, p3, p3/2
a1 oscil k1, p5, 1
out a1
endin
===========================
f1 0 256 10 1 0 0.33 0 0.2 ; SCORE i1 0 1.0 25000 440 e
Csound Function GEN07 & GEN05: Linear and Exponential Segments
GEN07 - Linear Segments
p1 - function table number
p2 - initialization time
p3 - number of samples in table
p4 - 7
p5 - data (starting) value
p6 - number of samples between p5 and p7
p7 - data value
p8 - number of samples between p7 and p9 (optional)
p9 - data value (optional) ...etc....
GEN05 - Exponential Segments
same as above, except
p4 = 5
p5,p7,p9,etc. cannot be 0 and must be alike in sign (cannot cross over zero-point)
Using Oscillator Algorithm for Amplitude Envelope
Store normalized envelope function in wave table (max. amplitude = 1)
Use oscillator algorithm with f0 = 1/D.
Scale each oscillator output by amplitude A to generate volume control for audio wave.
Csound Example: Amplitude Control (Again) using Oscillator for Amplitude Envelope
sr = 22000 ; ORCHESTRA
kr = 1100
ksmps = 20
nchnls = 1
; p3 = duration, p4 = amplitude, p5 = pitch
instr 1
k1 oscil p4, 1/p3, 2
a1 oscil k1, p5, 1
out a1
endin
===========================
f1 0 256 10 1 0 0.33 0 0.2 ; SCORE f2 0 256 7 0 32 1 96 1 128 0 i1 0 1.0 25000 440 e
Csound Signal Display and Audio Output
display xsig, iprd iprd - the period of display (in seconds) examples: k1 linen p4, p3/8, p3, p3/2 display k1, p3 a1 oscil p4, p5, 1 display a1, p3 ; bad display a2 oscil p4, p5, 1 display a2, 1/p5 ; good display - one cycle
out asig ; output for one channel (monophonic) outs asig1, asig2 ; output for two channels (stereophonic)
COURSE INFORMATION | HOMEWORK ASSIGNMENTS
COURSE PROJECT | CS240 HELP DESK