Monday, May 12, 2014

17. Using the Csound Module


Now, we will go over a test run in IPython.




After changing into the directory with the module, we import all, that is, all the module functions are known to the current namespace, and the L List is created, and referenced with an alias of same name, and copies of all String tags are made. Strings are Immutable and copies will be made, not aliases. The Strings are the tags to start csound, and start and stop an empty command-line option statment. We will use only one command option, namely the o option to output an audio file, and it will be used via a system call.




Using %whos, we can see the names known to the namespace.




Now, the first 3 tags are added to the L List. Note, add() can be used with any number of terms.




Next we add the start instrument tag and the four header lines. Note, the List L has 5 more entries.




The instrument block is defined as a multi-line string. Basically, this particular instrument creates audio at the two rates of csound. Notice, the first character, as well as the last character, are newline characters. This is made a requirement in our Python module, as there is a test in the instrument() function, and should the String be not properly formatted, it exits after displaying a helpful printout.




Now we can add the String to the L List. In the function, String is converted to a List using newline as points of separations. The print functions, show the three operations happening to the String.




The instrument section is ended and the score section is started by inserting two tags.




Finally a score line is added. This score uses instrument 1, starts at time 0 and lasts for 2 seconds. The duration can also be in beats, that is, if we want to use a tempo besides the default 60 beats per minute. For 60 beats per minute, each beat is 1 second and thus seconds and beats are the same.




Finally, the score is ended as well as csound, by adding the two tags.




Now we can write out the list L, and run the resulting csound file.




This is the output audio file. It is not interesting to listen. It is merely to show what the Line opcode does, as well as to illustrate the difference between audio and control rates.




We could also write a main Python program to do all the steps in creating and running a csound file. Note, that, instead of having many add() calls, we may make the function accept a larger Tuple.


# main.py
from moduleCsound import *
add(startSyn,startOpt,stopOpt,startIns)
header(ksmps=8820, nch=2)
ins1="""
kline line -0.5,p3,0.5 ; control-rate
aline line -0.5,p3,0.5 ; audio-rate
asig upsamp kline      ; upsampling
out asig,aline
"""
instrument(1,ins1)
add(stopIns,startSco)
score(1,0,2)
add(stopSco,stopSyn)
writeRun('tut17')



You will find additional information at pythonaudio.blogspot.com, including the source code.



This is the video of Tutorial 17:


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.