VocalEasel/mma/MMA/common.py

213 lines
4.7 KiB
Python
Raw Normal View History

2006-11-10 08:07:56 +00:00
# common.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
These are a collection of miscellaneous routines used in various
parts of MMA. It is safe to load the whole works with:
2007-04-29 06:47:40 +00:00
from MMA.common import *
2006-11-10 08:07:56 +00:00
without side effects (yeah, right).
"""
from random import randrange
import sys
import gbl
class struct:
2007-04-29 06:47:40 +00:00
pass
2006-11-10 08:07:56 +00:00
def error(msg):
2007-04-29 06:47:40 +00:00
""" Print an error message and exit.
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
If the global line number is >=0 then print the line number
as well.
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
ln = ""
if gbl.lineno >= 0:
ln += "<Line %d>" % gbl.lineno
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if gbl.inpath:
ln += "<File:%s>" % gbl.inpath.fname
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if ln:
ln += '\n'
2009-05-17 22:34:44 +00:00
2007-04-29 06:47:40 +00:00
print "ERROR:%s %s" % (ln, msg)
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
# Parse though the error message and check for illegal characters.
# Report (first only) if any found.
for a in msg:
a=ord(a)
if a<0x20 or a >=0x80:
print "Corrupt input file? Illegal character 0x%2x found." % a
break
2007-04-29 06:47:40 +00:00
sys.exit(1)
2006-11-10 08:07:56 +00:00
def warning(msg):
2007-04-29 06:47:40 +00:00
""" Print warning message and return. """
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
if not gbl.noWarn:
ln = ""
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
if gbl.lineno >= 0:
ln = "<Line %d>" % gbl.lineno
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
if gbl.inpath:
ln += "<File:%s>" % gbl.inpath.fname
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
print "Warning:%s\n %s" % (ln, msg)
2006-11-10 08:07:56 +00:00
def getOffset(ticks, ran=None):
2007-04-29 06:47:40 +00:00
""" Calculate a midi offset into a song.
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
ticks == offset into the current bar.
ran == random adjustment from RTIME
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
When calculating the random factor the test ensures
that a note never starts before the start of the bar.
This is important ... voice changes, etc. will be
buggered if we put the voice change after the first
note-on event.
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
p = gbl.tickOffset + int(ticks) # int() cast is important!
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if ran:
r = randrange( -ran, ran+1 )
if ticks == 0 and r < 0:
r=0
p+=r
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
return p
2006-11-10 08:07:56 +00:00
def stoi(s, errmsg=None):
2007-04-29 06:47:40 +00:00
""" string to integer. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
try:
return int(s, 0)
except:
if errmsg:
error(errmsg)
else:
error("Expecting integer value, not %s" % s)
2006-11-10 08:07:56 +00:00
def stof(s, errmsg=None):
2007-04-29 06:47:40 +00:00
""" String to floating point. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
try:
return float(s)
except:
2009-05-17 22:34:44 +00:00
try:
return int(s,0)
except:
if errmsg:
error(errmsg)
else:
error("Expecting a value, not %s" % s)
2006-11-10 08:07:56 +00:00
def printList(l):
2007-04-29 06:47:40 +00:00
""" Print each item in a list. Works for numeric and string."""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
for a in l:
print a,
print
2006-11-10 08:07:56 +00:00
def pextract(s, open, close, onlyone=None):
2007-04-29 06:47:40 +00:00
""" Extract a parenthesized set of substrings.
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
s - original string
open - substring start tag \ can be multiple character
close - substring end tag / strings (ie. "<<" or "-->")
onlyone - optional, if set only the first set is extracted
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
returns ( original sans subs, [subs, ...] )
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
eg: pextract( "x{123}{666}y", '{', '}' )
Returns: ( 'xy', [ '123', '666' ] )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
subs =[]
while 1:
lstart = s.find(open)
lend = s.find(close)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if lstart>-1 and lstart < lend:
subs.append( s[lstart+len(open):lend].strip() )
s = s[:lstart] + s[lend+len(close):]
if onlyone:
break
else:
break
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
return s.strip(), subs
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
def seqBump(l):
""" Expand/contract an existing sequence list to the current seqSize."""
while len(l) < gbl.seqSize:
l += l
return l[:gbl.seqSize]
def lnExpand(ln, msg):
""" Validate and expand a list passed to a set command. """
if len(ln) > gbl.seqSize:
warning("%s list truncated to %s patterns" % (msg, gbl.seqSize) )
ln = ln[:gbl.seqSize]
last = None
for i,n in enumerate(ln):
if n == '/':
if not last:
error ("%s cannot use a '/' as the first item in list." % cmd)
else:
ln[i] = last
else:
last = n
return ln