pytakt.chord module

This module defines the classes related to chord symbols.

class Chord(name=None, *, kind=None, root=None, bass=None, modifications=[])

Bases: object

This is a class for objects representing chord symbols used in jazz, popular music, etc. It is based on the MusicXML representation of chords in the <harmony> element, although some chord types and information such as fretboard display are omitted.

Attributes

kind

One of the following strings indicating the type of the chord. For the meaning of each, see Description of Chord Names below and also the documents on the ‘kind’ entry in MusicXML’s <harmony> element <https://www.w3.org/2021/06/musicxml40/musicxml-reference/data-types/kind-value/>`.

‘major’, ‘major-sixth’, ‘major-seventh’, ‘major-ninth’, ‘major-11th’, ‘major-13th’, ‘major-minor’, ‘minor’, ‘minor-sixth’, ‘minor-seventh’, ‘minor-ninth’, ‘minor-11th’, ‘minor-13th’, ‘dominant’, ‘dominant-ninth’, ‘dominant-11th’, ‘dominant-13th’, ‘augmented’, ‘augmented-seventh’, ‘diminished’, ‘diminished-seventh’, ‘half-diminished’, ‘suspended-fourth’, ‘suspended-second’, ‘power’

Type:

str

root

Pitch of the chord root note. Its octave is also meaningful.

Type:

Pitch or int

bass

Pitch of the bass note of the chord. If None, it is assumed to be the same as root.

Type:

Pitch, int, or None

modifications

Represents the additions, alternations, and omissions of notes, corresponding to the <degree> element in MusicXML. Each element of the list is a tuple of length 3. The first item in each tuple is a string that is one of ‘add’, ‘alter’, and ‘subtract’. The second item is an integer representing the degrees from the root note. The third item is an integer representing the change in semitones from the base pitch. The base pitch means the pitch on the Mixolydian scale in the case of an added note, or the pitch of the component note of the chord specified by the kind element in the case of an altered note.

Example: The C7b9 chord has kind=’dominant’, root=C4, and modifications=[(‘add’, 9, -1)].

Type:

list of (str, int, int)

Constructor

Parameters:
  • name (str, optional) – Specifies a chord by the chord name defined below. If this argument is None, the kind and root arguments must be specified.

  • kind (str, optional) – Specifies the ‘kind’ attribute. If already specified by the chord name, it will be overridden.

  • root (pitch or int, optional) – Specifies the ‘root’ attribute. If already specified by the chord name, it will be overridden.

  • bass (Pitch or int, optional) – Specifies the ‘bass’ attribute. If already specified by the chord name, it will be overridden.

  • modifications (iterable of (str, int, int)) – Specifies the ‘modifications’ attribute. If it is already given by the chord name, tuple(s) specified by this argument are appended to the attribute value.

Description of Chord Names

This class uses the following string for describing a chord name. Spaces, commas, and parentheses can be freely inserted between each substring after <type>.

<root> <type> <modification>* [/<bass>]

<root> represents the pitch of the root note, consisting of the letters ‘A’ through ‘G’ (lowercase letters are also acceptable) followed by at most two sharps ‘#’ or flats ‘b’. When using chord names, the octave number is always 3. If you want to specify a different octave, use the root argument.

<type> indicates the type of the chord (value of the ‘kind’ attribute) shown in the following table (case-sensitive).

kind

<type>

Component notes

major

‘’

1, 3, 5

major-sixth

‘6’

1, 3, 5, 6

major-seventh

‘M7’ ‘maj7’ ‘Maj7’

1, 3, 5, 7

major-ninth

‘M9’ ‘maj9’ ‘Maj9’

1, 3, 5, 7, 9

major-11th

‘M11’ ‘maj11’ ‘Maj11’

1, 3, 5, 7, 9, 11

major-13th

‘M13’ ‘maj13’ ‘Maj13’

1, 3, 5, 7, 9, 11, 13

major-minor

‘mM7’

1, b3, 5, 7

minor

‘m’

1, b3, 5

minor-sixth

‘m6’

1, b3, 5, 6

minor-seventh

‘m7’

1, b3, 5, b7

minor-ninth

‘m9’

1, b3, 5, b7, 9

minor-11th

‘m11’

1, b3, 5, b7, 9, 11

minor-13th

‘m13’

1, b3, 5, b7, 9, 11, 13

