LWJGL
May 19, 2013, 03:24:47 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL is now using GitHub
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: [Solved] OpenAL Loop a Sound a specific amount of times  (Read 2477 times)
kick_master
Newbie
*
Posts: 4


« on: May 19, 2012, 06:56:56 »

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?
Logged
Matthias
Talks Too Much
***
Posts: 180


WWW
« Reply #1 on: May 19, 2012, 12:13:06 »

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

kick_master
Newbie
*
Posts: 4


« Reply #2 on: May 19, 2012, 12:51:34 »

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 (:
Logged
mattdesl
Newbie
*
Posts: 26


« Reply #3 on: May 19, 2012, 18:58:12 »

Something like this:
Code:
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);
Logged
kick_master
Newbie
*
Posts: 4


« Reply #4 on: May 21, 2012, 11:06:07 »

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();
   }
}
Logged
mattdesl
Newbie
*
Posts: 26


« Reply #5 on: May 21, 2012, 11:47:16 »

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:
Code:
     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:
Code:
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. Smiley


EDIT: Also, killALData() (and JVM shutdown) are unreachable... -.-
Logged
kick_master
Newbie
*
Posts: 4


« Reply #6 on: May 21, 2012, 12:28:22 »

thank you very much, now its working.  Smiley

YES, thanks (:
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!