我想在Java中将多个ULAW样本混合在一起。有没有办法在不转换为PCM的情况下这样做?我试图做一个8000Hz 8位VoIP应用程序。我使用ULAW编码测试AU文件(在goldwave中创建)。我目前的实施是:
[代码]
AudioFormat f = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000, 16, 1, 2, 8000, false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(f);
sdl.open(f);
sdl.start();
File file1 = new File("C:\\Scream3.au");
AudioInputStream ais1 = AudioSystem.getAudioInputStream(file1);
AudioInputStream aisTarget1 = AudioSystem.getAudioInputStream(f, ais1);
File file2 = new File("C:\\Blackout3.au");
AudioInputStream ais2 = AudioSystem.getAudioInputStream(file2);
AudioInputStream aisTarget2 = AudioSystem.getAudioInputStream(f, ais2);
byte[] data = new byte[10000];
int[] calc = new int[5000];
AudioInputStream[] streams = {aisTarget1, aisTarget2};
int count = streams.length + 1;
while (true) {
int r = -1;
for (int i = 0; i < streams.length; i++) {
r = streams[i].read(data, 0, data.length);
if (r == -1) break;
for (int j = 0; j < calc.length; j++) {
int tempVal = ((data[j * 2 + 1] << 8) | (data[j * 2] & 0xFF));
calc[j] += tempVal;
}
}
for (int i = 0; i < calc.length; i++) {
calc[i] /= count;
data[i * 2 + 0] = (byte) (calc[i] & 0xFF);
data[i * 2 + 1] = (byte) (calc[i] >> 8);
}
if (r == -1) break;
sdl.write(data, 0, data.length);
}
[/代码]
如果不能直接混合ulaw样本并且我必须转换为PCM,我该如何转换PCM格式(AudioFormat.Encoding.PCM_SIGNED,8000 Hz,16bits,1通道,2字节帧大小,8000帧速率,小端) 到ULAW(8bit 8000Hz)。
我做的事情是这样的: 1)将WAVE标头写入字节流 2)将PCM数据写入字节流 3)使用AudioSystem.getAudioInputStream(字节流)获取PCM AIS 4)使用AudioSystem.getAudioInputStream(ulawFormat,PCM AIS)获取ULAW目标AIS
感谢任何帮助。
编辑: 试图从Mu-LAW转换为PCM(试图使用http://en.wikipedia.org/wiki/Ulaw中的等式):
[CODE]
AudioFormat f = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000, 8, 1, 1, 8000, false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(f);
sdl.open(f);
sdl.start();
File file1 = new File("C:\\scream3.au");
FileInputStream fis = new FileInputStream(file1);
// Skip header
fis.skip(24);
byte[] data = new byte[8196];
while (true) {
int r = fis.read(data, 0, data.length);
if (r == -1) break;
for (int i = 0; i < r; i++) {
float y = ((float) data[i] / Byte.MAX_VALUE);
float sample = -1.0f * (float) (Math.signum(y) * (1.0f / 255.0f) * (Math.pow(1 + 255, Math.abs(y)) - 1.0f));
data[i] = (byte) (sample * Byte.MAX_VALUE);
}
sdl.write(data, 0, data.length);
}
sdl.drain();
sdl.stop();
sdl.close();
fis.close();
[/ CODE]
答案 0 :(得分:1)
我会使用Wikipedia:μ-law algorithm中的公式,手动转换/到线性编码,一次一个样本。
示例代码(基于OP的代码):
float sample1 = Math.signum(y1)/255.0*(Math.pow(256, Math.abs(y1))-1);
float sample2 = Math.signum(y2)/255.0*(Math.pow(256, Math.abs(y2))-1);
float combined = somethig * sample1 + (1-something) * sample2;
float result = Math.signum(combined)*Math.log(1+255*Math.abs(combined))/Math.log(256);