Author Topic: Loading audio files outside the jar  (Read 2779 times)

Offline xanhou

  • bit
  • Posts: 2
    • View Profile
Loading audio files outside the jar
« on: October 11, 2012, 04:58:03 PM »
Hello everyone reading this,

Although I have found the Sound System library hosted on this site to be by far the easiest sound library for lwjgl + openAL, I still have two questions.
The questions are about loading audio files that are outside the jar file when using the Sound System.

First the most important: How? How can I load files that are outside the jar file into the Sound System lib?
I know it is possible, since minecraft does it, but then again minecraft seems to have recompiled the sources instead of adding a jar file as library. So I hope that I do not need to change any code...

Second: Why is it so hard? I understand that it is easier for some user to load files in an easy way, but that is no reason to leave out a more advanced option to load files using a lower level API, like direct streams or the File class? I just cant imagine that there have been no thoughts about this subject.

Why I would want my audio files outside the jar?
To keep my resources structure uniform for all resources.
Textures, fonts, config files, etc... all those are outside the jar, and with decent reasons.
Putting them into the jar file creates a huge jar file when I get a lot of resources. The ClassLoader is then forced to index all of those files and the JRE has to load the whole jar. This cant be positive for performance.
I also dislike the idea that a user would have to change the jar just to change some texture or audio file.

I really like the simplicity of this library and loading files outside the jar would make it perfect for my goals, so I hope someone can help.

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3495
  • Developer
    • View Profile
    • PaulsCode.Com
Re: Loading audio files outside the jar
« Reply #1 on: October 11, 2012, 06:42:10 PM »
While loading files compiled inside the JAR is the way most small applications do it, it is just as easy to load files that are outside of the JAR too.  All the methods that load audio files (loadSound, quickPlay, etc) have two versions: one that takes a String filename parameter (for files inside the JAR), and one that takes URL and String identifier parameters.  The URL points to the file outside the JAR, and the identifier can be anything (it's just how you identify the sample later), but it should normally look like a filename, and must end in the correct extension for its format (.wav, .ogg, etc).  As with the methods that load files inside the JAR, the methods that load files through URL + identifier will only load the file into memory once, not every time they are called (so you can call quickPlay 100 times without the library loading the file 100 times, for example)

Take the following example, which loads a file compiled inside the JAR:
Code: [Select]
soundSystem.loadSound( "tada.wav" );
The equivalent for loading a file outside the JAR:
Code: [Select]
File file = new File( "C:/WINDOWS/Media/tada.wav" );
soundSystem.loadSound( file.toURL(), "whateveryouwant.wav" );

The identifier you come up with can be used anywhere a filename parameter is needed.
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted

Offline xanhou

  • bit
  • Posts: 2
    • View Profile
Re: Loading audio files outside the jar
« Reply #2 on: October 12, 2012, 06:17:13 AM »
Aha, that is indeed something I did not try. Many thanks.
I discarded the URL methods because I read something about http:// in the javadoc.
The javadoc is not the most detailed one I have ever seen.

Again, many thanks.

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3495
  • Developer
    • View Profile
    • PaulsCode.Com
Re: Loading audio files outside the jar
« Reply #3 on: October 12, 2012, 08:29:09 AM »
Sure, I'll take a look and see if I can improve the javadoc by mentioning some of the alternate versions of a couple methods, like the above loadSound.  Thanks for pointing that out!  Since I wrote the library and am intimately familiar with it, I probably tend to take some things for granted that may not be obvious to the average developer.
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted