Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/openal/AudioImpl.java
1461 views
1
package org.newdawn.slick.openal;
2
3
import org.lwjgl.openal.AL10;
4
import org.lwjgl.openal.AL11;
5
6
/**
7
* A sound that can be played through OpenAL
8
*
9
* @author Kevin Glass
10
* @author Nathan Sweet <[email protected]>
11
*/
12
public class AudioImpl implements Audio {
13
/** The store from which this sound was loaded */
14
private SoundStore store;
15
/** The buffer containing the sound */
16
private int buffer;
17
/** The index of the source being used to play this sound */
18
private int index = -1;
19
20
/** The length of the audio */
21
private float length;
22
23
/**
24
* Create a new sound
25
*
26
* @param store The sound store from which the sound was created
27
* @param buffer The buffer containing the sound data
28
*/
29
AudioImpl(SoundStore store, int buffer) {
30
this.store = store;
31
this.buffer = buffer;
32
33
int bytes = AL10.alGetBufferi(buffer, AL10.AL_SIZE);
34
int bits = AL10.alGetBufferi(buffer, AL10.AL_BITS);
35
int channels = AL10.alGetBufferi(buffer, AL10.AL_CHANNELS);
36
int freq = AL10.alGetBufferi(buffer, AL10.AL_FREQUENCY);
37
38
int samples = bytes / (bits / 8);
39
length = (samples / (float) freq) / channels;
40
}
41
42
/**
43
* Get the ID of the OpenAL buffer holding this data (if any). This method
44
* is not valid with streaming resources.
45
*
46
* @return The ID of the OpenAL buffer holding this data
47
*/
48
public int getBufferID() {
49
return buffer;
50
}
51
52
/**
53
*
54
*/
55
protected AudioImpl() {
56
57
}
58
59
/**
60
* @see org.newdawn.slick.openal.Audio#stop()
61
*/
62
public void stop() {
63
if (index != -1) {
64
store.stopSource(index);
65
}
66
}
67
68
/**
69
* @see org.newdawn.slick.openal.Audio#isPlaying()
70
*/
71
public boolean isPlaying() {
72
if (index != -1) {
73
return store.isPlaying(index);
74
}
75
76
return false;
77
}
78
79
/**
80
* @see org.newdawn.slick.openal.Audio#playAsSoundEffect(float, float, boolean)
81
*/
82
public int playAsSoundEffect(float pitch, float gain, boolean loop) {
83
index = store.playAsSound(buffer, pitch, gain, loop);
84
return store.getSource(index);
85
}
86
87
88
/**
89
* @see org.newdawn.slick.openal.Audio#playAsSoundEffect(float, float, boolean, float, float, float)
90
*/
91
public int playAsSoundEffect(float pitch, float gain, boolean loop, float x, float y, float z) {
92
index = store.playAsSoundAt(buffer, pitch, gain, loop, x, y, z);
93
return store.getSource(index);
94
}
95
96
/**
97
* @see org.newdawn.slick.openal.Audio#playAsMusic(float, float, boolean)
98
*/
99
public int playAsMusic(float pitch, float gain, boolean loop) {
100
store.playAsMusic(buffer, pitch, gain, loop);
101
index = 0;
102
return store.getSource(0);
103
}
104
105
/**
106
* Pause the music currently being played
107
*/
108
public static void pauseMusic() {
109
SoundStore.get().pauseLoop();
110
}
111
112
/**
113
* Restart the music currently being paused
114
*/
115
public static void restartMusic() {
116
SoundStore.get().restartLoop();
117
}
118
119
/**
120
* @see org.newdawn.slick.openal.Audio#setPosition(float)
121
*/
122
public boolean setPosition(float position) {
123
position = position % length;
124
125
AL10.alSourcef(store.getSource(index), AL11.AL_SEC_OFFSET, position);
126
if (AL10.alGetError() != 0) {
127
return false;
128
}
129
return true;
130
}
131
132
/**
133
* @see org.newdawn.slick.openal.Audio#getPosition()
134
*/
135
public float getPosition() {
136
return AL10.alGetSourcef(store.getSource(index), AL11.AL_SEC_OFFSET);
137
}
138
}
139
140