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;
public class SoundManager {
private int[] buffers = new int[256]; private int[] sources; private IntBuffer scratchBuffer = BufferUtils.createIntBuffer(256); private boolean soundOutput; private int bufferIndex; private int sourceIndex; public SoundManager() { }
public void playEffect(int buffer) { if(soundOutput) { int channel = sources[(sourceIndex++ % (sources.length-1))]; AL10.alSourcei(channel, AL10.AL_BUFFER, buffers[buffer]); AL10.alSourcePlay(channel); } }
public void playSound(int buffer) { if(soundOutput) { AL10.alSourcei(sources[sources.length-1], AL10.AL_BUFFER, buffers[buffer]); AL10.alSourcePlay(sources[sources.length-1]); } } public boolean isPlayingSound() { return AL10.alGetSourcei(sources[sources.length-1], AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING; } public void initialize(int channels) { try { AL.create(); scratchBuffer.limit(channels); AL10.alGenSources(scratchBuffer); scratchBuffer.rewind(); scratchBuffer.get(sources = new int[channels]); if(AL10.alGetError() != AL10.AL_NO_ERROR) { throw new LWJGLException("Unable to allocate " + channels + " sources"); } soundOutput = true; } catch (LWJGLException le) { le.printStackTrace(); System.out.println("Sound disabled"); } } public int addSound(String path) { scratchBuffer.rewind().position(0).limit(1); AL10.alGenBuffers(scratchBuffer); buffers[bufferIndex] = scratchBuffer.get(0); WaveData wavefile = WaveData.create("sounds/" + path);
AL10.alBufferData(buffers[bufferIndex], wavefile.format, wavefile.data, wavefile.samplerate); wavefile.dispose(); return bufferIndex++; } public void destroy() { if(soundOutput) { scratchBuffer.position(0).limit(sources.length); scratchBuffer.put(sources).flip(); AL10.alSourceStop(scratchBuffer);
AL10.alDeleteSources(scratchBuffer); scratchBuffer.position(0).limit(bufferIndex); scratchBuffer.put(buffers, 0, bufferIndex).flip(); AL10.alDeleteBuffers(scratchBuffer); AL.destroy(); } } } |