OGG Help

Well, technically this is not really OGG related I don’t think. But I’m not sure where else to post this. But I have the craziest thing, I have an AudioInputStream that eventually just blocks on a read and never comes back. The crazy thing is the same class running on a different machine doesn’t block! And I’m using the same version of the JVM. I’m going nuts trying to figure this one out. I used the same OGG file to create the stream, have the same class, same jvm just different machines and on one machine the read blocks and the other the read returns an EOF properly. Please, any help would seriously be appreciated as I’m going crazy on this one. Here’s the class I’m using:


import javax.sound.sampled.*;
import java.io.*;

public class OggTest
{
      public static void main(String[] args)
      {
            try
            {
                  File songFile = new File(args[0]);
                  AudioInputStream ais = AudioSystem.getAudioInputStream(songFile);
                  AudioFormat afVorbis = AudioSystem.getAudioFileFormat(songFile).getFormat();
                  AudioFormat afPCM = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                        afVorbis.getSampleRate(), 16, afVorbis.getChannels(),
                        afVorbis.getChannels() * 2, afVorbis.getSampleRate(), false);
                  ais = AudioSystem.getAudioInputStream(afPCM, ais);

                  DataLine.Info dli = new DataLine.Info(SourceDataLine.class, ais.getFormat());
                  SourceDataLine sdl = (SourceDataLine) AudioSystem.getLine(dli);
                  sdl.open(ais.getFormat());

                  sdl.start();
                  int bytesRead = 0;
                  byte[] byteBuffer = new byte[128000];
                  while (bytesRead != -1)
                  {
// The problem is this read blocks on the very last read and never returns
                        bytesRead = ais.read(byteBuffer, 0, byteBuffer.length);
                        if (bytesRead != -1)
                        {
                              sdl.write(byteBuffer, 0, bytesRead);
                        }
                  }

                  sdl.drain();
                  sdl.stop();
                  sdl.close();
            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
            
            System.exit(0);
      }
}

i am not sure if it works but i usually do this

int bytesRead = 0;
byte[] byteBuffer = new byte[128000];
while (( bytesRead = ais.read(byteBuffer, 0, byteBuffer.length))!= -1)
{

 sdl.write(byteBuffer, 0, bytesRead); 

}

I’m not quite sure I understand. Isn’t that just the same thing I have just syntactically shortened? I appreciate your help but I’m not sure I catch your drift. Could you further explain?

Alex didn’t say anything new, just a stylistic preference.

I don’t know why the code would hand in one spot liek that for you unless it was a real JVM bug or you aren’t showing us all your code.

The contract for InputStream.read(byte[], int, int) does state that: “This method blocks until input data is available, end of file is detected, or an exception is thrown.” How a file is not available is beyond me.

Anyway, you may want to conider taking advantage of the InputStream. available() method.

I tried to run your code with the vorbis spi 0.6 and 0.7 but got no love. One thing you can do is press + at the console and look at the output and figure out where the “main” thread is currently blocking.

Ah, unfortunately this thread doesn’t have what I posted in a different thread. Too bad it can’t automagically update all related posts :slight_smile:

Anyhow, the problem lies in the fact that once converted to PCM, the AudioInputStream always returns -1 for getFrameLength() and available() always returns 0. This is due to something deep deep in the Tritonus API where the AudioInputStream is actually reading from an internal TCircularBuffer. This buffer blocks on the read with a

 while (buffer.available() == 0) { Object.wait(); } 

Since available() is always 0 at the end it always blocks. There’s also a condition where if frameLength == -1 then it skips the EOF check based on frameLength. When I found this out I was overjoyed and thought maybe I could fix it. Then I tried a smaller file, about 1MB. And lo and behold it worked perfectly fine. In fact it only starts this problem once the input file size gets to around 3MB. So I was back to square one. I emailed the VorbisSPI who are responsible for the OGG to PCM conversion code but they haven’t responded. And it looks like Tritonus is all but dead considering they haven’t updated in like 2 years.

P.S. That’s all the code necessary but you need to have the proper jars. VorbisSPI 0.6 should have all of them and I can’t get VorbisSPI 0.7 to recognize an OGG file and they haven’t been any help on that bug either.

Of course they were really helpful for a different problem I have, so what can I expect for free :slight_smile: And it does work really well except for the bugs I’m battling now.