Author Topic: 3D Sound System  (Read 296048 times)

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #195 on: September 17, 2008, 09:55:19 pm »
Why don't you propagate the LWJGLException (or maybe a simple RuntimeException) up to the constructor of SoundSystem, so that i can catch it!?

Sure, that is a good idea.  I'll post the update as soon as I finish, and I'll also have it throw any exceptions from the switchLibrary method as well.

-- EDIT --
OK, I'm dumb.  Hopefully you didn't read what I originally posted here (you'd think I didn't know how my own library works ;D).  ANYWAY, I created an exception called SoundSystemException, so that the SoundSystem class is not dependant of lwjgl just to throw the LWJGLException.  The SoundSystemException will contain the message from the LWJGLException, or a message explaining whatever problem occurred during load time.  The switchLibrary() method also throws a SoundSystemException if a problem occurred while trying to switch libraries.  Additionaly, I added in a checkLoadError() method into the SoundSystem class, which returns true if an error occurred during load time.  It is a synchronized interface, so it could be used from a seperate thread elsewhere in your program where you might need to know if an error had occurred during load time.  The checkLoadError() method can also be used after the switchLibrary() method to know if an error occurred while trying to switch to a different library.

BTW, technically I could get rid of the exception throwing in the constructor and just have you use the checkLoadError() method (would eliminate the need to put in a try/catch when instantiating the SoundSystem).  Personally I don't have a real preference either way (I'd only need to cut out a couple lines of code).  What are your thoughts on this?
« Last Edit: November 15, 2008, 04:19:56 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #196 on: September 18, 2008, 01:19:18 am »
-- EDIT --
Hehe, hopefully I got it right this time ;D.  Let me know if this doesn't catch the error you are experiencing.

JARS:

Sound System jPCT
Sound System
Sound Manager


Source Code:

Sound System jPCT
Sound System
Sound Manager


