PHP中的渲染波形-如何生成更压缩的渲染? [英] Rendering waveform in PHP - How to produce a more compressed render?

查看:61
本文介绍了PHP中的渲染波形-如何生成更压缩的渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP渲染波形,方法是使用lame编码器对波形进行下采样,然后从结果数据点绘制波形.我目前正在获得像这样的图像:

I am rendering a waveform in PHP by downsampling it with the lame encoder and then drawing the waveform from the resulting data points. I am currently getting images like this:

我想做的是修改我的代码,以使波形的视在动态范围实质上被压缩".要产生看起来更像这样的波形:

What I would like to do is modify my code so that the apparent dynamic range of the waveform is essentially 'compressed'. To produce a waveform that looks more like this:

我当前用于渲染每个数据点高度的方程式如下:-

The equation I am currently using to render the height of each data point is as follows:-

 // draw this data point
          // relative value based on height of image being generated
          // data values can range between 0 and 255
           $v = (int) ( $data / 255 * $height );


          // don't print flat values on the canvas if not necessary
          if (!($v / $height == 0.5 && !$draw_flat))
            // draw the line on the image using the $v value and centering it vertically on the canvas
            imageline(
              $img,
              // x1
              (int) ($data_point / DETAIL),
              // y1: height of the image minus $v as a percentage of the height for the wave amplitude
              $height * $wav - $v,
              // x2
              (int) ($data_point / DETAIL),
              // y2: same as y1, but from the bottom of the image
              $height * $wav - ($height - $v),
              imagecolorallocate($img, $r, $g, $b)
            );      

实际振幅由该代码的第一行定义:-

With the actual amplitude being defined by the first line of this code:-

  $v = (int) ( $data / 255 * $height );

不幸的是,我的数学技能充其量是很差的.我需要做的基本上是对$ v值应用一个曲线",以便当输入方程式的数量较低时,结果输出较高,并且随着输入数量的增加,方程式会减小放大倍数,直到最终输入达到255时,输出也应为255.曲线也应这样,以使输入为0时输出也为0.

Unfortunately my math skill is poor at best. What I need to do is essentially apply a 'curve' to the value of $v so that when the number input into the equation is lower, the resulting output is higher and as the input number is increased the equation reduces the amplification until finally when the input reaches 255 the output should also be 255. Also the curve should be such so that with an input of 0 the output is also 0.

如果不清楚这一点,我深表歉意,但由于我有限的数学经验,我很难说清楚这个问题.

I apologise if this is not clear but I am finding this question very hard to articulate with my limited math experience.

也许视觉表示将有助于描述我的意图:-

Perhaps a visual representation would help describe my intent:-

当$ v的值是0或255时,方程的输出应恰好是输入(0或255).但是,当输入是介于两者之间的值时,它应遵循上面曲线的结果输出. (以上只是一个粗略的图示.)

When the value of $v is either 0 or 255 the output of the equation should be exactly the input (0 or 255). However, when the input is a value inbetween, it should follow the resulting output of the curve above. (the above was only a rough drawing to illustrate.)

基于Alnitiks的"pow"功能解决方案,我现在正在生成如下所示的波形:-

Based on Alnitiks 'pow' function solution I am now generating waveforms that look like this:-

使用$ v变量的替换方程式如下:-

Using the replacement equation for the $v variable as follows:-

 $v = pow($data / 255.0, 0.4) * $height;

我尝试增加0.4的值,但结果仍然不符合预期.

I have tried upping the 0.4 value but the result is still not as intended.

这是我的$ data变量的原始数据转储,如下所示:

As requested here is a raw datadump of my $data variable:

原始数据

这被传递到等式中,以便在绘制波形之前返回$ v(您可以在上面发布的原始代码中看到对变量$ v所做的操作.$ height简单就是我拥有的高像素数设置要渲染的图像.

This gets passed into the equation to return $v before being used to draw the waveform (you can see what I do to variable $v in the original code I posted above. $height is simple the number of pixels high I have set the image to render.

此数据是逗号分隔的值列表.我希望这有帮助.您似乎认为平均值为128是正确的.到目前为止,我仍无法弄清您对此的纠正.恐怕这超出了我目前的理解范围.

This data is a comma seperated list of values. I hope this helps. It appears your assertion that the mean value is 128 is correct. So far I have been unable to get my head around your correction for this. I'm afraid it is slightly beyond my current understanding.

推荐答案

没有任何数学技能(可能有助于快速显示):

With no math skills (and probably useful to have a speedy display):

您有256个可能的值.创建一个包含以下每个值的动态"值的数组:

You have 256 possible values. Create an array that contains the "dynamic" value for each of these values:

$dynamic = array(
   0 => 0,
   1 => 2,
   ...
);

完成后,您可以轻松获得动态值:

That done, you can easily get the dynamic value:

$v = (int) ($dynamic[(int) $data / 255] * $height);

您可能会失去一些精度,但这可能很有用.

You might lose some precision, but it's probably useful.

自然动态值由数学正弦和余弦函数生成,在PHP中 sin ­ Docs (和其他链接在那里).

Natural dynamic values are generated by the math sine and cosine functions, in PHP this sin­Docs (and others linked there).

您可以使用循环,该函数也可以预填充数组并重新使用数组,以便您具有预先计算的值:

You can use a loop and that function to prefill the array as well and re-use the array so you have pre-computed values:

$sine = function($v)
{
    return sin($v * 0.5 * M_PI);
};

$dynamic = array();
$base = 255;
for ($i = 0; $i <= $base; $i++)
{
    $dynamic[$i] = $i/$base;
}

$dynamic = array_map($sine, $dynamic);

我在这里使用了一个可变函数,因此您可以编写多个函数,并可以轻松地测试哪一个满足您的需求.

I use a variable function here, so you can write multiple and can easily test which one matches your needs.

这篇关于PHP中的渲染波形-如何生成更压缩的渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