Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't think the concept of "minimum sample value" is very meaningful, since the sample value just represents the current "height" of the sound wave at a certain time instant. I.e. its absolute value will vary between the peak value of the audio clip and zero. Thus, having a <code>targetRangeMin</code> seems to be wrong and will probably cause some distortion of the waveform.</p> <p>I think a better approach might be to have some sort of weight function that decreases the sample value based on its size. I.e. bigger values are decreased by a large percentage than smaller values. This would also introduce some distortion, but probably not very noticeable. </p> <p>Edit: here is a sample implementation of such a method:</p> <pre><code>public short normalize(short value) { short rawMax = // max from step1 short targetMax = 8000; //This is the maximum volume reduction double maxReduce = 1 - targetMax/(double)rawMax; int abs = Math.abs(value); double factor = (maxReduce * abs/(double)rawMax); return (short) Math.round((1 - factor) * value); } </code></pre> <p>For reference, this is what your algorithm did to a sine curve with an amplitude of 10000: <img src="https://i.stack.imgur.com/0Jsg9.png" alt="Original algorithm"></p> <p>This explains why the audio quality becomes much worse after being normalized.</p> <p>This is the result after running with my suggested <code>normalize</code> method: <img src="https://i.stack.imgur.com/1gDgc.png" alt="Suggested algorithm"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload