pytakt.scale module

This module defines classes and utility functions related to musical scales.

class Scale(tonic, type='major')

Bases: object

A class for objects representing musical scales.

Attributes

tonic

Pitch of the starting tone of the scale (octave is also meaningful)

Type:

Pitch or int

tone_list

List of scale constituent tones. Each element represents the interval from the tonic (Interval object or integer representing the number of semitones).

Type:

list of Interval or int

minor_like

1 if the scale is close to a minor scale, 0 otherwise. This affects enharmonics in the Pitch object returned by pitch().

Type:

int

Constructor

Parameters:
  • tonic (Pitch or int) – Pitch of the starting tone

  • type (str or 2-tuple) – Type of the scale. It is either a string of class variable names registered in the ScaleLibrary class or a 2-tuple consisting of the values of the tone_list and minor_like attributes.

Examples

>>> Scale(C4, 'major')
Scale(C4, ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('M7')], 0))
>>> s = Scale(F4, ScaleLibrary.minor)
>>> s.pitches()
[F4, G4, Ab4, Bb4, C5, Db5, Eb5]
>>> s[1], s[14]
(G4, F6)
>>> s.demo().play()

Tone Number

Tone numbers are numbers assigned sequentially to the infinitely-continued tones on the scale with the tonic being 0. Negative tone numbers assigned to the tones below the tonic. For example, for the major scale with its tonic C4, the tone numbers of C5, G5 and F3 are 7, 11, and -4, respectively. Tone numbers are sometimes floating point numbers and are used to represent non-scale tones.

Arithmetic Rules

  • If s is a Scale object, then the value of s[i] is the Pitch object of the tone with tone number i (equivalent to s.pitch(i)).

  • len(a Scale object) returns the number of scale constituent tones.

  • Equivalence comparison (‘==’) between Scale objects results in true only if all the attribute values are equivalent.

  • If s is a Scale object and p is a pitch, then p in s is equivalent to s.is_scale_tone(p).


to_key() Key

Returns a Key object with the same tonic, a major key if the minor_like attribute is 0 and a minor key if it is 1.

is_scale_tone(pitch) bool

Returns true if pitch is a tone on the scale.

Parameters:

pitch (int) – MIDI note number

tonenum(pitch, enharmonic_delta=0.01) Union[int, float]

Converts a Pitch object to a tone number.

Parameters:
  • pitch (Pitch or int) – Pitch object or integer representing a MIDI note number

  • enharmonic_delta (float) – If pitch is a Pitch object and represents a non-scale tone, the result will be increased or decreased by this value depending on enharmonics. If the pitch is raised from the lower scale note, the value is decreased, and vice versa.

Returns:

Tone number. For non-scale tones, a floating-point number computed by linear interpolation is returned.

pitch(tone_number) Pitch

Converts a tone number to a Pitch object. If the tone number is not an integer, a real-valued MIDI note number is first computed by linear interpolation from the nearest scale tones, and then rounded to an integer (if two integer MIDI note numbers are equally closest, the upper one is chosen).

Parameters:

tone_number (int or float) – Tone number

pitches(low=None, high=None) List[Pitch]

Returns a list of pitches on the scale.

Parameters:
  • low (Pitch or int, optional) – Include only pitches of this value or above. By default, this is the scale starting pitch.

  • high (Pitch or int, optional) – Include only pitches of this value or below. By default, this is the highest pitch of the scale constituent tones.

get_near_scale_tone(pitch, round_mode='nearestup') Pitch

Returns the pitch on the scale close to pitch. If s is the Scale object, then s.get_near_scale_tone(p, r) is equivalent to s[takt_roundx(s.tonenum(p), r)].

Parameters:
  • pitch (Pitch or int) – Pitch object or integer representing a MIDI note number

  • round_mode (str or function) – Rounding mode passed to takt_roundx().

demo(noct=1, dir='up', **kwargs) Score

Returns a score of demo performance for the scale.

Parameters:
  • noct (int) – Number of octaves

  • dir (str) – Either ‘up’ (going up), ‘down’ (going down), or ‘updown’ (going up and then down)

  • kwargs – Additional arguments passed to the note function

class ScaleLibrary

Bases: object

A library of various scales. The value of each class variable is a 2-tuple corresponding to the tone_list and minor_like attributes in the Scale class.


major = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('M7')], 0)
ionian = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('M7')], 0)
dorian = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('m7')], 1)
phrygian = ([Interval('P1'), Interval('m2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('m7')], 1)
lydian = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('A4'), Interval('P5'), Interval('M6'), Interval('M7')], 0)
mixolydian = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('m7')], 0)
dominant = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('m7')], 0)
aeolian = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('m7')], 1)
locrian = ([Interval('P1'), Interval('m2'), Interval('m3'), Interval('P4'), Interval('d5'), Interval('m6'), Interval('m7')], 1)
naturalminor = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('m7')], 1)
minor = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('m7')], 1)
melodicminor = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('M7')], 1)
dorianf2 = ([Interval('P1'), Interval('m2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('m7')], 0)
phrygians6 = ([Interval('P1'), Interval('m2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('m7')], 0)
lydianaugmented = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('A4'), Interval('A5'), Interval('M6'), Interval('M7')], 0)
lydiandominant = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('A4'), Interval('P5'), Interval('M6'), Interval('m7')], 0)
overtone = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('A4'), Interval('P5'), Interval('M6'), Interval('m7')], 0)
lydian7th = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('A4'), Interval('P5'), Interval('M6'), Interval('m7')], 0)
mixolydianf6 = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('m7')], 0)
locrians2 = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P4'), Interval('d5'), Interval('m6'), Interval('m7')], 1)
aeolianf5 = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P4'), Interval('d5'), Interval('m6'), Interval('m7')], 1)
halfdiminished = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P4'), Interval('d5'), Interval('m6'), Interval('m7')], 1)
altereddominant = ([Interval('P1'), Interval('m2'), Interval('m3'), Interval('d4'), Interval('d5'), Interval('m6'), Interval('m7')], 1)
superlocrian = ([Interval('P1'), Interval('m2'), Interval('m3'), Interval('d4'), Interval('d5'), Interval('m6'), Interval('m7')], 1)
altered = ([Interval('P1'), Interval('m2'), Interval('A2'), Interval('M3'), Interval('A4'), Interval('m6'), Interval('m7')], 1)
harmonicminor = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('M7')], 1)
phrygiandominant = ([Interval('P1'), Interval('m2'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('m7')], 0)
harmonicminor5below = ([Interval('P1'), Interval('m2'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('m7')], 0)
ukrainiandorian = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('A4'), Interval('P5'), Interval('M6'), Interval('m7')], 0)
altereddorian = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('A4'), Interval('P5'), Interval('M6'), Interval('m7')], 0)
chromatic = ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 0)
wholetone = ([0, 2, 4, 6, 8, 10], 0)
diminished = ([0, 2, 3, 5, 6, 8, 9, 11], 0)
diminishedwh = ([0, 2, 3, 5, 6, 8, 9, 11], 0)
dominantdiminished = ([0, 1, 3, 4, 6, 7, 9, 10], 0)
combdiminished = ([0, 1, 3, 4, 6, 7, 9, 10], 0)
diminishedhw = ([0, 1, 3, 4, 6, 7, 9, 10], 0)
pentatonic = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('P5'), Interval('M6')], 0)
majorpentatonic = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('P5'), Interval('M6')], 0)
minorpentatonic = ([Interval('P1'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('m7')], 1)
japaneseyo = ([Interval('P1'), Interval('M2'), Interval('P4'), Interval('P5'), Interval('m7')], 0)
japaneseyodown = ([Interval('P1'), Interval('M2'), Interval('P4'), Interval('P5'), Interval('M6')], 0)
japanesein = ([Interval('P1'), Interval('m2'), Interval('P4'), Interval('P5'), Interval('m7')], 1)
japaneseindown = ([Interval('P1'), Interval('m2'), Interval('P4'), Interval('P5'), Interval('m6')], 1)
miyakobushi = ([Interval('P1'), Interval('m2'), Interval('P4'), Interval('P5'), Interval('m6')], 1)
minoryonanuki = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('P5'), Interval('m6')], 1)
ryukyu = ([Interval('P1'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('M7')], 0)
blues = ([Interval('P1'), Interval('m3'), Interval('P4'), Interval('d5'), Interval('P5'), Interval('m7')], 1)
doubleharmonic = ([Interval('P1'), Interval('m2'), Interval('M3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('M7')], 1)
gypsyminor = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('A4'), Interval('P5'), Interval('m6'), Interval('M7')], 1)
hungarianminor = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('A4'), Interval('P5'), Interval('m6'), Interval('M7')], 1)
hungariangypsy = ([Interval('P1'), Interval('M2'), Interval('m3'), Interval('A4'), Interval('P5'), Interval('m6'), Interval('m7')], 1)
neapolitanmajor = ([Interval('P1'), Interval('m2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('M6'), Interval('M7')], 1)
neapolitanminor = ([Interval('P1'), Interval('m2'), Interval('m3'), Interval('P4'), Interval('P5'), Interval('m6'), Interval('M7')], 1)
majorlocrian = ([Interval('P1'), Interval('M2'), Interval('M3'), Interval('P4'), Interval('d5'), Interval('m6'), Interval('m7')], 0)
DEG(n) int

Returns an integer whose absolute value is decreased by 1; if n is 0, an exception is raised. This function is useful in diatonic scales for expressing a distance of tone numbers by a scale degree or the number of an interval.

Examples

>>> s = Scale(C4, 'major')
>>> s[DEG(3)]
E4
>>> s[s.tonenum(D4) + DEG(6)]
B4