pitchtypes.spelled_array.SpelledPitchArray

class pitchtypes.spelled_array.SpelledPitchArray(fifths, octaves)

Bases: SpelledArray, AbstractSpelledArrayPitch, Pitch

Represents an array of spelled pitches.

__init__(fifths, octaves)

Takes two numpy arrays, one for fifths and one for (internal/dependent) octaves, both as integers.

Parameters:
  • fifths – the internal fifths of each pitch (numpy array of integers)

  • octaves – the internal octaves of each pitch (numpy array of integers)

Methods

__init__(fifths, octaves)

Takes two numpy arrays, one for fifths and one for (internal/dependent) octaves, both as integers.

alteration()

Return the number of semitones by which the interval is altered from its the perfect or major variant.

array_equal(other)

Returns True if self and other are the equal, False otherwise.

compare(other)

Element-wise comparison between two spelled arrays.

copy()

Returns a shallow copy of the array.

deepcopy()

Returns a deep copy of the array.

degree()

Return the "relative scale degree" (0-6) to which the interval points (unison=0, 2nd=1, octave=0, 2nd down=6, etc.).

embed()

For a pitch class, returns the corresponding pitch in a (type-dependent) default octave.

fifths()

Return the position of the interval on the line of fifths.

from_array(pitches)

Create a pitch array from an array of pitches.

from_independent(fifths, octaves)

Create a pitch array from fifths and indenpendent octaves.

from_onehot(onehot, fifth_low, octave_low)

Create a spelled pitch array from a one-hot tensor.

from_strings(strings)

Create a pitch array from an array of strings.

internal_octaves()

Return the internal octave representation of a pitch, which is dependent on the fifths.

interval_from(other)

Computes the interval from another pitch to this pitch.

interval_to(other)

Computes the interval from this pitch to another pitch.

letter()

Returns the letter associated with the pitch (without accidentals).

midi()

Return the MIDI number corresponding to the pitch.

name()

Returns the names of the objects in the array as an array of strings of the same shape.

octaves()

For intervals, return the number of octaves the interval spans.

onehot(fifth_range, octave_range[, dtype])

Returns a one-hot encoding of the pitches in fifths and independent octaves as the innermost dimensions.

parse_pitch(s)

Parse a string as a spelled pitch or spelled pitch class.

pc()

Returns the pitch class corresponding to the pitch.

semitones()

Return the number of chromatic semitones corresponding to the pitch.

steps()

Return the number of diatonic steps corresponding to the pitch.

to_class()

Alias for pc(), but also supported by interval types.

__add__(other)

Returns the pitch transposed by an interval

Parameters:

other – an interval of matching type

Returns:

the pitch transposed by other

__contains__(item)

Returns true if the array contains the given interval/pitch.

Parameters:

item – the potential item to test

Returns:

True if the array contains item, otherwise False

__eq__(other)

Element-wise <= between two spelled arrays.

Parameters:

other – the spelled array or scalar to compare to

Returns:

a boolean array that indicates where this array is == other

__ge__(other)

Element-wise >= between two spelled arrays.

Parameters:

other – the spelled array or scalar to compare to

Returns:

a boolean array that indicates where this array is >= other

__getitem__(index)

Returns an item or a subarray of the spelled array. Supports advanced indexing as on numpy arrays.

Parameters:

index – a numpy-compatible index into the array

Returns:

a new array or a scalar value (depending on the index)

__gt__(other)

Element-wise > between two spelled arrays.

Parameters:

other – the spelled array or scalar to compare to

Returns:

a boolean array that indicates where this array is > other

__hash__ = None
__le__(other)

Element-wise <= between two spelled arrays.

Parameters:

other – the spelled array or scalar to compare to

Returns:

a boolean array that indicates where this array is <= other

__len__()

Returns the length of the array (first dimension, as in numpy).

Returns:

the length of the array (1st dimension, integer)

__lt__(other)

Element-wise < between two spelled arrays.

Parameters:

other – the spelled array or scalar to compare to

Returns:

a boolean array that indicates where this array is < other

__ne__(other)

Element-wise <= between two spelled arrays.

Parameters:

other – the spelled array or scalar to compare to

Returns:

a boolean array that indicates where this array is != other

__repr__()

Return repr(self).

__setitem__(index, item)

Sets the given indices to the given item(s). Supports advanced indexing as on numpy arrays.

Parameters:
  • index – a numpy-compatible index into the array

  • item – an array or a scalar to assign to the indicated sub-array

__str__()

Return str(self).

__sub__(other)

When subtracting another pitch (p1 - p2), return the interval from p2 to p1. When subtracting an interval (p - i), transpose the pitch by the inverse interval (p + -i).

Parameters:

other – pitch or interval

Returns:

if other is an interval, the transposed pitch; if other is a pitch, the interval between both pitches

__weakref__

list of weak references to the object (if defined)

alteration()

