### 3.5.5 Controlling MIDI dynamics

MIDI dynamics are implemented by the Dynamic_performer which lives by default in the Voice context. It is possible to control the overall MIDI volume, the relative volume of dynamic markings and the relative volume of different instruments.

#### Dynamic marks

Dynamic marks are translated to a fixed fraction of the available MIDI volume range. The default fractions range from 0.25 for ppppp to 0.95 for fffff. The set of dynamic marks and the associated fractions can be seen in ‘`../scm/midi.scm`’, see Other sources of information. This set of fractions may be changed or extended by providing a function which takes a dynamic mark as its argument and returns the required fraction, and setting `Score.dynamicAbsoluteVolumeFunction` to this function.

For example, if a rinforzando dynamic marking, `\rfz`, is required, this will not by default have any effect on the MIDI volume, as this dynamic marking is not included in the default set. Similarly, if a new dynamic marking has been defined with `make-dynamic-script` that too will not be included in the default set. The following example shows how the MIDI volume for such dynamic markings might be added. The Scheme function sets the fraction to 0.9 if a dynamic mark of rfz is found, or calls the default function otherwise.

>     #(define (myDynamics dynamic)
>         (if (equal? dynamic "rfz")
>           0.9
>           (default-dynamic-absolute-volume dynamic)))

>     \score {
>       \new Staff {
>         \set Staff.midiInstrument = #"cello"
>         \set Score.dynamicAbsoluteVolumeFunction = #myDynamics
>         \new Voice {
>           \relative c'' {
>             a4\pp b c-\rfz
>           }
>         }
>       }
>       \layout {}
>       \midi {}
>     }

Alternatively, if the whole table of fractions needs to be redefined, it would be better to use the default-dynamic-absolute-volume procedure in ‘`../scm/midi.scm`’ and the associated table as a model. The final example in this section shows how this might be done.

#### Overall MIDI volume

The minimum and maximum overall volume of MIDI dynamic markings is controlled by setting the properties `midiMinimumVolume` and `midiMaximumVolume` at the `Score` level. These properties have an effect only at the start of a voice and on dynamic marks. The fraction corresponding to each dynamic mark is modified with this formula

>     midiMinimumVolume + (midiMaximumVolume - midiMinimumVolume) * fraction

In the following example the dynamic range of the overall MIDI volume is limited to the range 0.2 - 0.5.

>     \score {
>       <<
>         \new Staff {
>           \key g \major
>           \time 2/2
>           \set Staff.midiInstrument = #"flute"
>           \new Voice \relative c''' {
>             r2 g\mp g fis~
>             fis4 g8 fis e2~
>             e4 d8 cis d2
>           }
>         }
>         \new Staff {
>           \key g \major
>           \set Staff.midiInstrument = #"clarinet"
>           \new Voice \relative c'' {
>             b1\p a2. b8 a
>             g2. fis8 e
>             fis2 r
>           }
>         }
>       >>
>       \layout {}
>       \midi {
>         \tempo 2 = 72
>         \context {
>           \Score
>           midiMinimumVolume = #0.2
>           midiMaximumVolume = #0.5
>         }
>       }
>     }

#### Equalizing different instruments (i)

If the minimum and maximum MIDI volume properties are set in the `Staff` context the relative volumes of the MIDI instruments can be controlled. This gives a basic instrument equalizer, which can enhance the quality of the MIDI output remarkably.

In this example the volume of the clarinet is reduced relative to the volume of the flute.

>     \score {
>       <<
>         \new Staff {
>           \key g \major
>           \time 2/2
>           \set Staff.midiInstrument = #"flute"
>           \set Staff.midiMinimumVolume = #0.7
>           \set Staff.midiMaximumVolume = #0.9
>           \new Voice \relative c''' {
>             r2 g\mp g fis~
>             fis4 g8 fis e2~
>             e4 d8 cis d2
>           }
>         }
>         \new Staff {
>           \key g \major
>           \set Staff.midiInstrument = #"clarinet"
>           \set Staff.midiMinimumVolume = #0.3
>           \set Staff.midiMaximumVolume = #0.6
>           \new Voice \relative c'' {
>             b1\p a2. b8 a
>             g2. fis8 e
>             fis2 r
>           }
>         }
>       >>
>       \layout {}
>       \midi {
>         \tempo 2 = 72
>       }
>     }

#### Equalizing different instruments (ii)

If the MIDI minimum and maximum volume properties are not set LilyPond will, by default, apply a small degree of equalization to a few instruments. The instruments and the equalization applied are shown in the table instrument-equalizer-alist in ‘`../scm/midi.scm`’.

This basic default equalizer can be replaced by setting `instrumentEqualizer` in the `Score` context to a new Scheme procedure which accepts a MIDI instrument name as its only argument and returns a pair of fractions giving the minimum and maximum volumes to be applied to that instrument. This replacement is done in the same way as shown for resetting the `dynamicAbsoluteVolumeFunction` at the start of this section. The default equalizer, default-instrument-equalizer, in ‘`../scm/midi.scm`’ shows how such a procedure might be written.

The following example sets the relative flute and clarinet volumes to the same values as the previous example.

>     #(define my-instrument-equalizer-alist '())

>     #(set! my-instrument-equalizer-alist
>       (append
>         '(
>           ("flute" . (0.7 . 0.9))
>           ("clarinet" . (0.3 . 0.6)))
>         my-instrument-equalizer-alist))

>     #(define (my-instrument-equalizer s)
>       (let ((entry (assoc s my-instrument-equalizer-alist)))
>         (if entry
>           (cdr entry))))

>     \score {
>       <<
>         \new Staff {
>           \key g \major
>           \time 2/2
>           \set Score.instrumentEqualizer = #my-instrument-equalizer
>           \set Staff.midiInstrument = #"flute"
>           \new Voice \relative c''' {
>             r2 g\mp g fis~
>             fis4 g8 fis e2~
>             e4 d8 cis d2
>           }
>         }
>         \new Staff {
>           \key g \major
>           \set Staff.midiInstrument = #"clarinet"
>           \new Voice \relative c'' {
>             b1\p a2. b8 a
>             g2. fis8 e
>             fis2 r
>           }
>         }
>       >>
>       \layout { }
>       \midi {
>         \tempo 2 = 72
>       }
>     }

其他语言：deutsch, español, français, italiano, 日本語。
About automatic language selection.

#### LilyPond — Notation Reference v2.18.2 (稳定分支).