Android 将字节写入wav文件会增加背景噪音
我正在从wav文件读取值;仅选择其中一些值并将其写入另一个wav文件(以便从wav文件中删除静默期)。问题是,当我创建这个新的wav文件时,它有背景噪声(在原始wav文件中不存在)。我在这里添加了代码的一部分,该部分用于编写文件:Android 将字节写入wav文件会增加背景噪音,android,audio,bytearray,Android,Audio,Bytearray,我正在从wav文件读取值;仅选择其中一些值并将其写入另一个wav文件(以便从wav文件中删除静默期)。问题是,当我创建这个新的wav文件时,它有背景噪声(在原始wav文件中不存在)。我在这里添加了代码的一部分,该部分用于编写文件: private void writeToFile(String filePath) { short nChannels = 1; int sRate = 16000; short bSamples = 16;
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();
}
}
private void writeToFile(字符串文件路径){
短n通道=1;
int sRate=16000;
短b样本=16;
audioShorts=新短款[尺寸];
int nSamples=0;
对于(int i=0;i=slopeThreshold){//Voice region--应写入输出
音频短路[nSamples]=短反向字节((短)(a[i]*0x8000));
音频短路[nSamples+1]=短反向字节((短)(a[i+1]*0x8000));
nSamples+=2;
i++;
}
/*否则
音频短片[i]=0*/
}
finalShorts=新的短[nSamples];
对于(inti=0;i您的代码片段遗漏了一些细节(比如slope和slopeThreshold是什么),因此将此答案视为一个建议
一般来说,这种音频数据的截断会引入噪声。这取决于剪切发生的位置。如果剪切前的最后一个采样与剪切后的第一个采样相同,则是安全的,否则会引入咔嗒声
如果切割不频繁,你会听到个别的咔嗒声,但如果切割次数足够频繁,听起来可能像连续的噪音
要做到这一点而不点击,你需要添加一个简短的淡出和淡入周围的每个削减
编辑:尝试删除“if(slope[i]>=slopeThreshold)”条件,看看噪声是否消失。如果消失,噪声很可能是我所描述的结果。否则,您可能会在各种字节转换中出错。而不是:
data = new byte[finalShorts.length*2];
ByteBuffer buffer = ByteBuffer.wrap(data);
ShortBuffer sbuf = buffer.asShortBuffer();
sbuf.put(finalShorts);
data = buffer.array();
是否有必要将短[]转换为字节[]
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;
}
这对我有用。你是否应用了过滤器来去除背景噪音?因为麦克风不会过滤掉背景噪音,所以你必须通过编程来进行过滤。原始录制的wav文件(从中获取值)没有噪音。噪音会添加到我正在创建的新wav文件中。