1

我正在尝试处理从采样的 sourcedataline(Java Sound API)获得的字节数组。如果我将字节数组与分数相乘,播放流时会出现噪音。

在我播放声音之前,我将立体声 wav 文件分成左右声道。这工作正常。但是,如果我想使用取决于延迟因子的增益控制来处理通道,我会得到噪音。

for(int i=0; i<bufferSize; i++) { array[i] = (byte) (array[i] * gain); }

有谁知道如何解决这个问题?

//编辑:

我试图通过位移将这两个字节转换为短(2字节),例如:

short leftMask = 0xff00;
short rightMask = 0x00ff;
short sValue = (array[i] + array[i+1] <<8) * gain;

array[i] = (sValue & leftMask) >> 8;
array[i+1] = (sValue & rightMask);

但是当我将单个字节乘以增益值时,我得到了相同的结果。

//编辑

或者我应该将两个数组值添加到这样的短片中吗?

short shortValue = array[i] + array[i+1];
shortValue *= gain;
array[i] = ??? 

但是如何在不丢失声音的情况下将这个短字节转换为 2 个单字节呢?

//编辑分离方法中的一些代码:

public static void channelManipulation(byte[] arrayComplete) {
        int i=2; 
        char channel='L';
        int j=0; 

        /** 
         * The stereo stream will be divided into his channels - the Left and the Right channel. 
         * Every 2 bytes the channel switches. 
         * While data is collected for the left channel the right channel will be set by 0. Vice versa.
         */
        while(j<arrayComplete.length) {
            //while we are in the left channel we are collecting 2 bytes into the arrayLeft


            while(channel=='L') {
                if(i==0) {
                    channel='R'; //switching to the right channel
                    i=2;
                    break;
                }
                arrayLeft[j] = (byte)(arrayComplete[j]);
                arrayRight[j] = 0;
                i--; j++;
            }

            //while we are in the right channel we are collecting 2 bytes into the arrayRight
            while(channel=='R') {
                if(i==0) {
                    channel='L'; //switching to the left channel
                    i=2;
                    break;
                }
                arrayRight[j] = (byte) (arrayComplete[j]);
                arrayLeft[j] = 0;
                i--; j++;
            }
        }

    }
4

5 回答 5

3

即使您的音频数据是字节数组的形式,您的真实音频也是(我假设)一个短(2 字节)整数数组。当您将数组的每个单独字节乘以增益因子时,您会将 2 字节样本值变成乱码(又名噪声)。我不是 java 程序员,但您的解决方案是将字节数组转换为 2 字节整数数组(但是您在 java 中这样做),然后将每个 2 字节整数值乘以增益因子(然后,我想,在播放之前将其转换回字节数组)。

更新:在 C# 中,如果我有一个音频数据的字节数组(例如,从实际格式为 2 字节整数的 WAV 文件中提取),我将使用 BitConverter 和 Array 类应用增益,如下所示:

float gain = 0.5f;
for (int i = 0; i < audio.Length; i += 2)
{
    short val = BitConverter.ToInt16(audio, i);
    val = (short)((float)val * gain);
    Array.Copy(BitConverter.GetBytes(val), 0, audio, i, 2);
}

这很笨拙,而且我永远不会真正做。在 C# 世界中,我总是将音频作为 16 位或 32 位整数的数组,或者作为 32 位或 64 位浮点值。我真的不知道java音频是如何工作的,但是应该可以(并且更容易)在某个地方首先将您的音频作为一个16位整数数组 - 然后您就不必做任何事情了像这样的奇怪转换来应用增益或做任何你想做的事情。

更新 2:另外,我不确定您的原始音频源实际上是否包含 2 字节整数样本值。它实际上可能是 4 字节整数或(更有可能)4 字节浮点样本值,在这种情况下,我的样本代码仍会产生噪音。使用 4 字节浮点数,正确的代码是:

float gain = 0.5f;
for (int i = 0; i < audio.Length; i += 4)
{
    float val = BitConverter.ToSingle(audio, i);
    val *= gain;
    Array.Copy(BitConverter.GetBytes(val), 0, audio, i, 4);
}
于 2009-09-03T12:52:27.873 回答
1

您需要应用一些剪辑。假设您有一个值为 100 的样本,并且您正在应用 2 的增益。乘法的结果将为 200,然后最终截断为 -73。

尝试:

array[i] = Math.min(Math.max(array[i] * gain, -128), 127);

作为对此的测试 - 如果您应用的增益实际上是“安静”增益(例如 0.5),那么您现在不应该得到噪音。

编辑:如果“原始”值实际上不是单字节,则应首先从字节数组转换为这些值,然后应用增益,然后再转换回单字节。否则你确实会得到一些奇怪的结果......特别是如果本机格式实际上将字节视为无符号值......

于 2009-09-03T12:22:11.020 回答
1

不工作。我有这个代码片段:

for(int c=0; c<Constants.getBufferlength()-4; c+=4) {
            arrayLeft[c] = (byte) Math.min(Math.max((arrayLeft[c]*leftGain), -128), 127);

            arrayRight[c] = (byte) Math.min(Math.max((arrayRight[c]*rightGain),-128),127);

    }

我得到了像以前一样的噪音。

于 2009-09-03T12:42:03.553 回答
0

在遇到一个非常相似的问题后,我发现了这篇文章。FWIW我的问题是通过注意到代码来解决的

short sValue = (array[i] + array[i+1] <<8) 

不考虑 Java 有符号字节的影响。如果高位设置在低字节中(例如array[i]),那么这对short 没有影响,但影响分别对short 的两个字节进行算术运算的代码。可以通过以下代码轻松修复

if(array[i] < 0)
    array[i+1] += 1;

它将 256 添加到短字节 - 占低字节中缺少的 128 位和其余部分的二进制补码。根据您处理字节数组的方式,您可能需要对此进行一些更改。

于 2009-11-07T19:36:11.673 回答
0

试试这个:

byte[] decodedBuffer = Base64.decode(message64, Base64.NO_WRAP);
       // byte[] newdata;
        for (int i=0; i<decodedBuffer.length; i++) {
            Byte b = decodedBuffer[i];
            if (b<=Byte.MIN_VALUE||b>=Byte.MAX_VALUE) decodedBuffer[i] = Byte.MIN_VALUE;
        }
        if (audioTrack != null)
            audioTrack.write(decodedBuffer, 0, decodedBuffer.length);
于 2017-08-23T10:42:09.843 回答