EasySound (FmodEX)

Just add this class into your game and you can use both music and sound effects easily like so:


if(keys[KeyEvent.VK_1])
		{
			s1.playBGM(0);
		}
		if(keys[KeyEvent.VK_2])
		{
			s1.playBGM(1);
		}
		if(keys[KeyEvent.VK_3])
		{
		 	s1.playEffect(0);
		}
		if(keys[KeyEvent.VK_4])
		{
			s1.playEffect(1);
		}


EasySound Class


import static java.lang.System.exit;
import static java.lang.System.out;
import static org.jouvieje.FmodEx.Defines.FMOD_INITFLAGS.FMOD_INIT_NORMAL;
import static org.jouvieje.FmodEx.Defines.FMOD_MODE.FMOD_HARDWARE;
import static org.jouvieje.FmodEx.Defines.FMOD_MODE.FMOD_LOOP_OFF;
import static org.jouvieje.FmodEx.Defines.FMOD_TIMEUNIT.FMOD_TIMEUNIT_MS;
import static org.jouvieje.FmodEx.Defines.FMOD_MODE.FMOD_LOOP_NORMAL;
import static org.jouvieje.FmodEx.Defines.VERSIONS.FMOD_VERSION;
import static org.jouvieje.FmodEx.Defines.VERSIONS.NATIVEFMODEX_JAR_VERSION;
import static org.jouvieje.FmodEx.Defines.VERSIONS.NATIVEFMODEX_LIBRARY_VERSION;
import static org.jouvieje.FmodEx.Enumerations.FMOD_CHANNELINDEX.FMOD_CHANNEL_FREE;
import static org.jouvieje.FmodEx.Enumerations.FMOD_CHANNELINDEX.FMOD_CHANNEL_REUSE;
import static org.jouvieje.FmodEx.Enumerations.FMOD_RESULT.FMOD_ERR_CHANNEL_STOLEN;
import static org.jouvieje.FmodEx.Enumerations.FMOD_RESULT.FMOD_ERR_INVALID_HANDLE;
import static org.jouvieje.FmodEx.Enumerations.FMOD_RESULT.FMOD_OK;
import static org.jouvieje.FmodEx.Misc.BufferUtils.newByteBuffer;
import static org.jouvieje.FmodEx.Misc.BufferUtils.SIZEOF_INT;

import java.nio.ByteBuffer;

import org.jouvieje.FmodEx.Channel;
import org.jouvieje.FmodEx.FmodEx;
import org.jouvieje.FmodEx.Init;
import org.jouvieje.FmodEx.Sound;
import org.jouvieje.FmodEx.System;
import org.jouvieje.FmodEx.Defines.INIT_MODES;
import org.jouvieje.FmodEx.Enumerations.FMOD_RESULT;
import org.jouvieje.FmodEx.Exceptions.InitException;