Return the number of semitones by which the interval is altered from its the perfect or major variant. Positive alteration always indicates augmentation, negative alteration indicates diminution (minor or smaller) of the interval’s magnitude. For interval classes, alteration refers to the upward version of the interval (e.g. for m7/-M2 it is -1). For pitches, return the accidentals (positive=sharps, negative=flats, 0=natural).

Returns:

an array of alterations (integers)

array_equal(other)

Returns True if self and other are the equal, False otherwise.

Parameters:

other – another spelled array of the same type

Returns:

True if the two arrays are equal, False otherwise

compare(other)

Element-wise comparison between two spelled arrays.

Returns 0 where the elements are equal, 1 where the first element is greater, and -1 where the second element is greater.

Spelled pitches use diatonic ordering, for example F4 < F#4 < F####4 < Gb4 < G4.

This method can be indirectly used through binary comparison operators (including ==, < etc.). To test the overall equality of two spelled arrays, use array_equal

Parameters:

other – another SpelledPitchArray or SpelledPitch

Returns:

an array of -1 / 0 / 1 (integer)

copy()

Returns a shallow copy of the array. This also creates copies of the underlying numpy arrays.

Returns:

a copy of the array

deepcopy()

Returns a deep copy of the array.

Returns:

a deepcopy of the array

degree()

Return the “relative scale degree” (0-6) to which the interval points (unison=0, 2nd=1, octave=0, 2nd down=6, etc.). For pitches, return the integer that corresponds to the letter (C=0, D=1, …).

Returns:

an array of degrees (integers)

embed()

For a pitch class, returns the corresponding pitch in a (type-dependent) default octave. For non-class pitches, returns the pitch itself.

Returns:

a non-class version of this pitch

fifths()

Return the position of the interval on the line of fifths.

Returns:

an array of fifths (integers)

static from_array(pitches)

Create a pitch array from an array of pitches.

Parameters:

intervals – an array-like of SpelledPitch

Returns:

the corresponding pitch array

static from_independent(fifths, octaves)

Create a pitch array from fifths and indenpendent octaves. The fifths indicate the names of the pitches while the octaves indicate their octave numbers.

Parameters:
  • fifths – the internal fifths of each pitch (numpy array of integers)

  • octaves – the external/independent octaves of each pitch (numpy array of integers)

Returns:

the corresponding pitch array

static from_onehot(onehot, fifth_low, octave_low)

Create a spelled pitch array from a one-hot tensor. fifth_low denotes the lower bound of the fifth range used in the vector, octave_low the lower bound of the octave range. The shape of the resulting array will be equivalent to the first n-2 dimensions of the input tensor.

Parameters:
  • onehot – a one-hot tensor representing the pitches (numpy array)

  • fifth_low – the lowest fifth expressible in the one-hot tensor (integer)

  • octave_low – the lowest octave expressible in the one-hot tensor (integer)

Returns:

the corresponding pitch array

static from_strings(strings)

Create a pitch array from an array of strings.

Parameters:

strings – an array-like of pitch notation strings

Returns:

the corresponding pitch array

internal_octaves()

Return the internal octave representation of a pitch, which is dependent on the fifths.

Only use this if you know what you are doing.

Returns:

an array of internal/dependent octaves (integers)

interval_from(other)

Computes the interval from another pitch to this pitch.

Parameters:

other – another pitch

Returns:

the interval from other to self

interval_to(other)

Computes the interval from this pitch to another pitch.

Parameters:

other – another pitch

Returns:

the interval from self to other

letter()

Returns the letter associated with the pitch (without accidentals).

Returns:

an array of pitch letters (single-character strings)

midi()

Return the MIDI number corresponding to the pitch.

name()

Returns the names of the objects in the array as an array of strings of the same shape.

Returns:

an array of notation strings

octaves()

For intervals, return the number of octaves the interval spans. Negative intervals start with -1, decreasing. For pitches, return the absolute octave of the pitch.

Returns:

an array of external/independent octaves (integers)

onehot(fifth_range, octave_range, dtype=<class 'int'>)

Returns a one-hot encoding of the pitches in fifths and independent octaves as the innermost dimensions. The range of fifths and octaves is given by fifth_range and octave_range respectively, where each is a tuple (lower, upper). The outer shape of the output tensor is identical to the shape of the original array, so the resulting shape is original_shape + (n_fifths, n_octaves).

Parameters:
  • fifth_range – the (inclusive) range of fifths (pair of integers)

  • octave_range – the (inclusive) range of octaves (pair of integers)

  • dtype – dtype of the resulting array (default: int)

Returns:

a one-hot tensor (numpy array)

pc()

Returns the pitch class corresponding to the pitch. For pitch classes, it returns the pitch class itself.

Returns:

the pitch class that corresponds to this pitch

semitones()

Return the number of chromatic semitones corresponding to the pitch.

steps()

Return the number of diatonic steps corresponding to the pitch.

to_class()

Alias for pc(), but also supported by interval types.

Returns:

the pitch class that corresponds to this pitch