mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 20:28:00 +00:00
- 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.
This commit is contained in:
@@ -22,32 +22,34 @@ import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
class AsyncSoundRegistry {
|
||||
static Map<String, Integer> soundsPlayed = new HashMap<String, Integer>();
|
||||
|
||||
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) {
|
||||
@@ -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()) {
|
||||
|
||||
Reference in New Issue
Block a user