Index of music_essentials’s documentation

The ‘Note’ class

class music_essentials.note.Note(pitch, octave, accidental=None, random_instance=<random.Random object at 0x1ff35b8>)

A single note, defined by a pitch, octave, and (optional) accidentals.

VALID_PITCHES = ('C', 'D', 'E', 'F', 'G', 'A', 'B')

List of valid pitch characters.

VALID_ACCIDENTALS = ('#', '##', 'b', 'bb', None)

List of valid accidental representors.

__init__(pitch, octave, accidental=None, random_instance=<random.Random object at 0x1ff35b8>)

Create a new Note.

Args:
pitch : str
The pitch of the note. Should be one of VALID_PITCHES, but can be upper or lower case.
octave : int
The octave of the note. Should be in the range [-1, 9].
Kwags:
accidental : str (default None)
The accidental to apply to the note. Should be one of VALID_ACCIDENTALS.
duration : float (default None)
The duration of the note, in terms of how many would fit into one bar in common time. For example, a semibreve has a duration of 1; a quaver has a duration of 8.
dotted : boolean (default False)
If true, the duration of the note is multiplied by 1.5.
Returns:
Note
A new note with the given pitch, octave, and accidental.
Raises:
ValueError:
If an invalid pitch, octave, or accidental is provided.
TypeError:
If an incorrect type of value is given for pitch, octave, or accidental.
Examples:
>>> n = Note('A', 4, '##')
>>> print(n)
A4##
>>> n = Note('d', 7)
>>> print(n)
D7
>>> n = Note('x', 6)
ValueError: Invalid pitch: x
classmethod from_note_string(note_string, random_instance=<random.Random object at 0x20207d8>)

Create a new Note.

Processes the note string then uses the constructor __init__(). If the note string is ‘r’, a Rest is returned.

Args:
note_string : str
A string representing the note to create. Should be in the form:
<pitch><octave><accidental>

The pitch of the note should be one of VALID_PITCHES, but can be upper or lower case.

The octave of the note should be in the range [-1, 9].

The accidental is optional, but if used should be one of VALID_ACCIDENTALS.

Returns:
Note
A new note with the given pitch, octave, and accidental.
Raises:
ValueError:
If an invalid pitch, octave, or accidental is provided.
TypeError:
If the provided note string is not a string.
Examples:
>>> n = Note.from_note_string('A4##')
>>> print(n)
A4##
>>> n = Note.from_note_string('d7')
>>> print(n)
D7
>>> n = Note.from_note_string('x6')
ValueError: Invalid pitch: x
classmethod from_midi_num(midi_num, random_instance=<random.Random object at 0x20483c8>)

Create a new note.

Uses the provided MIDI number to set the note parameters.

Args:
midi_num : int
A number in the range [0, 127] representing a Note.
Returns:
Note
A new note with a pitch, octave, and accidental corresponding to the given MIDI note number.
classmethod random_note(lowest_midi_num=0, highest_midi_num=127, method='rand', chance_for_rest=0.01, random_instance=<random.Random object at 0x1fc1c48>)

Create and return a random Note within the MIDI note number range [lowest_midi_num, highest_midi_num].

Args:
lowest_midi_num : int (default 0)
The lowest MIDI number allowed.
highest_midi_num : int (default 127)
The highest MIDI number allowed.
method : str (default ‘rand’)
The method of random selection to use. If ‘rand’, a uniform distribution will be used. If ‘gauss’, a gaussian distribution will be used.
Returns:
Note
A new note with a randomly selected pitch, octave, and accidental.
midi_note_number()

Get the MIDI note number equivalent to this pitch.

Assumes that middle C corresponds to the MIDI note number 60, as described on Wikipedia:.

Returns:
int
The MIDI note number representing this pitch.
Examples:
>>> n = Note.from_note_string('C-1')
>>> print(n.midi_note_number())
0
>>> n = Note.from_note_string('G9')
>>> print(n.midi_note_number())
127
>>> n = Note.from_note_string('B0b')
>>> print(n.midi_note_number())
22
__add__(other)

Calculate and return the note found when adding an interval to this note.

