Previous topic

music21.base

Next topic

music21.braille.basic

Table Of Contents

Table Of Contents

This Page

music21.beam

The module defines Beam and Beams (note plural) objects.

The Beams object holds multiple Beam objects (e.g., a 32nd note might have three Beam objects in its Beam object).

The Beams object is stored in Note and Chord objects as their beams attributes. Beams objects can largely be treated as a list.

See meter.TimeSignature.:meth:~music21.meter.TimeSignature.getBeams for a way of getting beam information for a measure given the meter. The meter.TimeSignature.:attr:~music21.meter.TimeSignature.beamSequence attribute holds information about how to beam given the TimeSignature

Run stream.Stream.:meth:~music21.stream.Stream.makeBeams to set beaming information automatically given the current meter.

Suppose you had a measure of two eighths and a quarter and wanted to explicitly beam the two eighth notes. You could do this:

>>> m = stream.Measure()
>>> n1 = note.Note('C4', quarterLength = 0.5)
>>> n2 = note.Note('D4', quarterLength = 0.5)
>>> n3 = note.Note('E4', quarterLength = 1.0)
>>> m.append(n1)
>>> m.append(n2)
>>> m.append(n3)
>>> n1.beams.fill('eighth', type='start')
>>> n2.beams.fill('eighth', type='stop')
>>> n1.beams
<music21.beam.Beams <music21.beam.Beam 1/start>>
>>> n2.beams
<music21.beam.Beams <music21.beam.Beam 1/stop>>

But suppose you wanted something harder: two 16ths, an 8th, a quarter, with the first 3 notes beamed? The first note and 3rd are easy to do, using the method above:

>>> m = stream.Measure()
>>> n1 = note.Note('C4', quarterLength = 0.25)
>>> n2 = note.Note('D4', quarterLength = 0.25)
>>> n3 = note.Note('E4', quarterLength = 0.5)
>>> n4 = note.Note('F4', quarterLength = 1.0)
>>> for n in [n1, n2, n3, n4]:
...     m.append(n)
>>> n1.beams.fill('16th', type='start')
>>> n3.beams.fill('eighth', type='stop')

but the second note has an 8th beam that continues and a 16th beam that stops. So you will need to set them separately:

>>> n2.beams.append('continue')
>>> n2.beams.append('stop')
>>> n2.beams
<music21.beam.Beams <music21.beam.Beam 1/continue>/<music21.beam.Beam 2/stop>>

To get rid of beams on a note do:

>>> n2.beams.beamsList = []

Beams

class music21.beam.Beams

The Beams object stores in it attribute beamsList (a list) all the Beam objects defined above. Thus len(beam.Beams) tells you how many beams the note currently has on it, and iterating over a Beams object gives you each Beam.

>>> n = note.Note(type='16th')
>>> isinstance(n.beams, beam.Beams)
True
>>> n.beams.fill(2, 'start')
>>> len(n.beams)
2
>>> for thisBeam in n.beams:
...     thisBeam.type
'start'
'start'
>>> print n.beams
<music21.beam.Beams <music21.beam.Beam 1/start>/<music21.beam.Beam 2/start>>

Beams bases

Beams methods

Beams.append(type=None, direction=None)

Append a new Beam object to this Beams, automatically creating the Beam object and incrementing the number count.

>>> beams = beam.Beams()
>>> beams.append('start')
>>> beams.beamsList
[<music21.beam.Beam 1/start>]
>>> beams.append('partial', 'right')
>>> beams.beamsList
[<music21.beam.Beam 1/start>, <music21.beam.Beam 2/partial/right>]
Beams.fill(level=None, type=None)

A quick way of setting the beams list for a particular duration, for instance, fill(“16th”) will clear the current list of beams in the Beams object and add two beams. fill(2) will do the same (though note that that is an int, not a string).

It does not do anything to the direction that the beams are going in, or by default. Either set type here or call setAll() on the Beams object afterwards.

