newbe's re question

Eric_Dexter at msn.com Eric_Dexter at msn.com
Sun Sep 24 16:19:16 EDT 2006


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)
> >
> > http://www.msn.com
> > ..
> >
> >
> > Frederic Rentsch wrote:
> >
> >> Eric_Dexter at msn.com wrote:
> >>
> >>> Frederic Rentsch wrote:
> >>>
> >>>
> >>>> Eric_Dexter at msn.com wrote:
> >>>>
> >>>>
> >>>>> Frederic Rentsch wrote:
> >>>>>
> >>>>>
> >>>>>
> >>>>>> Eric_Dexter at msn.com wrote:
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>>> All I am after realy is to change this
> >>>>>>>
> >>>>>>>  reline = re.line.split('instr', '/d$')
> >>>>>>>
> >>>>>>> into something that grabs any line with instr in it take all the
> >>>>>>> numbers and then grab any comment that may or may not be at the end of
> >>>>>>> the line starting with ; until the end of the line including white
> >>>>>>> spaces..  this is a corrected version from
> >>>>>>>
> >>>>>>> http://python-forum.org/py/viewtopic.php?t=1703
> >>>>>>>
> >>>>>>> thanks in advance the hole routine is down below..
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>> [code]
> >>>>>>> def extractCsdInstrument (input_File_Name, output_File_Name,
> >>>>>>> instr_number):
> >>>>>>>
> >>>>>>>     "takes an .csd input file and grabs instr_number instrument and
> >>>>>>> creates output_File_Name"
> >>>>>>>     f = open (input_File_Name , 'r')                #opens file passed
> >>>>>>> in to read
> >>>>>>>     f2 = open (output_File_Name, 'w')               #opens file passed
> >>>>>>> in to write
> >>>>>>>     instr_yes = 'false'                             #set flag to false
> >>>>>>>
> >>>>>>>     for line in f:                                  #for through all
> >>>>>>> the lines
> >>>>>>>       if "instr" in line:                           #look for instr in
> >>>>>>> the file
> >>>>>>>            if instr_yes == 'true':                    #check to see if
> >>>>>>> this ends the instr block
> >>>>>>>                break                                #exit the block
> >>>>>>>
> >>>>>>>            reline = re.line.split('instr', '/d$')     #error probily
> >>>>>>> split instr and /d (decimal number into parts) $ for end of line
> >>>>>>>            number = int(reline[1])                  #convert to a
> >>>>>>> number maybe not important
> >>>>>>>                 if number == instr_number:            #check to see if
> >>>>>>> it is the instr passed to function
> >>>>>>>                 instr_yes = "true":                 #change flag to
> >>>>>>> true because this is the instr we want
> >>>>>>>       if instr_yes = "true":                        #start of code to
> >>>>>>> copy to another file
> >>>>>>>            f2.write(f.line)                         #write line to
> >>>>>>> output file
> >>>>>>>
> >>>>>>>     f.close                                         #close input file
> >>>>>>>     f2.close
> >>>>>>>
> >>>>>>> [/code]
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>> Eric,
> >>>>>>       From your problem description and your code it is unclear what
> >>>>>> exactly it is you want. The task appears to be rather simple, though,
> >>>>>> and if you don't get much useful help I'd say it is because you don't
> >>>>>> explain it very well.
> >>>>>>       I believe we've been through this before and your input data is
> >>>>>> like this
> >>>>>>
> >>>>>>    data = '''
> >>>>>>    <CsoundSynthesizer>;
> >>>>>>      ; test.csd - a Csound structured data file
> >>>>>>
> >>>>>>    <CsOptions>
> >>>>>>      -W -d -o tone.wav
> >>>>>>    </CsOptions>
> >>>>>>
> >>>>>>    <CsVersion>    ;optional section
> >>>>>>      Before 4.10  ;these two statements check for
> >>>>>>      After 4.08   ;   Csound version 4.09
> >>>>>>    </CsVersion>
> >>>>>>
> >>>>>>    <CsInstruments>
> >>>>>>      ; originally tone.orc
> >>>>>>      sr = 44100
> >>>>>>      kr = 4410
> >>>>>>      ksmps = 10
> >>>>>>      nchnls = 1
> >>>>>>      instr   1
> >>>>>>          a1 oscil p4, p5, 1 ; simple oscillator
> >>>>>>             out a1
> >>>>>>        endin
> >>>>>>    </CsInstruments>
> >>>>>>
> >>>>>>    <CsScore>
> >>>>>>      ; originally tone.sco
> >>>>>>      f1 0 8192 10 1
> >>>>>>      i1 0 1 20000 1000 ;play one second of one kHz tone
> >>>>>>      e
> >>>>>>    </CsScore>
> >>>>>>
> >>>>>>    </CsoundSynthesizer>
> >>>>>>
> >>>>>> Question 1: Is this your input?
> >>>>>> if yes:
> >>>>>>     Question 1.1: What do you want to extract from it? In what format?
> >>>>>> if no:
> >>>>>>     Question 1.1: What is your input?
> >>>>>>     Question 1.2: What do you want to extract from it? In what format?
> >>>>>> Question 2: Do you need to generate output file names from the data?
> >>>>>> (One file per instrument?)
> >>>>>> if yes:
> >>>>>>    Question 2.1: What do you want to make your file name from?
> >>>>>> (Instrument number?)
> >>>>>>
> >>>>>>
> >>>>>> Regards
> >>>>>>
> >>>>>> Frederic
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>> I want to pass the file name to the subroutine and return a comment
> >>>>> string if it is there maybe it should be simplier.  I probily should
> >>>>> have the option of grabbing the comment in other related routines.  I
> >>>>> am pretty ambitious with the main program.  I did notice some code in
> >>>>> tcl that would be usefull to the app If I compile it..  I am probily
> >>>>> not ready for that though..
> >>>>>
> >>>>> http://www.dexrow.com
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>> Eric,
> >>>>      I'm beginning to enjoy this. I'm sure we'll sort this out in no
> >>>> time if we proceed methodically. Imagine you are a teacher and I am your
> >>>> student. This is a quiz. I have to take it and you need to explain to me
> >>>> the problem you want me to solve. If you don't explain it clearly, I
> >>>> will not know what I have to do and cannot do the quiz. If you answer my
> >>>> questions above, your description of the problem will be clear and I can
> >>>> take the quiz. Okay?
> >>>>
> >>>> Frederic
> >>>>
> >>>>
> >>> instr   1
> >>>          a1 oscil p4, p5, 1 ; simple oscillator    ; comment is
> >>> sometimes here
> >>>             out a1
> >>>        endin
> >>>
> >>>
> >>> I need to know the file I wan't to grab this from I need to grab this
> >>> out of the larger file and put it into it's own file,  I need to know
> >>> what instr the user wants.  I need to know what file to put it into and
> >>> it would be usefull to have the comment line returned (if any).
> >>>
> >>> I did just get python essential reference 3rd edition..  If there is a
> >>> better reference on just the subject I am after I would be glad to grab
> >>> it when I get cash again.
> >>>
> >>>
> >>>
> >> Eric,
> >>
> >>    Tell us the story. Are you a student? A musician? What are you doing
> >> with these files? Where do you get them from? What for?
> >>
> >> Frederic
> >>
> >
> >
> Eric,
>       I found an excellent CSound doc at:
> ems.music.utexas.edu/program/mus329j/CSPrimer.pdf. I am myself
> interested in music and will definitely look into this more closely.
>       For the time being I get the impression that you are dealing with
> CSound instrument definition files which you can buy cheaply in great
> quantity but unfortunately are illegibly formatted. If this is so, what
> you would need is a formatter. I found references to CSound editors
> (e.g. http://flavio.tordini.org/csound-editor/). Those should be capable
> of formatting.
>       Writing your own formatter shouldn't be all that hard. Here's a
> quick try:
>
>
> def csound_formatter (in_file, out_file_file = sys.stdout):
>
>    INDENT      = 10
>    CODE_LENGTH = 50
>
>    for l in in_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))
>
>
> And here is a section of a csound file with white space squeezed out as
> would make the file smaller:
>
>  >>> csound = StringIO.StringIO ('''; Basic FM Instrument with Variable
> Vibrato
> ; p4=amp p5=pch(fund) p6=vibdel p7=vibrate p8=vibwth
> ; p9=rise p10=decay p11=max index p12=car fac p13=modfac
> ; p14=index rise p15=index decay p16=left channel factor p17=original p3
>
> instr 9,10,11,12
> ;--------------------------------------------------------------------------------
> ;initialization block:
> kpitch init cpspch (p5)
> itempo = p3/p17 ;ratio seconds/beats
> idelay = p6 * itempo ;convert beats to secs
> irise = p9 * itempo
> idecay = p10 * itempo
> indxris = (p14 == 0 ? irise : p14 * itempo)
> indxdec = (p15 == 0 ? idecay : p15 * itempo)
> if (p16 != 0) igoto panning ;if a panfac, use it, else
> ilfac = .707 ;default is mono (sqrt(.5))
> irfac = .707
> igoto perform
> panning:
> ilfac = sqrt(p16)
> irfac = sqrt(1-p16)
> ;--------------------------------------------------------------------------------
> ;performance block:
> perform:
> if (p7 == 0 || p8 == 0) goto continue
> kontrol oscil1 idelay,1,.5,2 ;vib control
> kvib oscili p8*kontrol,p7*kontrol,1 ;vibrato unit
> kpitch = cpsoct(octpch(p5)+kvib) ;varying fund pitch in hz
> continue:
> kamp linen p4,irise,p3,idecay
> kindex linen p11,indxris,p3,indxdec
> asig foscili kamp,kpitch,p12,p13,kindex,1 ;p12,p13=carfac,mod fac
> outs asig*ilfac,asig*irfac
> endin
> ''')
>
> Now we run this through the function defined above:
>
>  >>> csound_formatter (s)
>
> ; Basic FM Instrument with Variable Vibrato
> ; p4=amp p5=pch(fund) p6=vibdel p7=vibrate p8=vibwth
> ; p9=rise p10=decay p11=max index p12=car fac p13=modfac
> ; p14=index rise p15=index decay p16=left channel factor p17=original p3
>
>                     instr 9,10,11,12
> ;--------------------------------------------------------------------------------
> ;initialization block:
>                     kpitch init cpspch (p5)
>                     itempo = p3/p17                         ;ratio
> seconds/beats
>                     idelay = p6 * itempo                    ;convert
> beats to secs
>                     irise = p9 * itempo
>                     idecay = p10 * itempo
>                     indxris = (p14 == 0 ? irise : p14 * itempo)
>                     indxdec = (p15 == 0 ? idecay : p15 * itempo)
>                     if (p16 != 0) igoto panning             ;if a
> panfac, use it, else
>                     ilfac = .707                            ;default is
> mono (sqrt(.5))
>                     irfac = .707
>                     igoto perform
> panning:
>                     ilfac = sqrt(p16)
>                     irfac = sqrt(1-p16)
> ;--------------------------------------------------------------------------------
> ;performance block:
> perform:
>                     if (p7 == 0 || p8 == 0) goto continue
>                     kontrol oscil1 idelay,1,.5,2            ;vib control
>                     kvib oscili p8*kontrol,p7*kontrol,1     ;vibrato unit
>                     kpitch = cpsoct(octpch(p5)+kvib)        ;varying
> fund pitch in hz
> continue:
>                     kamp linen p4,irise,p3,idecay
>                     kindex linen p11,indxris,p3,indxdec
>                     asig foscili kamp,kpitch,p12,p13,kindex,1
> ;p12,p13=carfac,mod fac
>                     outs asig*ilfac,asig*irfac
>                     endin
>
>
> It works on a couple of presumptions (e.g. no code following labels). So
> it may not function perfectly but shouldn't be hard to tweak.
>
> Hope this helps
>
> Frederic

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




More information about the Python-list mailing list