Args:
other : Interval
The interval to add to this note.
Returns:
Note
The new note that comes from adding the provided interval to this note.
Raises:
TypeError:
If the object to add is not an Interval.
Examples:
>>> n = Note.from_note_string('C4')
>>> i = Interval.from_interval_string('M2')
>>> print(n + i)
D4
>>> n = Note.from_note_string('C4')
>>> i = Interval.from_interval_string('m14')
>>> print(n + i)
B5b
>>> n = Note.from_note_string('C4')
>>> i = Interval.from_interval_string('aug13')
>>> print(n + i)
A5#
is_enharmonic(other)

Check if two notes are enharmonic.

Args:
other : Note
The note to compare this to.
Returns:
bool
True if the two notes represent the same pitch, otherwise false.
Raises:
ValueError:
If anything other than a Note is given to compare to.
TypeError:
If the object to compare to is not a Note.
Examples:
>>> n1 = Note('C', 4)
>>> n2 = Note('D', 4)
>>> n1.is_enharmonic(n2)
False
>>> n1 = Note('C', 4, '#')
>>> n2 = Note('D', 4, 'b')
>>> n1.is_enharmonic(n2)
True
>>> n1 = Note('F', 4)
>>> n2 = Note('E', 4, '#')
>>> n1.is_enharmonic(n2)
True
>>> n1 = Note('F', 4)
>>> n2 = Note('G', 4, 'bb')
>>> n1.is_enharmonic(n2)
True
__eq__(other)

Check if this note is equal to another note.

Does not consider enharmonic notes to be equal.

Args:
other : Note
The note to compare this note to.
Returns:
bool
True if the notes have the same pitch, octave, and accidentals; otherwise false.
Raises:
TypeError:
If the object to compare to is not a Note.
Examples:
>>> n1 = Note.from_note_string('C4')
>>> n2 = Note('C', 4)
>>> n1 == n2
True
>>> n1 = Note.from_note_string('C4#')
>>> n2 = Note.from_note_string('D4b')
>>> n1 == n2
False        
__ne__(other)

Check if this note is note equal to another note.

Does not consider enharmonic notes to be equal.

Args:
other : Note
The note to compare this note to.
Returns:
bool
True if the notes do not have the same pitch, octave, and accidentals; otherwise false.
Raises:
TypeError:
If the object to compare to is not a Note.
Examples:
>>> n1 = Note.from_note_string('C4')
>>> n2 = Note('C', 4)
>>> n1 != n2
False
>>> n1 = Note.from_note_string('C4#')
>>> n2 = Note.from_note_string('D4b')
>>> n1 != n2
True    
__lt__(other)

Check if this note is less than another note.

Does not consider enharmonic notes to be equal. If two notes are enharmonic, the note with the lower written pitch is considered lower.

Args:
other : Note
The note to compare this note to.
Returns:
bool
True if this note is less than the other, otherwise false.
Raises:
TypeError:
If the object to compare to is not a Note.
Examples:
>>> n1 = Note.from_note_string('C4')
>>> n2 = Note('C', 4)
>>> n1 < n2
False
>>> n1 = Note.from_note_string('D4')
>>> n2 = Note.from_note_string('G4')
>>> n1 < n2
True
>>> n2 < n1
False    
__gt__(other)

Check if this note is greater than another note.

Does not consider enharmonic notes to be equal. If two notes are enharmonic, the note with the higher written pitch is considered higher.

Args:
other : Note
The note to compare this note to.
Returns:
bool
True if this note is greater than the other, otherwise false.
Raises:
TypeError:
If the object to compare to is not a Note.
Examples:
>>> n1 = Note.from_note_string('C4')
>>> n2 = Note('C', 4)
>>> n1 > n2
False
>>> n1 = Note.from_note_string('D4')
>>> n2 = Note.from_note_string('G4')
>>> n1 > n2
False
>>> n2 > n1
True 
__le__(other)

Check if this note is less than or equal to another note.

Args:
other : Note
The note to compare this note to.
Returns:
bool
True if this note is less than or equal to the other, otherwise false.
Raises:
TypeError:
If the object to compare to is not a Note.
Examples:
>>> n1 = Note.from_note_string('C4')
>>> n2 = Note('C', 4)
>>> n1 < n2
True
>>> n1 = Note.from_note_string('D4')
>>> n2 = Note.from_note_string('G4')
>>> n1 < n2
True
>>> n2 < n1
False    
__ge__(other)

Check if this note is greater than or equal to another note.