dominant

‘7’

1, 3, 5, b7

dominant-ninth

‘9’

1, 3, 5, b7, 9

dominant-11th

‘11’

1, 3, 5, b7, 9, 11

dominant-13th

‘13’

1, 3, 5, b7, 9, 11, 13

augmented

‘aug’

1, 3, #5

augmented-seventh

‘aug7’

1, 3, #5, b7

diminished

‘dim’

1, b3, b5

diminished-seventh

‘dim7’

1, b3, b5, 6

half-diminished

‘m7b5’

1, b3, b5, b7

suspended-fourth

‘sus4’

1, 4, 5

suspended-second

‘sus2’

1, 2, 5

power

‘5’, ‘power’

1, 5

In addition to these, the following strings (aliases) can be specified for <type>. Each of these has the same meaning as the chord shown to the right of the equal sign.

‘7sus4’ = ‘sus4add7’, ‘9sus4’ = ‘sus4(9)add7’, ‘7sus2’ = ‘sus2add7’, ‘mM11’ = ‘mM7(9,11)’, ‘mM13’ = ‘mM7(9,11,13)’, ‘aug9’ = ‘aug7(9)’, ‘aug11’ = ‘aug7(9,11)’, ‘aug13’ = ‘aug7(9,11,13)’, ‘dim9’ = ‘dim7(9)’, ‘dim11’ = ‘dim7(9,11)’, ‘m9b5’ = ‘m7b5(9)’, ‘m11b5’ = ‘m7b5(9,11)’, ‘m13b5’ = ‘m7b5(9,11,13)’, ‘7alt’ = ‘7(b9,#9,#11,b13)’

<modification> specifies an added, altered, or omitted note by one of the followings. Multiple modifications are allowed.

‘add<arbitrary number of #/b’s><integer>’, ‘alter<arbitrary number of #/b’s><integer>’, ‘omit<integer>’, ‘b5’, ‘#5’, ‘M7’, ‘M9’, ‘M11’, ‘M13’, ‘b9’, ‘9’, ‘#9’, ‘11’, ‘#11’, ‘13’, ‘b13’

‘add’ specifies addition of a note, ‘alter’ specifies alternation of a note, and ‘omit’ specifies omission of a note. Other modifications are treated as alternations if the designated degree is included in the base chord defined by <type>, and as additions otherwise. ‘M9’, ‘M11’, and ‘M13’ are equivalent to ‘M7,9’, ‘M7,9,11’, and ‘M7,9,11,13’, respectively.

/<bass> specifies the bass note (may be omitted). It is to be specified in the same way as <root>.

If there is ambiguity in the decomposition of the string into <root>, <type>, and each <modification>, the first (left) element is assigned the longest string possible (so-called the greedy rule). For example, ‘Ab9’ is interpreted as a dominant-ninth chord with the root ‘Ab’. If you want a major chord with the root note ‘A’ plus the tension of ‘b9’, put a separator between the two, e.g. ‘A(b9)’ or ‘A b9’. Similarly, ‘Ab5’ is interpreted as a power chord with the root note ‘Ab’, and ‘A(b5)’ as an A major chord with the fifth note lowered by a semitone.

Examples of chord names: ‘C7b9’, ‘C(9)’, ‘C69’, ‘C13#11’, ‘CaugM7’, ‘C#11’,

‘CdimM9’, ‘F#m7b5(11)’, ‘C7sus4b9’, ‘C7omit3add2’, ‘C7(alter#5,addb9)’, ‘C/E’, ‘FM7/G’

Arithmetic Rules

  • Equivalence comparison (‘==’) between Chord objects results in true only when all the attribute values are equivalent.

  • When c is a Chord object and p is a pitch, p in c is equivalent to c.is_chord_tone(p).


copy() Chord

Returns a duplicated Chord object. The ‘modifications’ attribute value is duplicated as a list.

name() str

Returns the chord name. Passing this chord name to the constructor creates an equivalent Chord object except for the octave numbers of the root note and bass note.

degrees(maxinterval=None) Dict[int, Tuple[int]]

Returns a dictionary (a dict object) representing the component tones categorized by degrees. The keys of the dictionary are integers representing degrees, and the values of the dictionary are tuples of integers, each representing the deviation (in semitones) from the pitch on the Mixolydian scale. Bass notes are not taken into account. The keys in the dictionary and the values in each tuple are not necessarily sorted.

Parameters:

maxinterval (Interval or int, optional) – When an Interval object or an integer representing the number of semitones is specified, only the tones whose intervals from the root note are within this value will be output. By default, all notes are output.

Examples

>>> Chord('CM7').degrees()
{1: (0,), 3: (0,), 5: (0,), 7: (1,)}
>>> Chord('G7(b13,#9,b9)').degrees()
{1: (0,), 3: (0,), 5: (0,), 7: (0,), 13: (-1,), 9: (1, -1)}
>>> Chord('CM13').degrees(maxinterval=Interval('M7'))
{1: (0,), 3: (0,), 5: (0,), 7: (1,)}
simplify(use_extended_chords=True) Chord

Returns a new Chord object, modified to minimize the number of elements in the ‘modifications’ attribute, without changing the chord component tones. The root note and bass note remain unchanged. In the returned chord, the modifications attribute is sorted with the keys of type, degrees, and amount of alternations in this order.

Parameters:

use_extended_chords (bool, optional) – If False, chords containing 9th, 11th, and 13th are not used as the base chord (the chord specified by the ‘kind’ attribute).

Examples

>>> Chord('C7add9')
Chord(kind='dominant', root=C3, bass=None, modifications=[('add', 9, 0)])
>>> Chord('C7add9').simplify()
Chord(kind='dominant-ninth', root=C3, bass=None, modifications=[])
>>> Chord('C7add9').simplify().name()
'C9'
>>> Chord("C13(#11)").simplify(False).name()
'C7(9,#11,13)'
pitches(maxinterval=None) List[Pitch]

Returns a list of pitches for the chord component notes. When a bass note is specified, it is included (In this case, other notes of the same pitch class as the bass note are removed, and for other notes lower than the bass note, their octaves are raised so that they are higher than the bass note).

Parameters:

maxinterval (Interval or int, optional) – Has the same meaning as the argument of the same name in degrees().

Examples

>>> Chord('Cdim7').pitches()
[C3, Eb3, Gb3, A3]
>>> Chord('G7/F').pitches()
[F3, G3, B3, D4]
>>> Chord('F/G').pitches()
[G3, A3, C4, F4]
pitches_above(pitch, num=None, maxinterval=None) Iterator[Pitch]

A generator function that yields the pitches of the chord component tones above pitch (including the bass note and notes with different octaves) in sequence.

Parameters:
  • pitch (Pitch or int) – Base pitch

  • stop (int, optional) – If specified, the output is limited to that number of pitches.

  • maxinterval (Interval or int, optional) – Has the same meaning as the same name argument of degrees().

Examples

>>> list(Chord('C7').pitches_above(C4, 5))
[E4, G4, Bb4, C5, E5]
pitches_below(pitch, num=None, maxinterval=None) Iterator[Pitch]

A generator function that yields the pitches of the chord component tones below pitch (including the bass note and notes with different octaves) in sequence.

Parameters:
  • pitch (Pitch or int) – Base pitch

  • stop (int, optional) – If specified, the output is limited to that number of pitches.

  • maxinterval (Interval or int, optional) – Has the same meaning as the same name argument of degrees().

is_chord_tone(pitch, maxinterval=None) bool

Returns True if the chord component notes (including the bass note) contains a note of the same pitch class as pitch, or False otherwise.

Parameters:

maxinterval (Interval or int, optional) – Has the same meaning as the same name argument of degrees().

demo(**kwargs) Score

Returns a score of the demo performance for the chord.

Parameters:

kwargs – Additional arguments passed to the note() function

static from_chroma_profile(chroma_profile, bass=None) Chord

Returns a chord inferred from the chroma profile (see chroma_profile()). Only the truth value of each element in the chroma profile is significant. If all the elements are false in the chroma profile, an exception is raised. [Experimental]

Parameters:

bass (Pitch or int, optional) – Specifies the bass note of the chord. This is not only set as the bass note in the output chord, but also acts as a hint for the root note when guessing.

Examples

>>> cp = [1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0]
>>> Chord.from_chroma_profile(cp).name()
'Dm9'
>>> Chord.from_chroma_profile(cp, bass=F3).name()
'FM7(13)'
>>> Chord.from_chroma_profile(cp, bass=C3).name()
'Dm9/C'