summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/WavSoundPlayer.java
blob: c1bff8a844708a0cfc14a7ad657a8d628072076e (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package NET.worlds.scape;

import NET.worlds.core.IniFile;
import NET.worlds.network.URL;
import java.util.Vector;

public class WavSoundPlayer extends SoundPlayer implements Runnable {
   float ang;
   float dist;
   float vol;
   private String playingSoundFile = "";
   private URL url;
   private static Vector soundStack = new Vector();
   private static Thread activeThread;
   int repeatsLeft;
   private static int playingThreads;
   private static int systemPaused;
   static boolean ignoreVolumeChanges = IniFile.gamma().getIniInt("ignoreVolumeChanges", 1) != 0;
   private static WavSoundTerminator terminator = new WavSoundTerminator();
   private static float lastLeftVol;
   private static float lastRightVol;

   static {
      nativeInit();
   }

   public WavSoundPlayer(Sound owner) {
      super(owner);
   }

   public static native void nativeInit();

   @Override
   public boolean open(float volume, float stopDist, boolean atten, boolean pan) {
      return true;
   }

   @Override
   public void close() {
      this.stop();
   }

   @Override
   public boolean position(Point3Temp cam, Point3Temp obj, Point3Temp out, Point3Temp up) {
      Point3Temp toObj = Point3Temp.make(obj).minus(cam);
      Point3Temp right = Point3Temp.make(out).cross(up);
      float y = toObj.dot(out);
      float x = toObj.dot(right);
      this.ang = (float)(Math.atan2(y, x) / Math.PI);
      this.dist = toObj.length();
      return this.setVolume(this.vol);
   }

   @Override
   public boolean setVolume(float v) {
      this.vol = v;
      float leftFrac = 0.5F;
      if (this.owner != null && this.owner.getPanning()) {
         leftFrac = Math.abs(this.ang);
         if (this.ang < 0.0F) {
            v = this.vol * (float)(0.5 + Math.abs(0.5 + this.ang));
         }
      }

      if (this.owner != null && this.owner.getAttenuate()) {
         float stopDist = this.owner.getStopDistance();
         if (this.dist > stopDist) {
            this.volume(0.0F, 0.0F);
            return false;
         }

         v *= (stopDist - this.dist) / stopDist;
      }

      this.volume(v * leftFrac, v * (1.0F - leftFrac));
      return true;
   }

   @Override
   public int getState() {
      return soundStack.contains(this) ? 0 : 1;
   }

   public void start(URL u) {
      this.url = u;
      this.start(1);
   }

   private static WavSoundPlayer getActive() {
      return !soundStack.isEmpty() ? (WavSoundPlayer)soundStack.lastElement() : null;
   }

   public static boolean isActive() {
      return !soundStack.isEmpty();
   }

   private void play(int repeatCount, boolean reuseLoop) {
      this.playingSoundFile = this.url.unalias();
      soundStack.removeElement(this);
      soundStack.addElement(this);
      this.repeatsLeft = repeatCount;
      if (this.repeatsLeft < 0) {
         if (!reuseLoop && systemPaused == 0) {
            this.nativePlay(true);
         }

         activeThread = null;
      } else {
         activeThread = new Thread(this);
         activeThread.start();
      }
   }

   @Override
   public void start(int repeatCount) {
      synchronized (soundStack) {
         WavSoundPlayer active = getActive();

         assert this.repeatsLeft == 0 && active != this;

         if (this.owner != null) {
            this.url = this.owner.getURL();
         }

         boolean activeWasLoop = activeThread == null;
         boolean reuseLoop = active != null && activeWasLoop && repeatCount < 0 && active.playingSoundFile.equals(this.url.unalias());
         if (active != null) {
            active.stop(reuseLoop);
            if (activeWasLoop) {
               soundStack.addElement(active);
            }
         }

         this.play(repeatCount, reuseLoop);
      }
   }

   @Override
   public void run() {
      synchronized (soundStack) {
         if (systemPaused > 0) {
            this.repeatsLeft = 0;
            return;
         }

         playingThreads++;
      }

      while (this.repeatsLeft > 0) {
         synchronized (soundStack) {
            if (activeThread != Thread.currentThread()) {
               break;
            }

            if (this.repeatsLeft > 0) {
               this.repeatsLeft--;
            }
         }

         this.nativePlay(false);
      }

      synchronized (soundStack) {
         if (activeThread == Thread.currentThread()) {
            assert getActive() == this;

            soundStack.removeElement(this);
            activeThread = null;
            WavSoundPlayer active = getActive();
            if (active != null) {
               active.play(-1, false);
            }
         }

         playingThreads--;
         soundStack.notifyAll();
      }
   }

   private void stop(boolean leaveLoopRunning) {
      if (getActive() == this) {
         boolean isLoop = activeThread == null;

         assert this.repeatsLeft < 0 && isLoop || !leaveLoopRunning;

         this.repeatsLeft = 0;
         if (!leaveLoopRunning && isLoop && systemPaused == 0) {
            this.nativeStop();
         }

         activeThread = null;
      }

      soundStack.removeElement(this);
   }

   @Override
   public void stop() {
      synchronized (soundStack) {
         if (getActive() == this) {
            this.stop(false);
            WavSoundPlayer active = getActive();
            if (active != null) {
               active.play(-1, false);
            }
         } else {
            soundStack.removeElement(this);
         }
      }
   }

   public static void pauseSystem() {
      pauseSystemExceptASF();
   }

   public static void pauseSystemExceptASF() {
      synchronized (soundStack) {
         WavSoundPlayer active = getActive();
         boolean isLoop = activeThread == null;
         if (active != null) {
            if (isLoop) {
               active.nativeStop();
            } else {
               active.stop(false);
            }
         }

         systemPaused++;
         if (active != null) {
            active.play(-1, false);
         }

         while (playingThreads > 0) {
            try {
               soundStack.wait();
            } catch (InterruptedException var4) {
            }
         }
      }
   }

   public static void resumeSystem() {
      resumeSystemExceptASF();
   }

   public static void resumeSystemExceptASF() {
      synchronized (soundStack) {
         if (systemPaused > 0) {
            systemPaused = 0;
            WavSoundPlayer active = getActive();
            if (active != null && activeThread == null) {
               active.play(-1, false);
            }
         }
      }
   }

   public void volume(float left, float right) {
      if (!ignoreVolumeChanges) {
         synchronized (soundStack) {
            if (getActive() == this && (lastLeftVol != left || lastRightVol != right)) {
               lastLeftVol = left;
               lastRightVol = right;
               this.nativeVolume(left, right);
            }
         }
      }
   }

   private native void nativePlay(boolean var1);

   private native void nativeVolume(float var1, float var2);

   private native void nativeStop();
}