The ‘Interval’ class

class music_essentials.interval.Interval(interval_type, size)

Representation of an interval (i.e., gap) between notes.

NAMED_INTERVAL_TYPES = ('M', 'm', 'P', 'dim', 'aug')

Explicit interval types supported - major, minor, diminished, augmented.

VALID_INTERVAL_TYPES = ('dim1', 'P1', 'aug1', 'dim2', 'm2', 'aug2', 'M2', 'dim3', 'm3', 'M3', 'aug3', 'dim4', 'P4', 'aug4', 'dim5', 'P5', 'aug5', 'dim6', 'm6', 'M6', 'aug6', 'dim7', 'm7', 'M7', 'aug7')

List of valid intervals up to (but not including) a perfect octave.

Compound intervals are formed/processed internally by adding perfect octaves to these valid interval types.

Inclues:

  • 'dim1'/'P1'/'aug1': diminished/perfect/augmented unison
  • 'dim2'/'m2'/'M2'/'aug2': diminished/minor/major/augmented second
  • 'dim3'/'m3'/'M3'/'aug3': diminished/minor/major/augmented third
  • 'dim4'/'P4'/'aug4': diminished/perfect/augmented fourth
  • 'dim5'/'P5'/'aug5': diminished/perfect/augmented fifth
  • 'dim6'/'m6'/'M6'/'aug6': diminished/minor/major/augmented sixth
  • 'dim7'/'m7'/'M7'/'aug7': diminished/minor/major/augmented seventh
__init__(interval_type, size)

Create a new Interval.

Args:
interval_type : str
The type of interval. Should be one of NAMED_INTERVAL_TYPES.
distance : int
The size of the interval. Should be positive.
Returns:
Interval
A new interval of the given type and size.
Raises:
ValueError:
If an invalid interval type, size, or combination of type and size is provided.
TypeError:
If the interval type is not a string, or size is not an integer.
Examples:
>>> i = Interval('M', 3)
>>> print(i)
M4
>>> i = Interval('dim', 13)
>>> print(i)
dim13
>>> i = Interval('i', 6)
ValueError: Unsupported interval type specified: i
>>> i = Interval('m', -1)
ValueError: Expected interval distance to be positive, got -1
>>> i = Interval('M', 5)
ValueError: Impossible interval specified: M5
classmethod from_interval_string(interval_string)

Create a new Interval.

Processes the interval string then uses the constructor __init__()

Args:
interval_string : str
A string representing the interval to create. Should be in the form:
<interval type><size>

The interval type should be one of NAMED_INTERVAL_TYPES.

The size of the interval should be positive.

Returns:
Interval
A new interval of the given type and size.
Raises:
ValueError:
If an invalid interval type, size, or combination of type and size is provided.
TypeError:
If the interval string is not a string.
Examples:
>>> i = Interval.from_interval_string('M3')
>>> print(i)
M4
>>> i = Interval.from_interval_string('dim13')
>>> print(i)
dim13
>>> i = Interval.from_interval_string('i6')
ValueError: Unsupported interval type specified: i
>>> i = Interval.from_interval_string('m-1')
ValueError: Expected interval distance to be positive, got -1
>>> i = Interval.from_interval_string('M5')
ValueError: Impossible interval specified: M5
__str__()

Create a string representation of the interval in the form <interval type><size>

Can be used as an interval string argument for from_interval_string().

Examples:
>>> i = Interval.from_interval_string('m7')
>>> print(i)
m7