gpt4 book ai didi

java - 将音频流转换为 PCM

转载 作者:行者123 更新时间:2023-12-03 01:47:27 29 4
gpt4 key购买 nike

我正在尝试从文本到语音接口(interface) (MaryTTS) 获取音频流并将其流式传输到 SIP RTP session 中(使用 Peers)。

同行想要 SoundSource流式传输音频,这是一个定义为的接口(interface)

public interface SoundSource {

byte[] readData();

}

和 MaryTTS 合成一个 StringAudioInputStream .我试图简单地读取流并将其缓冲到实现 SoundSource 的 Peers ,在
MaryInterface tts = new LocalMaryInterface();
AudioInputStream audio = tts.generateAudio("This is a test.");
SoundSource soundSource = new SoundSource() {

@Override
public byte[] readData() {
try {
byte[] buffer = new byte[1024];
audio.read(buffer);
return buffer;
} catch (IOException e) {
return null;
}
}
};
// issue call with soundSource using Peers

电话响了,我听到的是缓慢、低沉、嘈杂的声音,而不是合成语音。我想这可能与 SIP RTP session 所期望的音频格式有关,因为 Peers 文档指出

The sound source must be raw audio with the following format: linear PCM 8kHz, 16 bits signed, mono-channel, little endian.



如何转换/阅读 AudioInputStream满足这些要求?

最佳答案

我知道的一种方法是 - 鉴于您正在使用的系统,我不知道它是否会通过:

ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
try {
byte[] data=new byte[1024];
while(true) {
k=audioInputStream.read(data, 0, data.length);
if(k<0) break;
outputStream.write(data, 0, k);
}
AudioFormat af=new AudioFormat(8000f, 16, 1, true, false);
byte[] audioData=outputStream.toByteArray();
InputStream byteArrayInputStream=new ByteArrayInputStream(audioData);
AudioInputStream audioInputStream2=new AudioInputStream(byteArrayInputStream, af, audioData.length/af.getFrameSize());
outputStream.close();
}
catch(Exception ex) { ex.printStackTrace(); }
}

还有这个
AudioSysytem.getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream)

您可以将其与上述参数一起使用。

关于java - 将音频流转换为 PCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498622/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com