2017-02-28 232 views
0

我试图从文本到语音接口(MaryTTS)获取音频流并在SIP RTP会话(使用同行)中对其进行流式传输。将音频流转换为PCM

同行想要一个SoundSource流音频,其是定义为

public interface SoundSource { 

    byte[] readData(); 

} 

一个接口和MaryTTS synthesises一个StringAudioInputStream。我想简单的读取流和缓冲它同行实施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会话期望的音频格式有关,因为Peers文档状态为

声源必须是以下格式的原始音频:线性PCM 8kHz,16位有符号,单声道,小端。

如何转换/读取AudioInputStream以满足这些要求?我知道

回答

0

一种方法是这样的 - 因为你使用的是我不知道系统是否会通过:

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) 

,您可以与使用以上参数。