Java写入输出AudioInputStream + AudioFormat将wFormat设置为FLOAT?

时间:2017-02-28 04:03:45

标签: java audio file-io encoding floating-point

我已经确认我的程序“有效”,带有各种有符号整数输出格式。但是,当我尝试使用32位浮点时,输出音频元数据表明它是32位有符号整数,这会导致播放中断。

这是我的音频格式:

AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_FLOAT,
                                          48000, // Hz sample rate
                                          32, // bits per sample
                                          2, // channels
                                          8, // bytes per frame
                                          48000, // Hz frame rate
                                          false), // not big-endian

这被发送到处理器功能(我已使​​用其他输出格式确认“有效”):

public void mixToFile(AudioFormat format,
                      String outputPath,
                      int totalFrames) throws Exception {

  ByteBuffer outputBytes = byteBufferOf(mix()); // the big show

  AudioInputStream ais = new AudioInputStream(
    new ByteArrayInputStream(outputBytes.array()), outputFormat,
    totalFrames
  );

  AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(outputPath));
}

结果是失败;该文件具有32位signed int的元数据格式,请参阅:

Playing WAVE '/tmp/output.wav' : Signed 32 bit Little Endian, Rate 48000 Hz, Stereo

我正在寻找之前处理过的How to write wav file with 32-bit float data? 的等价物,在编写RIFF容器时,手动将'fmt'块中的wFormat标记设置为WAVE_FORMAT_IEEE_FLOAT(3)。

是否可以使用AudioSystem.writeAudioInputStreamAudioFormat来实现这一目标?

0 个答案:

没有答案