Kara-Moon Forum
August 11, 2020, 06:54:34 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: You can go back to the main site here: Kara-Moon site
 
   Home   Help Search Login Register  
Pages: 1 2 [3]
  Print  
Author Topic: Cresc / Decresc / Swell  (Read 2129 times)
bvdp
Kara-Moon Master
****
Posts: 1218


WWW
« Reply #30 on: January 12, 2020, 06:09:05 PM »

Attached a total redesigned calcVolume. I used regexp matching to show you how powerful this is and that it makes code easier.

Two remarks:

* you need to add "import re" somewhere to the start of the module
* method setVolume needs a small fix to get the debug message right (reported values are already percentages):

    if MMA.debug.debug:
        dPrint("Volume: %s%%" % (100*volume))


Did you attach the correct replacement? When I grab this I end up with a function called  calcVolumeAA() and no references to re.

I have applied the debug patch. Thanks.
Logged

My online life: http://www.mellowood.ca
sciurius
Sr. Member
****
Posts: 320



« Reply #31 on: January 12, 2020, 06:48:48 PM »

Oops, sorry for that! Correct version attached.

* calcVolume.py.txt (1.1 KB - downloaded 20 times.)
Logged
bvdp
Kara-Moon Master
****
Posts: 1218


WWW
« Reply #32 on: January 12, 2020, 09:19:27 PM »

I'm on the edge here ... but when re.compile() is used this way, I _think_ it is compiled each time the enclosing function is called. Is it not better to have code like this:

    volumeRe = re.compile( ....
    def calcVolume(new, old):
         volumeRd.match(...

While we're discussing regular expressions ... I have to ask what does r'(?P<mn>[ofpm]+)$' do? Smiley
Logged

My online life: http://www.mellowood.ca
sciurius
Sr. Member
****
Posts: 320



« Reply #33 on: January 12, 2020, 10:22:25 PM »

Yes, you can take the re.compile out of the subroutine since its content is static.

The patterns consists of two parts, concatenated by '|' . So either the first part matches, or the second part, or nothing.

If the first part matches, group 'val' gets a value, and maybe 'pre' and 'post'.

If the second part matches, group 'mn' gets a value.

As you may guess, r'(?P<mn>[ofpm]+)' specifies a regex that matches any sequence of o f p m (which covers all valid mnemonics, and more). If it matches, match group 'mn' gets the value of what was matched.

The final '$' means that the pattern is to extend to the end of the string, i.e., must match the string completely from start to end. Note that the '$' applies to the whole rexegp, not just to the second part. Using parentheses for clarity the pattern is ( first | second ) $ , not ( first | second $ ) .

HTH
Logged
bvdp
Kara-Moon Master
****
Posts: 1218


WWW
« Reply #34 on: January 12, 2020, 11:09:32 PM »

Thanks for the lesson Smiley

I will take the re.compile out of the function. I'm sure that if we compile a million or so songs this will make a marked difference Smiley
Logged

My online life: http://www.mellowood.ca
bvdp
Kara-Moon Master
****
Posts: 1218


WWW
« Reply #35 on: January 12, 2020, 11:37:12 PM »

Did a mass compile of my existing songs ... and the first thing I have noticed is that the patch doesn't like uppercase volume names. I think that, since most things in MMA are case insensitive, there is no reason not to accept both "F" and "f", etc. You?

Might be easiest just to add a line to the top of the function: new = new.upper() and change the regex. That way we can dump the other conversion as well.
Logged

My online life: http://www.mellowood.ca
sciurius
Sr. Member
****
Posts: 320



« Reply #36 on: January 13, 2020, 07:26:09 AM »

While I have never seen a music score with MP or FFF, the fix is easy: add re.IGNORECASE as second arg to the re.compile:

volumepat = re.compile(
                       # opt. +/-, numeric value, opt. %
                       r'(?P<pre>[-+])?(?P<val>[\d.]+)(?P<post>%)?'
                       + '|' +
                       # mnemonic
                       r'(?P<mn>[ofpm]+)$',
                       re.IGNORECASE )
Logged
bvdp
Kara-Moon Master
****
Posts: 1218


WWW
« Reply #37 on: January 14, 2020, 08:59:31 PM »

Reason for upper or lower case for symbolic values is just because we are trying to keep MMA case insensitive.

Note, also, that more than one symbolic value is not standard music: 'off' and 'm'.

Problem/error with the code is that it accepts things like '80+20%' but doesn't do anything with it ... accepts it as 80. 'FF-20%' is flagged as an error. Can you fix that.

I have gone though my 1000+ files can fixed all the volume settings without the % signs. Damn, I didn't know there were so many Smiley Working on the docs right now.

Best,
Logged

My online life: http://www.mellowood.ca
sciurius
Sr. Member
****
Posts: 320



« Reply #38 on: January 14, 2020, 09:18:54 PM »

Sure.

volumepat = re.compile(
                       # opt. +/-, numeric value, opt. %
                       r'(?P<pre>[-+])?(?P<val>[\d.]+)(?P<post>%)?$'
                       + '|' +
                       # mnemonic
                       r'(?P<mn>[ofpm]+)$',
                       re.IGNORECASE )
Logged
bvdp
Kara-Moon Master
****
Posts: 1218


WWW
« Reply #39 on: January 14, 2020, 10:38:47 PM »

Great. We're having nice cold weather here (-10c) ... great time to have a nice fire in the stove and cuddle up with the .re reference Smiley

The new version works like a dream. Thanks.
Logged

My online life: http://www.mellowood.ca
Pages: 1 2 [3]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.056 seconds with 20 queries.