mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 03:08:02 +00:00
Refactor sound support so it can work on both desktop and android
This commit is contained in:
@@ -63,6 +63,9 @@ import forge.screens.match.controllers.CStack;
|
||||
import forge.screens.match.views.VField;
|
||||
import forge.screens.match.views.VHand;
|
||||
import forge.screens.match.views.VPrompt;
|
||||
import forge.sound.AltSoundSystem;
|
||||
import forge.sound.AudioClip;
|
||||
import forge.sound.IAudioClip;
|
||||
import forge.toolbox.FButton;
|
||||
import forge.toolbox.FOptionPane;
|
||||
import forge.toolbox.FSkin;
|
||||
@@ -435,4 +438,14 @@ public class GuiDesktop implements IGuiBase {
|
||||
public LobbyPlayer getQuestPlayer() {
|
||||
return getGuiPlayer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAudioClip createAudioClip(String filename) {
|
||||
return AudioClip.fileExists(filename) ? new AudioClip(filename) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startAltSoundSystem(String filename, boolean isSynchronized) {
|
||||
new AltSoundSystem(filename, isSynchronized).start();
|
||||
}
|
||||
}
|
||||
|
||||
117
forge-gui-desktop/src/main/java/forge/sound/AltSoundSystem.java
Normal file
117
forge-gui-desktop/src/main/java/forge/sound/AltSoundSystem.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package forge.sound;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author agetian
|
||||
*/
|
||||
class AsyncSoundRegistry {
|
||||
static Map<String, Integer> soundsPlayed = new HashMap<String, Integer>();
|
||||
|
||||
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 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 synchronized static boolean isRegistered(String soundName) {
|
||||
return soundsPlayed.containsKey(soundName);
|
||||
}
|
||||
|
||||
public synchronized static int getNumIterations(String soundName) {
|
||||
return soundsPlayed.containsKey(soundName) ? soundsPlayed.get(soundName) : 0;
|
||||
}
|
||||
}
|
||||
|
||||
public class AltSoundSystem extends Thread {
|
||||
|
||||
private String filename;
|
||||
private boolean isSync;
|
||||
|
||||
private final int EXTERNAL_BUFFER_SIZE = 524288;
|
||||
private final int MAX_SOUND_ITERATIONS = 5;
|
||||
|
||||
public AltSoundSystem(String wavfile, boolean synced) {
|
||||
filename = wavfile;
|
||||
isSync = synced;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (isSync && AsyncSoundRegistry.getNumIterations(filename) >= 1) {
|
||||
return;
|
||||
}
|
||||
if (AsyncSoundRegistry.getNumIterations(filename) >= MAX_SOUND_ITERATIONS) {
|
||||
return;
|
||||
}
|
||||
|
||||
File soundFile = new File(filename);
|
||||
if (!soundFile.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
AudioInputStream audioInputStream = null;
|
||||
try {
|
||||
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
|
||||
} catch (UnsupportedAudioFileException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
AudioFormat format = audioInputStream.getFormat();
|
||||
SourceDataLine audioLine = null;
|
||||
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
|
||||
|
||||
try {
|
||||
audioLine = (SourceDataLine) AudioSystem.getLine(info);
|
||||
audioLine.open(format);
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
|
||||
audioLine.start();
|
||||
AsyncSoundRegistry.registerSound(filename);
|
||||
|
||||
int nBytesRead = 0;
|
||||
byte[] audioBufData = new byte[EXTERNAL_BUFFER_SIZE];
|
||||
|
||||
try {
|
||||
while (nBytesRead != -1) {
|
||||
nBytesRead = audioInputStream.read(audioBufData, 0, audioBufData.length);
|
||||
if (nBytesRead >= 0)
|
||||
audioLine.write(audioBufData, 0, nBytesRead);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
} finally {
|
||||
audioLine.drain();
|
||||
audioLine.close();
|
||||
try {
|
||||
audioInputStream.close();
|
||||
} catch (IOException e) {
|
||||
// Can't do much if closing it fails.
|
||||
}
|
||||
AsyncSoundRegistry.unregisterSound(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
112
forge-gui-desktop/src/main/java/forge/sound/AudioClip.java
Normal file
112
forge-gui-desktop/src/main/java/forge/sound/AudioClip.java
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Forge: Play Magic: the Gathering.
|
||||
* Copyright (C) 2012 Forge Team
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package forge.sound;
|
||||
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
|
||||
import forge.properties.ForgeConstants;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.MissingResourceException;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* SoundSystem - a simple sound playback system for Forge.
|
||||
* Do not use directly. Instead, use the {@link forge.sound.SoundEffectType} enumeration.
|
||||
*
|
||||
* @author Agetian
|
||||
*/
|
||||
public class AudioClip implements IAudioClip {
|
||||
private Clip clip;
|
||||
private final int SOUND_SYSTEM_DELAY = 30;
|
||||
|
||||
public static boolean fileExists(String fileName) {
|
||||
File fSound = new File(ForgeConstants.SOUND_DIR, fileName);
|
||||
return fSound.exists();
|
||||
}
|
||||
|
||||
public AudioClip(final String filename) {
|
||||
File fSound = new File(ForgeConstants.SOUND_DIR, filename);
|
||||
if (!fSound.exists()) {
|
||||
throw new IllegalArgumentException("Sound file " + fSound.toString() + " does not exist, cannot make a clip of it");
|
||||
}
|
||||
|
||||
try {
|
||||
AudioInputStream stream = AudioSystem.getAudioInputStream(fSound);
|
||||
AudioFormat format = stream.getFormat();
|
||||
DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(), ((int) stream.getFrameLength() * format.getFrameSize()));
|
||||
clip = (Clip) AudioSystem.getLine(info);
|
||||
clip.open(stream);
|
||||
return;
|
||||
|
||||
} catch (IOException ex) {
|
||||
System.err.println("Unable to load sound file: " + filename);
|
||||
} catch (LineUnavailableException ex) {
|
||||
System.err.println("Error initializing sound system: " + ex);
|
||||
} catch (UnsupportedAudioFileException ex) {
|
||||
System.err.println("Unsupported file type of the sound file: " + fSound.toString() + " - " + ex.getMessage());
|
||||
clip = null;
|
||||
return;
|
||||
}
|
||||
throw new MissingResourceException("Sound clip failed to load", this.getClass().getName(), filename);
|
||||
}
|
||||
|
||||
public final void play() {
|
||||
if (null == clip) {
|
||||
return;
|
||||
}
|
||||
clip.setMicrosecondPosition(0);
|
||||
try {
|
||||
Thread.sleep(SOUND_SYSTEM_DELAY);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
clip.start();
|
||||
}
|
||||
|
||||
public final void loop() {
|
||||
if (null == clip) {
|
||||
return;
|
||||
}
|
||||
clip.setMicrosecondPosition(0);
|
||||
try {
|
||||
Thread.sleep(SOUND_SYSTEM_DELAY);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
clip.loop(Clip.LOOP_CONTINUOUSLY);
|
||||
}
|
||||
|
||||
public final void stop() {
|
||||
if (null == clip) {
|
||||
return;
|
||||
}
|
||||
clip.stop();
|
||||
}
|
||||
|
||||
public final boolean isDone() {
|
||||
if (null == clip) {
|
||||
return false;
|
||||
}
|
||||
return !clip.isRunning();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user