Oh, BTW, I recently reorganized things a little better on my server.  I updated all the links in this thread, and removed any dead links.
« Last Edit: November 15, 2008, 04:19:35 am by paulscode »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: 3D Sound using lwjgl binding of Open AL
« Reply #197 on: September 18, 2008, 11:26:54 pm »
The exception is thrown, i can catch it and revert to Javasound...all is fine (except that the exception happens...but it's not your fault, the dll is linked to a missing VC++9.0 dll).

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #198 on: September 18, 2008, 11:50:47 pm »
Sound System Without Exception in Constructor

JARS:

Sound System jPCT  (Download Source Code)

Sound System  (Download Source Code)

Sound Manager  (Download Source Code)


This is basicaly the same as the last release, except without the exception thrown in the constructor.  After a lively discussion on the gamedev forums, I decided to remove the exception being thrown in the constructor at the request of two other programmers who have been using the SoundSystem library in their games (as well as input from a couple of other random people on the forum).  Their argument was that since the checkLoadError() method can be used to determine when an error occurred while loading the sound library, there is no need to make the constructor more complicated than it needs to be.  As a compromise, I merely commented out "throws SoundSystemException" in the two constructors and added in a try/catch.  This can be easily changed back if desired (it is difficult to please everyone, but I think this is the best choice all around).  Any future releases of the SoundSystem will not throw the exception in the constructor either.

The only new thing that I added to this release is a synchronized method in the SoundSystem class called initialized(), which will return false if a sound library is busy loading.  I thought that this might also be usefull from a program that uses multiple threads which might need to check if the SoundSystem is busy switching between libraries (shouldn't be an issue since commands are all executed in sequence from a single command thread, but just in case someone finds this functionality useful, it is there).

-- EDIT --
BTW, what I could do if you need to know what exception was thrown during the constructor, add a method which will retrieve the exception that was thrown.
« Last Edit: November 15, 2008, 04:18:56 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #199 on: September 19, 2008, 01:22:56 pm »
I thought of a way to kill two birds with one stone.  I can add a static method called libraryCompatible( int ) to the SoundSystem class.  It will return true if a library is compatible or false if not.  For OpenAL, it will try to call AL.create(), and if an LWJGLException is thrown, it will return false.  For JavaSound, it will search for the Mixer, and if it is not found, it will return false.

So in your program, you would be able to check if a particular library is compatible before instantiating the SoundSystem.  i.e.:

Code: [Select]
int useLibrary;

if( SoundSystem.libraryCompatible( SoundSystemConfig.LIBRARY_OPENAL ) )
    useLibrary = SoundSystemConfig.LIBRARY_OPENAL;
else if( SoundSystem.libraryCompatible( SoundSystemConfig.LIBRARY_JAVASOUND ) )
    useLibrary = SoundSystemConfig.LIBRARY_JAVASOUND;
else
    useLibrary = SoundSystemConfig.LIBRARY_NOSOUND;

SoundSystem soundSys = new SoundSystem( useLibrary );

How does that idea sound?
« Last Edit: November 15, 2008, 04:18:27 am by paulscode »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: 3D Sound using lwjgl binding of Open AL
« Reply #200 on: September 19, 2008, 02:06:47 pm »
Sounds ok to me. However, doing it the exception-way is ok for me too. Do it in the way you prefer and i'll adapt to it.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #201 on: September 19, 2008, 11:11:57 pm »
Sound System with library compatibility checking

JARS:

Sound System jPCT  (Download Source Code)

Sound System  (Download Source Code)

Sound Manager  (Download Source Code)


I finished adding the static libraryCompatible( int  ) method into the SoundSystem class.  It can be used as I mentioned in my last post, to check if a particular library is compatible on the user's system.

I removed the checkLoadError() method, and replaced it with the static method getLastException(), which returns the last exception thrown or null if none.

Additionally, I altered the SoundSystemException class, adding a getType() method, which returns one of several possible int identifiers for what caused the exception (see the JavaDoc for more details).

I think I've covered all the bases this time ;D  Let me know if you think of anything else that I can add to make the library better.
« Last Edit: November 15, 2008, 04:17:50 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #202 on: September 20, 2008, 03:15:53 pm »
I have decided to put in compatibility testing into the first SoundSystem constructor which doesn't take a library type parameter.  That way compatibilty checking would only be required when manually specifying which sound library to use.  I am also going to add a synchronized static method to the SoundSystem class called currentLibrary(), which will return the library type that is currently loaded.

-- EDIT --
I hate to keep flip-flopping on the exception-throwing issue, but I think I am going to change the second SoundSystem constructor (the one which takes a library-type parameter) back so that it throws a SoundSystemException again.  The reason I changed my mind on this again is that I kind of want to force the programmer to think about library compatibility when manually selecting a libraries (the switchLibrary() method also throws a SoundSystemException).  The first SoundSystem constructor (the one with no parameters) still won't throw an exception, since it will be intelligent enough to check for library compatibility automatically.

By having the second constructor throw an exception, this should accomplish my goal of forcing the programmer to think about compatibility issues when manually selecting a library.

By having the first constructor not throw an exception will hopefully be helpful for those who want to put as little thought into the sound library as possible.
-- END EDIT --
« Last Edit: November 15, 2008, 04:17:27 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #203 on: September 21, 2008, 12:27:42 am »
Sound System with automatic compatibility checking

JARS:

Sound System jPCT  (Download Source Code)

Sound System  (Download Source Code)

Sound Manager  (Download Source Code)

As mentioned in my last post, the constructor without parameters will automatically attempt to initialize the highest priority library first, and if that fails it will try the next one, and so on.  The default priority order for loading libraries is OpenAL first, then JavaSound, then No Sound.  This order can be changed by creating an int array of library types in their order of priority, and then passing that to the static method SoundSystemConfig.setLibraryPriorities:

Code: [Select]
int[] libraries = new int[3];
libraries[0] = SoundSystemConfig.LIBRARY_JAVASOUND;  // will try to load first
libraries[1] = SoundSystemConfig.LIBRARY_OPENAL;     // will try this second
libraries[2] = SoundSystemConfig.LIBRARY_NOSOUND;    // this one last

SoundSystemConfig.setLibraryPriorities( libraries );
mySoundSystem = new SoundSystem();

I also finished writing my tutorial-style guide.  It is in PDF format.  I put links to it and the example programs  for the guide on page one of this thread.
« Last Edit: November 15, 2008, 04:16:37 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #204 on: September 24, 2008, 12:44:08 am »
Sound System, "applet no-sound" bug fix

JARS:

Sound System jPCT  (Download Source Code)

Sound System  (Download Source Code)

Sound Manager  (Download Source Code)

I fixed an OpenAL thread-synchronization bug that would occur on some computers in cases where the SoundSystem object was created from outside the main thread, such as in an applet's init() method.

The no-sound bug probably cropped up after I rearranged things when I added the code to propagate exceptions back to the SoundSystem constructor.  For some reason, I had moved the library initialization code from being run by the Command Thread.  This was a simple fix.  I moved initialization back to where it is run from the command thread.  To keep the exception propagation working, I now have the thread which instantiated the SoundSystem wait up to ten seconds for initialization to finish.  If initialization is not complete by that time, it throws an exception.  Likewise, if initialization resulted in an exception, the constructor retreives that exception with the getLastException() method and throws it.

This change is completely in the background, so none of the methods or constructors are different than the last release of SoundSystem.
« Last Edit: November 15, 2008, 04:16:06 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #205 on: September 26, 2008, 01:02:37 am »
Hey, Egon, this is likely to be one of the last posts on this thread, so before it is left to slowly sink out of sight beneath all the awsome new future projects, I would like to request that this thread be made sticky, or that a link to Sound System be added to the 3rd party downloads section (at your discretion, of course).

The reason for this request is that developers who are new to jPCT may also be looking for a 3D sound library to use in their project, and making SoundSystem easy to find could be helpful.

If you choose to put a link in the 3D party downloads section, the following zip file contains all the JAR's, javaDoc, source code, tutorial guide, and example applications.  If I make any future updates, tutorials, etc., I will re-zip everything and place it at the same location, so this link will always be correct:

http://www.paulscode.com/downloads/SoundSystem.zip (last updated: 26 November 2008)
« Last Edit: November 27, 2008, 12:35:48 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #206 on: November 15, 2008, 03:14:07 am »
Sound System, automatic compatibility check bug fix

JARS:

Sound System jPCT  (Download Source Code)

Sound System  (Download Source Code)

Sound Manager  (Download Source Code)

Fixed a problem some people were having on slower machines, where the automatic library compatibility check did not wait long enough for OpenAL to load before switching to JavaSound.  Fixed it so SoundSystem waits up to 10 seconds before giving up and switching.  This seems to be long enough for everyone who was having the problem.
« Last Edit: November 15, 2008, 04:12:25 am by paulscode »

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: 3D Sound System
« Reply #207 on: November 15, 2008, 08:45:33 pm »
Thanks for the work.  Hopefully I'll be using it on my next game.  I didn't download yet, though, because I'm really just getting started on it.  I was working on something else for a while.
click here->Fireside 7 Games<-

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #208 on: November 16, 2008, 06:42:54 am »
Sound System, streaming sources no longer pre-loaded!


JARS:

Sound System jPCT  (Download Source Code)

Sound System  (Download Source Code)

I went back and looked at compatibility issues, and figured out the source of all my previous problems.  It was related to endianness.  Understanding this allowed me to write a working method for streaming directly from .ogg and .wav files.  I was able to remove the annoying workaround where I had to preload every audio file.

This is an enormous reduction in the amount of memory required for streaming sources, since SoundSystem now streams directly from the file or URL instead of from a huge pre-loaded audio buffer.
« Last Edit: November 16, 2008, 06:46:57 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: 3D Sound System
« Reply #209 on: November 16, 2008, 05:23:14 pm »
SoundSystem library-switching bug fix, SoundSystemSmall released.


JARS:

Sound System jPCT  (Download Source Code)

Sound System  (Download Source Code)

Fixed a bug where audio data was being pre-loaded for streaming sources when switching between libraries.


I also decided to make a smaller version of SoundSystem for use in projects where memory is a concern, such as in web applets.  I removed support for OpenAL, eliminated all MIDI stuff, and cut out many of the redundant methods.  I was able to reduce the size of the JAR from 280KB down to 110KB.  The j-ogg library source is not compiled into the JAR, so to play .ogg files, the j-ogg library jar must be included at run time.  I stuck the compiled j-ogg jar inside the SourceCode .zip file):

Sound System Small  (Download Source Code)