|
From: <mp...@us...> - 2012-06-10 12:18:09
|
Revision: 9888
http://freecol.svn.sourceforge.net/freecol/?rev=9888&view=rev
Author: mpope
Date: 2012-06-10 12:18:02 +0000 (Sun, 10 Jun 2012)
Log Message:
-----------
Reduce SoundPlayer allocations.
Modified Paths:
--------------
freecol/trunk/build.xml
freecol/trunk/src/net/sf/freecol/client/gui/sound/SoundPlayer.java
freecol/trunk/src/net/sf/freecol/server/model/ServerGame.java
freecol/trunk/test/src/net/sf/freecol/client/gui/sound/SoundTest.java
freecol/trunk/test/src/net/sf/freecol/server/model/ServerBuildingTest.java
Modified: freecol/trunk/build.xml
===================================================================
--- freecol/trunk/build.xml 2012-06-10 05:16:52 UTC (rev 9887)
+++ freecol/trunk/build.xml 2012-06-10 12:18:02 UTC (rev 9888)
@@ -131,8 +131,8 @@
deprecation="off"
source="${java.target.version}"
target="${java.target.version}"
- includeantruntime="false"
- />
+ includeantruntime="false">
+ </javac>
</target>
<target name="build" depends="init,compile" description="Compiles the source files.">
Modified: freecol/trunk/src/net/sf/freecol/client/gui/sound/SoundPlayer.java
===================================================================
--- freecol/trunk/src/net/sf/freecol/client/gui/sound/SoundPlayer.java 2012-06-10 05:16:52 UTC (rev 9887)
+++ freecol/trunk/src/net/sf/freecol/client/gui/sound/SoundPlayer.java 2012-06-10 12:18:02 UTC (rev 9888)
@@ -145,7 +145,9 @@
private boolean playDone = true;
+ private byte[] data = new byte[8192];
+
public SoundPlayerThread() {
super(FreeCol.CLIENT_THREAD + "SoundPlayer");
}
@@ -232,72 +234,81 @@
}
private boolean playSound(File file) {
- BufferedInputStream bis;
+ boolean ret = false;
+
+ FileInputStream fis = null;
try {
- bis = new BufferedInputStream(new FileInputStream(file));
- bis.mark(1000);
- bis.skip(1);
- bis.reset();
+ fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
logger.warning("Could not find audio file: " + file.getName());
return false;
} catch (IOException e) {
logger.warning("Could not prepare stream for: "
+ file.getName());
- return false;
}
- AudioInputStream in;
- try {
- in = AudioSystem.getAudioInputStream(bis);
- } catch (Exception e) {
- logger.warning("Could not get audio input stream for: "
- + file.getName());
- return false;
+ AudioInputStream in = null;
+ if (fis != null) {
+ try {
+ in = AudioSystem.getAudioInputStream(fis);
+ } catch (Exception e) {
+ logger.warning("Could not get audio input stream for: "
+ + file.getName());
+ }
}
- boolean ret = false;
- AudioFormat baseFormat = in.getFormat();
- AudioFormat decodedFormat
- = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
+ AudioInputStream din = null;
+ AudioFormat decodedFormat = null;
+ if (in != null) {
+ AudioFormat baseFormat = in.getFormat();
+ decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * (16 / 8),
baseFormat.getSampleRate(),
baseFormat.isBigEndian());
- AudioInputStream din
- = AudioSystem.getAudioInputStream(decodedFormat, in);
- if (din == null) {
- logger.warning("Can not get decoded audio input stream");
- } else {
- SourceDataLine line = openLine(decodedFormat);
- if (line != null) {
- try {
- startPlaying();
- rawplay(din, line);
- ret = true;
- } catch (IOException e) {
- logger.log(Level.WARNING, "Error playing: "
- + file.getName(), e);
- } finally {
- stopPlaying();
- line.drain();
- line.stop();
- line.close();
- }
+ din = AudioSystem.getAudioInputStream(decodedFormat, in);
+ if (din == null) {
+ logger.warning("Can not get decoded audio stream for: "
+ + file.getName());
}
}
+
+ SourceDataLine line = null;
+ if (decodedFormat != null) {
+ line = openLine(decodedFormat);
+ }
+
+ if (line != null) {
+ try {
+ startPlaying();
+ rawplay(din, line);
+ ret = true;
+ } catch (IOException e) {
+ logger.log(Level.WARNING, "Error playing: "
+ + file.getName(), e);
+ } finally {
+ stopPlaying();
+ }
+ }
+
try {
+ if (line != null) {
+ line.drain();
+ line.stop();
+ line.close();
+ }
if (din != null) din.close();
- in.close();
+ if (in != null) in.close();
+ if (fis != null) fis.close();
} catch (IOException e) {} // Ignore errors on close
+
return ret;
}
private void rawplay(AudioInputStream din, SourceDataLine lin)
throws IOException {
- byte[] data = new byte[8192];
for (;;) {
if (!keepPlaying()) {
break;
Modified: freecol/trunk/src/net/sf/freecol/server/model/ServerGame.java
===================================================================
--- freecol/trunk/src/net/sf/freecol/server/model/ServerGame.java 2012-06-10 05:16:52 UTC (rev 9887)
+++ freecol/trunk/src/net/sf/freecol/server/model/ServerGame.java 2012-06-10 12:18:02 UTC (rev 9888)
@@ -227,7 +227,7 @@
setTurn(getTurn().next());
cs.addTrivial(See.all(), "newTurn", ChangePriority.CHANGE_NORMAL,
"turn", Integer.toString(getTurn().getNumber()));
- logger.info("ServerGame.csNewTurn, turn is " + getTurn().toString());
+ logger.finest("ServerGame.csNewTurn, turn is " + getTurn().toString());
for (Player player : getPlayers()) {
if (!player.isUnknownEnemy() && !player.isDead()) {
Modified: freecol/trunk/test/src/net/sf/freecol/client/gui/sound/SoundTest.java
===================================================================
--- freecol/trunk/test/src/net/sf/freecol/client/gui/sound/SoundTest.java 2012-06-10 05:16:52 UTC (rev 9887)
+++ freecol/trunk/test/src/net/sf/freecol/client/gui/sound/SoundTest.java 2012-06-10 12:18:02 UTC (rev 9888)
@@ -36,21 +36,24 @@
private SoundPlayer soundPlayer = null;
- private SoundPlayer getSoundPlayer() {
- if (soundPlayer == null) {
- ClientOptions clientOptions = new ClientOptions();
- final AudioMixerOption amo = (AudioMixerOption) clientOptions.getOption(ClientOptions.AUDIO_MIXER);
- final PercentageOption po = (PercentageOption) clientOptions.getOption(ClientOptions.AUDIO_VOLUME);
- po.setValue(10); // 10% volume
- try {
- soundPlayer = new SoundPlayer(amo, po);
- } catch (Exception e) {
- fail("Could not construct sound player: " + e.getMessage());
- }
+ @Override
+ public void setUp() {
+ ClientOptions clientOptions = new ClientOptions();
+ final AudioMixerOption amo = (AudioMixerOption) clientOptions.getOption(ClientOptions.AUDIO_MIXER);
+ final PercentageOption po = (PercentageOption) clientOptions.getOption(ClientOptions.AUDIO_VOLUME);
+ po.setValue(10); // 10% volume
+ try {
+ soundPlayer = new SoundPlayer(amo, po);
+ } catch (Exception e) {
+ fail("Could not construct sound player: " + e.getMessage());
}
- return soundPlayer;
}
+ @Override
+ public void tearDown() {
+ soundPlayer = null;
+ }
+
private void playSound(String id) {
File file = ResourceManager.getAudio(id);
if (file == null) {
@@ -63,16 +66,15 @@
} else {
try {
assertNotNull(AudioSystem.getAudioInputStream(file));
+ soundPlayer.playOnce(file);
+ try { // Just play the beginning of the sound to check it works
+ Thread.sleep(100);
+ soundPlayer.stop();
+ Thread.sleep(50);
+ } catch (InterruptedException e) {}
} catch (Exception e) {
fail("Could not play " + id + ": " + e.getMessage());
}
- SoundPlayer soundPlayer = getSoundPlayer();
- soundPlayer.playOnce(file);
- try {
- // Just play the beginning of the sound to check it works
- Thread.sleep(300);
- } catch (InterruptedException e) {}
- soundPlayer.stop();
}
}
@@ -114,5 +116,4 @@
playSound("sound.event.sellCargo");
playSound("sound.event.shipSunk");
}
-
}
Modified: freecol/trunk/test/src/net/sf/freecol/server/model/ServerBuildingTest.java
===================================================================
--- freecol/trunk/test/src/net/sf/freecol/server/model/ServerBuildingTest.java 2012-06-10 05:16:52 UTC (rev 9887)
+++ freecol/trunk/test/src/net/sf/freecol/server/model/ServerBuildingTest.java 2012-06-10 12:18:02 UTC (rev 9888)
@@ -293,12 +293,8 @@
while (4 == getUnitList(colony, freeColonistType).size()) {
ServerTestHelper.newTurn();
- System.out.println("new turn");
}
- for (Unit unit: colony.getUnitList()) {
- System.out.println(unit);
- }
assertEquals(3, getUnitList(colony, freeColonistType).size());
assertEquals(1, getUnitList(colony, masterBlacksmithType).size());
assertEquals(2, getUnitList(colony, expertLumberJackType).size());
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|