Store the largest Clip building blocks we can in the cache (rather than just

the byte[] data) so that subsequent plays can happen faster.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1948 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2002-11-15 02:06:41 +00:00
parent 6b68cb5530
commit 51194278ca
@@ -1,5 +1,5 @@
// //
// $Id: SoundManager.java,v 1.11 2002/11/15 01:49:47 ray Exp $ // $Id: SoundManager.java,v 1.12 2002/11/15 02:06:41 ray Exp $
package com.threerings.media; package com.threerings.media;
@@ -263,16 +263,14 @@ public class SoundManager
} }
// get the sound data from our LRU cache // get the sound data from our LRU cache
byte[] data = getAudioData(path); Object[] stuff = getAudioData(path);
if (data == null) { if (stuff == null) {
return; // borked! return; // borked!
} }
try { try {
AudioInputStream stream = AudioSystem.getAudioInputStream( DataLine.Info info = (DataLine.Info) stuff[0];
new ByteArrayInputStream(data)); AudioInputStream stream = (AudioInputStream) stuff[1];
DataLine.Info info = new DataLine.Info(
Clip.class, stream.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info); Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(stream); clip.open(stream);
@@ -281,14 +279,13 @@ public class SoundManager
rec.start(type); rec.start(type);
_active.add(rec); _active.add(rec);
// and rewind the stream to the beginning for next time
stream.reset();
} catch (IOException ioe) { } catch (IOException ioe) {
Log.warning("Error loading sound file [path=" + path + Log.warning("Error loading sound file [path=" + path +
", e=" + ioe + "]."); ", e=" + ioe + "].");
} catch (UnsupportedAudioFileException uafe) {
Log.warning("Unsupported sound format [path=" + path + ", e=" +
uafe + "].");
} catch (LineUnavailableException lue) { } catch (LineUnavailableException lue) {
Log.warning("Line not available to play sound [path=" + path + Log.warning("Line not available to play sound [path=" + path +
", e=" + lue + "]."); ", e=" + lue + "].");
@@ -337,24 +334,33 @@ public class SoundManager
/** /**
* Get the audio data for the specified path. * Get the audio data for the specified path.
*/ */
protected byte[] getAudioData (String path) protected Object[] getAudioData (String path)
{ {
byte[] data = (byte[]) _dataCache.get(path); Object[] stuff = (Object[]) _dataCache.get(path);
if (data != null) { if (stuff != null) {
return data; return stuff;
} }
try { try {
data = StreamUtils.streamAsBytes( byte[] data = StreamUtils.streamAsBytes(
_rmgr.getResource(path), BUFFER_SIZE); _rmgr.getResource(path), BUFFER_SIZE);
_dataCache.put(path, data); AudioInputStream stream = AudioSystem.getAudioInputStream(
new ByteArrayInputStream(data));
DataLine.Info info = new DataLine.Info(
Clip.class, stream.getFormat());
stuff = new Object[] { info, stream };
_dataCache.put(path, stuff);
} catch (UnsupportedAudioFileException uafe) {
Log.warning("Unsupported sound format [path=" + path + ", e=" +
uafe + "].");
} catch (IOException ioe) { } catch (IOException ioe) {
Log.warning("Error loading sound file [path=" + path + ", e=" + Log.warning("Error loading sound file [path=" + path + ", e=" +
ioe + "]."); ioe + "].");
} }
return data; return stuff;
} }
/** /**