Types

Here we examine the key data types and demonstrate the rich reprs.

from pyabc2 import PitchClass, Pitch, Note, Key, Tune

Pitch class

A pitch class is a pitch without octave. For PitchClass, the key attribute is value, which represents the distance (in half steps) from C.

We can initialize it that way:

PitchClass(6)
F♯

Or we can initialize it using a name string:

Fb = PitchClass.from_name("Fb")
Fb
F♭

PitchClasss have various useful attributes derived from the value.

Fb.equivalent_sharp
D𝄪

Pitch

For Pitch, the key attribute similarly is value, but now that represents the distance (in half steps) from C₀ (four octaves below C₄, “middle C”).

Pitch(39)
E♭3
Pitch.from_name("Fb4")
F♭4

Pitchs have various useful attributes derived from the value.

p = Pitch.from_name("A4")
p
A4
p.equal_temperament_frequency
440.0
p.piano_key_number
49
p.octave
4

Note

A note (Note) has a pitch and a duration. The easiest way to create one is using ABC notation:

Note.from_abc("G2")
G4𝅘𝅥
Note.from_abc("f24")
F5(3𝅝)
Note.from_abc("D,,3/")
D2𝅘𝅥𝅮.

Pitch attributes work here too.

n = Note.from_abc("A2")
n
A4𝅘𝅥
n.equal_temperament_frequency
440.0

Interval

A SignedInterval is created when two pitches are subtracted.

p = Pitch.from_name("D3")

for dv in range(-14, 32):
    pn = Pitch(p.value + dv)
    i = pn - p
    print(p.unicode(), "→", pn.unicode(), "\t", i)
D₃ → C₂ 	 -[P8+M2]
D₃ → C♯₂ 	 -[P8+m2]
D₃ → D₂ 	 -[P8]
D₃ → E♭₂ 	 -[M7]
D₃ → E₂ 	 -[m7]
D₃ → F₂ 	 -[M6]
D₃ → F♯₂ 	 -[m6]
D₃ → G₂ 	 -[P5]
D₃ → G♯₂ 	 -[A4]
D₃ → A₂ 	 -[P4]
D₃ → B♭₂ 	 -[M3]
D₃ → B₂ 	 -[m3]
D₃ → C₃ 	 -[M2]
D₃ → C♯₃ 	 -[m2]
D₃ → D₃ 	 P1
D₃ → E♭₃ 	 m2
D₃ → E₃ 	 M2
D₃ → F₃ 	 m3
D₃ → F♯₃ 	 M3
D₃ → G₃ 	 P4
D₃ → G♯₃ 	 A4
D₃ → A₃ 	 P5
D₃ → B♭₃ 	 m6
D₃ → B₃ 	 M6
D₃ → C₄ 	 m7
D₃ → C♯₄ 	 M7
D₃ → D₄ 	 P8
D₃ → E♭₄ 	 P8+m2
D₃ → E₄ 	 P8+M2
D₃ → F₄ 	 P8+m3
D₃ → F♯₄ 	 P8+M3
D₃ → G₄ 	 P8+P4
D₃ → G♯₄ 	 P8+A4
D₃ → A₄ 	 P8+P5
D₃ → B♭₄ 	 P8+m6
D₃ → B₄ 	 P8+M6
D₃ → C₅ 	 P8+m7
D₃ → C♯₅ 	 P8+M7
D₃ → D₅ 	 2(P8)
D₃ → E♭₅ 	 2(P8)+m2
D₃ → E₅ 	 2(P8)+M2
D₃ → F₅ 	 2(P8)+m3
D₃ → F♯₅ 	 2(P8)+M3
D₃ → G₅ 	 2(P8)+P4
D₃ → G♯₅ 	 2(P8)+A4
D₃ → A₅ 	 2(P8)+P5

Key

A Key is most easily created by passing an ABC notation key/mode spec string.

Key("C")
Key(tonic=C, mode='Major')
Key("Dmaj")
Key(tonic=D, mode='Major')
Key("Ador")
Key(tonic=A, mode='Dorian')
Key("Gm")
Key(tonic=G, mode='Minor')

The tonic is a PitchClass.

Key("Ebmix").tonic
E♭

Tune

Pass an ABC string to create a Tune.

from pyabc2.sources import load_example_abc

abc = load_example_abc("For the Love of Music")

print(abc)
T:For The Love Of Music
R:slip jig
C:Liz Carroll
M:9/8
L:1/8
K:G
GED DEG AGB | AGG GBd edd | GED DEG AGc | Bee BAB GEE :|
ged e/f/gB GB/c/d | ged egb a2f | ged e/f/gB AB/c/d | edB dBG ABd |
ged e/f/gB GB/c/d | ged egb a2a | bee e/f/ge a2g | edB dBG ABA ||
tune = Tune(abc)
tune
Tune(title='For The Love Of Music', key=Gmaj, type='slip jig')
tune.key
Key(tonic=G, mode='Major')

Tune initialization (currently) parses the ABC and expands repeats/endings into measures of notes, here from 12 written measures into 16.

len(tune.measures)
16
tune.measures[8]
[Note(value=67, name='G5', duration=1/8),
 Note(value=64, name='E5', duration=1/8),
 Note(value=62, name='D5', duration=1/8),
 Note(value=64, name='E5', duration=1/16),
 Note(value=66, name='F#5', duration=1/16),
 Note(value=67, name='G5', duration=1/8),
 Note(value=59, name='B4', duration=1/8),
 Note(value=55, name='G4', duration=1/8),
 Note(value=59, name='B4', duration=1/16),
 Note(value=60, name='C5', duration=1/16),
 Note(value=62, name='D5', duration=1/8)]
display(*tune.measures[8])
G5𝅘𝅥𝅮
E5𝅘𝅥𝅮
D5𝅘𝅥𝅮
E5𝅘𝅥𝅯
F♯5𝅘𝅥𝅯
G5𝅘𝅥𝅮
B4𝅘𝅥𝅮
G4𝅘𝅥𝅮
B4𝅘𝅥𝅯
C5𝅘𝅥𝅯
D5𝅘𝅥𝅮
tune.print_measures(n=9)
01: G E D D E G A G B
02: A G G G B d e d d
03: G E D D E G A G c
04: B e e B A B G E E
05: G E D D E G A G B
06: A G G G B d e d d
07: G E D D E G A G c
08: B e e B A B G E E
09: g e d e/2 f/2 g B G B/2 c/2 d