Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:19:52+00:00 2026-05-27T23:19:52+00:00

I am rendering a waveform in PHP by downsampling it with the lame encoder

  • 0

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:

enter image description here

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:

enter image description here

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 );

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:-

enter image description here

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.)

EDIT:

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

enter image description here

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

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

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

EDIT 2:

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

Raw Data

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:19:52+00:00Added an answer on May 27, 2026 at 11:19 pm

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

    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.


    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rendering a 3D-map from an Openstreetmap data can be pretty cumbersome . But what
Getting a rendering error for this form: 'NoneType' object has no attribute 'widget' http://dpaste.com/88585/
I am rendering audio waveforms directly from MP3's on the fly as they are
I am rendering my div with a javascript from the server side code. The
I am rendering a scene to a texture, then using that texture and its
I'm currently rendering the visible part of a view to a PNG, like so:
Before rendering into a view model should be formatted: multilingual data localized; date, time
My research on rendering 2d images led me to this... Spritesheet in Silverlight This
Having an issue rendering jstl tags from within a facelets ui:composition My current xmlns
When rendering a Zend_View, the php code inside it is not executed and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.