public class EasySound
{
	Channel channel;
	Channel[] efchannel;
	System system;
	Sound bgm;
	Sound[] effect;
	//Sound sound3;
	FMOD_RESULT result;
	int version;
	int max_effects = 2;
	ByteBuffer buffer;
	public boolean loaded = false;
	private boolean usesfx = true;
	private boolean usemusic = true;
	private float mvol = 0.7f;
	private float evol = 0.7f;
	
			
	public EasySound()
	{
		try
		{
			Init.loadLibraries(INIT_MODES.INIT_FMOD_EX_MINIMUM);
		}
		catch(InitException e)
		{
			out.printf("NativeFmodEx error! %s\n", e.getMessage());
			//exit(1);
		}
		
		// Checking NativeFmodEx version
		if(NATIVEFMODEX_LIBRARY_VERSION != NATIVEFMODEX_JAR_VERSION)
		{
			out.printf("Error!  NativeFmodEx library version (%08x) is different to jar version (%08x)\n", 
			NATIVEFMODEX_LIBRARY_VERSION, NATIVEFMODEX_JAR_VERSION);
			//exit(0);
		}
		

		java.lang.System.out.println("one");
		channel = new Channel();
		
		
		
		if(usesfx)
		{
			efchannel = new Channel[max_effects];
			
			for(int i = 0; i < max_effects; i++)
			{	
				efchannel[i] = new Channel();
	//			
			}
		}
		system = new System();
		
		java.lang.System.out.println("two");
		
		result = FmodEx.System_Create(system);
		ERRCHECK(result);
		java.lang.System.out.println("three");
		result = system.init(32, FMOD_INIT_NORMAL, null);
		ERRCHECK(result);
		
		java.lang.System.out.println("four/five");
		
		//Buffer used to store all datas received from FMOD.
		buffer = newByteBuffer(99999);
		
		//Create a System object and initialize.
		
		java.lang.System.out.println("six");
		result = system.getVersion(buffer.asIntBuffer());
		ERRCHECK(result);
		version = buffer.getInt(0);
		
		if(version < FMOD_VERSION)
		{
			out.printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", version, FMOD_VERSION);
		//	exit(0);
		}
		
		java.lang.System.out.println("seven");
		
		if(usesfx)
		{
			effect = new Sound[max_effects];
				
			for(int i = 0; i < max_effects; i++)
			{
				effect[i] = new Sound();
				
				result = system.createSound("effect" + i + ".wav", FMOD_HARDWARE, null, effect[i]);
				ERRCHECK(result);
					
				result = effect[i].setMode(FMOD_LOOP_OFF);
				ERRCHECK(result);
					
				java.lang.System.out.println("effect[" + i + "] = " + effect[i]);
			}
		}
		
		java.lang.System.out.println("eight");
	}
	
	private static void ERRCHECK(FMOD_RESULT result)
	{
		/*if(result != FMOD_OK)
		{
			out.printf("FMOD error! (%d) %s\n", result.asInt(), FmodEx.FMOD_ErrorString(result));
		//	exit(1);
		}*/
	}
	
	public void playTest()
	{
		//for testing
	}
	
	public void playBGM(int num)
	{
		if(usemusic)
		{		
			loaded = false;
			
			if(bgm != null)
				bgm.release();
	
			
			bgm = new Sound();
			
			
			result = system.createSound("bgm" + num + ".mp3", FMOD_HARDWARE, null, bgm);
			ERRCHECK(result);
	
			
			result = bgm.setMode(FMOD_LOOP_NORMAL);
			ERRCHECK(result);
			
			
			result = system.playSound(FMOD_CHANNEL_FREE, bgm, false, channel);
			ERRCHECK(result);
			
			channel.setVolume(mvol);
		}
		loaded = true;
	}
	
	public void playEffect(int num)
	{	
		if(usesfx)
		{
			result = system.playSound(FMOD_CHANNEL_REUSE, effect[num], false, efchannel[num]);
			ERRCHECK(result);
			efchannel[num].setVolume(evol);
		}
	}
}

Note: This uses NativeFMODex, so you need to have that installed to use this.

Cool link. fmod is pretty popular.

Does it work with applets?

How big is the add on in mb?

I’m heavily interested in FMOD

I assume its this: http://jerome.jouvie.free.fr/index.php

Btw there is a new version of NativeFModEx. So some of the code has changed minorly.

I am using Fmod (NativeFmodEx) for my audio system and its amazing.
Why not use a standard that professional game developers use, right?

So if anyone is interested check out http://jerome.jouvie.free.fr

What’s the license for this?

It is under the LGPL (less restrictive than the straight GPL). In other words, you do not have to license your project under the LGPL or make it open-source. However, you do need to provide the end user with the sourcecode (for the LGPL-licensed parts only), the LGPL user agreement, and a method for swapping out the LGPL-licensed parts for other versions than the one you ship with your application. The modular nature of Java makes this pretty simple to do - just keep the LGPL-licensed parts in a seperate JAR, and the user can easily swap that JAR with another that they downloaded/compiled themselves. The requirements are the same for applets - you need to provide a way for them to acquire the library’s sourcecode, the LGPL user agreement, and some method for swapping the library out with different versions.

Oh, thanks for the info. I ended up just using the regular Java sound API; after renaming things that were changed in FModEx updates I got a strange NullPointerException coming from inside the library somewhere.