# Why is MIDI gain based on a factor of 40?

Many documents state that the MIDI standard gain is `40 * log10(CC7/127)`.

I've found only one person asking why, with no replies. This document says that voltage (amplitude) gain is a factor of 20, not 40.

So why a factor of 40? What is the theory behind it? What is the nature of gain being addressed in the MIDI standard?

The answer will be important for me, because I am trying to match the volume of MIDI to an OPL4 device which has a logarithmic scale, and using the MIDI formula clearly gives an incorrect volume level for quiet notes. By comparing output produced by the MIDI formula, I clearly hear that Windows Media Player has higher volumes for quieter notes.

Linear volume boost does not help here, making the sound even uglier: making loud notes louder and not much affecting quiet notes.

Update:

thank you very much for the answers. It was somewhat hard to choose the best one. Therefore our conclusions are: simplicity in implementation (V * V * sample), covering broader dynamic range (96 dB instead of 48 dB).

My suspicion was actually about breaking the human perception of 20 * log having 40 * log, which has steeper curve. I replaced volume table 20 * log, and it now sounds very similar to other players I tried (including WMP).

Regarding questions asked: OPL4 is my own implementation, designed basing on YMF278B datasheet, therefore I have full access to its internals - here's the outcome of my work heavily using hardware acceleration playing more or less properly on the 3.58 MHz machine.

I suspect that at TL=127 chip must mute the output, however it is not declared in the datasheet, and my implementation does not make this assumption.

If total silence is required immediately, panpot=8 can be used until envelope reaches max 1023 under key off condition with appropriate RR=15.

OPL4 (YMF278B) contains OPL3 implementation, however slightly different than in OPL3 and earlier chips because of running at 49.515 instead of 49.715 kHz.

• The terms “theory” and “MIDI” don't go well together. MIDI is just a heap of hacks upon hacks that were created to get the devices of the time to kinda work together, within their respective technical limitations. The whole standard should have been burnt a long time ago and replaced with something properly designed, but for some reason that has never happened. Commented May 23, 2022 at 11:12
• @leftaroundabout: You mean MIDI 2.0? (ducks and runs) Commented May 23, 2022 at 16:31
• @leftaroundabout I suppose you'd also like sheet music notation to be burnt and properly designed? ;) How about music discourse? It's like dancing about architecture, man, a big historical mush of misunderstandings. :/ Actually, the whole music culture in its entirety is unbearably non-designed! It's such a big mess, it's almost as if humans were involved. * shrug * Commented May 23, 2022 at 18:56
• @piiperiReinstateMonica standard notation is one of the most remarkable things that have happened in human history, because it somehow managed to evolve naturally into a form that does its job amazingly well – I could hardly think of any way that a plan-designed system could possibly be better for common practice music. Now, perhaps you'll say that MIDI also does “its job” just fine, of linking the digital synthesizers of its time together. Only, that's not what most people use it for, nor was its original ambition so limited. It's at this point a as if Beethoven had used mensural notation. Commented May 23, 2022 at 19:49
• @leftaroundabout I personally love Midi because of the way it can hacked and bend from its original design, but maybe it's just me :D
– Tom
Commented May 23, 2022 at 19:50

This definition of MIDI volume appears in the General MIDI Level 2 specification, on the bottom of PDF page 10 and top of PDF page 11 (printed pages 6 and 7):

Regarding the curve of volume change messages, the square of the value is proportional to the volume.

Example

CC#7 amplitude proportional to
127 0 dB 127 × 127 = 16129
96 -4.9 dB 96 × 96 = 9216
64 -11.9 dB 64 × 64 = 4096
32 -23.9 dB 32 × 32 = 1024
16 -36.0 dB 16 × 16 = 256
0 -∞ 0 × 0 = 0

The formula used is: gain in dB = 40 * log10(cc7/127)

To simplify the math in my descriptions, I will use a relative MIDI volume from 0 to 1.

V is the relative MIDI volume from 0 to 1.
V = (MIDI volume value) / 127

L is the relative sound pressure level in decibels from -∞ to 0.
L = 40 log10 V

A is relative sound wave amplitude from 0 to 1.
L = 20 log10 A

20 log10 A = 40 log10 V
log10 A = 2 log10 V
log10 A = log10 V2
A = V2

My guess as to why this definition for MIDI volume was chosen is that it is possibly supposed to roughly represent perceptual loudness: The MIDI volume value halfway between the minimum and maximum roughly represents a sound "half as loud" as the sound represented by the maximum MIDI volume value.

As a rough approximation of a relative "perceptual loudness" measure, I looked at the definition of a sone. In a grossly simplified terms:

• doubling the perceived loudness doubles the sone value.
• an increase in sound pressure level of 10 dB corresponds to a doubling of the loudness in sones.

I used these simplified definitions to create a "relative sones" value like this:

N is the relative perceptual loudness in "relative sones" from 0 to 1.

L N
0 1
-10 1/2
-20 1/4
-30 1/8
-40 1/16

N = 2(L / 10)
log2 N = L / 10
10 log2 N = L

To see how relative MIDI volume V compares to relative perceptual loudness N, I plugged in the earlier definition of L in terms of V and solved for N:

10 log2 N = 40 log10 V
log2 N = 4 log10 V
(log10 N) / (log10 2) = 4 log10 V
log10 N = (4 log10 2) (log10 V)
log10 N = (log10 24) (log10 V)
log10 N = log10 V(log10 24)
N = V(log10 16)
N = V1.204...

