VocalEasel/mma/MMA/patch.py

160 lines
4.1 KiB
Python
Raw Normal View History

2009-05-17 22:34:44 +00:00
# patch.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
Bob van der Poel <bob@mellowood.ca>
This module contains the various patch manger routines.
"""
from MMA.common import *
from MMA.miditables import *
import MMA.file
import MMA.midiC
def patch(ln):
""" Main routine to manage midi patch names. """
for i,a in enumerate(ln):
2011-07-26 22:49:39 +00:00
2009-05-17 22:34:44 +00:00
if a.count('=') == 1:
a,b = a.split('=')
else:
b=''
a=a.upper()
if a == "LIST":
b=b.upper()
if b == "ALL":
plistall()
elif b == "EXT":
plistext()
elif b == "GM":
plistgm()
else:
error("Expecting All, EXT or GM argument for List")
2011-07-26 22:49:39 +00:00
elif a == "RENAME":
2009-05-17 22:34:44 +00:00
prename(ln[i+1:])
break
2011-07-26 22:49:39 +00:00
elif a == 'SET':
2009-05-17 22:34:44 +00:00
patchset(ln[i+1:])
break
2011-07-26 22:49:39 +00:00
else:
error("Unknown option for Patch: %s" % a)
2009-05-17 22:34:44 +00:00
# Set a patch value=name
def patchset(ln):
if not ln:
error("Patch Set expecting list of value pairs.")
2011-07-26 22:49:39 +00:00
notopt, ln = opt2pair(ln, 1)
if notopt:
error("PATCH SET: All options must be VALUE=PAIR items.")
for v,n in ln:
2009-05-17 22:34:44 +00:00
v=v.split('.')
if len(v) > 3 or len(v) < 1:
error("Patch Set: Expecting a voice value Prog.MSB.LSB." )
2011-07-26 22:49:39 +00:00
# Parse out value. Can be 'nn', 'nn.nn' or 'nn.nn.nn'
# the parts are for MSB(ctrl32), LSB(ctrl0), and Patch
2009-05-17 22:34:44 +00:00
voc = 0
if len(v) > 2: # ctrl32
i = stoi(v[2], "Patch Set LSB expecting integer.")
if i<0 or i>127:
error("LSB must be 0..127, not '%s'." % i)
voc = i << 16
if len(v) > 1: # ctrl0
i = stoi(v[1], "Patch Set MSB expecting integer.")
if i<0 or i>127:
error("MSB must be 0..127, not '%s'." % i)
voc += i << 8
i = stoi(v[0], "Patch Set Voice expecting integer.")
if i<0 or i>127:
error("Program must be 0..127, not '%s'." % i)
voc += i
2011-07-26 22:49:39 +00:00
# Handle the name.
2009-05-17 22:34:44 +00:00
if voc in voiceNames:
warning("Patch Set duplicating voice name %s with %s=%s" % \
(voiceNames[voc], n, MMA.midiC.extVocStr(voc) ))
2011-07-26 22:49:39 +00:00
if n in voiceInx:
2009-05-17 22:34:44 +00:00
warning("Patch Set duplicating voice value %s with %s=%s" % \
2011-07-26 22:49:39 +00:00
(MMA.midiC.extVocStr(voiceInx[n]), MMA.midiC.extVocStr(voc), n) )
2009-05-17 22:34:44 +00:00
voiceNames[voc]=n
2011-07-26 22:49:39 +00:00
voiceInx[n]=voc
2009-05-17 22:34:44 +00:00
# Rename
def prename(ln):
if not ln:
error("Patch Rename expecting list of value pairs.")
2011-07-26 22:49:39 +00:00
notopt, ln = opt2pair(ln, 1)
2009-05-17 22:34:44 +00:00
2011-07-26 22:49:39 +00:00
if notopt:
error("PATCH RENAME: expecting OLDNAME=NEWNAME pairs.")
2009-05-17 22:34:44 +00:00
2011-07-26 22:49:39 +00:00
for a, b in ln:
if not a in voiceInx:
error("PATCH RENAME: oldname '%s' doen't exist, can't be renamed." % a)
2009-05-17 22:34:44 +00:00
2011-07-26 22:49:39 +00:00
if b in voiceInx:
error("PATCH RENAME: newname '%s' already exists." % b)
2009-05-17 22:34:44 +00:00
2011-07-26 22:49:39 +00:00
v = voiceInx[a]
2009-05-17 22:34:44 +00:00
voiceNames[v]=b
2011-07-26 22:49:39 +00:00
del voiceInx[a]
voiceInx[b]=v
2009-05-17 22:34:44 +00:00
# list funcs
def plistgm():
2011-07-26 22:49:39 +00:00
""" List GM voices. """
2009-05-17 22:34:44 +00:00
for v in sorted(voiceNames.keys()):
if v <= 127:
print "%s=%s" % (MMA.midiC.extVocStr(v), voiceNames[v] )
def plistext():
2011-07-26 22:49:39 +00:00
""" List extended voices. """
2009-05-17 22:34:44 +00:00
for v in sorted(voiceNames.keys()):
if v>127:
print "%s=%s" % (MMA.midiC.extVocStr(v), voiceNames[v])
2011-07-26 22:49:39 +00:00
def plistall():
""" List all voices. """
plistgm()
plistext()