VocalEasel/mma/MMA/docs.py

328 lines
9.3 KiB
Python
Raw Normal View History

2006-11-10 08:07:56 +00:00
# docs.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
"""
import os
import time
2009-05-17 22:34:44 +00:00
import MMA.midiC
import MMA.grooves
2006-11-10 08:07:56 +00:00
import gbl
from MMA.common import *
2009-05-17 22:34:44 +00:00
2006-11-10 08:07:56 +00:00
def docDrumNames(order):
2007-04-29 06:47:40 +00:00
""" Print LaTex table of drum names. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
notenames = ['E\\flat', 'E', 'F', 'G\\flat', 'G', 'A\\flat',
'A', 'B\\flat', 'B', 'C', 'D\\flat', 'D'] * 5
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
n=zip( MMA.midiC.drumNames, range(27,len(MMA.midiC.drumNames)+27), notenames )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if order == "a":
for a,v,m in sorted(n):
print "\\insline{%s} {%s$^{%s}$}" % (a, v, m )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
else:
for a,v,m in n:
print "\\insline{%s} {%s$^{%s}$}" % (v, a, m)
2006-11-10 08:07:56 +00:00
def docCtrlNames(order):
2007-04-29 06:47:40 +00:00
""" Print LaTex table of MIDI controller names. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
n=zip( MMA.midiC.ctrlNames, range(len(MMA.midiC.ctrlNames)) )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if order == "a":
for a,v in sorted(n):
print "\\insline{%s} {%02x}" % (a, v)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
else:
for a,v in n:
print "\\insline{%02x} {%s}" % (v, a)
2006-11-10 08:07:56 +00:00
def docInstNames(order):
2007-04-29 06:47:40 +00:00
""" Print LaTex table of instrument names. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
n=zip( MMA.midiC.voiceNames, range(len(MMA.midiC.voiceNames)) )
if order == "a":
for a,v in sorted(n):
a=a.replace('&', '\&')
print "\\insline{%s} {%s}" % (a, v)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
else:
for a,v in n:
a=a.replace('&', '\&')
print "\\insline{%s} {%s}" % (v, a)
2006-11-10 08:07:56 +00:00
""" Whenever MMA encounters a DOC command, or if it defines
2009-05-17 22:34:44 +00:00
a groove with DEFGROOVE it calls the docAdd() function.
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
The saved docs are printed to stdout with the docDump() command.
This is called whenever parse() encounters an EOF.
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
Both routines are ignored if the -Dx command line option has
not been set.
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
Storage is done is in the following arrays.
2006-11-10 08:07:56 +00:00
"""
2009-05-17 22:34:44 +00:00
fname = ''
author = ""
notes = ""
defs = []
variables = []
2006-11-10 08:07:56 +00:00
def docAuthor(ln):
2007-04-29 06:47:40 +00:00
global author
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
author = ' '.join(ln)
2006-11-10 08:07:56 +00:00
def docNote(ln):
2007-04-29 06:47:40 +00:00
""" Add a doc line. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
global fname, notes
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
if not gbl.createDocs or not ln:
2007-04-29 06:47:40 +00:00
return
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Grab the arg and data, save it
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
fname = os.path.basename(gbl.inpath.fname)
if notes:
notes += ' '
notes += ' '.join(ln)
def docVars(ln):
""" Add a VARIABLE line (docs vars used in lib file)."""
global fname, variables
2009-05-17 22:34:44 +00:00
if not gbl.createDocs or not ln:
2007-04-29 06:47:40 +00:00
return
fname = os.path.basename(gbl.inpath.fname)
variables.append([ln[0], ' '.join(ln[1:]) ] )
2006-11-10 08:07:56 +00:00
def docDefine(ln):
2007-04-29 06:47:40 +00:00
""" Save a DEFGROOVE comment string.
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
Entries are stored as a list. Each item in the list is
complete groove def looking like:
defs[ [ Name, Seqsize, Description, [ [TRACK,INST]...]] ...]
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
global defs
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
l = [ ln[0], gbl.seqSize, ' '.join(ln[1:]) ]
for a in sorted(gbl.tnames.keys()):
c=gbl.tnames[a]
if c.sequence and len(c.sequence) != c.sequence.count(None):
if c.vtype=='DRUM':
v=MMA.midiC.valueToDrum(c.toneList[0])
else:
v=MMA.midiC.valueToInst(c.voice[0])
l.append( [c.name, v ] )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
defs.append(l)
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 docDump():
2007-04-29 06:47:40 +00:00
""" Print the LaTex docs. """
global fname, author, notes, defs, variables
2009-05-17 22:34:44 +00:00
if gbl.createDocs == 1: # latex docs
2007-04-29 06:47:40 +00:00
if notes:
if fname.endswith(gbl.ext):
fname='.'.join(fname.split('.')[:-1])
print "\\filehead{%s}{%s}" % (totex(fname), totex(notes))
print
if variables:
print " \\variables{"
for l in variables:
print " \\insvar{%s}{%s}" % ( totex(l[0]), totex(l[1]) )
print " }"
print
if defs:
for l in defs:
2009-05-17 22:34:44 +00:00
alias = MMA.grooves.getAlias(l[0])
if alias:
if len(alias)>1:
alias="Aliases: %s" % alias
else:
alias="Alias: %s" % alias
else:
alias=''
print " \\instable{%s}{%s}{%s}{%s}{" % \
(totex(l[0]), totex(l[2]), l[1], alias)
2007-04-29 06:47:40 +00:00
for c,v in l[3:]:
print " \\insline{%s}{%s}" % (c.title(), totex(v))
print " }"
2009-05-17 22:34:44 +00:00
if gbl.createDocs == 2: # html docs
2007-04-29 06:47:40 +00:00
if notes:
print '<!-- Auto-Generated by MMA on: %s -->' % time.ctime()
print '<HTML>'
print '<BODY BGCOLOR="#B7DFFF" Text=Black>'
if fname.endswith(gbl.ext):
fname='.'.join(fname.split('.')[:-1])
print "<H1>%s</H1>" % fname.title()
print "<P>%s" % notes
if variables:
print "<P>"
print '<Table Border=3 CELLSPACING=0 CELLPADDING=5 BGColor="#eeeeee" Width="60%">'
print ' <TR><TD>'
print ' <H2> Variables </H2> '
print ' </TD></TR>'
print ' <TR><TD>'
print ' <Table CELLSPACING=0 CELLPADDING=5 BGColor="#eeeeee" Width="100%">'
for l in variables:
print " <TR>"
print " <TD Valign=Top> <B> %s </B> </TD> " % l[0]
print " <TD Valign=Top> %s </TD>" % l[1]
print " </TR>"
print ' </Table>'
print ' </TD></TR>'
print '</Table>'
if defs:
print "<ul>"
for l in defs:
print "<LI><A Href=#%s>%s</a>" % (l[0], l[0])
print "</ul>"
for l in defs:
print '<A Name=%s></a>' % l[0]
print '<Table Border=3 CELLSPACING=0 CELLPADDING=5 BGColor="#eeeeee" Width="60%">'
print ' <TR><TD>'
print ' <H2> %s </H2> ' % l[0]
2009-05-17 22:34:44 +00:00
alias=MMA.grooves.getAlias(l[0])
if alias:
if len(alias)>1:
ll="Aliases"
else:
ll="Alias"
print ' <H4> %s: %s </H4>' % (ll, alias)
2007-04-29 06:47:40 +00:00
print ' %s <B>(%s)</B> ' % ( l[2], l[1] )
print ' </TD></TR>'
print ' <TR><TD>'
print ' <Table CELLSPACING=0 CELLPADDING=5 BGColor="#eeeeee" Width="10%">'
for c,v in l[3:]:
print " <TR><TD> %s </TD> <TD> %s </TD></TR>" % (c.title(), v)
print ' </Table>'
print ' </TD></TR>'
print '</Table>'
print
print '</Body></HTML>'
2009-05-17 22:34:44 +00:00
if gbl.createDocs == 3:
if notes:
if fname.endswith(gbl.ext):
fname='.'.join(fname.split('.')[:-1])
print "%s.mma %s" % (fname, notes)
print
if variables:
print " Variables:"
for l in variables:
print " %s %s" % ( l[0], l[1] )
print
if defs:
for l in defs:
print "Groove %s" % l[0].title()
MMA.grooves.grooveDo(l[0].upper())
for t in sorted(gbl.tnames):
tr = gbl.tnames[t]
sq = tr.sequence
if sq[0]:
rt = []
for a in range(gbl.seqSize):
s=sq[a]
x = '{' + tr.formatPattern(sq[a]) + '}'
rt.append(x)
print " %s Sequence %s" % (tr.name, ' '.join(rt))
if tr.vtype == 'DRUM':
print " %s Tone %s" % (tr.name,
' '.join([MMA.midiC.valueToDrum(a) for a in tr.toneList]))
else:
print " %s Voice %s" % (tr.name,
' '.join([MMA.midiC.voiceNames[a] for a in tr.voice]))
print
2007-04-29 06:47:40 +00:00
defs = []
variables=[]
notes = ""
author = ""
2006-11-10 08:07:56 +00:00
def totex(s):
2007-04-29 06:47:40 +00:00
""" Parse a string and quote tex stuff.
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
Also handles proper quotation style.
2007-04-29 06:47:40 +00:00
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
s = s.replace("$", "\$")
s = s.replace("*", "$*$")
s = s.replace("_", "\\_")
#s = s.replace("\\", "\\\\")
s = s.replace("#", "\\#")
s = s.replace("&", "\\&")
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
q="``"
while s.count('"'):
s=s.replace('"', q, 1)
if q=="``":
q="''"
else:
q="``"
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
return s
2009-05-17 22:34:44 +00:00
def docVerbose():
""" Print verbose pattern/sequence info: -Dp command line. """
global fname, author, notes, defs, variables
defs = []
variables=[]
notes = ""
author = ""