2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
# translate.py
|
|
|
|
|
|
|
|
"""
|
|
|
|
This module is an integeral part of the program
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
This module handles voice name translations.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import MMA.midiC
|
2009-05-17 22:34:44 +00:00
|
|
|
|
|
|
|
import gbl
|
2006-11-10 08:07:56 +00:00
|
|
|
from MMA.common import *
|
|
|
|
|
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
|
2006-11-10 08:07:56 +00:00
|
|
|
""" Translation table for VOICE. This is ONLY used when a voice is set
|
2007-04-29 06:47:40 +00:00
|
|
|
from the VOICE command. If a translation exists the translation is
|
|
|
|
substituted.
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Vtable:
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.table = {}
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def retlist(self):
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
l=[]
|
|
|
|
for n in sorted(self.table.keys()):
|
|
|
|
l.append("%s=%s" % (n.title(), self.table[n]))
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
return ' '.join(l)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def set(self, ln):
|
|
|
|
""" Set a name/alias for voice translation, called from parser. """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if not ln:
|
|
|
|
self.table = {}
|
|
|
|
if gbl.debug:
|
|
|
|
print "Voice Translaion table reset."
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
return
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
for l in ln:
|
|
|
|
l=l.upper()
|
|
|
|
if l.count('=') != 1:
|
|
|
|
error("Each translation pair must be in the format Voice=Alias")
|
|
|
|
v,a = l.split('=')
|
|
|
|
self.table[v] = a
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if gbl.debug:
|
|
|
|
print "Voice Translations: ",
|
|
|
|
for l in ln:
|
|
|
|
print l,
|
|
|
|
print
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def get(self, name):
|
|
|
|
""" Return a translation or original. """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
name=name.upper()
|
2009-05-17 22:34:44 +00:00
|
|
|
try:
|
2007-04-29 06:47:40 +00:00
|
|
|
return self.table[name]
|
2009-05-17 22:34:44 +00:00
|
|
|
except KeyError:
|
2007-04-29 06:47:40 +00:00
|
|
|
return name
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
vtable=Vtable() # Create single class instance.
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
""" This is just like the Vtable, but it is used for DRUM TONES. We use
|
2007-04-29 06:47:40 +00:00
|
|
|
this translation when a TONE is set for a drum in setTone() and when a
|
|
|
|
tone is selected in a Solo/Melody DRUM track.
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
class Dtable:
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.table = {}
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def retlist(self):
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
l=[]
|
|
|
|
for n in sorted(self.table.keys()):
|
|
|
|
l.append("%s=%s" % ( MMA.midiC.valueToDrum(n),
|
|
|
|
MMA.midiC.valueToDrum(self.table[n])))
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
return ' '.join(l)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def set(self, ln):
|
|
|
|
""" Set a name/alias for drum tone translation, called from parser. """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if not ln:
|
|
|
|
self.table = {}
|
|
|
|
if gbl.debug:
|
|
|
|
print "DrumTone Translaion table reset."
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
return
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
for l in ln:
|
|
|
|
l=l.upper()
|
|
|
|
if l.count('=') != 1:
|
|
|
|
error("Each translation pair must be in the format Voice=Alias")
|
|
|
|
v,a = l.split('=')
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
v1=MMA.midiC.drumToValue(v)
|
|
|
|
if v1<0:
|
|
|
|
error("Drum Tone '%s' not defined." % v)
|
|
|
|
a1=MMA.midiC.drumToValue(a)
|
|
|
|
if a1<0:
|
|
|
|
error("Drum Tone '%s' not defined." % a)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
self.table[v1] = a1
|
2007-04-29 06:47:40 +00:00
|
|
|
if gbl.debug:
|
|
|
|
print "DrumTone Translation: %s=%s" % \
|
|
|
|
(MMA.midiC.valueToDrum(v), MMA.midiC.valueToDrum(a))
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def get(self, name):
|
2009-05-17 22:34:44 +00:00
|
|
|
""" Return a translation or original. Note that this also does
|
|
|
|
validation of 'name'. It is called from patDrum and patSolo.
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
v=MMA.midiC.drumToValue(name)
|
2009-05-17 22:34:44 +00:00
|
|
|
|
|
|
|
if v<0:
|
|
|
|
error("Drum Tone '%s' not defined." % name)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
try:
|
2007-04-29 06:47:40 +00:00
|
|
|
return self.table[v]
|
2009-05-17 22:34:44 +00:00
|
|
|
except KeyError:
|
2007-04-29 06:47:40 +00:00
|
|
|
return v
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dtable=Dtable()
|
|
|
|
|
|
|
|
|
|
|
|
""" Volume adjustment. Again, similar to voice/tone translations,
|
2007-04-29 06:47:40 +00:00
|
|
|
but this is for the volume. The table creates a percentage adjustment
|
|
|
|
for tones/voices specified. When a TRACK VOLUME is set in
|
|
|
|
MMApat.setVolume() the routine checks here for an adjustment.
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
class VoiceVolTable:
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.table = {}
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def retlist(self):
|
|
|
|
l=[]
|
|
|
|
for n in sorted(self.table.keys()):
|
|
|
|
l.append("%s=%s" % ( MMA.midiC.valueToInst(n), self.table[n]))
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
return ' '.join(l)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def set(self, ln):
|
|
|
|
""" Set a name/alias for voice volume adjustment, called from parser. """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if not ln:
|
|
|
|
self.table = {}
|
|
|
|
if gbl.debug:
|
|
|
|
print "Voice Volume Adjustment table reset."
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
return
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
for l in ln:
|
|
|
|
l=l.upper()
|
|
|
|
if l.count('=') != 1:
|
|
|
|
error("Each translation pair must be in the format Voice=Ajustment")
|
|
|
|
v,a = l.split('=')
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
v=MMA.midiC.instToValue(v)
|
|
|
|
a=stoi(a)
|
|
|
|
if a<1 or a>200:
|
|
|
|
error("Voice volume adjustments must be in range 1 to 200, not %s" % a)
|
|
|
|
self.table[v] = a/100.
|
|
|
|
if gbl.debug:
|
|
|
|
print "Voice Volume Adjustment: %s=%s" % (MMA.midiC.valueToInst(v), a)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def get(self, v, vol):
|
|
|
|
""" Return an adjusted value or original. """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
try:
|
2007-04-29 06:47:40 +00:00
|
|
|
vol = int(vol * self.table[v])
|
2009-05-17 22:34:44 +00:00
|
|
|
except KeyError:
|
|
|
|
return vol
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
voiceVolTable=VoiceVolTable()
|
|
|
|
|
|
|
|
class DrumVolTable:
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.table = {}
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def retlist(self):
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
l=[]
|
|
|
|
for n in sorted(self.table.keys()):
|
|
|
|
l.append("%s=%s" % ( MMA.midiC.valueToDrum(n), self.table[n]))
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
return ' '.join(l)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def set(self, ln):
|
|
|
|
""" Set a name/alias for voice volume adjustment, called from parser. """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if not ln:
|
|
|
|
self.table = {}
|
|
|
|
if gbl.debug:
|
|
|
|
print "Drum Volume Adjustment table reset."
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
return
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
for l in ln:
|
|
|
|
l=l.upper()
|
|
|
|
if l.count('=') != 1:
|
|
|
|
error("Each translation pair must be in the format Drum=Ajustment")
|
|
|
|
v,a = l.split('=')
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
v=MMA.midiC.instToValue(v)
|
|
|
|
a=stoi(a)
|
|
|
|
if a<1 or a>200:
|
|
|
|
error("Drum volume adjustments must be in range 1 to 200, not %s" % a)
|
|
|
|
self.table[v] = a/100.
|
|
|
|
if gbl.debug:
|
|
|
|
print "Drum Volume Adjustment: %s=%s" % (MMA.midiC.valueToDrum(v), a)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
def get(self, v, vol):
|
|
|
|
""" Return an adjusted value or original. """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
try:
|
2007-04-29 06:47:40 +00:00
|
|
|
vol = int(vol * self.table[v])
|
2009-05-17 22:34:44 +00:00
|
|
|
except KeyError:
|
|
|
|
return vol
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
drumVolTable=DrumVolTable()
|
|
|
|
|
|
|
|
|