Programming => OpenAL => Topic started by: jmguillemette on November 02, 2013, 22:14:10

Title: Odd Null Pointer error
Post by: jmguillemette on November 02, 2013, 22:14:10
Im trying to play a wav file. the file is located in my resource folder which i used to load images and other game assets.

I first create a File and ensure the wav file exists.
I then use the WaveData utility to create WaveData.
The wave data utility return null every time.

I can use a String or  FileInputStream when calling the create() and i get the same result


Please not this is test code not final :)

Code: [Select]
package com.iowind.mocha.sound;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
import org.lwjgl.util.WaveData;

public class SoundUtil {

private IntBuffer buffer = BufferUtils.createIntBuffer(1); // hold sound data
private IntBuffer source = BufferUtils.createIntBuffer(1); // sources of sounds
FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f }); // postion of sound source
FloatBuffer sourceVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f }); // velocity of sound
FloatBuffer listenerPos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f }); // position of listener
FloatBuffer listenerVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f }); // velocity of listener
FloatBuffer listenerOri = BufferUtils.createFloatBuffer(6).put(new float[] { 0.0f, 0.0f, -1.0f,  0.0f, 1.0f, 0.0f });/** Orientation of the listener. (first 3 elements are "at", second 3 are "up") */

public void play(){
String wavFile  = "resources/sounds/music/DreamRaidPartI.wav";

File file = new File(wavFile);
System.out.println("File Exists");
//WaveData waveData = WaveData.create(wavFile);
WaveData waveData = WaveData.create(new FileInputStream(file));

System.out.println("WaveData = "+ waveData);
if(AL10.alGetError() == AL10.AL_NO_ERROR){
System.out.println("No Errors");
AL10.alBufferData(buffer.get(0), waveData.format, waveData.data, waveData.samplerate);

AL10.alSourcei(source.get(0), AL10.AL_BUFFER,   buffer.get(0) );
   AL10.alSourcef(source.get(0), AL10.AL_PITCH,    1.0f          );
   AL10.alSourcef(source.get(0), AL10.AL_GAIN,     1.0f          );
   AL10.alSource (source.get(0), AL10.AL_POSITION, sourcePos     );
   AL10.alSource (source.get(0), AL10.AL_VELOCITY, sourceVel     );

   AL10.alListener(AL10.AL_POSITION,    listenerPos);
   AL10.alListener(AL10.AL_VELOCITY,    listenerVel);
   AL10.alListener(AL10.AL_ORIENTATION, listenerOri);

}catch(LWJGLException | IOException e){


protected void finalize() throws Throwable {

public static void main(String[] args) {
SoundUtil util = new SoundUtil();

The output of this code is:

Code: [Select]
File Exists
WaveData = null
No Errors
Exception in thread "main" java.lang.NullPointerException
at com.iowind.mocha.sound.SoundUtil.play(SoundUtil.java:43)
at com.iowind.game.apollo.MusicTest.main(MusicTest.java:9)
AL lib: (EE) alc_cleanup: 1 device not closed

Title: Re: Odd Null Pointer error
Post by: jmguillemette on November 02, 2013, 22:41:04
I resolved the problem by preloading the file into an ByteArrayOutput stream and then converting it to an ByteArrayInputStream and feeding that to the exact same code, in place of my file input stream.

To me this is a bug.

If someone can point me to the LWJGL source repo for this util ill do some work and submit a patch.

Title: Re: Odd Null Pointer error
Post by: jmguillemette on November 02, 2013, 22:52:06
next challenge i ran into was having to rewind the buffers...


Is it really the developers responsibility to rewind position buffers before calling the utility? or should the utility do this for you to be nice "just in case".

I'd be happy to code up a proposed patch.. just point me to the source :)

Title: Re: Odd Null Pointer error
Post by: quew8 on November 03, 2013, 12:15:26
LWJGL does not do this because in the event that you don't want to rewind the buffer, ie the data started mid-way through the buffer (which is often the case when sharing buffers), it would be impossible.

Essentially these buffers work like pointers in C. In C you could just specify a new pointer with an offset into the first one, but this isn't possible with Java so we use the position of the buffer. Likewise with length, we son't have to specify the size of the data as we would in C by using the limit of the buffer. 

Just call flip() after writing the data into the buffer and everything will turn out fine.
Title: Re: Odd Null Pointer error
Post by: jmguillemette on November 03, 2013, 23:05:05
that would address the buffer rewind.. but thats just a nice to have..

the null pointer when loading a file that is within the classpath but not within a jar .. that we should improve.
Im not sure if the fact that wav files are fairly large is also a factor.. but using the exact same code and loading the data to a byteArrayInputStream vs using FileInputStream was the difference between working and and receiving a WaveData that was null.

I think we can make improvements in this util to account for this. Thoughts?