Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  issue with multiple sound sources  (Read 2765 times)
0 Members and 1 Guest are viewing this topic.
Offline gamerk2

Junior Newbie





« Posted 2007-11-28 01:35:44 »

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.<init>(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...
Offline broumbroum

Junior Devvie





« Reply #1 - Posted 2007-11-28 01:55:51 »

if you hadn't used a cached Map impl. before, it be the problem causing this error, because your buffers stores all data in RAM (arrays [] don't clear itselves) which has to be frequently cleared to avoid overflows. You may switch your buffers arrays to some disk cached map, thereby your buffers will be accessible throughout fileChannels which are on heap-memory safe. Smiley

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline gamerk2

Junior Newbie





« Reply #2 - Posted 2007-11-28 06:52:26 »

i would, if i knew how.  I've only been working with openAL/JOAL for two weeks.  I get the concept though, although i'm hoping theres a simpler solution, as i have to get this done within a week or two...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ultraq

Junior Devvie




That's what she said


« Reply #3 - Posted 2007-11-30 06:18:30 »

I can see where the error occurs in the JOAL source code, and it does try to allocate a direct buffer to your wave file.  What size are the wave files you're trying to load?
Maybe you could try loading the files into indirect buffers by copying the JOAL source code that does the file loading, and just replacing the ByteBuffer.allocateDirect() method with ByteBuffer.allocate().

As for the unloadWavData() method, that method doesn't seem to exist anymore with the latest JOAL.

Ultraq's Final MooCow
Bits and Pieces by Emanuel Rabina
Offline broumbroum

Junior Devvie





« Reply #4 - Posted 2007-11-30 15:27:59 »

I've never used this Wavloader but I think the best and quicker solution would be to frag your files in multiple smaller parts. Those parts can be loaded one each after while playing is started. What do you think ? Roll Eyes

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (24 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (46 views)
2014-12-05 12:39:02

SHC (60 views)
2014-12-03 16:27:13

CopyableCougar4 (62 views)
2014-11-29 21:32:03

toopeicgaming1999 (124 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!