Clip can play mp3 but OpenAL just gives silence (Java)

541 views Asked by At

When I test an mp3 file using sound.sampled.Clip, it plays just as expected. When I test it with OpenAL however, I get no sound, and no exception.

(I'm using JMF's mp3plugin.jar as well as the OpenAL libraries)

This is a difficult problem to demonstrate with simple code, since working with openal requires a number of libraries and a bit of setup code, however I've tried my best:

public static void main(String[] args) throws Exception {
    init(); //initialises OpenAL's device, context and alCapabilities

    Thread.sleep(1000);

    System.out.println("Clip");

    AudioInputStream ais = AudioSystem.getAudioInputStream(new File("grumble.mp3"));
    ais = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais);        
    Clip clip = AudioSystem.getClip();
    clip.open(ais);
    clip.start(); //plays both wav and mp3

    Thread.sleep(2000);

    clip.close();
    ais.close();
    System.out.println("OpenAL");

    ais = AudioSystem.getAudioInputStream(new File("grumble.mp3"));
    ais = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais);
    AudioFormat format = ais.getFormat();
    byte[] byteArr = new byte[ais.available()];
    ais.read(byteArr);

    ByteBuffer buf = BufferUtils.createByteBuffer(byteArr.length);
    buf.put(byteArr);
    byteArr = null;
    ais.close();
    buf.flip();

    int buffer = alGenBuffers();
    alBufferData(buffer, getALFormat(format), buf, (int)format.getSampleRate() );
    int source = alGenSources();
    alSourcei(source, AL_BUFFER, buffer);

    alSourcePlay(source); //plays wav, but is silent when trying to play mp3

    Thread.sleep(2000);

    System.out.println("end");
    alDeleteSources(source);
    alcCloseDevice(device);
}

public static int getALFormat(AudioFormat format)
{
    int alFormat = -1;
    if (format.getChannels() == 1) {
        System.out.println("mono ");
        if (format.getSampleSizeInBits() == 8) {
            alFormat = AL_FORMAT_MONO8;
        } else if (format.getSampleSizeInBits() == 16) {
            alFormat = AL_FORMAT_MONO16;
        } else {
            System.out.println("sample size: "+format.getSampleSizeInBits());
        }
    } else if (format.getChannels() == 2) {
        System.out.println("stereo ");
        if (format.getSampleSizeInBits() == 8) {
            alFormat = AL_FORMAT_STEREO8;
        } else if (format.getSampleSizeInBits() == 16) {
            alFormat = AL_FORMAT_STEREO16;
        } else {
            System.out.println("sample size: "+format.getSampleSizeInBits());
        }
    }

    return alFormat;
}

and this is all I get in console:

Clip
OpenAL
stereo 
end

In-case you're wondering why I need to use OpenAL, OpenAL provides both panning and pitch manipulation that Clip is a lot more touchy with. I have been able to pan and pitch any wav file I give it, whereas Clip frequently won't do either (and has latency when adjusting panning/gain, whereas OpenAL is instant).

0

There are 0 answers