From 4f30cd58ec1b074ec27098c46a0d57fecbcb1265 Mon Sep 17 00:00:00 2001 From: Agetian Date: Wed, 10 Apr 2013 05:16:46 +0000 Subject: [PATCH] - Renamed AsyncSoundPlayer to AltSoundSystem (better represents its purpose). - Fixed the synchronous sounds playing asynchronously on the alternative sound system. - Some minor fixes and changes to the alternative sound system. --- .gitattributes | 2 +- ...yncSoundPlayer.java => AltSoundSystem.java} | 18 ++++++++++-------- src/main/java/forge/sound/SoundSystem.java | 8 ++++---- 3 files changed, 15 insertions(+), 13 deletions(-) rename src/main/java/forge/sound/{AsyncSoundPlayer.java => AltSoundSystem.java} (81%) diff --git a/.gitattributes b/.gitattributes index 53567bd8159..92d677725a8 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14324,7 +14324,7 @@ src/main/java/forge/quest/io/QuestDataIO.java svneol=native#text/plain src/main/java/forge/quest/io/ReadPriceList.java svneol=native#text/plain src/main/java/forge/quest/io/package-info.java svneol=native#text/plain src/main/java/forge/quest/package-info.java svneol=native#text/plain -src/main/java/forge/sound/AsyncSoundPlayer.java -text +src/main/java/forge/sound/AltSoundSystem.java -text src/main/java/forge/sound/AudioClip.java -text src/main/java/forge/sound/EventVisualizer.java -text src/main/java/forge/sound/IAudioClip.java -text diff --git a/src/main/java/forge/sound/AsyncSoundPlayer.java b/src/main/java/forge/sound/AltSoundSystem.java similarity index 81% rename from src/main/java/forge/sound/AsyncSoundPlayer.java rename to src/main/java/forge/sound/AltSoundSystem.java index 642c6cb9831..79677bc5fc4 100755 --- a/src/main/java/forge/sound/AsyncSoundPlayer.java +++ b/src/main/java/forge/sound/AltSoundSystem.java @@ -22,32 +22,34 @@ import javax.sound.sampled.UnsupportedAudioFileException; class AsyncSoundRegistry { static Map soundsPlayed = new HashMap(); - public static void registerSound(String soundName) { + public synchronized static void registerSound(String soundName) { if (soundsPlayed.containsKey(soundName)) { soundsPlayed.put(soundName, soundsPlayed.get(soundName) + 1); } else { soundsPlayed.put(soundName, 1); } + //System.out.println("Register: Count for " + soundName + " = " + soundsPlayed.get(soundName)); } - public static void unregisterSound(String soundName) { - if (soundsPlayed.containsKey(soundName) && soundsPlayed.get(soundName) != 1) { + public synchronized static void unregisterSound(String soundName) { + if (soundsPlayed.containsKey(soundName) && soundsPlayed.get(soundName) > 1) { soundsPlayed.put(soundName, soundsPlayed.get(soundName) - 1); } else { soundsPlayed.remove(soundName); } + //System.out.println("Unregister: Count for " + soundName + " = " + soundsPlayed.get(soundName)); } - public static boolean isRegistered(String soundName) { + public synchronized static boolean isRegistered(String soundName) { return soundsPlayed.containsKey(soundName); } - public static int getNumIterations(String soundName) { + public synchronized static int getNumIterations(String soundName) { return soundsPlayed.containsKey(soundName) ? soundsPlayed.get(soundName) : 0; } } -public class AsyncSoundPlayer extends Thread { +public class AltSoundSystem extends Thread { private String filename; private boolean isSync; @@ -55,13 +57,13 @@ public class AsyncSoundPlayer extends Thread { private final int EXTERNAL_BUFFER_SIZE = 524288; private final int MAX_SOUND_ITERATIONS = 5; - public AsyncSoundPlayer(String wavfile, boolean synced) { + public AltSoundSystem(String wavfile, boolean synced) { filename = wavfile; isSync = synced; } public void run() { - if (isSync && AsyncSoundRegistry.isRegistered(filename)) { + if (isSync && AsyncSoundRegistry.getNumIterations(filename) >= 1) { return; } if (AsyncSoundRegistry.getNumIterations(filename) >= MAX_SOUND_ITERATIONS) { diff --git a/src/main/java/forge/sound/SoundSystem.java b/src/main/java/forge/sound/SoundSystem.java index 62877d6d5b6..706d243193b 100644 --- a/src/main/java/forge/sound/SoundSystem.java +++ b/src/main/java/forge/sound/SoundSystem.java @@ -72,7 +72,7 @@ public class SoundSystem { */ public void play(SoundEffectType type) { if (isUsingAltSystem()) { - new AsyncSoundPlayer(String.format("%s/%s", IAudioClip.PathToSound, type.getResourceFileName()), false).start(); + new AltSoundSystem(String.format("%s/%s", IAudioClip.PathToSound, type.getResourceFileName()), false).start(); } else { fetchResource(type).play(); } @@ -83,7 +83,7 @@ public class SoundSystem { */ public void play(String resourceFileName) { if (isUsingAltSystem()) { - new AsyncSoundPlayer(String.format("%s/%s", IAudioClip.PathToSound, resourceFileName), false).start(); + new AltSoundSystem(String.format("%s/%s", IAudioClip.PathToSound, resourceFileName), false).start(); } else { fetchResource(resourceFileName).play(); } @@ -96,7 +96,7 @@ public class SoundSystem { */ public void playSync(String resourceFileName) { if (isUsingAltSystem()) { - new AsyncSoundPlayer(String.format("%s/%s", IAudioClip.PathToSound, resourceFileName), false).start(); + new AltSoundSystem(String.format("%s/%s", IAudioClip.PathToSound, resourceFileName), true).start(); } else { IAudioClip snd = fetchResource(resourceFileName); if (snd.isDone()) { @@ -112,7 +112,7 @@ public class SoundSystem { */ public void playSync(SoundEffectType type) { if (isUsingAltSystem()) { - new AsyncSoundPlayer(String.format("%s/%s", IAudioClip.PathToSound, type.getResourceFileName()), false).start(); + new AltSoundSystem(String.format("%s/%s", IAudioClip.PathToSound, type.getResourceFileName()), true).start(); } else { IAudioClip snd = fetchResource(type); if (snd.isDone()) {