amads.pitch.transformations#

Basic functionality for transforming pitch lists expressed as integers (MIDI numbers or pitch classes) through transposition, inversion, retrograde, rotation, and more.

Most apply equally to any pitch class sequence and can therefore be use in melody and harmony settings.

amads.pitch.transformations.every_nth(pitches, start_index=0, step_size=5) list[source]#

Cycle through a list of pitches with a step size of n mod the length of the list

By default, start at index 0 and iterate 12 times (0-12), though both the start index and the range() are settable arguments, hence this equivlance:

Parameters:
  • pitches (list) – Any list or tuple of integers representing pitches as MIDI numbers or pitch classes.

  • start_index (int) – The index position in the list to start at.

  • step_size (int) – The gap between successive elements.

Returns:

A new list of the same length as the input.

Return type:

list

Examples

>>> test_pitches = [11, 9, 6, 8, 5, 10, 0, 4, 3, 1, 2, 7]
>>> every_nth(test_pitches, step_size=5)
[11, 10, 2, 8, 3, 9, 0, 7, 5, 1, 6, 4]
>>> every_nth(test_pitches, step_size=5, start_index=5)
[10, 2, 8, 3, 9, 0, 7, 5, 1, 6, 4, 11]
amads.pitch.transformations.invert(pitches, use_first_not_0=True, mod_12=True) list[source]#

Invert a list of pitch classes around a specified pitch: the starting pitch or 0.

Parameters:
  • pitches (Iterable) – Any list or tuple of integers representing pitches as MIDI numbers or pitch classes.

  • use_first_not_0 (bool) – If true, use the first number of the list as the centre of the inversion.

  • mod_12 (bool) – If True, return values modulo 12 (necessary for pitch class sets, not for MIDI numbers)

Returns:

A new list of the same length as the input.

Return type:

list

Examples

>>> invert([5, 6, 7])
[5, 4, 3]
>>> invert([5, 6, 7], use_first_not_0=False)
[7, 6, 5]
amads.pitch.transformations.pitches_to_intervals(pitches, wrap=False, mod_12=True) list[source]#

Get the interval succession of a list of pitches.

Parameters:
  • pitches (Iterable) – Any list or tuple of integers representing pitches as MIDI numbers or pitch classes.

  • wrap (bool) – If true, include the interval from the last element to the first in addition.

  • mod_12 (bool) – If True, return values modulo 12 (necessary for pitch class sets, not for MIDI numbers)

Returns:

A new list of the same length as the input (if wrap), otherwise, one less.

Return type:

list

Examples

>>> pitches_to_intervals([0, 2, 5])
[2, 3]
>>> pitches_to_intervals([0, 2, 5], wrap=True)
[2, 3, 7]
amads.pitch.transformations.retrograde(pitches) list[source]#

Retrograde (reverse) a list of pitches.

Parameters:

pitches (Iterable) – Any list or tuple of integers representing pitches as MIDI numbers or pitch classes.

Returns:

A new list of the same length as the input.

Return type:

list

Examples

>>> retrograde([2, 6, 9])
[9, 6, 2]
amads.pitch.transformations.rotate(pitches, steps=1) list[source]#

Rotates a list of pitch classes through N steps (i.e. starts on the Nth element).

Should be called on an integer less than the length of the pitch list. If called on a larger integer, the value modulo the length of the pitch list. (e.g. 15 becomes 3 for a pitch list of length 12).

Parameters:
  • pitches (Iterable) – Any list or tuple of integers representing pitches as MIDI numbers or pitch classes.

  • steps (int) – If true, include the interval from the last element to the first in addition.

Returns:

A new list of the same length as the input.

Return type:

list

Examples

>>> rotate([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 4)
[4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3]
amads.pitch.transformations.transpose_by(pitches, semitones, mod_12=True) list[source]#

Transposes a list of pitches by an interval of size set by the value of semitones.

Parameters:
  • pitches (Iterable) – Any list or tuple of integers representing pitches as MIDI numbers or pitch classes.

  • semitones (int) – How far to transpose, expressed in semitones (1 per MIDI note).

  • mod_12 (bool) – If True, return values modulo 12 (necessary for pitch class sets, not for MIDI numbers)

Returns:

A new list of the same length as the input.

Return type:

list

Examples

>>> transpose_by([0, 1, 2, 3,], 16, mod_12=True)
[4, 5, 6, 7]
>>> transpose_by([0, 1, 2, 3,], 16, mod_12=False)
[16, 17, 18, 19]
amads.pitch.transformations.transpose_to(pitches, start=0, mod_12=True) list[source]#

Transpose a list of pitch classes to start on 0 (by default), or any another number set by the value of start.

Parameters:
  • pitches (Iterable) – Any list or tuple of integers representing pitches as MIDI numbers or pitch classes.

  • start (int) – The first number of the new list.

  • mod_12 (bool) – If True, return values modulo 12 (necessary for pitch class sets, not for MIDI numbers)

Returns:

A new list of the same length as the input.

Return type:

list

Examples

>>> transpose_to([0, 1, 2, 3,], 16, mod_12=True)
[4, 5, 6, 7]
>>> transpose_to([0, 1, 2, 3,], 16, mod_12=False)
[16, 17, 18, 19]