JNAL - JNA OpenAL for Java

About

The JNA OpenAL Wrapper for Java project enables easy integration of true 3D sound in your java application. Define multiple sound sources, play them concurrently, control their position, pitch, gain and more - all with a few lines of java code.

Use it in your project

The easiest way to get JNAL is using maven. Just add the following lines to your pom.xml under the dependencies section:

<dependency>
	<groupId>org.urish.openal</groupId>
	<artifactId>java-openal</artifactId>
	<version>1.0.0</version>
</dependency>
				

You can find the project source code on github.

License

The JNAL project is licensed under the GNU General Public License with the Classpath Exception.

Sample Code

First of all, make sure that the OpenAL Soft shared library/DLL is installed on your system. Alternatively, set the system property "jna.library.path" to point to the directory where you have the OpenAL Soft library binaries installed.

Here is a simple program that loads a wave file and plays it using Java OpenAL. If you want to control the position of the sound source (using the setPosition() method), make sure you use a mono sound sample. OpenAL supports setting the position of sound source only for single channel sound samples.

package org.urish.openal.example;

import java.io.File;

import org.urish.openal.OpenAL;
import org.urish.openal.Source;

public class OpenALExample {
	public static void main(String[] args) throws Exception {
		/* Initialize OpenAL, load a sound file and play it */
		OpenAL openal = new OpenAL();
		Source source = openal.createSource(new File("example.wav"));
		source.play();
		
		/* Play with various sound parameters */
		source.setGain(0.75f);        // 75% volume
		source.setPitch(0.85f);       // 85% of the original pitch
		source.setPosition(-1, 0, 0); // -1 means 1 unit to the left
		source.setLooping(true);      // Loop the sound effect
		Thread.sleep(10000);          // Wait for 10 seconds

		/* Cleanup */
		source.close();
		openal.close();
	}
}
				

Go to homepage