Convert 8kHz mulaw to 16KHz PCM in real time

百般思念 提交于 2020-04-11 04:36:19

问题


In my POC I'm receiving a conversation streaming from Twilio in 8kHz mulaw and I want to transcribe it using Amazon Transcribe that needs to get the audio in 16KHz and PCM.

I found here how to convert a file but failed to do this in streaming... The code for a file is:

File sourceFile = new File("<Source_Path>.wav");
File targetFile = new File("<Destination_Path>.wav");
AudioInputStream sourceAudioInputStream = AudioSystem.getAudioInputStream(sourceFile);

AudioInputStream targetAudioInputStream=AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, sourceAudioInputStream);
System.out.println("Sample Rate1 "+targetAudioInputStream.getFormat().getFrameRate());
AudioFormat targetFormat = new AudioFormat(new AudioFormat.Encoding("PCM_SIGNED"), 16000, 16, 1, 2, 8000, false);

AudioInputStream targetAudioInputStream1 = AudioSystem.getAudioInputStream(targetFormat, targetAudioInputStream);
System.out.println("Sample Rate "+targetAudioInputStream1.getFormat().getFrameRate());

try {
    AudioSystem.write(targetAudioInputStream1, AudioFileFormat.Type.WAVE, targetFile);
} catch (IOException e) {
    e.printStackTrace();
}

Actually Twilio gives me a playload in Base64 (8KHz, mulaw) but I have to convert it to 16KHz, PCM.


回答1:


You need a G.711 Decoder and Audio Resampler.

Steps to be followed :

  1. use base64 decoder to decode the Payload received.

  2. use this payload buffer and decode using the G.711 decoder (mulaw to pcm)

  3. output of the G.711 decoder need to be given to the resampler for upsampling ( 8->16 KHz)

Finally all the buffers are ready in PCM 16KHz.



来源:https://stackoverflow.com/questions/59767373/convert-8khz-mulaw-to-16khz-pcm-in-real-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!