FSOUND_Sample_Load

Hi! Could someone answer to the following basic question please? Thanks.

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=cluebies;action=display;num=1101922757

Yes and no.

The sample is completely loaded and played from memory at the time of playback. Loading a sample does not prime the sound for this. Instead, it merely tells FMOD what file and playback properties to use. This is fine though as samples are meant to be small. Thus, you shouldn’t take a huge performance hit when doing this.

If you’re a speed fanatic, or if you’re trying to load a big sample and don’t want to stream it, there are tricks you can use.

  1. The FMOD community has a few open source projects revolving around this. The projects involve loading samples into ram for faster playback. You can take a look around the web. Unfortunatly, I don’t have any more information than this… I read too much and remember so little on where it came from.

  2. Although it’s more of a hack than a fix, you could put a sample into a module (filetype .it, .mod, .s3m) using a freeware tracker of some sort. (Impulse Tracker, FastTracker, Modplug Tracker) FMOD loads modules into a memory buffer, including it’s samples. It is faster to load a sample from a module than it is to load a sample off of your disk. You can do it like this:

// Modue containing the sample desired.
FMusicModule module = A VALID MODULE
// Index is the instrument number that the sample is assigned in the module.
int index = 0;
// Get the sample from the module.
FSoundSample sample = FMusic.FMUSIC_GetSample(module, index);

Messing around with a tracker might be a bit more work than what you’re looking for. However, if for some reason you want to go out of your way and make FMOD faster than it already is, you’re probably crazy enough to do just about anything.

OK!

Thanks a lot for these useful informations.

Since I use fmod3 in Mighty Bubbles the performance of sound is…ho my god! very very fast! There is no latency before playback (I mean no perceptible) and this is pretty cool! :smiley: Thanks to princec and Matzon!

Everything is working fine locally but in Webstart I need to open the sounds as resources with an InputStream. Is it possible to do that with the FSound class?

Thanks again!

Last time I tried to use the Fmod with LWJGL, the FSOUNDSample_Load(bytebuffer) wasn’t working properly. On top of that, because you’re using webstart, you can’t just give Fmod a the file’s path as a string because FMOD (c++) doesn’t know how to get things out of jars. This is where I got the idea to pack my sounds into a module.

            else if(ref.endsWith(".it"))
            {
                  ByteBuffer buffer = null;

                  try
                  {
                        BufferedInputStream in = new BufferedInputStream(StreamSound.class.getClassLoader().getResourceAsStream(ref));
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                  
                        int bufferLength = 4096;
                        byte[] readBuffer = new byte[bufferLength];
                        int read = -1;
                  
                        while ((read = in.read(readBuffer, 0, bufferLength)) != -1)
                              out.write(readBuffer, 0, read);
                                          
                        in.close();
                  
                        buffer = ByteBuffer.allocateDirect(out.size());
                        buffer.order(ByteOrder.nativeOrder());
                        buffer.put(out.toByteArray());
                        buffer.flip();
            }
                  catch(Exception ioe)
                  {
                        ioe.printStackTrace();
                  }
                  
                  FMusicModule module = FMusic.FMUSIC_LoadSongEx(buffer, 0, buffer.remaining(), FSound.FSOUND_LOADMEMORY, null);

Since FModules/FSoundStreams can be loaded via bytebuffer, this works for loading them. As for sounds, I keep all my wavs in an .it file and grab them using the code I gave you in the first post. Right now you’ll have to use this workaround until the native code is updated.

take a look at org.lwjgl.test.fmod3.StreamPlayerMemory which does a:

ByteBuffer data = getData(args[0]);
            FSoundStream stream = FSound.FSOUND_Stream_Open(data, FSound.FSOUND_LOADMEMORY, 0, data.capacity());

where getData is implemented as:

/**
       * Reads the file into a ByteBuffer
       * 
       * @param filename Name of file to load
       * @return ByteBuffer containing file data
       */
      static protected ByteBuffer getData(String filename) {
            ByteBuffer buffer = null;
            
            try {
                  BufferedInputStream bis = new BufferedInputStream(StreamPlayerMemory.class.getClassLoader()
                                                                                                                                                                        .getResourceAsStream(filename));
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  
                  int bufferLength = 4096;
                  byte[] readBuffer = new byte[bufferLength];
                  int read = -1;
                  
                  while ((read = bis.read(readBuffer, 0, bufferLength)) != -1) {
                        baos.write(readBuffer, 0, read);
                  }
                  
                  //done reading, close
                  bis.close();
                  
                  // if ogg vorbis data, we need to pass it unmodified to alBufferData
                  buffer = ByteBuffer.allocateDirect(baos.size());
                  buffer.order(ByteOrder.nativeOrder());
                  buffer.put(baos.toByteArray());
                  buffer.flip();
            } catch (Exception ioe) {
                  ioe.printStackTrace();
            }
            return buffer;
      }

I managed to use the FSOUND_Sample_Load with a ByteBuffer. One question: with this nethod why do I need to pass the FSOUND_LOADMEMORY flag since the ByteBuffer clearly provides the data?

Thanks for your help!

coz the java binding isn’t intelligent

TheAnalogKid, completely off topic. Could you please make that “play mighty bubble” non-moveable. Its distracting when I read the thread :stuck_out_tongue:

done