0

我正在从 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();
        }
    }
4

2 回答 2

0

您的代码片段遗漏了一些细节(例如斜率和斜率阈值是什么),因此仅将此答案视为建议。

一般来说,这种对音频数据的斩波会引入噪声。这取决于切割发生的位置。如果剪切前的最后一个样本与它之后的第一个样本相同,则您是安全的,否则您将引入点击。

如果切割不频繁,您会听到单独的咔嗒声,但如果切割发生的频率足够高,它可能听起来像是连续的噪音。

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

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

于 2013-11-07T15:15:28.507 回答
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;
}

这对我有用。

于 2013-11-26T20:44:59.590 回答