Write wave files to memory in Java

Posted by Cliff on Stack Overflow See other posts from Stack Overflow or by Cliff
Published on 2010-05-14T17:13:48Z Indexed on 2010/06/16 2:12 UTC
Read the original article Hit count: 352

Filed under:
|
|

I'm trying to figure out why my servlet code creates wave files with improper headers. I use:

AudioSystem.write(
  new AudioInputStream(
  new ByteArrayInputStream(memoryBytes), new AudioFormat(22000, 16, 1, true,false), memoryBytes.length
  ),
  AudioFileFormat.Type.WAVE, servletOutputStream
);

taking a byte array from memory containing raw PCM samples and a servlet output stream that gets returned to the client. In the result I get a normal wave file but with zeros in the chunk size fields. Is the API broken? I would think that the size could be filled in using the size passed in the audio input stream. But now, after typing this out I'm thinking its not making this info available to the outer write() method on AudioSystem. It seems like the AudioSystem.write call needs a size parameter unless it is able to pull the size from the stream... which wouldn't work with an arbitrary sized stream. Does anyone know how to make this example work?

© Stack Overflow or respective owner

Related posts about java

Related posts about servlets