VocalEasel/mma/MMA/translate.py

290 lines
7.0 KiB
Python
Raw Permalink 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 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:
2011-07-26 22:49:39 +00:00
print "Voice Translation table reset."
2007-04-29 06:47:40 +00:00
return
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
ln, opts = opt2pair(ln, toupper=1)
if ln:
error("VOICETR: Each translation pair must be in the format Alias=Voice.")
for v, a in opts:
2007-04-29 06:47:40 +00:00
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: ",
2011-07-26 22:49:39 +00:00
for v, a in opts:
print "%s=%s" % (v,a),
2007-04-29 06:47:40 +00:00
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:
2011-07-26 22:49:39 +00:00
print "DrumTone Translation table reset."
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
return
2011-07-26 22:49:39 +00:00
ln, opts = opt2pair(ln, 1)
if ln:
error("TONETR: Each translation pair must be in the format Tone=NewTone.")
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
for v, a in opts:
2009-05-17 22:34:44 +00:00
v1=MMA.midiC.drumToValue(v)
if v1<0:
2011-07-26 22:49:39 +00:00
error("TONETR: Tone '%s' not defined." % v)
2009-05-17 22:34:44 +00:00
a1=MMA.midiC.drumToValue(a)
if a1<0:
2011-07-26 22:49:39 +00:00
error("TONETR: 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
2011-07-26 22:49:39 +00:00
if gbl.debug:
print "TONETR Translations:",
for v, a in opts:
print "%s=%s" % (MMA.midiC.valueToDrum(v), MMA.midiC.valueToDrum(a)),
print
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
2011-07-26 22:49:39 +00:00
ln, opts = opt2pair(ln)
if ln:
error("VOICEVOLTR: Expecting VOICE=VOLUME pairs.")
for v, a in opts:
val=MMA.midiC.instToValue(v)
if val<0:
error("VOICEVOLTR: unknown voice '%s'." % v)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
a=stoi(a)
if a<1 or a>200:
2011-07-26 22:49:39 +00:00
error("VOICEVOLTR: adjustments must be in range 1 to 200, not '%s'." % a)
self.table[val] = a/100.
if gbl.debug:
print "VOICEVOLTR: ",
for v, a in opts:
print "%s=%s" % (v.upper(), a),
print
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:
2011-07-26 22:49:39 +00:00
return vol * self.table[v]
except:
2009-05-17 22:34:44 +00:00
return vol
2011-07-26 22:49:39 +00:00
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):
2011-07-26 22:49:39 +00:00
""" Set a name/alias for drumtone 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:
2011-07-26 22:49:39 +00:00
print "DRUMVOLTR: 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
2011-07-26 22:49:39 +00:00
ln, opt = opt2pair(ln, 1)
if ln:
error("DRUMVOLTR: Each option must be in the format TONE=AJUSTMENT.")
for v, a in opt:
val = MMA.midiC.drumToValue(v)
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
if val < 0:
error("DRUMVOLTR: Unknown tone '%s'." % v)
a = stoi(a)
2007-04-29 06:47:40 +00:00
if a<1 or a>200:
2011-07-26 22:49:39 +00:00
error("DRUMVOLTR: adjustments must be in range 1 to 200, not '%s'." % a)
self.table[val] = a/100
if gbl.debug:
print "DRUMVOLTR: Adjustments",
for v, a in opt:
print "%s=%s" % (MMA.midiC.valueToDrum(val), a),
print
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:
2011-07-26 22:49:39 +00:00
return vol * self.table[v]
except: # not the best, but any errors just return the orignal volume.
2009-05-17 22:34:44 +00:00
return vol
2006-11-10 08:07:56 +00:00
drumVolTable=DrumVolTable()