Thursday, December 14, 2006


how to hardcode csound instruments with file names for code generation in python

Any csound instrument that uses a filename hardcoded in the .orc or .sco filename is a good canadate for a code generator. The first step I did when creating my code generator was to get the filename. I started by generating some code in boa-constructor for a filename picker.

dlg = wx.FileDialog(self,"load orc file", ".", "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()

(this is just part of the code generated)

The top line of this generated code can be modified to change the message on the top or to limit the type of files that can be loaded (by changing *.* to *.ext).

After we get the file name we have to get it in a format that will work in csound. I found that using the full path name will not work in csound so that the file that you are using needs to be in the same directory as csound. In the above example filename is the entire filename including the path. To shorten it up you would use the command os.path.basename(filename). That will get rid of the path that was included in the filename before. If you want to change the extension name for any reason you can use [:-4] and then the name of the extension you want to use

batfilenametemp = os.path.basename(filename[:-4]) + '.bat'

In order for our instrument to be generated we have to write it out to a file. To do that we have to create a file and prepare it for our instrument.

sampleplayerfile = open(orcfilenametemp, 'w')

orcfilenametemp in this example is the name of our file. If you want a constant file name you would use a single quote 'sample.orc' instead of the variable name we used. To write to the file we use the command


using triple quotes we can place all the code we want written to the file and it will save all our formatting including new lines. when we are ready to place our file name in the file we would end the string by using """) and then


This creates a new line.

sampleplayerfile.write('asig soundin ')

this created the non variable part of the line.


this generates the quotation mark we need around the filename


this will write the filename out to the file.

Once we reach that part we can finish it off and go back to triple quotes


The entire example is available as part of Dex Tracker.

Labels: , ,

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?