如何增加原始音频字节的音量/振幅



我正在处理电话原始声音和录音,我想在.Net C#项目中将它们标准化到特定的音量级别。

声音是原始音频字节的集合(单声道未加音频的16位签名PCM音频16000Hz)。

音频被分成3200字节的块==100ms。

有什么建议可以提高音量/振幅,让声音更大?

我不知道是否需要添加常量或乘值,或者是否需要对每1,2,3…个字节进行加法运算?也许已经有一个开源的解决方案了?

回答我自己的问题(为他人)。

解决方案是将每个样本(当16位PCM为2字节时)与一个常数值相乘。

请避免溢出\过多增加,您可以通过查找最高采样值来计算您可以使用的最高常数值,并计算乘法因子以使其达到可能的最高采样值,在16位PCM的情况下,即32676或其他值。

下面是一个小例子:

    public byte[] IncreaseDecibel(byte[] audioBuffer, float multiplier) 
    {
        // Max range -32768 and 32767
        var highestValue = GetHighestAbsoluteSample(audioBuffer);
        var highestPosibleMultiplier = (float)Int16.MaxValue/highestValue; // Int16.MaxValue = 32767
        if (multiplier > highestPosibleMultiplier)
        {
            multiplier = highestPosibleMultiplier;
        }
        for (var i = 0; i < audioBuffer.Length; i = i + 2)
        {
            Int16 sample = BitConverter.ToInt16(audioBuffer, i);
            sample *= (Int16)(sample * multiplier);
            byte[] sampleBytes = GetLittleEndianBytesFromShort(sample);
            audioBuffer[i] = sampleBytes[sampleBytes.Length-2];
            audioBuffer[i+1] = sampleBytes[sampleBytes.Length-1];
        }
        return audioBuffer;
    }

//添加了GetHighestAbsoluteSample,希望它仍然正确,因为代码随着时间的推移而改变

    /// <summary>
    /// Peak sample value
    /// </summary>
    /// <param name="audioBuffer">audio</param>
    /// <returns>0 - 32768</returns>
    public static short GetHighestAbsoluteSample(byte[] audioBuffer)
    {
        Int16 highestAbsoluteValue = 0;
        for (var i = 0; i < (audioBuffer.Length-1); i = i + 2)
        {
            Int16 sample = ByteConverter.GetShortFromLittleEndianBytes(audioBuffer, i);
            // prevent Math.Abs overflow exception
            if (sample == Int16.MinValue)
            {
                sample += 1;
            }
            var absoluteValue = Math.Abs(sample);
            if (absoluteValue > highestAbsoluteValue)
            {
                highestAbsoluteValue = absoluteValue;
            }
        }
        return (highestAbsoluteValue > LowestPossibleAmplitude) ?
                    highestAbsoluteValue : LowestPossibleAmplitude;
    }

最新更新