[Solved] OpenAL Loop a Sound a specific amount of times

Started by kick_master, May 19, 2012, 13:56:56

Previous topic - Next topic

kick_master

hey guys, i have been searching for over an hour, but couldnot find anything, so i hope i could find help here;)

is it possible to loop a sound a specific amount of times?
if yes, how?

i just know how to loop a sound infinitely long
AL10.alSourcei(sources.get(arListObjIndex), AL10.AL_LOOPING,
                     AL10.AL_TRUE);
but how can i loop it, for example 3 times?

Matthias

Configure the source for streaming and queue that buffer 3 times.

kick_master

thank you for the quick answer. but anyhow, i dont really understand how to transpose what you meant. can you give me a example please, would be really nice, thanks (:

mattdesl

Something like this:
int repeats = 2;
			AL10.alSourcei(sourceID, AL10.AL_LOOPING, AL10.AL_FALSE);
			for(int i = 0; i < repeats; i++)
				AL10.alSourceQueueBuffers(sourceID, bufferID);
			AL10.alSourcePlay(sourceID);

kick_master

hey mate,

i tried all, i dont know what do do anymore, it just dosent work, i just create a simple example, which is really similar to my projekt..maybe u see what is wrong :/ plz help me..

Quote
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
import org.lwjgl.util.WaveData;

import org.lwjgl.openal.*;

public class Lesson5 {
   public static final int NUM_BUFFERS = 6;
   IntBuffer buffer = BufferUtils.createIntBuffer(NUM_BUFFERS);
   IntBuffer source = BufferUtils.createIntBuffer(128);

   
   public Lesson5() {
   }

   int loadALData() {
      AL10.alGenBuffers(buffer);

      if (AL10.alGetError() != AL10.AL_NO_ERROR)
         return AL10.AL_FALSE;

      WaveData waveFile = WaveData.create("explosion.wav");
      AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data,
            waveFile.samplerate);
      waveFile.dispose();
      
      if (AL10.alGetError() == AL10.AL_NO_ERROR)
         return AL10.AL_TRUE;

      return AL10.AL_FALSE;
   }

   private void addSource(int type) {

      AL10.alGenSources(source);

      AL10.alSourcei(source.get(type), AL10.AL_BUFFER, buffer.get(type));
      AL10.alSourcef(source.get(type), AL10.AL_PITCH, 1.0f);
      AL10.alSourcef(source.get(type), AL10.AL_GAIN, 1.0f);
      AL10.alSource3f(source.get(type), AL10.AL_POSITION, 0.0f, 0.0f, 0.0f);
      AL10.alSource3f(source.get(type), AL10.AL_VELOCITY, 0.0f, 0.0f, 0.0f);

      int repeats = 2;
      AL10.alSourcei(source.get(type), AL10.AL_LOOPING, AL10.AL_FALSE);
      for(int i = 0; i < repeats; i++)
         AL10.alSourceQueueBuffers(source.get(type), buffer.get(type));
      
      AL10.alSourcePlay(source.get(type));
   }

   private void stopSource(int type) {
      AL10.alSourceStop(source.get(type));
   }

   void setListenerValues() {
      AL10.alListener3f(AL10.AL_POSITION, 0.0f, 0.0f, 0.0f);
      AL10.alListener3f(AL10.AL_VELOCITY, 0.0f, 0.0f, 0.0f);
      AL10.alListener3f(AL10.AL_ORIENTATION, 0.0f, 0.0f, 0.0f);
   }

   void killALData() {
      AL10.alDeleteSources(source);

      AL10.alDeleteBuffers(buffer);
   }

   public void execute() {
      try {
         AL.create();
      } catch (LWJGLException le) {
         le.printStackTrace();
         return;
      }
      AL10.alGetError();
      
      if (loadALData() == AL10.AL_FALSE) {
         System.out.println("Error loading data.");
         return;
      }

      setListenerValues();

      System.out.print("Controls:\n");
      System.out.print("w) Play sample.\n");
      System.out.print("q) Quit\n\n");

      // Loop.
      char c = ' ';
      while (c != 'q') {
         try {
            c = (char) System.in.read();
         } catch (IOException ioe) {
            c = 'q';
         }

         switch (c) {
         case 'w':
            addSource(0);
            break;
         }
      }
      killALData();
   }

   public static void main(String[] args) {
      new Lesson5().execute();
   }
}

mattdesl

Use glGetError to debug through your program.

You'll find that your listener orientation is causing an error. It should be set up like so:
     FloatBuffer ori = BufferUtils.createFloatBuffer(6);
      ori.put(0).put(0).put(-1).put(0).put(1).put(0).flip();
      AL10.alListener(AL10.AL_ORIENTATION, ori);


The alSourceQueueBuffers is also causing an error. That's because you've already set up a buffer with your source. Comment out the following line and the sound will start looping:
AL10.alSourcei(src, AL10.AL_BUFFER, buffer);


You might always think about using the methods alGenSources() and alGenBuffers() that return an integer; they exist for convenience so you don't need to deal with buffers. :)


EDIT: Also, killALData() (and JVM shutdown) are unreachable... -.-

kick_master

thank you very much, now its working.  :)

YES, thanks (: