Java algorithm for normalizing audio

后端 未结 2 1837
萌比男神i
萌比男神i 2020-12-28 20:01

I\'m trying to normalize an audio file of speech.

Specifically, where an audio file contains peaks in volume, I\'m trying to level it out, so the quiet sections are

2条回答
  •  不知归路
    2020-12-28 20:54

    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 targetRangeMin seems to be wrong and will probably cause some distortion of the waveform.

    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.

    Edit: here is a sample implementation of such a method:

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

    For reference, this is what your algorithm did to a sine curve with an amplitude of 10000: Original algorithm

    This explains why the audio quality becomes much worse after being normalized.

    This is the result after running with my suggested normalize method: Suggested algorithm

提交回复
热议问题