Take the 2-minute tour ×

I am designing a VST Audio plug-in that requires amplitude data be extracted from the incoming signal to be used for velocity settings in the midi domain.

Essentially, I will be receiving values between 0-1(float) and need to convert them to 0-127 int.

Currently the process will be to multply the float value by 100 to give a whole value of +3 decimal places i.e. 103.4567685 OR 005.6778787282

From here I will then round the floats to ints using the floor() function.

However, this will leave me with values between 0-100; however, I need to scale these to 0-127.

Any suggestions on how this could be made possible would be greatly appreciated.

share|improve this question
add comment

6 Answers

If you take some of the suggestions presented here and multiply by 127, you're going to find that 127 is not very well represented in your output. Maybe this won't be a problem for you, but there's a small change that makes a big difference.

You might think that rounding would help, but it does not. The number of values at 127 will still be only half that of the other values, and now the number of values at 0 will also be half of the average.

The correct formula:

int amplitude = static_cast<int>(value * (128-epsilon)); // where epsilon is a very small floating point value
share|improve this answer
You can't give advice like this without telling the poor OP how to obtain epsilon! –  TonyK Jan 27 '11 at 16:10
100 seemed acceptable to him, so 127 probably is/was, however he may not have thought of this or noticed it! –  winwaed Jan 27 '11 at 16:11
@TonyK, the exact value of epsilon isn't terribly important for most real world applications. You can use FLT_EPSILON from limits.h or numeric_limits<float>::epsilon() from limits, or just an arbitrary small number. I wouldn't use numeric_limits<double>::epsilon() because it's only valid for a value of 1.0, and would be too small for 128.0. –  Mark Ransom Jan 27 '11 at 18:19
You are not addressing my point. (See my answer for some common sense.) –  TonyK Jan 27 '11 at 19:55
add comment

The proper way to do this is:

int amplitude = 128 * value ;
if (amplitude == 128) amplitude = 127 ;
share|improve this answer
TonyK is right. The reason for this is, that 64 is the center value for MIDI, implementations in MIDI devices usually use the method above. –  Zuppa Dec 12 '12 at 21:36
add comment

Just multiply your value by 127 and cast it to int. [0,1]*127 => [0,127]

share|improve this answer
Yeah that seems like a good idea. many thanks! –  Forge_13 Jan 27 '11 at 15:04
-1: this does not map all values correctly - see e.g. TonyK's answer or Mark R's answer for a better solution –  Paul R Dec 5 '11 at 22:26
add comment

Range conversion is not that simple. In the case of scaling an integer range [-n; n] to float, using the naive approach of simply multiplying has the effect, that the inverse mapping will likely not map into the original values. Say you've got the set of all numbers in the integer range, and a set of same magnitude in the floating point range, the naive mapping is not bijective.

There's a nice paper about this and example code, for how to implement monotony and ordering preserving mappings at http://kaba.hilvi.org/programming/range/index.htm

share|improve this answer
add comment

I think I must be missing something. why not multiply by 127.0 instead of 100?

share|improve this answer
oh yeah. I didn't think of that. I feel like a fool –  Forge_13 Jan 27 '11 at 15:03
add comment

Multiply the resulting value by 1.27 before using floor()?

share|improve this answer
thankyou! much appreciated –  Forge_13 Jan 27 '11 at 15:04
add comment

Your Answer


