com.soundhelix.misc
Class Chord

java.lang.Object
  extended by com.soundhelix.misc.Chord

public class Chord
extends Object

Defines a chord. A chord is immutable and consists of 3 different pitches, which cannot span more than 11 halftones. A chord may be one of the common type major, minor, diminished or augmented (all of them in all three inversion flavors) or can have any other pitch combination. All chords can be rotated up or down. Rotating a chord up means replacing the chord's low pitch with the same pitch transposed one octave up. Rotating a chord down means replacing the chord's high pitch with the same pitch transposed one octave down. All common type chords (except for augmented chords) can be normalized, which means that the chords are rotated up or down so that their low pitch equals the root pitch of the chord (e.g., "C4" and "C6" are normalized to "C"), if that is not already the case. Augmented chords cannot be normalized, because they don't have a unique root pitch (e.g., the chords "Caug", "Eaug6" and "G#aug4" are not distinguishable if you look at their pitches).

Author:
Thomas Schuerger (thomas@schuerger.com)

Constructor Summary
Chord(int pitch1, int pitch2, int pitch3)
          Instantiates a chord.
 
Method Summary
 boolean containsPitch(int pitch)
          Returns true if the given pitch is a note that is contained in the chord, false otherwise.
 boolean equals(Object other)
          Implements an equality check.
 boolean equalsNormalized(Object other)
          Implements an equality check.
 Chord findChordClosestTo(Chord otherChord)
          Returns a chord that is a rotated version of this chord whose middle pitch is as close to the middle pitch of the given other chord as possible.
 int getHighPitch()
          Returns the pitch of the high note of the chord.
 int getLowPitch()
          Returns the pitch of the low note of the chord.
 int getMiddlePitch()
          Returns the pitch of the middle note of the chord.
 int getPitch(int offset)
          Returns the pitch of the given chord offset. 0 will return the low pitch, 1 the middle pitch, 2 the high pitch, 3 the low pitch transposed up by 1 octave, etc.
 int hashCode()
           
 boolean isAugmented()
          Returns true iff this chord is an augmented chord.
 boolean isDiminished()
          Returns true iff this chord is a diminished chord.
 boolean isMajor()
          Returns true iff this chord is a major chord.
 boolean isMajor7()
          Returns true iff this chord is a major7 chord.
 boolean isMinor()
          Returns true iff this chord is a minor chord.
 boolean isMinor7()
          Returns true iff this chord is a minor7 chord.
 Chord normalize()
          Normalizes the chord.
static Chord parseChord(String chordString, int crossoverPitch)
          Parses the given chord string and returns a Chord instance that represents the chord string.
 Chord rotateDown()
          Rotates the chord down by one chord offset.
 Chord rotateUp()
          Rotates the chord up by one chord offset.
 String toString()
          Returns a string representation of this chord.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Chord

public Chord(int pitch1,
             int pitch2,
             int pitch3)
Instantiates a chord. Pitches are sorted by their value and are used as the low, middle and high pitch, respectively.

Parameters:
pitch1 - the first pitch
pitch2 - the second pitch
pitch3 - the third pitch
Method Detail

isMajor

public boolean isMajor()
Returns true iff this chord is a major chord.

Returns:
true iff the chord is a major chord

isMajor7

public boolean isMajor7()
Returns true iff this chord is a major7 chord.

Returns:
true iff the chord is a major7 chord

isMinor

public boolean isMinor()
Returns true iff this chord is a minor chord.

Returns:
true iff the chord is a minor chord

isMinor7

public boolean isMinor7()
Returns true iff this chord is a minor7 chord.

Returns:
true iff the chord is a minor7 chord

isDiminished

public boolean isDiminished()
Returns true iff this chord is a diminished chord.

Returns:
true iff this chord is a diminished chord.

isAugmented

public boolean isAugmented()
Returns true iff this chord is an augmented chord.

Returns:
true iff this chord is an augmented chord.

equals

public boolean equals(Object other)
Implements an equality check. Two chords are equivalent iff they are based on the same low pitch and have the same flavor (which is equivalent to having the same 3 pitches).

Overrides:
equals in class Object
Parameters:
other - the other chord to compare this chord to
Returns:
true if the two objects are equal, false otherwise

equalsNormalized

public boolean equalsNormalized(Object other)
Implements an equality check. Two chords are equivalent if they are equivalent when both are normalized.

Parameters:
other - the other chord to compare this chord to
Returns:
true if the two objects are equal when they are normalized, false otherwise

toString

public String toString()
Returns a string representation of this chord. If the chord has a canonical name, its canonical name is returned, otherwise a generic string representation is returned.

Overrides:
toString in class Object
Returns:
the string represenation

getLowPitch

public int getLowPitch()
Returns the pitch of the low note of the chord.

Returns:
the pitch of the low note

getMiddlePitch

public int getMiddlePitch()
Returns the pitch of the middle note of the chord.

Returns:
the pitch of the middle note

getHighPitch

public int getHighPitch()
Returns the pitch of the high note of the chord.

Returns:
the pitch of the high note

getPitch

public int getPitch(int offset)
Returns the pitch of the given chord offset. 0 will return the low pitch, 1 the middle pitch, 2 the high pitch, 3 the low pitch transposed up by 1 octave, etc. Negative offsets are also supported.

Parameters:
offset - the chord offset
Returns:
the pitch

normalize

public Chord normalize()
Normalizes the chord. Any major, minor or diminished chord with first or second inversion will be converted to its counterpart without inversion. For all other chords the original chord will be returned. Augmented chords cannot be normalized, because they don't have a unique root pitch.

Returns:
the chord

rotateUp

public Chord rotateUp()
Rotates the chord up by one chord offset. Effectively, this method creates a copy of this chord where the low note is transposed one octave up.

Returns:
the chord rotated up by one chord offset

rotateDown

public Chord rotateDown()
Rotates the chord down by one chord offset. Effectively, this method creates a copy of this chord where the high note is transposed one octave down.

Returns:
the chord rotated down by one chord offset

containsPitch

public boolean containsPitch(int pitch)
Returns true if the given pitch is a note that is contained in the chord, false otherwise. All involved pitches are normalized, i.e., octave does not matter.

Parameters:
pitch - the pitch
Returns:
true if the pitch belongs to the chord, false otherwise

findChordClosestTo

public Chord findChordClosestTo(Chord otherChord)
Returns a chord that is a rotated version of this chord whose middle pitch is as close to the middle pitch of the given other chord as possible. For example, the closest version of chord "C" to chord "Am" is chord "C6".

Parameters:
otherChord - the other chord
Returns:
the closest chord

hashCode

public int hashCode()
Overrides:
hashCode in class Object

parseChord

public static Chord parseChord(String chordString,
                               int crossoverPitch)
Parses the given chord string and returns a Chord instance that represents the chord string.

Parameters:
chordString - the chord as a string
crossoverPitch - the crossover pitch (between 1 and 12)
Returns:
the Chord