0

Trying to pipe an AudioInputStream I got from a libary to this process:

oggenc - -o wtf.ogg

via

AudioSystem.write(audio, AudioFileFormat::Type::WAVE, process.getOutputStream());

gives me the error:

IOException
stream length not specified

file: com.sun.media.sound.WaveFileWriter.write(WaveFileWriter.java)
line: 129

So it seems audio stream length needs to be specified. This is a 44.8 kHz stereo PCM format audio with no length specified. I got this from a library so I can't modify their code. I tried creating another AudioInputStream from this stream, but I couldn't find a proper length. So how can I specify the length ?

nurettin
  • 11,090
  • 5
  • 65
  • 85
  • 1
    Where is your code and what exact exception it throws? What is `AudioFileFormat::Type::WAVE`? This is not Java syntax. – Dims Dec 08 '12 at 14:00
  • I believe you but there is not sufficuent information. Actually, you don't need to specify length if you have correct audio stream. – Dims Dec 08 '12 at 14:03
  • I will try to paste whatever information I can find – nurettin Dec 08 '12 at 14:03
  • What is the type of `audio` variable? – Dims Dec 08 '12 at 14:04
  • For example, while constructing `AudioInputStream` you can pass `AudioSystem.NOT_SPECIFIED` as the length of the stream. – Dims Dec 08 '12 at 14:07
  • Yes, I found your error source here, http://javasourcecode.org/html/open-source/jdk/jdk-6u23/com/sun/media/sound/WaveFileWriter.java.html – Dims Dec 08 '12 at 14:14
  • *"I tried creating another AudioInputStream from this stream, but I couldn't find a proper length"* combined with *"I got this from a library"* suggests that library is broken & producing invalid sound streams. *"so I can't modify their code."* So find a library that is ***not*** broken & closed source. I suggest to try Xuggle or Jffmpeg. – Andrew Thompson Dec 08 '12 at 21:59

2 Answers2

1

So, I found that you are reading audio from somewhere unknown, and then trying to write it into WAV format. But WAV format requires to write header, which should contain file length. Anyway WaveFileWriter throws exception if feeded by stream without knowing length.

I guess your direction is Java -> oggenc.

So, you are to learn oggenc and know if it accepts headless WAV stream. If so, then just pass your audio to output stream, without processing with AudioSystem.write() which is for headed WAVs

According to here http://www.linuxcommand.org/man_pages/oggenc1.html you are able to accept RAW with oggenc.

Dims
  • 47,675
  • 117
  • 331
  • 600
0

You will be able to solve the issue with length if you reimplement AudioSystem.write method. It doesn't work with stream and WAVE format because of data length in the header. You should write audio data to some array and then prepare correct WAVE Header. Example: How to record audio to byte array

Kanaris007
  • 304
  • 2
  • 6