VocalEasel/mma/MMA/harmony.py

161 lines
4.1 KiB
Python
Raw Permalink Normal View History

2006-11-10 08:07:56 +00:00
# harmony.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
"""
from MMA.common import *
def harmonize(hmode, note, chord):
2007-04-29 06:47:40 +00:00
""" Get harmony note(s) for given chord. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
hnotes = []
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
for tp in hmode.split('+'):
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if tp in ('2', '2BELOW'):
hnotes.append( gethnote(note, chord) )
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
elif tp == '28Below':
hnotes.append( gethnote(note, chord)-12)
2007-04-29 06:47:40 +00:00
elif tp == '2ABOVE':
hnotes.append( gethnote(note, chord)+12 )
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
elif tp == '28ABOVE':
hnotes.append( gethnote(note, chord)+24 )
2007-04-29 06:47:40 +00:00
elif tp in ( '3', '3BELOW'):
a = gethnote(note, chord)
b = gethnote(a, chord)
hnotes.extend( [a, b] )
2011-07-26 22:49:39 +00:00
elif tp == '38BELOW':
a = gethnote(note, chord)
b = gethnote(a, chord)
hnotes.extend( [a-12, b-12] )
2007-04-29 06:47:40 +00:00
elif tp == '3ABOVE':
a = gethnote(note, chord)
b = gethnote(a, chord)
hnotes.extend( [a+12, b+12] )
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
elif tp == '38ABOVE':
a = gethnote(note, chord)
b = gethnote(a, chord)
hnotes.extend( [a+24, b+24] )
2007-04-29 06:47:40 +00:00
elif tp in ('OPEN', "OPENBELOW"):
a=gethnote(note, chord)
hnotes.append( gethnote(a, chord))
2011-07-26 22:49:39 +00:00
elif tp == 'OPEN8BELOW':
a = gethnote(note, chord)
hnotes.append( gethnote(a-12, chord))
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif tp == 'OPENABOVE':
a=gethnote(note, chord)
hnotes.append( gethnote(a, chord) + 12 )
2011-07-26 22:49:39 +00:00
elif tp == 'OPEN8ABOVE':
a=gethnote(note, chord)
hnotes.append( gethnote(a, chord) + 24 )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif tp in ('8', '8BELOW'):
hnotes.append( note - 12 )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif tp == '8ABOVE':
hnotes.append( note + 12 )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif tp in ('16', '16BELOW'):
hnotes.append( note - (2 * 12) )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif tp == '16ABOVE':
hnotes.append( note + (2 * 12) )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif tp in ('24', '24BELOW'):
hnotes.append( note - (3 * 12) )
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif tp == '24ABOVE':
hnotes.append( note + (3 * 12) )
else:
error("Unknown harmony type '%s'" % tp)
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
""" Strip out duplicate notes from harmony list.
Cute trick here ... using set().
2007-04-29 06:47:40 +00:00
"""
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
return list(set(hnotes))
2006-11-10 08:07:56 +00:00
def gethnote(note, chord):
2007-04-29 06:47:40 +00:00
""" Determine harmony notes for a note based on the chord.
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
note - midi value of the note
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
chord - list of midi values for the chord
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
This routine works by creating a chord list with all
its notes having a value less than the note (remember, this
is all MIDI values). We then grab notes from the end of
the chord until one is found which is less than the original
note.
2007-04-29 06:47:40 +00:00
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
wm="No harmony note found since no chord, using note " + \
"0 which will sound bad"
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if not chord: # should never happen!
warning(wm)
return 0
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
ch = list(chord) # copy chord and sort
ch.sort()
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# ensure that the note is in the chord
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
while ch[-1] < note:
for i,n in enumerate(ch):
ch[i]+=12
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
while ch[0] >= note:
for i,v in enumerate(ch):
ch[i]-=12
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# get one lower than the note
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
while 1:
if not ch: # this probably can't happen
warning(wm)
return 0
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
h=ch.pop()
if h<note: break
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
return h
2006-11-10 08:07:56 +00:00