Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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  
  multiple sounds in openal  (Read 797 times)
0 Members and 1 Guest are viewing this topic.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Posted 2004-11-04 22:34:35 »

just a question i'm messing around with the space invaders demo, trying to understand how openal works, but i'm having problems playing more then one sound at a time if i do try play another sound the sound that is currently running restarts, and new one is not played, i've had a look at some of the tutorials but just can't get it to run, i think it may need a for loop or something in the playSound() method, but not sure any help would, thx

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  
package org.newdawn.spaceinvaders;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.AL10;
import org.lwjgl.test.openal.WaveData;


/**
 * $Id: SoundManager.java,v 1.1 2004/06/12 19:59:20 matzon Exp $
 * <p>
 * Simple sound manager for OpenAL using n sources accessed in
 * a round robin schedule. Source n is reserved for a single buffer and checking for
 * whether it's playing.
 * </p>
 * @author Brian Matzon <brian@matzon.dk>
 * @version $Revision: 1.1 $
 */

public class SoundManager {

  /** We support at most 256 buffers*/
  private int[] buffers = new int[256];
 
  /** Number of sources is limited tby user (and hardware) */
  private int[] sources;
 
  /** Our internal scratch buffer */
  private IntBuffer scratchBuffer = BufferUtils.createIntBuffer(256);
 
  /** Whether we're running in no sound mode */
  private boolean soundOutput;
 
  /** Current index in our buffers */
  private int bufferIndex;
 
  /** Current index in our source list */
  private int sourceIndex;
 
  /**
   * Creates a new SoundManager
   */

  public SoundManager() {
  }

  /**
   * Plays a sound effect
   * @param buffer Buffer index to play gotten from addSound
   */

  public void playEffect(int buffer) {
    if(soundOutput) {
      // make sure we never choose last channel, since it is used for special sounds
         int channel = sources[(sourceIndex++ % (sources.length-1))];
     
      // link buffer and source, and play it
         AL10.alSourcei(channel, AL10.AL_BUFFER, buffers[buffer]);
          AL10.alSourcePlay(channel);
    }
  }

  /**
   * Plays a sound on last source
   * @param buffer Buffer index to play gotten from addSound
   */

  public void playSound(int buffer) {
    if(soundOutput) {
      AL10.alSourcei(sources[sources.length-1], AL10.AL_BUFFER, buffers[buffer]);
      AL10.alSourcePlay(sources[sources.length-1]);
    }
  }
 
  /**
   * Whether a sound is playing on last source
   * @return true if a source is playing right now on source n
   */

  public boolean isPlayingSound() {
        return AL10.alGetSourcei(sources[sources.length-1], AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING;
  }
 
  /**
   * Initializes the SoundManager
   *
   * @param sources Number of sources to create
   */

  public void initialize(int channels) {
    try {
     AL.create();
     
     // allocate sources
    scratchBuffer.limit(channels);
     AL10.alGenSources(scratchBuffer);
     scratchBuffer.rewind();
     scratchBuffer.get(sources = new int[channels]);
     
     // could we allocate all channels?
    if(AL10.alGetError() != AL10.AL_NO_ERROR) {
           throw new LWJGLException("Unable to allocate " + channels + " sources");
     }
     
     // we have sound
    soundOutput = true;
    } catch (LWJGLException le) {
          le.printStackTrace();
      System.out.println("Sound disabled");
    }
  }
 
  /**
   * Adds a sound to the Sound Managers pool
   *
   * @param path Path to file to load
   * @return index into SoundManagers buffer list
   */

  public int addSound(String path) {
    // Generate 1 buffer entry
   scratchBuffer.rewind().position(0).limit(1);
    AL10.alGenBuffers(scratchBuffer);
    buffers[bufferIndex] = scratchBuffer.get(0);
   
    // load wave data from buffer
   WaveData wavefile = WaveData.create("sounds/" + path);

    // copy to buffers
   AL10.alBufferData(buffers[bufferIndex], wavefile.format, wavefile.data, wavefile.samplerate);
   
    // unload file again
   wavefile.dispose();        
   
    // return index for this sound
       return bufferIndex++;
  }
 
  /**
   * Destroy this SoundManager
   */

  public void destroy() {
    if(soundOutput) {
     
      // stop playing sounds
     scratchBuffer.position(0).limit(sources.length);
      scratchBuffer.put(sources).flip();
      AL10.alSourceStop(scratchBuffer);

      // destroy sources
     AL10.alDeleteSources(scratchBuffer);
     
      // destroy buffers
     scratchBuffer.position(0).limit(bufferIndex);
      scratchBuffer.put(buffers, 0, bufferIndex).flip();
      AL10.alDeleteBuffers(scratchBuffer);
     
      // destory OpenAL
         AL.destroy();
    }
  }
}
Offline Matzon

JGO Knight


Medals: 19
Projects: 2


I'm gonna wring your pants!


« Reply #1 - Posted 2004-11-05 04:34:33 »

Hehe - thats because the SoundManager I wrote for Space Invaders has two methods to make sound output.
1 - playSound - This method ALWAYS uses the last channel to play on. That way I can guarentee that no one else is using that slot.
2 - playEffect - this method uses all channels BUT the last one. Any already playing sounds will be cut.

Apparently you have been testing playSound all the time Wink

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 70
Projects: 15


★★★★★


« Reply #2 - Posted 2004-11-05 12:14:46 »

Quote
Apparently you have been testing playSound all the time Wink


:oGAH, how could i miss that, thanks for the help.Wink

*bangs head repeatedly against wall*
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.

xsi3rr4x (22 views)
2014-04-15 18:08:23

BurntPizza (17 views)
2014-04-15 03:46:01

UprightPath (31 views)
2014-04-14 17:39:50

UprightPath (15 views)
2014-04-14 17:35:47

Porlus (31 views)
2014-04-14 15:48:38

tom_mai78101 (57 views)
2014-04-10 04:04:31

BurntPizza (114 views)
2014-04-08 23:06:04

tom_mai78101 (214 views)
2014-04-05 13:34:39

trollwarrior1 (182 views)
2014-04-04 12:06:45

CJLetsGame (189 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!