Details of harmony analysis for VAMP, a program to make MIDI renditions
            of traditional tunes for contra dance music.
----------------------------
Harmony analysis, expanded syntax:
                                
harmony [?] [details,nodetails] [log] [interactive] 
        [guitar] [#number#] 
        [restrict] [all,accented,bounding] [measure,half] 
        [center] [phrase,entire] 
        [statistics] 
                                
guide to keywords
weights and preferences
example(guitar)
example(old scheme)

One problem with traditional melodies is that they usually do not have 
any chords specified.  These harmony analysis tools attempt to provide 
some guidance in the assignment of chords (and achieve something like 
70 to 80% agreement with human selections), but the ultimate test is 
musical acceptability.

The key or mode of a piece, along with the actual notes determines which
chords are "best".  The key signature will indicate the major key,
but the tune may be in minor or in one of several modes (Dorian,
Mixolydian).  (This is usually indicated by the relation of the
final tone to the key signature.)  The command "harmony center"
guesses the tonal center by using a consonance/dissonance 
measure of intervals due to Malmberg (1918) [cf. Harry F. Olson,
"Music, Physics and Engineering", Dover, p.260]  Tests of the
"center" command suggest that it is most likely to pick the
dominant (fifth) note of the scale, although sometimes it gets 
the tonic.

As a result of collaboration with Matt Kaufmann, detailed rules
for chord preferences have been implemented. 

Our most successful chord selection heuristics have used both
a weighted sum of the durations of tones in a measure (to pick
the best match between possible chords) and a set of chord
preference scores to break ties.  A bewildering variety of
weighting schemes and preferences can be specifed (see below
for details), but the selections are surprisingly independent
of which particular weights and preferences are used.

The older chord analysis is based on two rules: the fraction of time 
the notes of the measure [or other time interval] spend on the notes 
of the chord, and whether the first and last notes of the "measure" are 
in the chord ("bounding" option,  thanks to Roy Wilhite).
Chords are ranked from best to worst matching.  Other heuristics
may be tried in the future (e.g. accented notes as suggested by
Chris Smith in "Celtic Back-Up for all Instrumentalists", Mel Bay, 1999).

                                       
----  KEYWORDS ----------------------------

Harmony keywords (options) may be abbreviated to a non-ambiguous short 
string (one letter for everything but "all" and "accented" or the prefix "no"); 
an ambiguous option (e.g. "a") will be flagged as an error and ignored.


The default settings correspond to:
                                
harmony guitar nodetails nolog 15
                                
Note that the number "15" is the analysis method, specifying the preference
schema (1x) and the tone weighting (5).  See weights and preferences below.

Most keywords set defaults for analysis, which need only be restated if you 
wish to change them. With the defaults as above, only the word "harmony" need 
be given as a command.

"harmony guitar nodetails nolog 15" can be abbreviated to "h g nod nol 15".  


"details" turns on a verbose mode for more output; "d d" will get even 
more details.  "?" prints out a list of the available options.  
"log" records the output to the active log file, if one was specified
when the program was started.

The prefix "no" will turn off an option.  "nodetails" turns off
verbose mode, "nolog" turns off logging, "noguitar" switches
to the older chord heuristics ("restrict, all, noaccented, bounding").

#number# designates an integer.  In "guitar" mode, the integer is
the sum of the chord class and the weight scheme; a negative number
modifies the summary chord printout. In "noguitar" mode, the integer gives
the number of ticks for the analysis (see "measure, half, #number#" below).

"guitar" mode accesses the newer algorithms, utilizing both a
preference heuristic and weight scheme as specified by a number.
    (See weights and preferences below.)
If ABC guitar chords are specfied, their scores are also given.
Both the full and half measures are scored, as seen in the example.  
If you desire to print only the best full measure chords
in the summary printout (e.g. in a waltz), you give the class+weight
method as negative (e.g. -15 instead of 15 above).


"all, accented, bounding" all control an older type of chord 
analysis invoked under the condition "noguitar".
"restrict" considers only 6 possible chords (1 5 4 6m 3m 2m) based
on the major key indicated by the key signature, "norestrict"
widens the scoring to all 24 possible major and minor chords.

"measure, half, #number#"  specify the number of notes used as a
complete measure, half a measure or a number of midi ticks.
The half measure scoring can be confused if a single note bridges
the boundary ( e.g. | AC2E | rather than |AC CE| or | AC-CE| ).

If no number is given, it uses measure delimiters.  Otherwise one specifies
the number of ticks to span (useful for half measures).  Will currently
[032.10] terminate at a bar or a guitar chord. 

"center" guesses at the tonal center, for the "entire" piece and
for the two parts (strains) A and B, assuming a standard AABB structure.
"phrase" is intended to give a shorter span, but is not implemented.

"interactive" allows detailed analysis of specific measures.
(one specifies individual measure numbers, and exits by giving 
a carriage return). 

"statistics" gives counts of the tonic-relative semitones and 
semitone intervals for all the notes in the piece.  It also prints
the weights for a C major chord and the chord preference scores for
the current option number.  For example, "harm stat 15" will give weights 
of type 5 for heuristic class 1x.

                                      
-------  WEIGHTS and PREFERENCES  -----------------------

Info on weighting schemes and chord preferences:

All notes are reduced to the 12 semitones of a single octave whose
durations are multiplied by weights which reward notes in a chord
and penalize nearby notes (e.g. those a half tone away).  For notes
in a major triad, the weights for the ten options 0, 1, ... , 9 are:

wtype do      re      mi  fa     sol      la      ti    (diatonic scale)

 2     1   0   0   0   1   0   0   1   0   0   0   0
 3     1  -1   0  -1   1  -1  -1   1  -1   0   0  -1
 4     4  -2   0  -2   2  -2  -2   2  -2   0   0  -1
 5     1  -1   0  -1   1   0  -1   1  -1   0   0   0
 6     1  -1   0  -1   1  -1  -1   1  -1   0   0  -0.5
 7     1  -1   0  -1   1   0  -1   1  -1   0   0   0

 
   
Scheme 2 has no penalties and is similar to the older "noguitar norestrict" 
option.  Schemes 3, 5, and 7 penalize tones a half tone away from the notes
in the chord. Scheme 5 does not penalize half tones (fa, ti) in the
scale determined by the key signature; scheme 7 determines the
scale relative to the root of the particular chord being considered. 
Schemes 4 and 6 are attempts to add additional weight to the root note 
of a chord, but are less successful than heuristic 5x below.

Heuristic classes of chord preference scores used to break ties
for equal raw scores from the weight sums (for a major mode):

class   1  5  4  6m  3m  2m  2  3  6  7*   (chord 1==I, 5==V, 6m==vi, etc.)
 0x     0  0  0  0   0   0   0  0  0  0
 1x     6  5  4  3   1   2   2  2  3  1
 2x     6  5  4  3   3   2   0  0  0  1
 3x     9  8  7  5   3   6   5  4  4  1
 4x     9  8  7  5   3   6   5  4  4  1   [see comments below on 4x]   
 5x     9  8  7  5   3   6   5  4  4  1
 6x     9  8  7  5   3   6   5  4  4  1

The scores will be modified for minor mode by switching 1<->6m etc.
(e.g.   1m 5m 4m 6  3    2   2m 3m 6m 7*
 3x     9  8  7  5  3    6   5  4  4  1    )

The x in the units place is to be replaced by the desired weight scheme.

Class 0x relies strictly on the weight sum and characteristically
has several chords (some of which are not key related) with the same
score (use " interactive d d" to see all 24 chord scores).
Class 1x is our first attempt at preferences and was similar to 
the older analysis, but extended beyond the six basic chords in a
key (1 5 4 6m 3m 2m).  Class 2x attempts to do a better job with
various modes (minor, dorian, mixolydian).  Classes 3x through 6x 
include more nonstandard chords with lower scores.  The scores
in 4x are modified by the tonal content of the measure, but
have not proved very useful.  Class 5x looks for the tone of longest 
duration in a measure and considers that for root of the chord to 
select if the chord otherwise has a high weighted duration sum.
Class 6x uses a "jump heuristic" suggested by Lee Couch.  Both 5x 
and 6x are still under evaluation.
* The treatment of seventh chords is incomplete and partly incorrect.

In my experience, options 15, 25, 35, 22 all work reasonably
well, along with 55 for checking root preferences.

                                        
----  Example, Guitar chords compared with full and half measure assignments -----

Here is an example with the first 8 measures from Petronella, with full measure
chords found in a previous analyis (harm all bound) supplied as "guitar chords".

Eight bars from the melody in ABC (supplied guitar chords are given within 
double quotes):

    |: "G"B2dB A2dB  | "G"G2G2G2 B,D  | "C"G2CE A2G2  |   "D"FAGE D2gd  |
       "G"B2dB A2dB  | "G"G2G2G2 B,D  | "C"G2Ce A2F2  |[1 "G"G2G2 G2gd :|
 
Part of the result from "harmony detail detail 15"

 1.    G   186  |  G   126  D    95 ..  + G      186 126
 2.    G   246  |  G   126  G   126 ..    G      246 126
 3.    C   184  |  C   124  G    66 ..  * C      184 124
 4-    G   156  |  D    65  G   126 ..  + D      155  65
 5.    G   186  |  G   126  D    95 ..  + G      186 126
 6.    G   246  |  G   126  G   126 ..    G      246 126
 7-    D   125  |  C   124  D   125 ..  + C      124 124
 8.    G   246  |  G   126  G   126 ..    G      246 126
 ....

Format: measure number, flag1, flag2, full measure chord and score "|"
        half measure chords and scores ".." flag3, guitar chords,
        guitar chord scores (full, half_1, half_2)
             ( half_2 appears if two guitar chords are supplied in one measure)

The scores are the sum of the chord weighted duration (ChWtTi) and the key/mode
dependent preferences (Pref) noted above.  Petronella is in the key of G,
and with the default 120 MIDI ticks per quarter note, each 1/16 note
has 30 ticks, for 240 ticks in a measure.  Consider measure 4  ("D"FAGE D2gd ).
The notes are reduced to a standard octave, giving the following durations for 
each semitone:

        fa     sol      la      ti  do      re      mi
         C       D       E      ^F   G       A       B
         0   0  90   0  30   0  30  60   0  30   0   0

The weights (given above in scheme 5 for a C chord) are rotated for different
chord roots. Note that the adjacent half note penalty is 0 for notes in the G 
scale.  The chord preference score for class 1x is given on the right.
                                                             Pref
  G      0  -1   1  -1   0   0   0   1  -1   0  -1   1         6
  D      0  -1   1  -1   0  -1   1   0  -1   1  -1   0         5
  C      1  -1   0  -1   1  -1   0   1  -1   0   0   0         4

Multiplying the durations by the weights and summing gives these ChWtTi's
for G (150), D(150), and C(90).  It is only the added preference which
distinguishes the scores for G (156) and D (155).  Thus method 15 selects
the tonic chord G over the dominant D for the full measure, whereas 
method 55 would prefer D because the duration of D (90) is greater than
that of G (60). Repeating this analysis for the half measures (FAGE and D2gd)
says that using different chords for each half measure (D G) gives a higher
ChWtTi sum (180) than the ChWtTi for G in the full measure.

    A distinction is made between the printed "score" == ChWtTi + Pref 
    and the ChWtTi which does not depend on the preference.  The ChWtTi is a 
    weighted sum of the durations of the notes which appear in the chord 
    and penalties for notes only a half step away.  The chord preference
    is a tie-breaking increment based on the key so that chords are
    chosen in the order tonic(1), dominant(5), subdominant(4), ...
    if they have the same ChWtTi. 

 flag1:  "."    full or half measure assignments agree with those for supplied chords
              If one guitar chord was supplied:
         "-"    ChWtTi's same for assigned and supplied chords
         ">"   ChWtTi for assigned chord > (at least not == ) that for supplied chord
              If two guitar chords were supplied:
         "~"   ChWtTi same, preference weighting makes choice different
         "#"   more complicated differences
 
 flag2:  " "   all notes in measure are in designated key
         "@"   one or more notes are accidentals.

Thus the measures with flag1 other than "." are of specific interest.
 
 flag3:  " "   both half measure assignments identical to full measure assignment
         "u"   half measure assignments not identical, 
                   (sum of half measure ChWtTi <= full measure ChWtTi )
         "+"   sum of half measure ChWtTi > full measure ChWtTi 
                  (indicating that this measure may do better with two chords)
 
Some summary statistics on how well the assigned chords agree with the supplied chords

28 match,  18 identical,   0 ambiguous,  15 method

For the full 32 measures of Petronella, 28 of them had either the same assigned
chord as the supplied guitar chord, or at least one of the two half measure
"improved" assignments agreed with the guitar chord.  In 18 measures, the
half measure assignments were identical to the full measure. "Ambiguous"
counts the number of instances for which there is another chord with the
same total score as the assigned chord.  This arises most often when
chord preference increment is small or zero, and can be investigated by 
using the interactive option to see all the chord scores for specific measures.
Ambiguous chords will be flagged with an "=" following the chord score
with "detail" or "detail detail" verbosity.

Predicted chords for all measures (GD means G and D played for a half measure each)

  GD    G     C     DG    GD    G     CD    G
  GD    G     C     DG    GD    G     CD    G
  G    AmD    D     G     G    AmD    D     G
  G    AmD    D     G     G    AmD    D     G
 
A command with option 0 will print the supplied guitar chords;
                                
harmony norestrict guitar 0
                                

Guitar chords
key:  G 7  center  G 7
  G     G     C     D     G     G     C     G
  G     G     C     D     G     G     C     G
  G    Am     D     G     G    Am     D     G
  G    Am     D     G     G    Am     D     G
 
Note on detail levels:  "nodetail" just gives the
short 4 line 32 chord summaries.  "detail" also prints the
measure lines for which flag1 is not ".". "d d" prints
information for all measures, one line per measure as above.

The half measure scoring can be confused if a single note bridges
the boundary ( e.g. | AC2E | rather than |AC CE| or | AC-CE| ).
This can happen in waltzes (where a half measure analysis probably 
shouldn't be used anyhow) or in the final measure of a phrase
where lead-in notes to the next phrase may diverge from the cadence.

                                       
--------Example, original method ----------------
(This is included for historical reference and may require 
 reading the code for full understanding. )

The older [August 2003] default settings correspond to:
                                
harmony all bounding measure restrict noguitar nocenter nodetails nolog
                                
which prints the "best" chord for each measure based on all notes 
(and the first and last notes used as a tie breaker).  Except for the 
"bounding" test, this is roughly equivalent to method 12 above.

Any guitar chords supplied in the ABC will appear at the end of the 
line in brackets. The terse [nodetail] option gives the score for the 
top two chords and the ranking for six chords (i j t u v w,  a single
letter code for the usual notation I II III IV V VI: i u v are
the major tonic, subdominant and dominant chords, while w j t
are the related minor tonic, subdominant and dominant chords).
The detail option gives the total durations of the the relative
semitones [do di re ri mi ... ti] in the analysis interval and a 
second line gives the scores for all six chords along with 
the best chord and any guitar chords in brackets [].


example, key of A major, default note length 1/16 [30 ticks]

 measure 3: |       c2B2B2 cB |
 measure 4: |  "A7" AGAB A2CD |

terse [nodetail] output:

 measure   3 [ 1501  1501]    v j i w t u   E
 measure   4 [ 1503  1502]    u i w j v t   D [A7]

[ 1501, etc. is obscure -- Rosie; 
  SMS: maybe I should junk "iuvwjt" notation and old method, or revise output]

detailed output:

    0   0 150   0  90   0   0   0   0   0   0   0  measure 3 duration 240
 i   90 2, u    0 0, v  150 1, w   90 2, j  150 1, t   90 2, v j i w t u   E
  120   0  30   0  30  30   0   0   0   0   0  30  measure 4 duration 240
 i  150 2, u  150 3, v   60 0, w  150 2, j   60 1, t   60 0, u i w j v t   D [A7]

The sequence "i  150 2" means that for the tonic chord (A), the sum of the
durations is 150 and the bounding score is 2; this is less than the bounding
score of 3 for "u  150 3".  The bounding score is 2 if the first note of the
measure is in the chord, 1 if the last note is included, and 3 if both are.

                    contents
                    top

    Copyright 2002-2005, Stanley M. Swanson.
      12 Sept. 2002; revised 24 Dec. 2002, 10 Feb. 2003, 17 May 2003,
                     31 July 2003, 8 Aug 2003, 1 Mar. 2004, 17 Apr. 2004,
		     10 May 2004, 10 June 2004, 14 July 2004, 24 Feb 2005

    This information is distributed under the terms of the 
    GNU Free Documentation License (with no invariant sections and no
    cover requirements).  The accompanying program, VAMP,
    is distributed under the terms of the GNU General Public License,
    version 2 or later (GPL v2).
    NO WARANTEES are made for either the documentation or the software.
    For the full text of the licenses, refer to the Free Software Foundation,
     GNU licenses