我正在尝试进行一些音频处理,我真的陷入了立体声到单声道的转换。我在互联网上查看了立体声到单声道的转换。
据我所知,我可以采用左声道,右声道,将它们相加并除以2.但是当我再次将结果转换为WAV文件时,我得到了很多前景噪声。我知道处理数据时可能会产生噪音,字节变量会有一些溢出。
这是我从MP3文件中检索byte []数据块的类:
公共类InputSoundDecoder {
private int BUFFER_SIZE = 128000;
private String _inputFileName;
private File _soundFile;
private AudioInputStream _audioInputStream;
private AudioFormat _audioInputFormat;
private AudioFormat _decodedFormat;
private AudioInputStream _audioInputDecodedStream;
public InputSoundDecoder(String fileName) throws UnsuportedSampleRateException{
this._inputFileName = fileName;
this._soundFile = new File(this._inputFileName);
try{
this._audioInputStream = AudioSystem.getAudioInputStream(this._soundFile);
}
catch (Exception e){
e.printStackTrace();
System.err.println("Could not open file: " + this._inputFileName);
System.exit(1);
}
this._audioInputFormat = this._audioInputStream.getFormat();
this._decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 1, 44100, false);
this._audioInputDecodedStream = AudioSystem.getAudioInputStream(this._decodedFormat, this._audioInputStream);
/** Supported sample rates */
switch((int)this._audioInputFormat.getSampleRate()){
case 22050:
this.BUFFER_SIZE = 2304;
break;
case 44100:
this.BUFFER_SIZE = 4608;
break;
default:
throw new UnsuportedSampleRateException((int)this._audioInputFormat.getSampleRate());
}
System.out.println ("# Channels: " + this._decodedFormat.getChannels());
System.out.println ("Sample size (bits): " + this._decodedFormat.getSampleSizeInBits());
System.out.println ("Frame size: " + this._decodedFormat.getFrameSize());
System.out.println ("Frame rate: " + this._decodedFormat.getFrameRate());
}
public byte[] getSamples(){
byte[] abData = new byte[this.BUFFER_SIZE];
int bytesRead = 0;
try{
bytesRead = this._audioInputDecodedStream.read(abData,0,abData.length);
}
catch (Exception e){
e.printStackTrace();
System.err.println("Error getting samples from file: " + this._inputFileName);
System.exit(1);
}
if (bytesRead > 0)
return abData;
else
return null;
}
}
这意味着,每次调用getSamples时,它都会返回一个数组:
buff = {Lchannel,Rchannel,Lchannel,Rchannel,Lchannel,Rchannel,Lchannel,Rchannel ......}
转换为单声道的处理程序如下:
byte[] buff = null;
while( (buff = _input.getSamples()) != null ){
/** Convert to mono */
byte[] mono = new byte[buff.length/2];
for (int i = 0 ; i < mono.length/2; ++i){
int left = (buff[i * 4] << 8) | (buff[i * 4 + 1] & 0xff);
int right = (buff[i * 4 + 2] <<8) | (buff[i * 4 + 3] & 0xff);
int avg = (left + right) / 2;
short m = (short)avg; /*Mono is an average between 2 channels (stereo)*/
mono[i * 2] = (byte)((short)(m >> 8));
mono[i * 2 + 1] = (byte)(m & 0xff);
}
}
使用以下方法写入wav文件:
public static void writeWav(byte [] theResult, int samplerate, File outfile) {
// now convert theResult into a wav file
// probably should use a file if samplecount is too big!
int theSize = theResult.length;
InputStream is = new ByteArrayInputStream(theResult);
//Short2InputStream sis = new Short2InputStream(theResult);
AudioFormat audioF = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
samplerate,
16,
1, // channels
2, // framesize
samplerate,
false
);
AudioInputStream ais = new AudioInputStream(is, audioF, theSize);
try {
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, outfile);
} catch (IOException ioe) {
System.err.println("IO Exception; probably just done with file");
return;
}
}
以44100作为采样率。
请记住,实际上我已经得到它的byte []数组已经是pcm,所以mp3 - &gt; pcm转换是通过指定
完成的this._decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 1, 44100, false); this._audioInputDecodedStream = AudioSystem.getAudioInputStream(this._decodedFormat, this._audioInputStream);
正如我所说,在写入Wav文件时,我有很多噪音。我假装将每个字节块应用于FFT,但我认为由于噪音很大,结果不正确。
因为我正在拍两首歌,其中一首是另一首20秒的裁剪,当将裁剪结果与原始的20秒子集进行比较时,它根本不匹配。
我认为这是不正确的转换立体声 - &gt;单声道的原因。
希望有人知道这件事,
问候。
答案 0 :(得分:7)
正如评论中所指出的,字数可能是错误的。此外,转换为带符号的短路并将其移位可能会导致第一个字节为0xFF。
尝试:
int HI = 0; int LO = 1;
int left = (buff[i * 4 + HI] << 8) | (buff[i * 4 + LO] & 0xff);
int right = (buff[i * 4 + 2 + HI] << 8) | (buff[i * 4 + 2 + LO] & 0xff);
int avg = (left + right) / 2;
mono[i * 2 + HI] = (byte)((avg >> 8) & 0xff);
mono[i * 2 + LO] = (byte)(avg & 0xff);
然后切换HI和LO的值以查看它是否变好。