【问题标题】:I need to convert audio file from μ-law to PCM我需要将音频文件从 μ-law 转换为 PCM
【发布时间】:2017-07-06 16:37:10
【问题描述】:

我需要将 wav 文件从 FORMAT 1 转换为 FORMAT 2

格式 1: μ 律,8000Hz,64 kbps,单声道

格式 2: 容器 WAV 编码 PCM 速率 16K 样本格式 16 位 频道单声道

下面是代码 sn-p :

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:mu-law 到 μ-law

【问题讨论】:

  • 你的输入格式是 μ-law 吗?我没听说过 Mu-Laq
  • @scott 是的,μ-law。
  • tagtraum.com/ffsampledsp 可能会带你到那里。

标签: java audio javasound pcm jmf


【解决方案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();
        }

【讨论】:

    【解决方案2】:

    您需要使用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"));
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2019-04-04
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      相关资源
      最近更新 更多