Both “eighth” and “8th” work. Adding more than six beams (i.e. things like 512th notes) raises an error.

>>> a = beam.Beams()
>>> a.fill('16th')
>>> len(a)
2
>>> a.fill('32nd', type='start')
>>> len(a)
3
>>> a.beamsList[2]
<music21.beam.Beam 3/start>
>>> a.beamsList[2].type
'start'

Filling a smaller number wipes larger numbers of beams:

>>> a.fill('eighth', type='start')
>>> len(a)
1
Beams.getByNumber(number)

Gets an internal beam object by number.

>>> a = beam.Beams()
>>> a.fill('16th')
>>> a.setAll('start')
>>> a.getByNumber(2).type
'start'
>>> a.getByNumber(30)
Traceback (most recent call last):
IndexError: beam number 30 cannot be accessed
Beams.getNumbers()

Returns a list of all defined beam numbers; it should normally be a set of consecutive integers, but it might not be.

>>> a = beam.Beams()
>>> a.fill('32nd')
>>> a.getNumbers()
[1, 2, 3]
Beams.getTypeByNumber(number)

Get beam type, with direction, by number

>>> a = beam.Beams()
>>> a.fill('16th')
>>> a.setAll('start')
>>> a.setByNumber(2, 'partial-right')
>>> a.getTypeByNumber(2)
'partial-right'
>>> a.getTypeByNumber(1)
'start'
Beams.getTypes()

Returns a list of all beam types defined for the current beams

>>> a = beam.Beams()
>>> a.fill('16th')
>>> a.setAll('start')
>>> a.getTypes()
['start', 'start']
Beams.setAll(type, direction=None)

setAll is a method of convenience that sets the type of each of the beam objects within the beamsList to the specified type. It also takes an optional “direction” attribute that sets the direction for each beam (otherwise the direction of each beam is set to None) Acceptable directions (start, stop, continue, etc.) are listed under Beam() above.

>>> a = beam.Beams()
>>> a.fill('16th')
>>> a.setAll('start')
>>> a.getTypes()
['start', 'start']
>>> a.setAll('sexy')
Traceback (most recent call last):
BeamException: beam type cannot be sexy
Beams.setByNumber(number, type, direction=None)

Set an internal beam object by number, or rhythmic symbol level.

>>> a = beam.Beams()
>>> a.fill('16th')
>>> a.setAll('start')
>>> a.setByNumber(1, 'continue')
>>> a.beamsList[0].type
'continue'
>>> a.setByNumber(2, 'stop')
>>> a.beamsList[1].type
'stop'
>>> a.setByNumber(2, 'partial-right')
>>> a.beamsList[1].type
'partial'
>>> a.beamsList[1].direction
'right'
>>> a.setByNumber(30, 'stop')
Traceback (most recent call last):
IndexError: beam number 30 cannot be accessed
>>> a.setByNumber(2, 'crazy')
Traceback (most recent call last):
BeamException: beam type cannot be crazy

Beams instance variables

Beams.feathered

Boolean determining if this is a feathered beam or not (does nothing for now).

Beam

class music21.beam.Beam(type=None, direction=None)

A Beam is an object representation of one single beam, that is, one horizontal line connecting two notes together (or less commonly a note to a rest). Thus it takes two separate Beam objects to represent the beaming of a 16th note.

The Beams object (note the plural) is the object that handles groups of Beam objects; it is defined later on.

Here are two ways to define the start of a beam

>>> b1 = beam.Beam(type='start')
>>> b2 = beam.Beam('start')

Here is a partial beam (that is, one that does not connect to any other note, such as the second beam of a dotted eighth, sixteenth group)

Two ways of doing the same thing

>>> b3 = beam.Beam(type='partial', direction='left')
>>> b4 = beam.Beam('partial', 'left')
>>> b4.number = 1
>>> print b4
<music21.beam.Beam 1/partial/left>
>>> print b2
<music21.beam.Beam None/start>

Beam bases