2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
# volume.py
|
|
|
|
|
|
|
|
"""
|
2007-04-29 06:47:40 +00:00
|
|
|
This module is an integeral part of the program
|
2006-11-10 08:07:56 +00:00
|
|
|
MMA - Musical Midi Accompaniment.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
Bob van der Poel <bob@mellowood.ca>
|
|
|
|
|
2006-11-10 08:07:56 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from MMA.common import *
|
|
|
|
|
|
|
|
|
|
|
|
""" Volumes are specified in musical terms, but converted to
|
|
|
|
midi velocities. This table has a list of percentage changes
|
2007-04-29 06:47:40 +00:00
|
|
|
to apply to the current volume. Used in both track and global
|
|
|
|
situations. Note that the volume for 'ffff' is 200%--this will
|
|
|
|
most likely generate velocities outside the midi range of 0..127.
|
|
|
|
But that's fine since mma will adjust volumes into the valid
|
|
|
|
range. Using very high percentages will ensure that 'ffff' notes
|
|
|
|
are (most likely) sounded with a maximum velocity.
|
2006-11-10 08:07:56 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
vols={ 'OFF': 0.00, 'PPPP': 0.05, 'PPP': 0.10,
|
|
|
|
'PP': 0.25, 'P': 0.40, 'MP': 0.70,
|
2007-04-29 06:47:40 +00:00
|
|
|
'M': 1.00, 'MF': 1.10, 'F': 1.30,
|
|
|
|
'FF': 1.60, 'FFF': 1.80, 'FFFF': 2.00 }
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
volume = vols['M'] # default global volume
|
2006-11-10 08:07:56 +00:00
|
|
|
lastVolume = volume
|
|
|
|
futureVol = []
|
|
|
|
vTRatio = .6
|
|
|
|
vMRatio = 1-vTRatio
|
|
|
|
|
|
|
|
|
|
|
|
def adjvolume(ln):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Adjust the ratio used in the volume table and track/global ratio. """
|
|
|
|
|
|
|
|
global vols, vTRatio, vMRatio
|
|
|
|
|
|
|
|
if not ln:
|
|
|
|
error("Use: AdjustVolume DYN=RATIO [..]")
|
|
|
|
|
|
|
|
for l in ln:
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
try:
|
|
|
|
v,r = l.split('=')
|
|
|
|
except:
|
|
|
|
error("AdjustVolume expecting DYN=RATIO pair, not '%s'" % l)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
v=v.upper()
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if v == 'RATIO':
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
r=stof(r)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if r<0 or r>100:
|
|
|
|
error("VolumeRatio must be value 0 to 100")
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
vTRatio = r/100
|
|
|
|
vMRatio = 1-vTRatio
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
elif v in vols:
|
|
|
|
vols[v] = calcVolume(r, vols[v])
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
else:
|
|
|
|
error("Dynamic '%s' for AdjustVolume is unknown" % v )
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if gbl.debug:
|
|
|
|
print "Volume Ratio: %s%% Track / %s%% Master" % ( vTRatio * 100, vMRatio * 100)
|
|
|
|
print "Volume table:",
|
|
|
|
for a in vols:
|
|
|
|
print "%s=%s" % (a, int(vols[a] * 100)),
|
|
|
|
print
|
|
|
|
|
|
|
|
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
def calcVolume(new, old):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Calculate a new volume "new" possibly adjusting from "old". """
|
|
|
|
|
|
|
|
if new[0] == '-' or new[0] == '+':
|
|
|
|
a = stoi(new, "Volume expecting value for %% adjustment, not %s" % new)
|
|
|
|
v = old + (old * a/100.)
|
|
|
|
|
|
|
|
elif new[0] in "0123456789":
|
|
|
|
v = stoi(new, "Volume expecting value, not '%s'" % new) / 100.
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
else:
|
|
|
|
new = new.upper()
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
adj = None
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if '+' in new:
|
|
|
|
new,adj = new.split('+')
|
|
|
|
elif '-' in new:
|
|
|
|
new,adj = new.split('-')
|
|
|
|
adj = '-' + adj
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if not new in vols:
|
|
|
|
error("Unknown volume '%s'" % new)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
v=vols[new]
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if adj:
|
|
|
|
a = stoi(adj, "Volume expecting adjustment value, not %s" % adj)
|
|
|
|
v += (v * (a/100.))
|
|
|
|
|
|
|
|
return v
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setVolume(ln):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Set master volume. """
|
|
|
|
|
|
|
|
global volume, lastVolume
|
|
|
|
|
|
|
|
lastVolume = volume
|
|
|
|
|
|
|
|
if len(ln) != 1:
|
|
|
|
error ("Use: Volume DYNAMIC")
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
volume = calcVolume(ln[0], volume)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if gbl.debug:
|
|
|
|
print "Volume: %s%%" % volume
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
# The next 2 are called from the parser.
|
|
|
|
|
|
|
|
def setCresc(ln):
|
2007-04-29 06:47:40 +00:00
|
|
|
setCrescendo(1, ln)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
def setDecresc(ln):
|
2007-04-29 06:47:40 +00:00
|
|
|
setCrescendo(-1, ln)
|
|
|
|
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
def setCrescendo(dir, ln):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Combined (de)cresc() """
|
|
|
|
|
|
|
|
global futureVol, volume, lastVolume
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
lastVolume = volume
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if len(ln) not in (2, 3):
|
|
|
|
error("Usage: (De)Cresc [start-Dynamic] final-Dynamic bar-count")
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if len(ln) == 3:
|
|
|
|
setVolume([ln[0]])
|
|
|
|
ln=ln[1:]
|
|
|
|
|
|
|
|
futureVol = fvolume(dir, volume, ln)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Used by both the 2 funcs above and from TRACK.setCresc()
|
|
|
|
|
|
|
|
def fvolume(dir, startvol, ln):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Create a list of future vols. Called by (De)Cresc. """
|
|
|
|
|
|
|
|
# Get destination volume
|
|
|
|
|
|
|
|
destvol = calcVolume(ln[0], startvol)
|
|
|
|
|
|
|
|
bcount = stoi(ln[1], "Type error in bar count for (De)Cresc, '%s'" % ln[1] )
|
|
|
|
|
|
|
|
if bcount <= 0:
|
|
|
|
error("Bar count for (De)Cresc must be postive")
|
|
|
|
|
|
|
|
if dir > 0 and destvol < startvol:
|
|
|
|
warning("Cresc volume less than current setting" )
|
|
|
|
|
|
|
|
elif dir < 0 and destvol > startvol:
|
|
|
|
warning("Decresc volume greater than current setting" )
|
|
|
|
|
|
|
|
elif destvol == startvol:
|
|
|
|
warning("(De)Cresc volume equal to current setting" )
|
|
|
|
|
|
|
|
bcount -= 1
|
|
|
|
step = ( destvol-startvol ) / bcount
|
|
|
|
|
|
|
|
volList=[startvol]
|
|
|
|
|
|
|
|
for a in range(bcount-1):
|
|
|
|
startvol += step
|
|
|
|
volList.append( startvol)
|
|
|
|
|
|
|
|
volList.append(destvol)
|
|
|
|
|
|
|
|
return volList
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|