The ‘Note’ class¶
-
class
music_essentials.note.
Note
(pitch, octave, accidental=None, random_instance=<random.Random object at 0x3671588>)¶ 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 0x3671588>)¶ 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 0x366e1e8>)¶ Create a new Note.
Processes the note string then uses the constructor
__init__()
. If the note string is ‘r’, aRest
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 0x366ebf8>)¶ 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 0x3641b18>)¶ 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.
- other :
- 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.
- other :
- 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.
- other :
- 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.
- other :
- 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.
- other :
- 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.
- other :
- 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.
- other :
- 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.
- other :
- 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¶
-