2006-11-10 08:07:56 +00:00
|
|
|
# midiC.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
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
This module contains interface for MIDI constants and conversion routines.
|
2006-11-10 08:07:56 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from MMA.common import *
|
2007-04-29 06:47:40 +00:00
|
|
|
from MMA.miditables import *
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
def voice2tup(x):
|
|
|
|
""" Convert integer into 3 byte tuple: Voice, LSB, MSB. """
|
|
|
|
|
|
|
|
if x>0xffff:
|
|
|
|
msb = x >> 16
|
|
|
|
x &= 0xffff
|
|
|
|
else:
|
|
|
|
msb = 0
|
|
|
|
|
|
|
|
if x>0xff:
|
|
|
|
lsb = x >> 8
|
|
|
|
x &= 0xff
|
|
|
|
else:
|
|
|
|
lsb = 0
|
|
|
|
|
|
|
|
return (x, lsb, msb)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
def extVocStr(v):
|
|
|
|
|
|
|
|
v = "%s.%s.%s" % voice2tup(v)
|
|
|
|
if v[-2:] == '.0':
|
|
|
|
v=v[:-2]
|
|
|
|
if v[-2:] == '.0':
|
|
|
|
v=v[:-2]
|
2007-04-29 06:47:40 +00:00
|
|
|
return v
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
###############################
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
def drumToValue(name):
|
|
|
|
""" Get the value of the drum tone (-1==error).
|
|
|
|
Note that this is quite different from instToValue() ... in that
|
|
|
|
case the caller does a bunch of validation stuff for controllers, etc.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try: # assuming that 'name' is an integer
|
|
|
|
i = int(name, 0)
|
|
|
|
if i>=0 and i<=127:
|
|
|
|
return int(name)
|
|
|
|
else:
|
|
|
|
return -1
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
return drumInx[ name.upper() ]
|
|
|
|
except KeyError:
|
|
|
|
return -1
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
|
|
|
|
def valueToDrum(val):
|
|
|
|
""" Get the name of the drum tone. """
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
try:
|
2009-05-17 22:34:44 +00:00
|
|
|
return drumNames[val]
|
|
|
|
except KeyError:
|
|
|
|
return str(val)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
def instToValue(name):
|
|
|
|
""" Get the value of the instrument name (-1==error). """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
try:
|
2009-05-17 22:34:44 +00:00
|
|
|
return voiceInx[name.upper()]
|
|
|
|
except KeyError:
|
2007-04-29 06:47:40 +00:00
|
|
|
return -1
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
|
2006-11-10 08:07:56 +00:00
|
|
|
def valueToInst(val):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Get the name of the inst. (or 'ERR'). """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
try:
|
2009-05-17 22:34:44 +00:00
|
|
|
return voiceNames[val]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
int(val)
|
|
|
|
except:
|
|
|
|
return "ERROR"
|
|
|
|
return extVocStr(val)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
def ctrlToValue(name):
|
|
|
|
""" Get the value of the controler name (-1==error). """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2009-05-17 22:34:44 +00:00
|
|
|
try:
|
|
|
|
return ctrlInx[name.upper()]
|
|
|
|
except keyError:
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
def valueToCtrl(val):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Get the name of the controller (or 'ERR'). """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
try:
|
2009-05-17 22:34:44 +00:00
|
|
|
return ctrlNames[val]
|
|
|
|
except KeyError:
|
2007-04-29 06:47:40 +00:00
|
|
|
return "ERROR"
|