2017-07-06 142 views
0

我需要wav文件從格式轉換1FORMAT 2我需要從μ律轉換音頻文件爲PCM

格式1: μ律,8000Hz的64 kbps的,單聲道

FORMAT 2: 集裝箱WAV 編碼PCM 速率16K 樣本格式16位 通道單

以下是代碼片段:

File file = new File("audio_before_conversion.wav"); 
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true , true); 
AudioInputStream audioInputStream1 = new AudioInputStream(
    new FileInputStream(file), audioFormat, numFrames); 
AudioSystem.write(audioInputStream1, Type.WAVE, 
    new File("audio_after_conversion.wav")); 

問題: 但是,這是行不通的。它播放一些噪音,也減少了我的音頻文件的長度。

編輯1:μ律到μ律

+0

是您的輸入格式μ-law?我沒有聽說過Mu-Laq –

+0

@scott是的,μ律。 –

+0

http://www.tagtraum.com/ffsampledsp/可能會幫助你。 – hendrik

回答

0

您需要使用AudioSystem和格式轉換和音頻寫作分成兩個不同的步驟:

final File file = new File("audio_before_conversion.wav"); 
// open the audio stream 
final AudioInputStream pcmStream8k = AudioSystem.getAudioInputStream(file); 
// specify target format 
final AudioFormat targetFormat = new AudioFormat(16000, 16, 1, true , true); 
// this converts your audio stream 
final AudioInputStream pcmStream16k = AudioSystem.getAudioInputStream(targetFormat, pcmStream8k); 
// this writes the audio stream 
AudioSystem.write(pcmStream16k, AudioFileFormat.Type.WAVE, new File("audio_after_conversion.wav")); 
1

下面的代碼爲我工作 -

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) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }