Bug in Mac OS X version: flipped bytes

There’s a bug in the OS X version of JOAL that several people have posted about here and other forums where projects (like Xith) use JOAL. As I understand it, OS X uses big-endian words, and so in 16-bit wav files (ogg seems to be affected by this too) JOAL reads the bytes in reverse order, resulting in static sound (8-bit files, since there’s nothing to flip, are read correctly). This seems like it could be easily fixed if JOAL simply checks if the OS is OS X and reads the files in big-endian format. I’ve checked out the CVS source and the bug is still in there.

I patched WAVLoader.java with this:


        ByteBuffer buffer = ByteBuffer.allocateDirect(size);
        aChannel.read(buffer);
      
      // On Mac we need to convert this to big endian
      if (bits == 16 && ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
      {
            buffer.rewind();
            ShortBuffer tmp = buffer.duplicate().order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
            while(tmp.hasRemaining())
                  buffer.putShort(tmp.get());
      }

        result = new WAVData(buffer, format, size, freq, false);
        aIn.close();

[quote]I patched WAVLoader.java with this:


        ByteBuffer buffer = ByteBuffer.allocateDirect(size);
        aChannel.read(buffer);
      
      // On Mac we need to convert this to big endian
      if (bits == 16 && ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
      {
            buffer.rewind();
            ShortBuffer tmp = buffer.duplicate().order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
            while(tmp.hasRemaining())
                  buffer.putShort(tmp.get());
      }

        result = new WAVData(buffer, format, size, freq, false);
        aIn.close();

[/quote]
Awesome! That works perfectly :slight_smile:

Java programmers will need to be aware that JOAL on Mac expects 16-bit samples in the native (Big Endian) byte order. Loaders for OGG will need similar tweaks.

If you are making samples algorithmically with 16-bit samples be sure to set the ByteBuffer to the native byte order before you stuff it with ‘shorts’.

Yes, it’s perfect! thanks swpalmer!!

But, why is this not actually in a new version??
I cannot understand why this issue still isn’t fixed in the new versions, given that this post dates back to november 2004!! (almost a year ago).

Actually, I will put a new post regarding this… for anyone that has that problem.
Thanks again

Just a heads up that if you do any byte flipping here, because if you tested for MacOS X, make sure you also check for the architecture. I say this since this will break otherwise on the new x86 machines due out in 2006. Truth is, this should be fixed in the native library.

If you take a closer look at my fix… I used “ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN”, I chose that on-purpose in the hope that it would work for unforseen platforms (e.g. Linux PPC). So hopefully OS X on Intel will “just work”.

swpalmer: thanks for the patch. I’ll apply something similar to the source base soon.

Regarding the lack of updates on JOAL, the original maintainer left Sun a while back and the project has been somewhat orphaned since then. I’ll post an update in another thread.