Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/openal/StreamSound.java
1461 views
1
package org.newdawn.slick.openal;
2
3
import java.io.IOException;
4
import java.nio.IntBuffer;
5
6
import org.lwjgl.BufferUtils;
7
import org.lwjgl.openal.AL10;
8
import org.newdawn.slick.util.Log;
9
10
/**
11
* A sound implementation wrapped round a player which reads (and potentially) rereads
12
* a stream. This supplies streaming audio
13
*
14
* @author kevin
15
* @author Nathan Sweet <[email protected]>
16
* @author Rockstar playAsMusic cleanup
17
*/
18
public class StreamSound extends AudioImpl {
19
/** The player we're going to ask to stream data */
20
private OpenALStreamPlayer player;
21
22
/**
23
* Create a new sound wrapped round a stream
24
*
25
* @param player The stream player we'll use to access the stream
26
*/
27
public StreamSound(OpenALStreamPlayer player) {
28
this.player = player;
29
}
30
31
/**
32
* @see org.newdawn.slick.openal.AudioImpl#isPlaying()
33
*/
34
public boolean isPlaying() {
35
return SoundStore.get().isPlaying(player);
36
}
37
38
/**
39
* @see org.newdawn.slick.openal.AudioImpl#playAsMusic(float, float, boolean)
40
*/
41
public int playAsMusic(float pitch, float gain, boolean loop) {
42
try {
43
cleanUpSource();
44
45
player.setup(pitch);
46
player.play(loop);
47
SoundStore.get().setStream(player);
48
} catch (IOException e) {
49
Log.error("Failed to read OGG source: "+player.getSource());
50
}
51
52
return SoundStore.get().getSource(0);
53
}
54
55
/**
56
* Clean up the buffers applied to the sound source
57
*/
58
private void cleanUpSource() {
59
SoundStore store = SoundStore.get();
60
61
AL10.alSourceStop(store.getSource(0));
62
IntBuffer buffer = BufferUtils.createIntBuffer(1);
63
int queued = AL10.alGetSourcei(store.getSource(0), AL10.AL_BUFFERS_QUEUED);
64
65
while (queued > 0)
66
{
67
AL10.alSourceUnqueueBuffers(store.getSource(0), buffer);
68
queued--;
69
}
70
71
AL10.alSourcei(store.getSource(0), AL10.AL_BUFFER, 0);
72
}
73
74
/**
75
* @see org.newdawn.slick.openal.AudioImpl#playAsSoundEffect(float, float, boolean, float, float, float)
76
*/
77
public int playAsSoundEffect(float pitch, float gain, boolean loop, float x, float y, float z) {
78
return playAsMusic(pitch, gain, loop);
79
}
80
81
/**
82
* @see org.newdawn.slick.openal.AudioImpl#playAsSoundEffect(float, float, boolean)
83
*/
84
public int playAsSoundEffect(float pitch, float gain, boolean loop) {
85
return playAsMusic(pitch, gain, loop);
86
}
87
88
/**
89
* @see org.newdawn.slick.openal.AudioImpl#stop()
90
*/
91
public void stop() {
92
SoundStore.get().setStream(null);
93
}
94
95
/**
96
* @see org.newdawn.slick.openal.AudioImpl#setPosition(float)
97
*/
98
public boolean setPosition(float position) {
99
return player.setPosition(position);
100
}
101
102
/**
103
* @see org.newdawn.slick.openal.AudioImpl#getPosition()
104
*/
105
public float getPosition() {
106
return player.getPosition();
107
}
108
}
109
110