That seems like relative MIDI volume V is pretty close to relative perceptual loudness N, and I guess it's convenient that relative MIDI volume squared gets the relative sound wave amplitude (A = V2). So that's my wild guess why this definition for the MIDI volume was chosen.

• This should be the accepted answer! Commented May 25, 2022 at 5:35
• Is this new for General MIDI Level 2? For their GM System Level 1 compliant OPL4 chip, Yamaha chose to have only 47.625 dB of attenuation range for values 0-127, which is why the OP has this question. Or are "General MIDI Level" and "GM System Level" different concepts. Downloading the spec requires registration, so I'm not interested. Commented May 25, 2022 at 12:37
• @piiperiReinstateMonica There is no difference in meaning between "GM System" and "General MIDI". For example, the download page for the GM1 spec calls it the "'General MIDI System Level 1' specification -- also known as 'GM', 'General MIDI 1' and 'GM 1'". The GM1 spec's title page says "General MIDI System Level 1" so I guess that's the official name of it. The GM2 spec's title page says "General MIDI 2". Commented May 26, 2022 at 1:41
• @piiperiReinstateMonica The GM1 spec doesn't mention this volume defintion. I looked again and the "General MIDI System Level 1 Developer Guidelines" does mention this definition. It also says it "follows the standard 'A' and 'K' potentiometer tapers" and it came from Yamaha: "The recommended volume response curves [...] were provided [...] by Yamaha Corporation. Roland uses the same response curve, and other Japanese manufacturers who are members of the AMEI have agreed to do the same." Commented May 26, 2022 at 1:49
• Maybe the OPL4 designers had a technical reason to do the voice level control the way they did, or maybe it's because for some other reason, cultural, communicational, ... arbitrary preference? Who knows. But with that implementation, even though it's advertised as being compliant, it's not possible to cover the 84 dB range, and it's not possible to mute a voice and bring it back up without stopping and re-starting the sample. Go figure. Commented May 27, 2022 at 17:02

I don’t know the reason but a bit of math gives a reason.

First, a note: how a sound generator responds to a CC value is entirely up to the manufacturer or developer of the generator. What I found about CC7 is that the `40*log(CC7/127)` formula is a suggestion. Also, this is not gain, it’s “volume”. What does volume mean in this context? Whatever the manufacturer or developer of the sound generator wants it to mean.

One good reason to use a factor of 40 instead of 20 or 10 is that the possible output values of that function range from -84 to 0 (ignoring the input value of zero for the moment). With a factor of 20, the output only ranges from -42 to 0, and for a factor of 10 it’s only 21 to 0.

Of course the relationship inside the sound generator between the output value of the function and the gain (or attenuation) of the output amp can be made in any way. Also the characteristics of the amplifier are up in the air. That said, if the output of the function is used to set dB of attenuation, then a range of 84 dB is much more useful than 42 or less.

A factor could be chosen such that the output is -127 to 0, but in dB that is actually an overly broad range for output amp attenuation.

Why are we ignoring the input value of 0? Because the mathematical log of 0 is -infinity, which obviously will have to be handled specially in some way in and computer system applying this scaling function.

One way to handle it is to change the function to `40*log((CC7+1)/128)`, which outputs -84 to 0. Another way is to insert an `if CC7==0` statement that catches the invalid input and can just set the output to 0. Other solutions exist.

• Interestingly, according to page 18 of the Yamaha OPL4 aka YMF278B's Application Manual (link on Wikipedia), the maximum attenuation that can be set for a voice (there are 24 simultaneous voices) by setting all 7 bits of the TOTAL LEVEL register is only -(24+12+6+3+1.5+0.75+0.375) = -47.625 dB. And complete silence is not possible at all, except by stopping the voice, AFAIK. So even though the OPL4 is from Yamaha and the manual says that with an external sample ROM module it "complies with GM System Level 1", apparently Yamaha didn't think -84 dB of attenuation is needed, let alone -infinity. Commented May 24, 2022 at 15:07

The standard states

Regarding the curve of volume change messages, the square of the value is proportional to the volume.

The squaring means that you get 40dB as the proportionality factor rather than 20dB.

MIDI volume is interpreted by multiplying sample values with the square of the controller value after normalisation. This is a 7-bit controller, and synthesis is typically done using 16-bit D/A converters. Squaring the value makes the multiplier 14 bits, covering the possible dynamic range pretty much completely while making for a simple implementation.

The formula is what results in terms of dB values and does not really reflect the simplicity of what actually happens in terms of implementation. That is somewhat similar to how a logarithmic potentiometer for volume control actually is done in practice, with "logarithmic" being not much more than a euphemism for "non-linear" inspired by the logarithmicity of loudness scales.

• This answer confuses me in many ways. Right off the bat, what about midi volume for synths that have no samples and no D/A converters? Commented May 23, 2022 at 12:03
• Also, logarithmic is not a euphemism for non-linear. There are many non-linear curves, functions, etc that are also not logarithmic. Geometric functions are an example. In terms of audio electronics, the concept of linearity is further complicated by the fact that “linear” means something a bit different in electronics from what it means in mathematics. Commented May 23, 2022 at 12:12
• Also, if you multiply a sample with a volume value of 14bits, this sample cannot be bigger than 2 bits to fit in the 16 bits output. That's very low quality! You can of course reduce it afterward but squaring does not improve the midi information actually.
– Tom
Commented May 23, 2022 at 15:15
• @ToddWilcox: While "logarithmic" is not generally a euphemism for non-linear, the term "logarithmic pot" is often used to refer to a wide range of potentiometers. Commented May 23, 2022 at 21:16