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
This commit is contained in:
Dave Hoover
2007-10-31 23:34:03 +00:00
parent 5ab6e41f2f
commit bbbb5a2498
@@ -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, * On a spooling thread,
*/ */
@@ -499,30 +522,21 @@ public class SoundManager
} }
AudioInputStream stream = AudioSystem.getAudioInputStream( AudioInputStream stream = setupAudioStream(data);
new ByteArrayInputStream(data));
if (key.cmd == LOOP) { if (key.cmd == LOOP && stream.markSupported()) {
stream.mark(data.length); stream.mark(data.length);
} }
// open the sound line // open the sound line
AudioFormat format = stream.getFormat(); 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( line = (SourceDataLine) AudioSystem.getLine(
new DataLine.Info(SourceDataLine.class, format)); new DataLine.Info(SourceDataLine.class, format));
line.open(format, LINEBUF_SIZE); line.open(format, LINEBUF_SIZE);
float setVolume = 1; float setVolume = 1;
float setPan = PAN_CENTER; float setPan = PAN_CENTER;
line.start(); line.start();
_soundSeemsToWork = true; _soundSeemsToWork = true;
long startTime = System.currentTimeMillis(); 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) { 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); } while (key.cmd == LOOP && key.running);