newbe's re question
Frederic Rentsch
anthra.norell at vtxmail.ch
Tue Sep 26 04:25:57 EDT 2006
Eric_Dexter at msn.com wrote:
> Frederic Rentsch wrote:
>
>> Eric_Dexter at msn.com wrote:
>>
>>> These are csound files. Csound recently added python as a scripting
>>> language and is allowing also allowing csound calls from outside of
>>> csound. The nice thing about csound is that instead of worrying about
>>> virus and large files it is an interpiter and all the files look
>>> somewhat like html. 4,000 virus free instruments for $20 is available
>>> at
>>> http://www.csounds.com and the csound programming book is also
>>> available. The downside is that csound is can be realy ugly looking
>>> (that is what I am trying to change) and it lets you write ugly looking
>>> song code that is almost unreadable at times (would look nice in a
>>> grid)
>>>
>
snip snip snip snip snip .........
> I was hoping to add and remove instruments.. although the other should
> go into my example file because it will come in handy at some point.
> It would also be cool to get a list of instruments along with any
> comment line if there is one into a grid but that is two different
> functions.
>
> http://www.dexrow.com
>
>
Eric,
Below the dotted line there are two functions.
"csound_filter ()" extracts and formats instrument blocks from
csound files and writes the output to another file. (If called without
an output file name, the output displays on the screen).
The second function, "make_instrument_dictionaries ()", takes the
file generated by the first function and makes two dictionaries. One of
them lists instrument ids keyed on instrument description, the other one
does it the other way around, listing descriptions by instrument id.
Instrument ids are file name plus instrument number.
You cannot depend on this system to function reliably, because it
extracts information from comments. I took my data from
"ems.music.utexas.edu/program/mus329j/CSPrimer.pdf" the author of which
happens to lead his instrument blocks with a header made up of comment
lines, the first of which characterizes the block. That first line I use
to make the dictionaries. If your data doesn't follow this practice,
then you may not get meaningful dictionaries and are in for some
hacking. In any case, this doesn't look like a job that can be fully
automated. But if you can build your data base with a manageable amount
of manual work you should be okay.
The SE filter likewise works depending on formal consistency with
my sample. If it fails, you may have to either tweak it or move up to a
parser.
I'll be glad to provide further assistance to the best of my
knowledge. But you will have to make an effort to express yourself
intelligibly. As a matter of fact the hardest part of proposing
solutions to your problem is guessing what your problem is. I suggest
you do this: before you post, show the message to a good friend and edit
it until he understands it.
Regards
Frederic
.
------------------------------------------------------------------------------------------------------------------------
def csound_filter (csound_file_name,
name_of_formatted_instrument_blocks_file = sys.stdout):
"""
This function filters and formats instrument blocks out of a
csound file.
csound_formatter (csound_file_name,
name_of_formatted_instrument_blocks_file)
csound_formatter (csound_file_name) # Single argument: screen output
"""
import SE
Instruments_Filter = SE.SE ('<EAT> "~;.*~==(10)"
"~instr(.|\n)*?endin~==(10)(10)"')
INDENT = 10
CODE_LENGTH = 50
def format ():
for l in instruments_file:
line = l.strip ()
if line == '':
out_file.write ('\n')
else:
if line [0] == ';': # Comment line
out_file.write ('%s\n' % line)
else:
code = comment = ''
if line [-1] == ':': # Label
out_file.write ('%s\n' % line)
else:
if ';' in line:
code, comment = line.split (';')
out_file.write ('%*s%-*s;%s\n' % (INDENT, '',
CODE_LENGTH, code, comment))
else:
out_file.write ('%*s%s\n' % (INDENT, '', line))
instruments_file_name = Instruments_Filter (csound_file_name)
instruments_file = file (instruments_file_name, 'w+a')
if name_of_formatted_instrument_blocks_file != sys.stdout:
out_file = file (name_of_formatted_instrument_blocks_file, 'wa')
owns_out_file = True
else:
out_file = name_of_formatted_instrument_blocks_file
owns_out_file = False
format ()
if owns_out_file: out_file.close ()
def make_instrument_dictionaries (name_of_formatted_instrument_blocks_file):
"""
This function takes a file made by the previous function and
generates two dictionaries.
One records instrument ids by description. The other one
instrument descriptions by id.
Instrument ids are made up of file name and instrument number.
If these two dictionaries are pickled they can be used like data
bases and added to
incrementally.
"""
import re
instrument_numbers_re = re.compile ('instr *([0-9,]+)')
in_file = file (name_of_formatted_instrument_blocks_file, 'ra')
instrument_descriptions_by_id = {}
instrument_ids_by_description = {}
inside = False
for line in in_file:
line = line.strip ()
if not inside:
if line:
instrument_description = line [1:].rstrip ()
inside = True
else:
if line == '':
inside = False
if inside:
if line.startswith ('instr'):
instrument_numbers = instrument_numbers_re.match
(line).group (1).split (',')
instrument_ids = ['%s: %s' %
(name_of_formatted_instrument_blocks_file, i_n) for i_n in
instrument_numbers]
instrument_ids_by_description [instrument_description] =
instrument_ids
for instrument_id in instrument_ids:
instrument_descriptions_by_id [instrument_id] =
instrument_description
in_file.close ()
return instrument_descriptions_by_id, instrument_ids_by_description
---------------------------------------------------------------------------------------------
>>> for instrument_id in instrument_descriptions_by_id:
print '%s - %s' % (instrument_id,
instrument_descriptions_by_id [instrument_id])
T:\instruments: 5 - Simple Gating Instrument with Chorus
T:\instruments: 4 - Portamento/Panning Instrument
T:\instruments: 12 - Basic FM Instrument with Variable Vibrato
T:\instruments: 11 - Basic FM Instrument with Variable Vibrato
T:\instruments: 10 - Basic FM Instrument with Variable Vibrato
---------------------------------------------------------------------------------------------
>>> for description in instrument_ids_by_description:
print description
for instrument in instrument_ids_by_description [description]:
print ' ', instrument
Basic FM Instrument with Variable Vibrato
T:\instruments: 9
T:\instruments: 10
T:\instruments: 11
T:\instruments: 12
Simple Gating Instrument with Chorus
T:\instruments: 5
T:\instruments: 6
T:\instruments: 7
T:\instruments: 8
Portamento/Panning Instrument
T:\instruments: 1
T:\instruments: 2
T:\instruments: 3
T:\instruments: 4
---------------------------------------------------------------------------------------------
More information about the Python-list
mailing list