将字节写入wav文件会增加背景噪音

时间:2013-11-07 10:18:55

标签: android audio bytearray

我正在从wav文件中读取值;只选择其中一些值并将它们写入另一个wav文件(为了从wav文件中删除静默期)。问题是,当我创建这个新的wav文件时,它有背景噪音(原始的wav文件中不存在)。我在这里添加了正在编写文件部分的代码部分:

private void writeToFile(String filePath) {
        short nChannels = 1;
        int sRate = 16000;
        short bSamples = 16;
        audioShorts = new short[size];
        int nSamples = 0;
        for(int i=0; i<size-1; i++) {
            //audioShorts[i] = Short.reverseBytes((short)(zff[i]*0x8000));
            if(slope[i] >= slopeThreshold) { // Voice region -- Should be written to output
                audioShorts[nSamples] = Short.reverseBytes((short)(a[i]*0x8000));
                audioShorts[nSamples+1] = Short.reverseBytes((short)(a[i+1]*0x8000));
                nSamples += 2;
                i++;
            }
            /*else
                audioShorts[i] = 0;*/
        }
        finalShorts = new short[nSamples];
        for(int i=0; i<nSamples; i++){
            finalShorts[i] = audioShorts[i];
        }
        data = new byte[finalShorts.length*2];
        ByteBuffer buffer = ByteBuffer.wrap(data);
        ShortBuffer sbuf =  buffer.asShortBuffer();
        sbuf.put(finalShorts);
        data = buffer.array();
        Log.d("Data length------------------------------", Integer.toString(data.length));
        RandomAccessFile randomAccessWriter;
        try {
            randomAccessWriter = new RandomAccessFile(filePath, "rw");
            randomAccessWriter.setLength(0); // Set file length to 0, to prevent unexpected behaviour in case the file already existed
            randomAccessWriter.writeBytes("RIFF");
            randomAccessWriter.writeInt(Integer.reverseBytes(36+data.length)); // File length 
            randomAccessWriter.writeBytes("WAVE");
            randomAccessWriter.writeBytes("fmt ");
            randomAccessWriter.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
            randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
            randomAccessWriter.writeShort(Short.reverseBytes(nChannels));// Number of channels, 1 for mono, 2 for stereo
            randomAccessWriter.writeInt(Integer.reverseBytes(sRate)); // Sample rate
            randomAccessWriter.writeInt(Integer.reverseBytes(sRate*bSamples*nChannels/8)); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8
            randomAccessWriter.writeShort(Short.reverseBytes((short)(nChannels*bSamples/8))); // Block align, NumberOfChannels*BitsPerSample/8
            randomAccessWriter.writeShort(Short.reverseBytes(bSamples)); // Bits per sample
            randomAccessWriter.writeBytes("data");
            randomAccessWriter.writeInt(Integer.reverseBytes(data.length)); // No. of samples
            randomAccessWriter.write(data);
            randomAccessWriter.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:0)

您的代码段会遗漏一些详细信息(例如slope和slopeThreshold),因此请将此答案视为建议。

通常,这种音频数据的斩波会引入噪音。这取决于切割发生的位置。如果切割前的最后一个样本与之后的第一个样本相同,那么您是安全的,否则您将引入一次点击。

如果切割不频繁,您将听到单击,但如果斩波经常发生,则可能听起来像连续的噪音。

要在没有点击的情况下执行此操作,您需要在每次剪切周围添加淡出淡出和淡入淡出。

编辑:尝试删除“if(slope [i]&gt; = slopeThreshold)”条件并查看噪音是否消失。如果是这样,噪音很可能是我所描述的结果。否则,您可能会遇到各种字节转换错误。

答案 1 :(得分:0)

而不是:

   data = new byte[finalShorts.length*2];
    ByteBuffer buffer = ByteBuffer.wrap(data);
    ShortBuffer sbuf =  buffer.asShortBuffer();
    sbuf.put(finalShorts);
    data = buffer.array();

是否有必要将short []转换为byte []?

data = shortToBytes(finalShorts);

public byte [] shortToBytes(short [] input){
  int short_index, byte_index;
  int iterations = input.length;

  byte [] buffer = new byte[input.length * 2];

  short_index = byte_index = 0;

  for(/*NOP*/; short_index != iterations; /*NOP*/)
  {
    buffer[byte_index]     = (byte) (input[short_index] & 0x00FF); 
    buffer[byte_index + 1] = (byte) ((input[short_index] & 0xFF00) >> 8);

    ++short_index; byte_index += 2;
  }

  return buffer;
}

这项工作适合我。