Thursday, May 8, 2014

15. Csound Introduction


Now, we will start going over some csound fundamentals.




A csound file has two sections, the orchestra and score. The orchestra defines the different instruments. The score defines the individual audio events, such as notes.




The orchestra might have a number of instruments. Each instrument will have some different identifying number. After the instrument number, a number of statements follow. These statements are known as the instrument block. The last line in the instrument block is usually an out opcode to route the audio output. The instrument is delimited by the opcode endin. The indentation is the usual indentation found in the csound documentation, but the csound compiler does not pay any attention to any whitespace, other than the requirement, that each opcode must be in a separate line.





This is an example of an instrument. This particular instrument has a block with 4 lines. As long as csound encounters the delimiters, instr, and ,endin, it will know where the block starts and ends. We have an out statement as the last opcode in the instrument block, so an audio signal is generated.




The score line has the format given in the example. The first character is the letter i. There are other score letters, which we will go over in further Tutorials. The first three numbers, here, correspond to the instrument number, the start time, and the duration.




The first 3 numbers in the score line are known as p1, p2 and p3. These are collectively called the p-fields. There might be other numbers such as p4, p5 and so on. The meaning of the additional p-fields will be instrument dependent. For example p4 might be the frequency.




csound uses two kinds of rates, known as audio-rate and control-rate. Any audio-rate or a-rate statement is run at a sampling rate, sr. Likewise, any control-rate or k-rate statement is run at a control rate, kr. Variables which are k-rate may influence, that is, control, audio-rate variables. Only audio-rate variables can be sent to the output audio channels. Further, in the final rendering to an audio file, we usually set the two rates equal.




The Line opcode creates a simple line, over two endpoints, which are separated by some time duration. We only have to give values at the two endpoints. Line opcode has two formats. One is an audio-rate format. The other is the control-rate format.




The upsamp opcode is also used in the example instrument block. The opcode merely changes the rate of a signal from control-rate to audio-rate. It does so by merely repeating values, not by doing any interpolation. We could also use an equal sign, instead of upsamp opcode, if we wish.




This is an example of the two rates. The way csound knows if we have a, k-rate, or a, a-rate, statement is whether a particular result variable begins with letter k, or the letter a. Here, the sampling rate , sr, is the usual CD rate, and kr is ten times lower, that is, one control-signal will control 10 audio samples. The two rates, given here, are the csound defaults.




In csound, different instruments may be called at different times from within the Score section. The Score section might have many score statements. When a particular instrument is called, all variables prefixed by the letter i, are calculated. These are called i-time variables, that is, initialization time variables.




All the instrument lines are in one block of statements. Even though the statements inside these blocks might seem to be executed one after another, they are not, and loops are implied depending on the prefixes of the result variables. First all i-time statements are done. Next a loop is started for k-rate statements. During each k-rate loop, a vector, that is, an array, of length ksmps is calculated for each a-rate variable. Thus, if kr is 10 times lower than sr, the vector length, ksmps, is 10. Any a-rate variable may be sent to the audio output.




Comments start after a semicolon and are ignored by the csound compiler. The comments can either be the entire line or just the end of a particular line.




This is the instrument 1 example with comments.




This is the csound csd file, that will be created by the Python program which will be described later in future Tutorials. Note, there are tags here, which structure the file into different sections.


<CsoundSynthesizer>
<CsOptions>
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 8820
nchnls = 2
0dbfs = 1
instr 1
  kline line -0.5,p3,0.5 ; control-rate
  aline line -0.5,p3,0.5 ; audio-rate
  asig upsamp kline      ; upsampling
  outs asig,aline
endin
</CsInstruments>
<CsScore>
i 1 0 2
</CsScore>
</CsoundSynthesizer>



You will find additional information at pythonaudio.blogspot.com.



This is the video of Tutorial 15:


No comments:

Post a Comment

About Me

I have used Python for the last 10+ years. I have a PhD in Electrical Engineering. I have taught Assembly Language programming of Intel-compatible chips as well as PC hardware interfacing. In my research, I have used Python to automate my calculations in physics and chemistry. I also use C++ and Java, often with Python.