Csound Csound-dev Csound-tekno Search About

Another newbie question

Date1997-10-06 19:57
FromSteven LeBeau
SubjectAnother newbie question
Though creating "orchestras" makes more sense to me, and I think I
understand "scores" pretty well, I have a couple of small questions.

    1. If I download an orchestra off the net that doesn't come with a
score, how do I know what the right opcode for the instrument is?
    2. What the heck does an opcode do? It mentioned the GEN routines
(which, someone who hasn't taken higher-level math makes very little
sense) and making wavetables, which I thought were something done by the
orchestra (i.e., you set up a virtual modual synth patch that generates
a sound each time the score sends it a note request). I've tried using
the DX emulated sounds, for example, and using the opcode in the manual
results in the thing referring to invalid wavetables!

I know that the manual talks about these things, but the manual caters
to someone who's had at least calculus and a familiarity with hardware
synths (and I have a little experience with the DX27, so I guess that
counts). This is all just a bit confusing!

    -Steven LeBeau


Date1997-11-29 18:31
FromHans Mikelson
SubjectIntroduction to Csound
Hello,

I'll try to give a little introduction to Csound as I understand it.

The Orchestra:

The orchestra starts with a header which is followed by a list of
instruments.  The header tells what the sample rate of the sound file will
be, what the control rate is, how many channels the sound file will have
(mono, stereo, quad).  Following is a typical header:

sr=44100   ; Sample Rate
kr=22050   ; Control Rate
ksmps=2    ; sr/kr As far as I know this is always the case
nchnls=2   ; 1=mono, 2=stereo, 4=quad

Everything following a semicolon on the same line is a comment.  The header
is followed by one or more instruments.  A simple instrument follows:

       instr  1               ; Instrument 1 begins here
aout   oscil  10000, 440, 1   ; An oscillator
       outs   aout, aout      ; Output the results to a stereo sound file
       endin                  ; Instrument 1 ends here

Every instrument starts with instr # and ends with endin.  Each line has a
single command or opcode.  The main command in this instrument is the
"oscil" opcode.  Every opcode has zero or more variables on the left side
of the opcode and zero or more parameters on the right side depending on
the opcode.  The oscil opcode is a simple oscillator.  The first parameter
for oscil tells how loud the signal should be, in this case 10000. (Note
that 16 bit of digital audio translates to +/- 32000 so that's the maximum
loudness)  The second parameter for oscil tells what the frequency of the
oscillator is, in this case 440 cycles/sec.  The third parameter tells the
waveform or table of the oscillator.  The waveforms are stored in the
score. (more on them later)

Variables:

There are several types of variables used in Csound.  The above instrument
uses one variable "aout".  The first letter of the variable usually tells
what kind of variable it is.  The letter "a" means audio rate and "k" means
control rate.  Variables usually start with an "a" or a "k".  Be careful
not to give them the same name as an opcode.  There are also some special
"p" variables or parameters.  The "p" paramters values are supplied by the
score.  The first three p parameters have a special meaning.  Variables
starting with the letter "i" are initialized to a value when the instrument
is started and do not usually change.

p1 is the instrument number.
p2 is the time the instrument starts.
p3 is the duration of the instrument.
p4, p5, p6 etc. can be used for different things.  The send values from the
score to the orchestra.

The Score:

There are two different things typically found in a score.  They are the
wave tables and the instrument calls.  Following is a typical score which
could be used with the above orchestra:

;Table#  Start  TableSize  TableGenerator   Parameter
f1       0      16384      10               1          ; Sine

;Instrument#  Start  Duration
i1            0      1

First I'll discuss the call to the instrument.  The i indicates that it is
an instrument call.  The 1 (p1) means to call instrument number 1.  The
next number (p2) is 0.  This tells when the instrument should start
playing.  The third number (p3) is the duration in this case 1 second.
These values can be accessed in the instrument as p1-p3 respectively.

Next consider the waveform function table (f).  There are five numbers on
this line.  The first number is the number the table will be referenced by
in the orchestra.  The second number is the time at which this table
becomes available to the orchestra.  The third number tells how many
samples are in the table.  The fourth number tells which GEN routine to use
to generate the table.  In this case GEN routine 10 is selected.  Any
following numbers on this line are used by the GEN routine to control what
is generated.  GEN 10 with a 1 as its only parameter generates a sine wave.
 Other GEN routines can be used to generate square, triangle or many other
shapes, including reading in user supplied samples.

Many opcodes make use of tables.  The table number is given to the opcode
as one of its parameters.  In the simple orchestra above the third
parameter tells oscil which table to use for its waveform.  The third
parameter is a 1 so table 1 (sine wave) is used.

One drawback of this orchestra/score is that it only plays one pitch at one
volume.  Following is a more versatile orchestra/score.

; ORCHESTRA
sr=44100   ; Sample Rate
kr=22050   ; Control Rate
ksmps=2    ; sr/kr As far as I know this is always the case
nchnls=2   ; 1=mono, 2=stereo, 4=quad

       instr  1               ; Instrument 1 begins here
iamp   =      p4
ifqc   =      p5
itabl1 =      p6
aout   oscil  iamp, ifqc, itabl1  ; An oscillator
       outs   aout, aout          ; Output the results to a stereo sound file
       endin                      ; Instrument 1 ends here

; SCORE
;Table#  Start  TableSize  TableGenerator   Parameter
f1       0      16384      10               1          ; Sine

;Instrument#(p1) Start(p2) Duration(p3) Amplitude(p4) Frequency(p5) Table(p6)
i1               0         1            10000         440           1

This does the same thing but more control has been moved to the score.
That way you can play a variety of notes by just making more "i" entries in
the score.  For example:

i1               0         1            2000         330           1
i1               1         1            4000         440           1
i1               2         1            6000         600           1
i1               3         1            8000         660           1

In this case a variety of volumes and pitches are generated.  There is one
more problem.  There is a loud click at the end.  That is because there is
no amplitude envelope.  The sound just ends abruptly rather than ramping
down smoothly.  To add an envelope use the following instrument:

       instr  1               ; Instrument 1 begins here
idur   =      p3
iamp   =      p4
ifqc   =      p5
itabl1 =      p6

;             Attack Decay  Sustain       Release
kamp   linseg 0, .1, 1, .2, .8, p3-.5, .8, .2, 0
aout   oscil  iamp, ifqc, itabl1   ; An oscillator
       outs   aout*kamp, aout*kamp ; Output the results to a stereo sound file
       endin                       ; Instrument 1 ends here

This uses the linseg opcode to add an envelpe to the sound.  linseg
generates a series of line segments defined by a list of levels and times.
In this example it starts at a zero level then ramps to a level of 1 in the
first .1 second.  During the next .2 seconds the level decays to .8.  The
level stays at .8 until .2 seconds before the end of the note and then
drops to zero.

This should be enough to get some of the beginners started.

Bye,
Hans Mikelson