From bbbb5a2498f202ccf4614b217d3326157f52c738 Mon Sep 17 00:00:00 2001 From: Dave Hoover Date: Wed, 31 Oct 2007 23:34:03 +0000 Subject: [PATCH] Only jump through extra hoops converting our sounds to pcm if we need to (e.g. decoding oggs, but not playing wavs) And then make looping work even if we're playing something that doesn't support resetting to marks in the stream (e.g. decoding oggs) git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@325 ed5b42cb-e716-0410-a449-f6a68f950b19 --- .../threerings/media/sound/SoundManager.java | 49 +++++++++++++------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/src/java/com/threerings/media/sound/SoundManager.java b/src/java/com/threerings/media/sound/SoundManager.java index 865f0181..90b755cb 100644 --- a/src/java/com/threerings/media/sound/SoundManager.java +++ b/src/java/com/threerings/media/sound/SoundManager.java @@ -475,6 +475,29 @@ public class SoundManager } } + /** + * Sets up an audio stream from the given byte array, and gets it to convert itself to PCM + * data for writing to our output line (if it isn't already that) + */ + protected AudioInputStream setupAudioStream (byte[] data) + throws UnsupportedAudioFileException, IOException + { + AudioInputStream stream = AudioSystem.getAudioInputStream(new ByteArrayInputStream(data)); + AudioFormat format = stream.getFormat(); + if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { + stream = AudioSystem.getAudioInputStream( + new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, + format.getSampleRate(), + 16, + format.getChannels(), + format.getChannels() * 2, + format.getSampleRate(), + false), stream); + } + + return stream; + } + /** * On a spooling thread, */ @@ -499,30 +522,21 @@ public class SoundManager } - AudioInputStream stream = AudioSystem.getAudioInputStream( - new ByteArrayInputStream(data)); - if (key.cmd == LOOP) { + AudioInputStream stream = setupAudioStream(data); + + if (key.cmd == LOOP && stream.markSupported()) { stream.mark(data.length); } // open the sound line AudioFormat format = stream.getFormat(); - stream = AudioSystem.getAudioInputStream( - new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, - format.getSampleRate(), - 16, - format.getChannels(), - format.getChannels() * 2, - format.getSampleRate(), - false), stream); - format = stream.getFormat(); - line = (SourceDataLine) AudioSystem.getLine( new DataLine.Info(SourceDataLine.class, format)); line.open(format, LINEBUF_SIZE); float setVolume = 1; float setPan = PAN_CENTER; line.start(); + _soundSeemsToWork = true; long startTime = System.currentTimeMillis(); @@ -558,9 +572,14 @@ public class SoundManager } } - // if we're going to loop, reset the stream to the beginning if (key.cmd == LOOP) { - stream.reset(); + // if we're going to loop, reset the stream to the beginning if we can, + // otherwise just remake the stream + if (stream.markSupported()) { + stream.reset(); + } else { + stream = setupAudioStream(data); + } } } while (key.cmd == LOOP && key.running);