I’ve been trying to work on a project, and was recently given the task of getting the sound up and running. I can get a single file to load properly, but have been unable to get multiple files to load right:
package ceruleanHero;
import java.nio.ByteBuffer;
import net.java.games.joal.AL;
import net.java.games.joal.ALFactory;
import net.java.games.joal.util.ALut;
public class Sound
{
static AL al;
static final int NUM_BUFFERS = 2;
static final int NUM_SOURCES = 2;
static final int MUSIC1 = 0;
static final int MUSIC2 = 1;
static int[] buffers = new int[NUM_BUFFERS];
static int[] sources = new int[NUM_SOURCES];
static float[][] sourcePos = new float[NUM_SOURCES][2];
static float[][] sourceVel = new float[NUM_SOURCES][2];
static float[] listenerPos = { 0.0f, 0.0f, 0.0f };
static float[] listenerVel = { 0.0f, 0.0f, 0.0f };
static float[] listenerOri = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
static int loadALData()
{
//variables to load into
int[] format = new int[1];
int[] size = new int[1];
ByteBuffer[] data = new ByteBuffer[1];
int[] freq = new int[1];
int[] loop = new int[1];
// load wav data into buffers
al.alGenBuffers(NUM_BUFFERS, buffers, 0);
if (al.alGetError() != AL.AL_NO_ERROR)
{
return AL.AL_FALSE;
}
//music file 1
ALut.alutLoadWAVFile(
"C:/Documents and Settings/Dan/Desktop/EVERYWHERE_YOU_GO.wav",
format,
data,
size,
freq,
loop);
al.alBufferData(
buffers[MUSIC1],
format[0],
data[0],
size[0],
freq[0]);
//ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); Line doesn't work...
//ERROR OCCURS NEXT LINE BELOW!
//music file 2
ALut.alutLoadWAVFile(
"C:/Documents and Settings/Dan/Desktop/ICE_MAN.wav",
format,
data,
size,
freq,
loop);
al.alBufferData(
buffers[MUSIC2],
format[0],
data[0],
size[0],
freq[0]);
//ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); Again, line doesn't work...
// bind buffers into audio sources
al.alGenSources(NUM_SOURCES, sources, 0);
al.alSourcei(sources[MUSIC1], AL.AL_BUFFER, buffers[MUSIC1]);
al.alSourcef(sources[MUSIC1], AL.AL_PITCH, 1.0f);
al.alSourcef(sources[MUSIC1], AL.AL_GAIN, 1.0f);
al.alSourcefv(sources[MUSIC1], AL.AL_POSITION, sourcePos[MUSIC1], 0);
al.alSourcefv(sources[MUSIC1], AL.AL_POSITION, sourceVel[MUSIC1], 0);
al.alSourcei(sources[MUSIC1], AL.AL_LOOPING, AL.AL_TRUE);
al.alSourcei(sources[MUSIC2], AL.AL_BUFFER, buffers[MUSIC2]);
al.alSourcef(sources[MUSIC2], AL.AL_PITCH, 1.0f);
al.alSourcef(sources[MUSIC2], AL.AL_GAIN, 1.0f);
al.alSourcefv(sources[MUSIC2], AL.AL_POSITION, sourcePos[MUSIC2], 0);
al.alSourcefv(sources[MUSIC2], AL.AL_POSITION, sourceVel[MUSIC2], 0);
al.alSourcei(sources[MUSIC2], AL.AL_LOOPING, AL.AL_FALSE);
// do another error check and return
if (al.alGetError() != AL.AL_NO_ERROR)
{
return AL.AL_FALSE;
}
return AL.AL_TRUE;
}
static void setListenerValues()
{
al.alListenerfv(AL.AL_POSITION, listenerPos, 0);
al.alListenerfv(AL.AL_VELOCITY, listenerVel, 0);
al.alListenerfv(AL.AL_ORIENTATION, listenerOri, 0);
}
static void killAllData()
{
al.alDeleteBuffers(NUM_BUFFERS, buffers, 0);
al.alDeleteSources(NUM_SOURCES, sources, 0);
ALut.alutExit();
}
static void playMusic1()
{
al.alSourcePlay(sources[MUSIC1]);
System.out.println("Sound File 1 Playing");
}
static void playMusic2()
{
al.alSourcePlay(sources[MUSIC2]);
System.out.println("Sound File 2 Playing");
}
static void stopAllMusic()
{
killAllData();
System.out.println("All Sounds OFF");
}
public static void main(String[] args)
{
ALut.alutInit();
al = ALFactory.getAL();
al.alGetError();
if(loadALData() == AL.AL_FALSE)
{
System.exit(1);
}
setListenerValues();
playMusic1();
playMusic2();
stopAllMusic();
}
}
Exception in thread “main” java.lang.OutOfMemoryError: Direct buffer memory
at java.nio.Bits.reserveMemory(Bits.java:633)
at java.nio.DirectByteBuffer.(DirectByteBuffer.java:95)
at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
at net.java.games.joal.util.WAVLoader.readFromStream(WAVLoader.java:112)
at net.java.games.joal.util.WAVLoader.loadFromFile(WAVLoader.java:69)
at net.java.games.joal.util.ALut.alutLoadWAVFile(ALut.java:115)
at ceruleanHero.Sound.loadALData(Sound.java:63)
at ceruleanHero.Sound.main(Sound.java:144)
This error occurs when i try to define the second sound file. I know in older versions of JOAL, the function ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); would unload the sound data, but no longer works as is. I’m stumped on this one, any help would be appriciated.
EDIT
Showed where error occurs and the code which doesn’t work. (Note the tutorials on the JOAL site are out of date; it took me some time getting what I have working)
Also, the error occurs at compile time (should have mentoned this before). I’m guessing the non-working ALut.alutUnloadWAV(format[0], data[0], size[0], freq[0]); function is the cause of this, but I have been unable to get that function working right…