gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:03:27 42 4
gpt4 key购买 nike

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

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

音频被分成 3200 字节 == 100 毫秒的 block 。

关于如何增加音量/振幅以使声音更响亮有什么建议吗?

如果我需要添加一个常量或乘以值,或者我是否需要对每 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;
}

关于c# - 如何增加原始音频字节的音量/振幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36355992/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com