44
44
import pl .asie .computronics .util .sound .Instruction .SetVolume ;
45
45
import pl .asie .computronics .util .sound .Instruction .SetWave ;
46
46
47
+ import java .util .ArrayDeque ;
47
48
import java .util .Collections ;
48
49
import java .util .HashMap ;
49
- import java .util .LinkedList ;
50
50
import java .util .Locale ;
51
51
import java .util .Queue ;
52
52
import java .util .Set ;
@@ -69,9 +69,8 @@ public DriverCardSound(EnvironmentHost host) {
69
69
70
70
if (host .world ().isRemote ) {
71
71
SyncHandler .envs .add (this );
72
- } else {
73
- buildBuffer = new LinkedList <Instruction >();
74
72
}
73
+ buildBuffer = new ArrayDeque <Instruction >();
75
74
}
76
75
77
76
private final IAudioReceiver internalSpeaker = new IAudioReceiver () {
@@ -112,7 +111,7 @@ public void receivePacket(AudioPacket packet, ForgeDirection direction) {
112
111
};
113
112
114
113
private AudioUtil .AudioProcess process ;
115
- private Queue <Instruction > buildBuffer ;
114
+ private final ArrayDeque <Instruction > buildBuffer ;
116
115
private Queue <Instruction > nextBuffer ;
117
116
118
117
private int buildDelay = 0 ;
@@ -163,7 +162,7 @@ public boolean canUpdate() {
163
162
@ Override
164
163
public void update () {
165
164
if (nextBuffer != null && System .currentTimeMillis () >= timeout - 100 ) {
166
- sendSound (nextDelay , nextBuffer );
165
+ sendSound (nextBuffer );
167
166
timeout = timeout + nextDelay ;
168
167
nextBuffer = null ;
169
168
} else if (codecId != null && System .currentTimeMillis () >= timeout + soundTimeoutMS ) {
@@ -188,11 +187,14 @@ public void load(NBTTagCompound nbt) {
188
187
}
189
188
} else {
190
189
if (nbt .hasKey ("bbuffer" )) {
191
- buildBuffer = Instruction .fromNBT (nbt .getTagList ("bbuffer" , Constants .NBT .TAG_COMPOUND ));
192
- buildDelay = 0 ;
193
- for (Instruction inst : buildBuffer ) {
194
- if (inst instanceof Delay ) {
195
- buildDelay += ((Delay ) inst ).delay ;
190
+ synchronized (buildBuffer ) {
191
+ buildBuffer .clear ();
192
+ buildBuffer .addAll (Instruction .fromNBT (nbt .getTagList ("bbuffer" , Constants .NBT .TAG_COMPOUND )));
193
+ buildDelay = 0 ;
194
+ for (Instruction inst : buildBuffer ) {
195
+ if (inst instanceof Delay ) {
196
+ buildDelay += ((Delay ) inst ).delay ;
197
+ }
196
198
}
197
199
}
198
200
}
@@ -220,7 +222,9 @@ public void save(NBTTagCompound nbt) {
220
222
process .save (processNBT );
221
223
if (buildBuffer != null ) {
222
224
NBTTagList buildNBT = new NBTTagList ();
223
- Instruction .toNBT (buildNBT , buildBuffer );
225
+ synchronized (buildBuffer ) {
226
+ Instruction .toNBT (buildNBT , buildBuffer );
227
+ }
224
228
nbt .setTag ("bbuffer" , buildNBT );
225
229
}
226
230
if (nextBuffer != null ) {
@@ -233,10 +237,12 @@ public void save(NBTTagCompound nbt) {
233
237
}
234
238
235
239
private Object [] tryAdd (Instruction inst ) {
236
- if (buildBuffer .size () >= maxInstructions ) {
237
- return new Object [] { false , "too many instructions" };
240
+ synchronized (buildBuffer ) {
241
+ if (buildBuffer .size () >= maxInstructions ) {
242
+ return new Object [] { false , "too many instructions" };
243
+ }
244
+ buildBuffer .add (inst );
238
245
}
239
- buildBuffer .add (inst );
240
246
return new Object [] { true };
241
247
}
242
248
@@ -289,7 +295,9 @@ public Object[] setTotalVolume(Context context, Arguments args) {
289
295
@ Callback (doc = "function(); Clears the instruction queue." , direct = true )
290
296
@ Optional .Method (modid = Mods .OpenComputers )
291
297
public Object [] clear (Context context , Arguments args ) {
292
- buildBuffer .clear ();
298
+ synchronized (buildBuffer ) {
299
+ buildBuffer .clear ();
300
+ }
293
301
buildDelay = 0 ;
294
302
return new Object [] {};
295
303
}
@@ -376,20 +384,22 @@ public Object[] setVolume(Context context, Arguments args) {
376
384
@ Callback (doc = "function(); Starts processing the queue; Returns true is processing began, false if there is still a queue being processed." , direct = true )
377
385
@ Optional .Method (modid = Mods .OpenComputers )
378
386
public Object [] process (Context context , Arguments args ) {
379
- if (buildBuffer .size () == 0 ) {
380
- return new Object [] { true };
381
- }
382
- if (nextBuffer == null ) {
383
- nextBuffer = buildBuffer ;
384
- nextDelay = buildDelay ;
385
- buildBuffer = new LinkedList <Instruction >();
386
- buildDelay = 0 ;
387
- if (System .currentTimeMillis () > timeout ) {
388
- timeout = System .currentTimeMillis ();
387
+ synchronized (buildBuffer ) {
388
+ if (buildBuffer .size () == 0 ) {
389
+ return new Object [] { true };
390
+ }
391
+ if (nextBuffer == null ) {
392
+ nextBuffer = new ArrayDeque <Instruction >(buildBuffer );
393
+ nextDelay = buildDelay ;
394
+ buildBuffer .clear ();
395
+ buildDelay = 0 ;
396
+ if (System .currentTimeMillis () > timeout ) {
397
+ timeout = System .currentTimeMillis ();
398
+ }
399
+ return new Object [] { true };
400
+ } else {
401
+ return new Object [] { false , System .currentTimeMillis (), timeout };
389
402
}
390
- return new Object [] { true };
391
- } else {
392
- return new Object [] { false , System .currentTimeMillis (), timeout };
393
403
}
394
404
}
395
405
@@ -403,9 +413,9 @@ private void sendMusicPacket(int delay, Queue<Instruction> instructions) {
403
413
pkt .sendPacket ();
404
414
}
405
415
406
- protected void sendSound (int delay , Queue <Instruction > buffer ) {
416
+ protected void sendSound (Queue <Instruction > buffer ) {
407
417
int counter = 0 ;
408
- Queue <Instruction > sendBuffer = new LinkedList <Instruction >();
418
+ Queue <Instruction > sendBuffer = new ArrayDeque <Instruction >();
409
419
while (!buffer .isEmpty () || process .delay > 0 ) {
410
420
if (process .delay > 0 ) {
411
421
if (counter + process .delay < packetSizeMS ) {
0 commit comments