v0.8.2a: adjusted sync checks in Sample to fix hitching

This commit is contained in:
Evan Debenham 2020-08-16 18:21:06 -04:00
parent da5c7bb527
commit 6bba3a6df8

View File

@ -116,7 +116,7 @@ public enum Sample {
float pitch; float pitch;
} }
private static HashSet<DelayedSoundEffect> delayedSFX = new HashSet<>(); private static final HashSet<DelayedSoundEffect> delayedSFX = new HashSet<>();
public void playDelayed( Object id, float delay ){ public void playDelayed( Object id, float delay ){
playDelayed( id, delay, 1 ); playDelayed( id, delay, 1 );
@ -130,7 +130,7 @@ public enum Sample {
playDelayed( id, delay, volume, volume, pitch ); playDelayed( id, delay, volume, volume, pitch );
} }
public synchronized void playDelayed( Object id, float delay, float leftVolume, float rightVolume, float pitch ) { public void playDelayed( Object id, float delay, float leftVolume, float rightVolume, float pitch ) {
if (delay <= 0) { if (delay <= 0) {
play(id, leftVolume, rightVolume, pitch); play(id, leftVolume, rightVolume, pitch);
return; return;
@ -141,16 +141,20 @@ public enum Sample {
sfx.leftVol = leftVolume; sfx.leftVol = leftVolume;
sfx.rightVol = rightVolume; sfx.rightVol = rightVolume;
sfx.pitch = pitch; sfx.pitch = pitch;
delayedSFX.add(sfx); synchronized (delayedSFX) {
delayedSFX.add(sfx);
}
} }
public synchronized void update(){ public void update(){
if (delayedSFX.isEmpty()) return; synchronized (delayedSFX) {
for (DelayedSoundEffect sfx : delayedSFX.toArray(new DelayedSoundEffect[0])){ if (delayedSFX.isEmpty()) return;
sfx.delay -= Game.elapsed; for (DelayedSoundEffect sfx : delayedSFX.toArray(new DelayedSoundEffect[0])) {
if (sfx.delay <= 0){ sfx.delay -= Game.elapsed;
delayedSFX.remove(sfx); if (sfx.delay <= 0) {
play(sfx.id, sfx.leftVol, sfx.rightVol, sfx.pitch); delayedSFX.remove(sfx);
play(sfx.id, sfx.leftVol, sfx.rightVol, sfx.pitch);
}
} }
} }
} }