I need to convert audio file from μ-law to PCM

痴心易碎 提交于 2021-01-28 10:18:27

问题


I need to convert wav file from FORMAT 1 to FORMAT 2

Format 1 : μ-law, 8000Hz, 64 kbps, mono

FORMAT 2 : Container WAV Encoding PCM Rate 16K Sample Format 16 bit Channels Mono

Following is the Code snippet :

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"));

Issue : But, this is not working. It playing some noise and also reducing my audio file length.

Edit 1: mu-Law to μ-law


回答1:


Following code worked for me --

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();
        }



回答2:


You need to use the AudioSystem and split up format conversion and audio writing into two different steps:

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"));


来源:https://stackoverflow.com/questions/44954593/i-need-to-convert-audio-file-from-%ce%bc-law-to-pcm

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