整数值的缩放(从浮点数)

发布于 2024-10-14 17:55:06 字数 295 浏览 6 评论 0原文

我正在设计一个 VST 音频插件,它需要从输入信号中提取幅度数据,以用于 midi 域中的速度设置。

本质上,我将接收 0-1(浮点)之间的值,并且需要将它们转换为 0-127 int。

目前,该过程是将浮点值乘以 100,得到 +3 位小数的整数值,即 103.4567685 或 005.6778787282

从这里,我将使用 Floor() 函数将浮点数舍入为整数。

然而,这会给我留下 0-100 之间的值;但是,我需要将它们缩放到 0-127。

任何有关如何实现这一点的建议将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

可是我不能没有你 2024-10-21 17:55:06

正确的方法是:

int amplitude = 128 * value ;
if (amplitude == 128) amplitude = 127 ;

The proper way to do this is:

int amplitude = 128 * value ;
if (amplitude == 128) amplitude = 127 ;
写给空气的情书 2024-10-21 17:55:06

范围转换并不是那么简单。在缩放整数范围 [-n; n] 为浮点数,使用简单相乘的简单方法会产生逆映射可能不会映射到原始值的效果。假设您有整数范围内所有数字的集合,以及浮点范围内相同大小的集合,则朴素映射不是双射的。

有一篇关于此内容和示例代码的好论文,介绍如何实现单调性和排序保留映射,位于 http ://kaba.hilvi.org/programming/range/index.htm

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

心在旅行 2024-10-21 17:55:06

如果您采用此处提供的一些建议并乘以 127,您将发现 127 在您的输出中没有得到很好的体现。也许这对您来说不是问题,但一个小小的改变就会带来很大的不同。

您可能认为四舍五入会有帮助,但事实并非如此。 127 处的值的数量仍然只是其他值的一半,现在 0 处的值的数量也将是平均值的一半。

正确的公式:

int amplitude = static_cast<int>(value * (128-epsilon)); // where epsilon is a very small floating point value

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
旧时模样 2024-10-21 17:55:06

只需将您的值乘以 127 并将其转换为 int 即可。 <代码>[0,1]*127 => [0,127]

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

云雾 2024-10-21 17:55:06

我想我一定错过了什么。为什么不乘以 127.0 而不是乘以 100?

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

小…楫夜泊 2024-10-21 17:55:06

我在 round() 函数的帮助下使用这种有效的解决方法来覆盖所有值。

输出 = round (ampiezza * 127.5 + 127.5)

ampiezza 是一个有符号浮点数,范围从 -1 到 +1
输出是一个 8 位整数,

输出范围从 0 到 255,以 127 到 128 为中心

希望这会有所帮助。

I use this effective workaround to cover all the values with the help of the round() function.

output = round (ampiezza * 127.5 + 127.5)

ampiezza is a signed float ranging from -1 to +1
output is an 8 bit integer

output range is from 0 to 255 centered between 127 and 128

Hope this can be helpful.

一绘本一梦想 2024-10-21 17:55:06

在使用floor()之前将结果值乘以1.27?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文