VocalEasel/mma/MMA/common.py

174 lines
3.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'
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
print "ERROR:%s %s" % (ln, msg)
2006-11-10 08:07:56 +00:00
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
2007-04-29 06:47:40 +00:00
if gbl.noWarn:
return
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
ln = ""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
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
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:
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
2007-04-29 06:47:40 +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
2007-04-29 06:47:40 +00:00
returns ( original sans subs, [subs, ...] )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +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