Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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  
  EasySound (FmodEX)  (Read 4875 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2009-05-16 19:41:20 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
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.
Offline Darrin

Junior Duke


Projects: 1



« Reply #1 - Posted 2009-05-17 12:17:37 »

Cool link.  fmod is pretty popular.

Does it work with applets?

How big is the add on in mb?


Offline Cero
« Reply #2 - Posted 2009-06-08 06:46:02 »

I'm heavily interested in FMOD

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2010-06-22 16:16:37 »

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

Offline Jacob_

Junior Duke


Projects: 3



« Reply #4 - Posted 2010-06-26 19:10:52 »

What's the license for this?
Offline paulscode

Senior Duke


Medals: 12


Staff Sergeant


« Reply #5 - Posted 2010-06-27 11:39:10 »

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.

We love death.  The US loves life.  That is the difference between us.  -Osama bin Laden, mass murderer
Offline Jacob_

Junior Duke


Projects: 3



« Reply #6 - Posted 2010-06-27 20:38:16 »

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.
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.

theagentd (16 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!