Midi In & Valuebox: -12 to +12 with message.int_value

Hi. I have created a valuebox that returns a value within the following range: -12 to +12

Through the midi_mapping of the vaulebox I have done the following:

rnt:add_midi_mapping {
  name = "Tools:PhraseTouch:Panel_1:Transpose:Transpose Valuebox",
  invoke = function( message )
    if message:is_abs_value() then
      vws["PHT_VAL_TRANS_1"].value = math.min( 12, math.max( message.int_value - 12, -12 ) )
    end
  end
}

math.min( 12, math.max( message.int_value - 12, -12 ) ) returnreturns the correct range, which is exactly this:

-12, -11 ... -2, -1, 00, 01, 02 ... +11, +12

The problem is that I can not return the correct range to fit the range of a rotating wheel, which is from 0 to 127. "0 to 127"corresponds to the value of “message.int_value”.

Ok,I have tried to do the following:

math.min( 12, math.max( (message.int_value/(128/25) -12 ), -12 ) )

rnt:add_midi_mapping {
  name = "Tools:PhraseTouch:Panel_1:Transpose:Transpose Valuebox",
  invoke = function( message )
    if message:is_abs_value() then
      vws["PHT_VAL_TRANS_1"].value = math.min( 12, math.max( message.int_value/(128/25) - 12, -12 ) )
    end
  end
}

…but this returns the next range:

-12, -11 ... -2, -1, -0, 00, 01, 02 ... +11, +12

The operation also returns a " -0" a value that should not be.If you change the divisor to any other number, there always appears a 0 that should not be, -0 or +0, but should only return a “0” (neither negative nor positive).

Is there a different way to achieve this correctly?The intention is that the rotating wheel turns completely ( -12 left, 0 up center, +12 right).

To be exact, this is the complete range that I want to return:

-12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12

Note:I also use tostring and tonumber to adjust the values of the valuebox. But the problem is in the message.int_value of the midi_mapping

Personally I’d write maybe:

value = math.floor((message.int_value * 0.1875) + 0.5) - 12

where I assume message.int_value takes values through 0 to 127 and value gets values -12 to 12 but that probably isn’t what you are looking for anyway. Just sayin’ Raul.

With the risk of me misunderstanding… If you want to “convert a value from one range to another”, here is the formula: https://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio

@4Tey. Ok, work perfectly! With math.floor you use a multiplier 24/128 = 0.1875.

I would have thought 25 instead of 24, because from -12 to +12 there are 25 values, counting 0. But no, they must be 24. Or 25/127.But no, they must be 24/128.

Thank you very much 4Tey!!!

@Joule. Thanks!All this information is very useful for specific mathematics. Sometimes I have trouble looking for certain things that are in English. Thanks for the link!

I consider the thread as already resolved…

value = math.floor( message.int_value * 25/128 ) - 12