VocalEasel/mma/MMA/translate.py

257 lines
6.3 KiB
Python
Raw Normal View History

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 gbl
import MMA.midiC
from MMA.common import *
""" 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()
if self.table.has_key(name):
return self.table[name]
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
else:
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
2007-04-29 06:47:40 +00:00
v=MMA.midiC.drumToValue(v)
a=MMA.midiC.drumToValue(a)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
self.table[v] = a
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):
""" Return a translation or original. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
v=MMA.midiC.drumToValue(name)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if self.table.has_key(v):
return self.table[v]
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
else:
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
2007-04-29 06:47:40 +00:00
if self.table.has_key(v):
vol = int(vol * self.table[v])
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
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
2007-04-29 06:47:40 +00:00
if self.table.has_key(v):
vol = int(vol * self.table[v])
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
return vol
2006-11-10 08:07:56 +00:00
drumVolTable=DrumVolTable()