music21.dynamics

Classes and functions for creating and manipulating dynamic symbols. Rather than subclasses, the Dynamic object is often specialized by parameters.

Dynamic

class music21.dynamics.Dynamic(value=None, **keywords)

Object representation of Dynamics.

>>> pp1 = dynamics.Dynamic('pp')
>>> pp1.value
'pp'
>>> pp1.longName
'pianissimo'
>>> pp1.englishName
'very soft'

Dynamics can also be specified on a 0 to 1 scale with 1 being the loudest (see dynamicStrFromDecimal() above)

>>> ppp = dynamics.Dynamic(0.15)  # on 0 to 1 scale
>>> ppp.value
'ppp'
>>> print('%.2f' % ppp.volumeScalar)
0.15

Note that we got lucky last time because the dynamic 0.15 exactly corresponds to what we’ve considered the default for ‘ppp’. Here we assign 0.98 which is close to the 0.9 that is the default for ‘fff’ – but the 0.98 will be retained in the .volumeScalar

>>> loud = dynamics.Dynamic(0.98)  # on 0 to 1 scale
>>> loud.value
'fff'
>>> print('%.2f' % loud.volumeScalar)
0.98

Transferring the .value (‘fff’) to a new Dynamic object will set the volumeScalar back to 0.9

>>> loud2 = dynamics.Dynamic(loud.value)
>>> loud2.value
'fff'
>>> print('%.2f' % loud2.volumeScalar)
0.90

Custom dynamics are possible:

>>> myDyn = dynamics.Dynamic('rfzsfmp')
>>> myDyn.value
'rfzsfmp'
>>> print(myDyn.volumeScalar)
0.5
>>> myDyn.volumeScalar = 0.87
>>> myDyn.volumeScalar
0.87

Dynamics can be placed anywhere in a stream.

>>> s = stream.Stream()
>>> s.insert(0, note.Note('E-4', type='half'))
>>> s.insert(2, note.Note('F#5', type='half'))
>>> s.insert(0, dynamics.Dynamic('pp'))
>>> s.insert(1, dynamics.Dynamic('mf'))
>>> s.insert(3, dynamics.Dynamic('fff'))
>>> s.show()
../_images/dynamics_simple.png

Dynamic bases

Dynamic read-only properties

Read-only properties inherited from Music21Object:

Read-only properties inherited from ProtoM21Object:

Dynamic read/write properties

Dynamic.value

Get or set the value of this dynamic, which sets the long and English names of this Dynamic. The value is a string specification.

>>> p = dynamics.Dynamic('p')
>>> p.value
'p'
>>> p.englishName
'soft'
>>> p.longName
'piano'
>>> p.value = 'f'
>>> p.value
'f'
>>> p.englishName
'loud'
>>> p.longName
'forte'
Dynamic.volumeScalar

Get or set the volume scalar for this dynamic. If not explicitly set, a default volume scalar will be provided. Any number between 0 and 1 can be used to set the volume scalar, overriding the expected behavior.

As mezzo is at 0.5, the unit interval range is doubled for generating final output. The default output is 0.5.

>>> d = dynamics.Dynamic('mf')
>>> d.volumeScalar
0.55...
>>> d.volumeScalar = 0.1
>>> d.volumeScalar
0.1
>>> d.value
'mf'

int(volumeScalar * 127) gives the MusicXML <sound dynamics=”x”/> tag

>>> xmlOut = musicxml.m21ToXml.GeneralObjectExporter().parse(d).decode('utf-8')
>>> print(xmlOut)
<?xml...
<direction>
    <direction-type>
      <dynamics default-x="-36" default-y="-80">
        <mf />
      </dynamics>
    </direction-type>
    <sound dynamics="12" />
</direction>...

Read/write properties inherited from Music21Object:

Dynamic methods

Methods inherited from Music21Object:

Methods inherited from ProtoM21Object:

Dynamic instance variables

Dynamic.englishName

the name of this dynamic in English.

>>> d = dynamics.Dynamic('pp')
>>> d.englishName
'very soft'
Dynamic.longName

the name of this dynamic in Italian.

>>> d = dynamics.Dynamic('pp')
>>> d.longName
'pianissimo'
Dynamic.placement

Staff placement: ‘above’, ‘below’, or None.

A setting of None implies that the placement will be determined by notation software and no particular placement is demanded.

This is not placed in the .style property, since for some dynamics, the placement above or below an object has semantic meaning and is not purely presentational. For instance, a dynamic placed between two staves in a piano part implies that it applies to both hands, while one placed below the lower staff would apply only to the left hand.

Instance variables inherited from Music21Object:

Crescendo

class music21.dynamics.Crescendo(*spannedElements, **keywords)

A spanner crescendo wedge.

>>> d = dynamics.Crescendo()
>>> d.spread
15
>>> d.spread = 20
>>> d.spread
20
>>> d.type
'crescendo'

Crescendo bases

Crescendo read-only properties

Read-only properties inherited from Music21Object:

Read-only properties inherited from ProtoM21Object:

Crescendo read/write properties

Read/write properties inherited from Music21Object:

Crescendo methods

Methods inherited from Spanner:

Methods inherited from Music21Object:

Methods inherited from ProtoM21Object:

Crescendo instance variables

Instance variables inherited from Music21Object:

Diminuendo

class music21.dynamics.Diminuendo(*spannedElements, **keywords)

A spanner diminuendo wedge.

>>> d = dynamics.Diminuendo()
>>> d.spread = 20
>>> d.spread
20

Diminuendo bases

Diminuendo read-only properties

Read-only properties inherited from Music21Object:

Read-only properties inherited from ProtoM21Object:

Diminuendo read/write properties

Read/write properties inherited from Music21Object:

Diminuendo methods

Methods inherited from Spanner:

Methods inherited from Music21Object:

Methods inherited from ProtoM21Object:

Diminuendo instance variables

Instance variables inherited from Music21Object:

DynamicWedge

class music21.dynamics.DynamicWedge(*spannedElements, **keywords)

Common base-class for Crescendo and Diminuendo.

DynamicWedge bases

DynamicWedge read-only properties

Read-only properties inherited from Music21Object:

Read-only properties inherited from ProtoM21Object:

DynamicWedge read/write properties

Read/write properties inherited from Music21Object:

DynamicWedge methods

Methods inherited from Spanner:

Methods inherited from Music21Object:

Methods inherited from ProtoM21Object:

DynamicWedge instance variables

Instance variables inherited from Music21Object:

Functions

music21.dynamics.dynamicStrFromDecimal(n)

Given a decimal from 0 to 1, return a string representing a dynamic with 0 being the softest (0.01 = ‘ppp’) and 1 being the loudest (0.9+ = ‘fff’) 0 returns “n” (niente), while ppp and fff are the loudest dynamics used.

>>> dynamics.dynamicStrFromDecimal(0.25)
'pp'
>>> dynamics.dynamicStrFromDecimal(1)
'fff'