VocalEasel/mma/MMA/patChord.py

422 lines
13 KiB
Python
Raw Permalink Normal View History

2006-11-10 08:07:56 +00:00
# patChord.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 random
2009-05-17 22:34:44 +00:00
import MMA.notelen
2006-11-10 08:07:56 +00:00
import gbl
from MMA.common import *
2009-05-17 22:34:44 +00:00
from MMA.pat import PC
2011-07-26 22:49:39 +00:00
import copy
2007-04-29 06:47:40 +00:00
2011-07-26 22:49:39 +00:00
class Voicing:
def __init__(self):
self.mode = None
self.range = 12
self.center = 4
self.random = 0
self.percent = 0
self.bcount = 0
self.dir = 0
2006-11-10 08:07:56 +00:00
class Chord(PC):
2007-04-29 06:47:40 +00:00
""" Pattern class for a chord track. """
vtype = 'CHORD'
2011-07-26 22:49:39 +00:00
sortDirection = 0 # used for tracking direction of strummed chords
def __init__(self, ln):
self.voicing = Voicing()
PC.__init__(self, ln)
def saveGroove(self, gname):
""" Save special/local variables for groove. """
PC.saveGroove(self, gname) # create storage. Do this 1st.
self.grooves[gname]['VMODE'] = copy.deepcopy(self.voicing)
def restoreGroove(self, gname):
""" Restore special/local/variables for groove. """
self.voicing = self.grooves[gname]['VMODE']
PC.restoreGroove(self, gname)
def clearSequence(self):
""" Set some initial values. Called from init and clear seq. """
PC.clearSequence(self)
self.voicing = Voicing()
# .direction was set in PC.clear.. we're changing to our default
self.direction = seqBump(['UP'])
2007-04-29 06:47:40 +00:00
def setVoicing(self, ln):
""" set the Voicing Mode options. Only valid for CHORDS. """
2011-07-26 22:49:39 +00:00
notopt, ln = opt2pair(ln, toupper=1)
2007-04-29 06:47:40 +00:00
2011-07-26 22:49:39 +00:00
if notopt:
error("Voicing: Each Voicing option must be a OPT=VALUE pair.")
2007-04-29 06:47:40 +00:00
2011-07-26 22:49:39 +00:00
for mode, val in ln:
2007-04-29 06:47:40 +00:00
if mode == 'MODE':
valid= ("-", "OPTIMAL", "NONE", "ROOT", "COMPRESSED", "INVERT")
if not val in valid:
error("Valid Voicing Modes are: %s" % " ".join(valid))
if val in ('-', 'NONE',"ROOT"):
val = None
if val and (max(self.invert) + max(self.compress)):
warning("Setting both VoicingMode and Invert/Compress is not a good idea")
""" When we set voicing mode we always reset this. This forces
2011-07-26 22:49:39 +00:00
the voicingmode code to restart its rotations.
2007-04-29 06:47:40 +00:00
"""
self.lastChord = []
self.voicing.mode = val
elif mode == 'RANGE':
2011-07-26 22:49:39 +00:00
val = stoi(val, "VOICING RANGE %s: Arg must be a value" % self.name)
2007-04-29 06:47:40 +00:00
if val < 1 or val > 30:
2011-07-26 22:49:39 +00:00
error("Voicing Range: Arg out-of-range; must be 1 to 30, not '%s'." % val)
2007-04-29 06:47:40 +00:00
self.voicing.range = val
elif mode == 'CENTER':
2011-07-26 22:49:39 +00:00
val = stoi(val, "Argument for %s VOICING CENTER must be a value" % self.name)
2007-04-29 06:47:40 +00:00
if val < 1 or val > 12:
2011-07-26 22:49:39 +00:00
error("VOICING CENTER: arg out-of-range; must be 1 to 12, not '%s'." % val)
2007-04-29 06:47:40 +00:00
self.voicing.center = val
elif mode == 'RMOVE':
2011-07-26 22:49:39 +00:00
val = stoi(val, "Argument for %s VOICING RANDOM must be a value" % self.name)
2007-04-29 06:47:40 +00:00
if val < 0 or val > 100:
2011-07-26 22:49:39 +00:00
error("VOICING RANDOM: arg must be 0 to 100, not %s" % val)
2007-04-29 06:47:40 +00:00
self.voicing.random = val
self.voicing.bcount = 0
elif mode == 'MOVE':
2011-07-26 22:49:39 +00:00
val = stoi(val, "Argument for %s VOICING MOVE must be a value" % self.name)
2007-04-29 06:47:40 +00:00
if val < 0 :
2011-07-26 22:49:39 +00:00
error("VOICING MOVE: bar count must >= 0, not %s" % val)
2007-04-29 06:47:40 +00:00
if val > 20:
2011-07-26 22:49:39 +00:00
warning("VOICING MOVE: bar count '%s' is quite large" % val)
2007-04-29 06:47:40 +00:00
self.voicing.bcount = val
self.voicing.random = 0
elif mode == 'DIR':
2011-07-26 22:49:39 +00:00
val = stoi(val, "Argument for %s VOICING DIR must be a value" % self.name)
2007-04-29 06:47:40 +00:00
if not val in (1,0,-1):
2011-07-26 22:49:39 +00:00
error("VOICING MOVE: Dir must be -1, 0 or 1, not '%s'." % val)
2007-04-29 06:47:40 +00:00
self.voicing.dir = val
if gbl.debug:
v=self.voicing
print "Set %s Voicing MODE=%s" % (self.name, v.mode),
print "RANGE=%s CENTER=%s" % (v.range, v.center),
print "RMOVE=%s MOVE=%s DIR=%s" % (v.random, v.bcount, v.dir)
def setDupRoot(self, ln):
""" set/unset root duplication. Only for CHORDs """
2009-05-17 22:34:44 +00:00
ln = lnExpand(ln, '%s DupRoot' % self.name)
2007-04-29 06:47:40 +00:00
tmp = []
for n in ln:
n = stoi(n, "Argument for %s DupRoot must be a value" % self.name)
if n < -9 or n > 9:
error("DupRoot %s out-of-range; must be -9 to 9" % n)
tmp.append( n * 12 )
self.dupRoot = seqBump(tmp)
if gbl.debug:
print "Set %s DupRoot to " % self.name,
printList(ln)
def getPgroup(self, ev):
""" Get group for chord pattern.
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
Tuples: [start, length, volume (,volume ...) ]
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if len(ev) < 3:
error("There must be at least 3 items in each group "
"of a chord pattern definition, not <%s>" % ' '.join(ev))
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
a = struct()
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
a.offset = self.setBarOffset(ev[0])
2009-05-17 22:34:44 +00:00
a.duration = MMA.notelen.getNoteLen(ev[1])
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
vv = ev[2:]
if len(vv)>8:
error("Only 8 volumes are permitted in Chord definition, not %s" % len(vv))
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
a.vol = [0] * 8
for i,v in enumerate(vv):
v=stoi(v, "Expecting integer in volume list for Chord definition")
a.vol[i]=v
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
for i in range(i+1,8): # force remaining volumes
a.vol[i]=v
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
return a
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
def restart(self):
self.ssvoice = -1
self.lastChord = None
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
def chordVoicing(self, chord, vMove):
""" Voicing algorithm by Alain Brenzikofer. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
sc = self.seq
vmode=self.voicing.mode
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if vmode == "OPTIMAL":
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Initialize with a voicing around centerNote
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
chord.center1(self.lastChord)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Adjust range and center
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if not (self.voicing.bcount or self.voicing.random):
chord.center2(self.voicing.center, self.voicing.range/2)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Move voicing
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif self.lastChord:
if (self.lastChord != chord.noteList ) and vMove:
chord.center2(self.voicing.center,self.voicing.range/2)
vMove = 0
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Update voicingCenter
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
sum=0
for n in chord.noteList:
sum += n
c=sum/chord.noteListLen
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
""" If using random voicing move it it's possible to
get way off the selected octave. This check ensures
that the centerpoint stays in a tight range.
Note that if using voicingMove manually (not random)
it is quite possible to move the chord centers to very
low or high keyboard positions!
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if self.voicing.random:
if c < -4: c=0
elif c >4: c=4
self.voicing.center=c
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif vmode == "COMPRESSED":
chord.compress()
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif vmode == "INVERT":
if chord.rootNote < -2:
chord.invert(1)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif chord.rootNote > 2:
chord.invert(-1)
chord.compress()
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
self.lastChord = chord.noteList[:]
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
return vMove
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
def trackBar(self, pattern, ctable):
""" Do a chord bar. Called from self.bar() """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
sc = self.seq
unify = self.unify[sc]
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
""" Set voicing move ONCE at the top of each bar.
The voicing code resets vmove to 0 the first
time it's used. That way only one movement is
done in a bar.
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
vmove = 0
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if self.voicing.random:
if random.randrange(100) <= self.voicing.random:
vmove = random.choice((-1,1))
elif self.voicing.bcount and self.voicing.dir:
vmove = self.voicing.dir
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
for p in pattern:
tb = self.getChordInPos(p.offset, ctable)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if tb.chordZ:
continue
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
self.crDupRoot = self.dupRoot[sc]
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
vmode = self.voicing.mode
vols = p.vol[0:tb.chord.noteListLen]
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Limit the chord notes. This works even if THERE IS A VOICINGMODE!
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if self.chordLimit:
tb.chord.limit(self.chordLimit)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
""" Compress chord into single octave if 'compress' is set
We do it here, before octave, transpose and invert!
Ignored if we have a VOICINGMODE.
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if self.compress[sc] and not vmode:
tb.chord.compress()
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Do the voicing stuff.
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if vmode:
vmove=self.chordVoicing(tb.chord, vmove)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Invert.
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if self.invert[sc]:
tb.chord.invert(self.invert[sc])
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
""" Voicing adjustment for 'jazz' or altered chords. If a chord (most
likely something like a M7 or flat-9 ends up with any 2 adjacent
notes separated by a single tone an unconfortable dissonance results.
This little check compares all notes in the chord and will cut the
volume of one note to reduce the disonance. Usually this will be
the root note volume being decreased.
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
nl=tb.chord.noteList
l=len(nl)
for j in range(l-1):
r = nl[j]
for i in range(j+1, l):
if nl[i] in (r-1, r+1, r-13, r+13) and vols[i] >= vols[0]:
vols[j] = vols[i]/2
break
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
loo = zip(nl, vols) # this is a note/volume array of tuples
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
""" Duplicate the root. This can be set from a DupRoot command
or by chordVoicing(). Notes:
- The volume for the added root will be the average of the chord
notes (ignoring OFF notes) divided by 2.
- If the new note (after transpose and octave adjustments
is out of MIDI range it will be ignored.
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if self.crDupRoot:
root = tb.chord.rootNote + self.crDupRoot
t = root + self.octave[sc] + gbl.transpose
if t >=0 and t < 128:
v=0
c=0
for vv in vols:
if vv:
v += vv
c += 2
v /= c
loo.append( (tb.chord.rootNote + self.crDupRoot, v))
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
# For strum we need to know the direction. Note that the direction
# is saved for the next loop (needed for alternating in BOTH).
sd = self.direction[sc]
if sd == 'BOTH':
if self.sortDirection:
self.sortDirection = 0
else:
self.sortDirection = 1
elif sd == 'DOWN':
self.sortDirection = 1
elif sd == 'RANDOM':
self.sortDirection = random.randint(0,1)
else:
self.sortDirection = 0
# take the list of notes and sort them in low to high order.
# reverse the list if direction is set.
loo.sort()
if self.sortDirection:
loo.reverse()
strumOffset = 0
for note, v in loo: # sorting low-to-high notes. Mainly for STRUM.
2007-04-29 06:47:40 +00:00
self.sendNote(
2011-07-26 22:49:39 +00:00
p.offset + strumOffset,
2007-04-29 06:47:40 +00:00
self.getDur(p.duration),
self.adjustNote(note),
2011-07-26 22:49:39 +00:00
self.adjustVolume(v, p.offset) )
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
strumOffset += self.getStrum(sc)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
tb.chord.reset() # important, other tracks chord object
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Adjust the voicingMove counter at the end of the bar
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if self.voicing.bcount:
self.voicing.bcount -= 1
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00