summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/MCISoundPlayer.java
blob: d87d2459baf266f5bf8b8ab48f2a1467b69e7e5d (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
package NET.worlds.scape;

import NET.worlds.network.URL;

public class MCISoundPlayer extends SoundPlayer {
   private static MCIThread mciThread = new MCIThread();
   float ang;
   float dist;
   float vol;
   int leftToRepeat;
   int running;
   MCISoundCommand poll = new MCISoundCommand() {
      @Override
      public void run() {
         MCISoundPlayer.this.gotFinished(MCISoundPlayer.this.nativeIsFinished());
      }
   };
   private URL url;
   private static MCISoundCommand activeStopCmd;
   MCISoundCommand volumeCmd = new MCISoundCommand() {
      @Override
      public void run() {
         if (!WavSoundPlayer.ignoreVolumeChanges) {
            MCISoundPlayer.this.nativeVolume(this.left, this.right);
         }
      }
   };

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

   @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() {
      if (!this.poll.isOnQueue) {
         mciThread.pushCommand(this.poll);
      }

      return this.running != 0 ? 0 : 1;
   }

   @Override
   public synchronized void start(int repeatCount) {
      if (repeatCount == 0) {
         this.running = 0;
      } else {
         this.leftToRepeat = repeatCount;
         if (this.leftToRepeat > 0) {
            this.leftToRepeat--;
         }

         this.running = 1;
         activeStopCmd = null;
         mciThread.pushCommand(new MCISoundCommand() {
            @Override
            public void run() {
               MCISoundPlayer.this.doStart();
            }
         });
      }
   }

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

   synchronized void doStart() {
      this.running = 2;
      if (!this.nativeStart((this.owner == null ? this.url : this.owner.getURL()).unalias())) {
         this.running = 3;
      }
   }

   synchronized void gotFinished(boolean f) {
      if (f && this.running == 2) {
         this.start(this.leftToRepeat);
      }
   }

   @Override
   public synchronized void stop() {
      this.leftToRepeat = 0;
      activeStopCmd = new MCISoundCommand() {
         @Override
         public void run() {
            if (MCISoundPlayer.activeStopCmd == this) {
               MCISoundPlayer.activeStopCmd = null;
               MCISoundPlayer.this.nativeStop();
            }
         }
      };
      mciThread.pushCommand(activeStopCmd);
   }

   public void volume(float left, float right) {
      this.volumeCmd.left = left;
      this.volumeCmd.right = right;
      if (!this.volumeCmd.isOnQueue) {
         mciThread.pushCommand(this.volumeCmd);
      }
   }

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

   private native boolean nativeStart(String var1);

   private native boolean nativeIsFinished();

   private native void nativeStop();

   public static native boolean isActive();

   static native void shutdown();
}