Args:
other : Note
The note to compare this note to.
Returns:
bool
True if this note is greater than or equal to the other, otherwise false.
Raises:
TypeError:
If the object to compare to is not a Note.
Examples:
>>> n1 = Note.from_note_string('C4')
>>> n2 = Note('C', 4)
>>> n1 > n2
True
>>> n1 = Note.from_note_string('D4')
>>> n2 = Note.from_note_string('G4')
>>> n1 > n2
False
>>> n2 > n1
True 
__str__()

Create a string representation of the note in the form <pitch><octave><accidental>.

Can be used as a note string argument for from_note_string().

Examples:
>>> n = Note('B', 9, '#')
>>> print(n)
B9#
>>> n = Note('g', 7)
>>> print(n)
G7
>>> n = Note('D', 3, 'B')
>>> print(n)
D3b
__hash__ = None

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

The ‘Scale’ class

class music_essentials.scale.Scale

Static class methods for building lists of notes according to pre-defined patterns.

classmethod build_scale(tonic, scale_type)

Build a scale.

The scale will be built from the provided tonic, for one ascending octave. If building the scale will result in creating an invalid note (e.g., a note for which there is no MIDI number), the program will crash.

Args:
tonic : Note
The tonic note of the scale.
scale_type : str

The type of scale to build. Supported scale types are:

  • ‘major’/’maj’: major scale
  • ‘minor’/’min’: harmonic minor scale
  • ‘natural minor’/’nat min’: natural minor scale
Returns:
list
The notes in the specified scale, in ascending order.
Raises:
ValueError:
If an scale type is provided.
TypeError:
If the tonic is not a Note., or scale type is not a string.

The ‘Note’ class

class music_essentials.chord.Chord(root_note)

Representation of group of notes that are played together.

__init__(root_note)

Create a new Chord.

Once the Chord has been created, additional notes can be added using add_note

Args:
root_note: Note
The first note to add to the chord.
Returns:
Chord
A new chord object, with a single note added.
Raises:
TypeError:
If anything but an instance of Note is provided for root_note.
Examples:
>>> c = Chord(Note.from_note_string('C4'))
>>> print(c)
C4
>>> c = Chord(Note.from_note_string('C4'))
>>> c.add_note(Note.from_note_string('E4'))
>>> print(c)
C4+E4
>>> c = Chord(5.5)
Expected Note for root note, got '5.5'
classmethod build_chord(tonic_key, chord_number, chord_type)

Build a chord.

Specify the tonic key, the chord number, and the type of chord to build. Receive an ordered list of the notes in the chord.

tonic_key : Note
They key in which the chord should be built
chord_number : str
The scale degree to start building the chord on
chord_type : str

The tonality of the key to build the cord in. Can be one of:

  • ‘major’/’maj’: Major tonality.
  • ‘minor’/’min’: Minor tonality.
list
The list of notes in the chord, in ascending order.
>>> c = Chord.build_chord(Note.from_note_string('C4'), 'I', 'major')
>>> print(c)
>>> C4+E4+G4
>>> c = Chord.build_chord(Note.from_note_string('C4'), 'V', 'major')
>>> print(c)
>>> G4+B4+D5
>>> c = Chord.build_chord(Note.from_note_string('C4'), 'IV', 'minor')
>>> print(c)
>>> F4+A4b+C5
root()

Get the root (i.e., lowest) note of the chord.

Returns:
Note
The lowest note of the chord.
Examples:
>>> c = Chord(Note.from_note_string('E4'))
>>> print(c.root())
E4
>>> c = Chord(Note.from_note_string('E4'))
>>> c.add_note(Note.from_note_string('D4'))
>>> print(c.root())
D4
add_note(new_note)

Add another note to the chord.

Args:
new_note : Note
The note to add.
Raises:
TypeError:
If new_note is not an instance of Note.
Examples:
>>> c = Chord(Note.from_note_string('C4'))
>>> c.add_note(Note.from_note_string('E4'))
>>> print(c)
C4+E4
>>> c = Chord(Note.from_note_string('G4'))
>>> c.add_note(Note.from_note_string('E4'))
>>> c.add_note(Note.from_note_string('D4'))
>>> print(c)
D4+E4+G4
__str__()

Get a string representation of the chord.

Returns:
str
A string representation of the chord, in the form <note_1>+<note_2>+...+<note_n>.

Indices and tables