music21.clef

This module defines numerous subclasses of Clef, providing object representations for all commonly used clefs. Clef objects are often found within Measure objects.

Functions

music21.clef.bestClef(streamObj, allowTreble8vb=False, recurse=False)

Returns the clef that is the best fit for notes and chords found in this Stream.

>>> import random
>>> a = stream.Stream()
>>> for x in range(30):
...    n = note.Note()
...    n.pitch.midi = random.randint(70, 81)
...    a.insert(n)
>>> b = clef.bestClef(a)
>>> b
<music21.clef.TrebleClef>
>>> b.line
2
>>> b.sign
'G'
>>> c = stream.Stream()
>>> for x in range(10):
...    n = note.Note()
...    n.pitch.midi = random.randint(45, 54)
...    c.insert(n)
>>> d = clef.bestClef(c)
>>> d
<music21.clef.BassClef>
>>> d.line
4
>>> d.sign
'F'

This does not automatically get a flat representation of the Stream.

There are a lot more high notes in a (30) than low notes in c (10), but it will not matter here, because the pitches in a will not be found:

>>> c.insert(0, a)
>>> clef.bestClef(c)
<music21.clef.BassClef>

But with recursion, it will matter:

>>> clef.bestClef(c, recurse=True)
<music21.clef.TrebleClef>
music21.clef.clefFromString(clefString, octaveShift=0)

Returns a Clef object given a string like “G2” or “F4” etc.

Does not refer to a violin/guitar string.

>>> tc = clef.clefFromString("G2")
>>> tc
<music21.clef.TrebleClef>
>>> nonStandard1 = clef.clefFromString("F1")
>>> nonStandard1
<music21.clef.FClef>
>>> nonStandard1.line
1
>>> nonStandard2 = clef.clefFromString("D4")
>>> nonStandard2
<music21.clef.PitchClef>
>>> nonStandard2.sign
'D'
>>> nonStandard2.line
4
>>> tc8vb = clef.clefFromString("G2", -1)
>>> tc8vb
<music21.clef.Treble8vbClef>

Three special clefs, Tab, Percussion, and None are also supported.

>>> tabClef = clef.clefFromString("TAB")
>>> tabClef
<music21.clef.TabClef>

Case does not matter.

>>> tc8vb = clef.clefFromString("g2", -1)
>>> tc8vb
<music21.clef.Treble8vbClef>
>>> percussionClef = clef.clefFromString('Percussion')
>>> percussionClef
<music21.clef.PercussionClef>
>>> noClef = clef.clefFromString('None')
>>> noClef
<music21.clef.NoClef>

Invalid line numbers raise an exception:

>>> invalidClef = clef.clefFromString("F6")
Traceback (most recent call last):
music21.clef.ClefException: line number (second character) must be 1-5;
            do not use this function for clefs on special staves such as 'F6'

Can find any clef in the module

>>> clef.clefFromString('Treble')
<music21.clef.TrebleClef>
>>> clef.clefFromString('trebleclef')
<music21.clef.TrebleClef>
>>> clef.clefFromString('treble8vb')
<music21.clef.Treble8vbClef>

Clef

class music21.clef.Clef

A Clef is a basic music21 object for representing musical clefs (Treble, Bass, etc.)

Some clefs only represent the graphical element of the clef, such as G clef, which is subclassed by TrebleClef() and FrenchViolinClef().

>>> tc = clef.TrebleClef()
>>> tc
<music21.clef.TrebleClef>
>>> tc.sign
'G'
>>> tc.line
2

Most clefs also have a “lowestLine” function which represents the diatonicNoteNum of the note that would fall on the lowest line if the Clef were put on a five-line staff. (Where C4,C#4,C##4,C-4 etc. = 29, all types of D4 = 30, etc.)

>>> tc.lowestLine
31

Clef bases

Clef read-only properties

Read-only properties inherited from Music21Object:

Clef read/write properties

Clef.octaveChange

The number of octaves that the clef “transposes”, generally 0.

>>> tc = clef.TrebleClef()
>>> tc.octaveChange
0
>>> clef.Treble8vbClef().octaveChange
-1

Changing octaveChange changes lowestLine (but not vice-versa)

>>> tc.lowestLine
31
>>> tc.octaveChange = 1
>>> tc.lowestLine
38
>>> tc.octaveChange = -1
>>> tc.lowestLine
24

Read/write properties inherited from Music21Object:

Clef methods

Methods inherited from Music21Object:

Clef instance variables

Clef.line

The line, counting from the bottom up, that the clef resides on.

>>> clef.AltoClef().line
3
>>> clef.TenorClef().line
4

May be None:

>>> print(clef.NoClef().line)
None
Clef.sign

The sign of the clef, generally, ‘C’, ‘G’, ‘F’, ‘percussion’, ‘none’ or None.

>>> alto = clef.AltoClef()
>>> alto.sign
'C'
>>> percussion = clef.PercussionClef()
>>> percussion.sign
'percussion'

Note the difference here:

>>> clef.Clef().sign is None
True
>>> clef.NoClef().sign
'none'

Instance variables inherited from Music21Object:

TrebleClef

class music21.clef.TrebleClef

The most common clef of all, a treble clef.

>>> a = clef.TrebleClef()
>>> a.sign
'G'
>>> a.line
2
>>> a.lowestLine
31
>>> note.Note('E4').pitch.diatonicNoteNum
31

TrebleClef bases

TrebleClef read-only properties

Read-only properties inherited from Music21Object:

TrebleClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

TrebleClef methods

Methods inherited from Music21Object:

TrebleClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

BassClef

class music21.clef.BassClef

A standard Bass Clef

>>> a = clef.BassClef()
>>> a.sign
'F'

BassClef bases

BassClef read-only properties

Read-only properties inherited from Music21Object:

BassClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

BassClef methods

Methods inherited from Music21Object:

BassClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

AltoClef

class music21.clef.AltoClef

A C AltoClef, common for violas.

>>> a = clef.AltoClef()
>>> a.sign
'C'
>>> a.line
3

AltoClef bases

AltoClef read-only properties

Read-only properties inherited from Music21Object:

AltoClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

AltoClef methods

Methods inherited from Music21Object:

AltoClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

Bass8vaClef

class music21.clef.Bass8vaClef

A rarely used Bass Clef an octave higher.

>>> a = clef.Bass8vaClef()
>>> a.sign
'F'

Bass8vaClef bases

Bass8vaClef read-only properties

Read-only properties inherited from Music21Object:

Bass8vaClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

Bass8vaClef methods

Methods inherited from Music21Object:

Bass8vaClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

Bass8vbClef

class music21.clef.Bass8vbClef

A bass clef configured to be an octave lower.

>>> a = clef.Bass8vbClef()
>>> a.sign
'F'
>>> a.octaveChange
-1

Bass8vbClef bases

Bass8vbClef read-only properties

Read-only properties inherited from Music21Object:

Bass8vbClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

Bass8vbClef methods

Methods inherited from Music21Object:

Bass8vbClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

CBaritoneClef

class music21.clef.CBaritoneClef

A Baritone C clef (as opposed to an F Baritone Clef)

>>> a = clef.CBaritoneClef()
>>> a.sign
'C'
>>> a.line
5

CBaritoneClef bases

CBaritoneClef read-only properties

Read-only properties inherited from Music21Object:

CBaritoneClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

CBaritoneClef methods

Methods inherited from Music21Object:

CBaritoneClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

CClef

class music21.clef.CClef

A generic C Clef, with no line set

>>> a = clef.CClef()
>>> a.sign
'C'

CClef bases

CClef read-only properties

Read-only properties inherited from Music21Object:

CClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

CClef methods

Methods inherited from Music21Object:

CClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

FBaritoneClef

class music21.clef.FBaritoneClef

an F Baritone Clef

>>> a = clef.FBaritoneClef()
>>> a.sign
'F'
>>> a.line
3
>>> b = clef.CBaritoneClef()
>>> a.lowestLine == b.lowestLine
True
>>> a.sign == b.sign
False

FBaritoneClef bases

FBaritoneClef read-only properties

Read-only properties inherited from Music21Object:

FBaritoneClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

FBaritoneClef methods

Methods inherited from Music21Object:

FBaritoneClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

FClef

class music21.clef.FClef

A generic F-Clef, like a Bass clef

>>> a = clef.FClef()
>>> a.sign
'F'

FClef bases

FClef read-only properties

Read-only properties inherited from Music21Object:

FClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

FClef methods

Methods inherited from Music21Object:

FClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

FrenchViolinClef

class music21.clef.FrenchViolinClef

A G Clef that appears in many old French Violin scores, appearing on the lowest line, and thus higher than a treble clef.

>>> a = clef.FrenchViolinClef()
>>> a.sign
'G'
>>> a.line
1

FrenchViolinClef bases

FrenchViolinClef read-only properties

Read-only properties inherited from Music21Object:

FrenchViolinClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

FrenchViolinClef methods

Methods inherited from Music21Object:

FrenchViolinClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

GClef

class music21.clef.GClef

A generic G Clef

>>> a = clef.GClef()
>>> a.sign
'G'
>>> a.lowestLine is None
True

GClef bases

GClef read-only properties

Read-only properties inherited from Music21Object:

GClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

GClef methods

Methods inherited from Music21Object:

GClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

GSopranoClef

class music21.clef.GSopranoClef

A G clef on the middle line, formerly occasionally used for soprano parts.

>>> a = clef.GSopranoClef()
>>> a.sign
'G'
>>> a.line
3

GSopranoClef bases

GSopranoClef read-only properties

Read-only properties inherited from Music21Object:

GSopranoClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

GSopranoClef methods

Methods inherited from Music21Object:

GSopranoClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

JianpuClef

class music21.clef.JianpuClef

Jianpu notation does not use a clef, but musicxml marks it with a specialized “jianpu” sign.

>>> jc = clef.JianpuClef()
>>> jc.sign
'jianpu'

JianpuClef bases

JianpuClef read-only properties

Read-only properties inherited from Music21Object:

JianpuClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

JianpuClef methods

Methods inherited from Music21Object:

JianpuClef instance variables

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

MezzoSopranoClef

class music21.clef.MezzoSopranoClef

A C clef with C on the second line. Perhaps the rarest of the C clefs

>>> a = clef.MezzoSopranoClef()
>>> a.sign
'C'
>>> a.line
2

MezzoSopranoClef bases

MezzoSopranoClef read-only properties

Read-only properties inherited from Music21Object:

MezzoSopranoClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

MezzoSopranoClef methods

Methods inherited from Music21Object:

MezzoSopranoClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

NoClef

class music21.clef.NoClef

represents the absence of a Clef.

>>> nc = clef.NoClef()
>>> nc.sign
'none'

Note that the sign is the string ‘none’ not the None object

>>> nc.sign is None
False

NoClef bases

NoClef read-only properties

Read-only properties inherited from Music21Object:

NoClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

NoClef methods

Methods inherited from Music21Object:

NoClef instance variables

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

PercussionClef

class music21.clef.PercussionClef

represents a Percussion clef.

>>> pc = clef.PercussionClef()
>>> pc.sign
'percussion'
>>> pc.line is None
True

Percussion clefs should not, technically. have a lowest line, but it is a common usage to assume that in pitch-centric contexts to use the pitch numbers from treble clef for percussion clefs. Thus:

>>> pc.lowestLine == clef.TrebleClef().lowestLine
True

PercussionClef bases

PercussionClef read-only properties

Read-only properties inherited from Music21Object:

PercussionClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

PercussionClef methods

Methods inherited from Music21Object:

PercussionClef instance variables

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

PitchClef

class music21.clef.PitchClef

superclass for all other clef subclasses that use pitches...

PitchClef bases

PitchClef read-only properties

Read-only properties inherited from Music21Object:

PitchClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

PitchClef methods

Methods inherited from Music21Object:

PitchClef instance variables

PitchClef.lowestLine

The diatonicNoteNumber of the lowest line of the clef. (Can be none...)

>>> clef.TrebleClef().lowestLine
31

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

SopranoClef

class music21.clef.SopranoClef

A soprano clef, with C on the lowest line (found in Bach often)

>>> a = clef.SopranoClef()
>>> a.sign
'C'
>>> a.line
1

SopranoClef bases

SopranoClef read-only properties

Read-only properties inherited from Music21Object:

SopranoClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

SopranoClef methods

Methods inherited from Music21Object:

SopranoClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

SubBassClef

class music21.clef.SubBassClef

An F clef on the top line.

>>> a = clef.SubBassClef()
>>> a.sign
'F'

SubBassClef bases

SubBassClef read-only properties

Read-only properties inherited from Music21Object:

SubBassClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

SubBassClef methods

Methods inherited from Music21Object:

SubBassClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

TabClef

class music21.clef.TabClef

represents a Tablature clef.

>>> a = clef.TabClef()
>>> a.sign
'TAB'

TabClef bases

TabClef read-only properties

Read-only properties inherited from Music21Object:

TabClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

TabClef methods

Methods inherited from Music21Object:

TabClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

TenorClef

class music21.clef.TenorClef

A C Tenor Clef, often used in bassoon and cello parts and orchestral trombone parts.

>>> a = clef.TenorClef()
>>> a.sign
'C'
>>> a.line
4

TenorClef bases

TenorClef read-only properties

Read-only properties inherited from Music21Object:

TenorClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

TenorClef methods

Methods inherited from Music21Object:

TenorClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

Treble8vaClef

class music21.clef.Treble8vaClef

A treble clef an octave up (such as for piccolos)

>>> a = clef.Treble8vaClef()
>>> a.sign
'G'
>>> a.octaveChange
1

Treble8vaClef bases

Treble8vaClef read-only properties

Read-only properties inherited from Music21Object:

Treble8vaClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

Treble8vaClef methods

Methods inherited from Music21Object:

Treble8vaClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object:

Treble8vbClef

class music21.clef.Treble8vbClef

A vocal tenor treble clef. Also for guitars.

>>> a = clef.Treble8vbClef()
>>> a.sign
'G'
>>> a.octaveChange
-1

Treble8vbClef bases

Treble8vbClef read-only properties

Read-only properties inherited from Music21Object:

Treble8vbClef read/write properties

Read/write properties inherited from Clef:

Read/write properties inherited from Music21Object:

Treble8vbClef methods

Methods inherited from Music21Object:

Treble8vbClef instance variables

Instance variables inherited from PitchClef:

Instance variables inherited from Clef:

Instance variables inherited from Music21Object: