有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将音频流转换为PCM

我试图从文本到语音接口(MaryTTS)获取音频流,并在SIP RTP会话(使用对等方)中进行流式传输

对等方需要一个SoundSource来流式传输音频,这是一个定义为

public interface SoundSource {

    byte[] readData();

}

MaryTTS将String合成为AudioInputStream。我试图简单地读取流,并将其缓冲到实现SoundSource的对等方,如

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会话所期望的音频格式有关,因为对等文档中指出

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

如何转换/读取AudioInputStream以满足这些要求


共 (1) 个答案

  1. # 1 楼答案

    我知道的一个方法是——考虑到您使用的系统,我不知道它是否会通过:

    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)
    

    可与上述参数一起使用