Slick Forums

Discuss the Slick 2D Library
It is currently Thu May 23, 2013 12:56 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Jul 18, 2011 9:01 pm 
Offline

Joined: Sun Jun 26, 2011 9:32 pm
Posts: 11
Hey,

I'm starting to use the OpenAL wrapper which is great. However, it doesn't give any controls over the OpenAL sources. I mean, I understand it's designed to hide everything under the hood and it's nice, but in my case, I'd like to get controls over sources for controlling SFX. Actually, I'd like to have a single SFX channel per player, so that some SFX gets "overwritten" by the preceding one.

Of course, I'd like to leverage what has been done in the SoundSStore. I think changing the following would do the job:
- Changing the visibility of "isPlaying" to public,
- Changing the visibility of "stopSource" to public,
- Adding a new playAsSoundAt method with a source parameter,
- Adding a convenience method playAsSound wih the source parameter.

Here is the modified code:
Code:

   /**
    * Stop a particular sound source
    *
    * @param index The index of the source to stop
    */
   public void stopSource(int index) {
      AL10.alSourceStop(sources.get(index));
   }
   
   /**
    * Play the specified buffer as a sound effect with the specified
    * pitch and gain.
    *
    * @param buffer The ID of the buffer to play
    * @param pitch The pitch to play at
    * @param gain The gain to play at
    * @param loop True if the sound should loop
    * @return source The source that will be used
    */
   int playAsSound(int buffer,float pitch,float gain,boolean loop) {
      return playAsSoundAt(buffer, pitch, gain, loop, 0, 0, 0, 0);
   }
   
   /**
     * Play the specified buffer as a sound effect with the specified
     * pitch and gain.
     *
     * @param buffer The ID of the buffer to play
     * @param pitch The pitch to play at
     * @param gain The gain to play at
     * @param loop True if the sound should loop
     * @param source The source to use, 0 to find a free source
     * @return source The source that will be used
     */
    int playAsSound(int buffer,float pitch,float gain,boolean loop, int source) {
        return playAsSoundAt(buffer, pitch, gain, loop, 0, 0, 0, source);
    }
   
    /**
     * Play the specified buffer as a sound effect with the specified
     * pitch and gain.
     *
     * @param buffer The ID of the buffer to play
     * @param pitch The pitch to play at
     * @param gain The gain to play at
     * @param loop True if the sound should loop
     * @param x The x position to play the sound from
     * @param y The y position to play the sound from
     * @param z The z position to play the sound from
     * @return source The source that will be used
     */
    int playAsSoundAt(int buffer,float pitch,float gain,boolean loop,float x, float y, float z) {
        return playAsSoundAt(buffer, pitch, gain, loop, x, y, z, 0);
    }
   
   /**
    * Play the specified buffer as a sound effect with the specified
    * pitch and gain.
    *
    * @param buffer The ID of the buffer to play
    * @param pitch The pitch to play at
    * @param gain The gain to play at
    * @param loop True if the sound should loop
    * @param x The x position to play the sound from
    * @param y The y position to play the sound from
    * @param z The z position to play the sound from
    * @param source The source to use, 0 to find a free source
    * @return source The source that will be used
    */
   int playAsSoundAt(int buffer,float pitch,float gain,boolean loop,float x, float y, float z, int source) {
      gain *= soundVolume;
      if (gain == 0) {
         gain = 0.001f;
      }
      if (soundWorks) {
         if (sounds) {
            int nextSource = source > 0 ? sources.get(source) : findFreeSource(); // the source 0 is reserved for music
            if (nextSource == -1) {
               return -1;
            }
            
            AL10.alSourceStop(sources.get(nextSource));
            
            AL10.alSourcei(sources.get(nextSource), AL10.AL_BUFFER, buffer);
            AL10.alSourcef(sources.get(nextSource), AL10.AL_PITCH, pitch);
            AL10.alSourcef(sources.get(nextSource), AL10.AL_GAIN, gain);
             AL10.alSourcei(sources.get(nextSource), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
            
             sourcePos.clear();
             sourceVel.clear();
            sourceVel.put(new float[] { 0, 0, 0 });
            sourcePos.put(new float[] { x, y, z });
             sourcePos.flip();
             sourceVel.flip();
             AL10.alSource(sources.get(nextSource), AL10.AL_POSITION, sourcePos);
             AL10.alSource(sources.get(nextSource), AL10.AL_VELOCITY, sourceVel);
            
            AL10.alSourcePlay(sources.get(nextSource));
            
            return nextSource;
         }
      }
      
      return -1;
   }
   
   /**
    * Check if a particular source is playing
    *
    * @param index The index of the source to check
    * @return True if the source is playing
    */
   boolean isPlaying(int index) {
      int state = AL10.alGetSourcei(sources.get(index), AL10.AL_SOURCE_STATE);
      
      return (state == AL10.AL_PLAYING);
   }
   


Tell me what you think of this. Do you think it's something you can add to Slick ?

Thanks,
TsuG


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2011 10:40 pm 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
Slick is Open Source, what you want to do ist very specific (Slick needs to satisfy a lot), so you could also copy stuff and make in work :D Just add credits and I guess no one will blame you.

Could add in some example in which way this is useful? (Can't think of any right now, beside the one you said)

_________________
Current Projects:
Image Mr. Hat I
Image Vegan Vs. Zombies
Projects:
RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2011 11:33 pm 
Offline

Joined: Sun Jun 26, 2011 9:32 pm
Posts: 11
Hey, thanks for your answer.

That's what I've done, copied/pasted some code and shazam !

It gets useful I think when you have a lot of simultaneous players (all "rendered" on the same screen) : http://code.google.com/p/tetrisattackjava/.

In that case, a bit of control over the sources is helpful not only for your code but also for your ears :)

But you're right, it must be somehow really specific.

Thanks for your feedback though.

PS: As for the credits, Slick is of course already credited :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2011 10:56 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Could you explain the case a little more - the sound store is trying to manage sources for you - maybe it could be smarter or give control in a more abstract way?

Kev


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group