cleanup, figured out what's needed to loop midis consistently. More soon.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1954 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2002-11-16 00:13:47 +00:00
parent d9838206c1
commit 125ef04e87
@@ -1,5 +1,5 @@
// //
// $Id: SoundManager.java,v 1.15 2002/11/15 21:38:31 ray Exp $ // $Id: SoundManager.java,v 1.16 2002/11/16 00:13:47 ray Exp $
package com.threerings.media; package com.threerings.media;
@@ -136,11 +136,11 @@ public class SoundManager
stopSequence(path); stopSequence(path);
} else if (LOCK == command) { } else if (LOCK == command) {
_dataCache.lock(path); _clipCache.lock(path);
getAudioData(path); // preload getClipData(path); // preload
} else if (UNLOCK == command) { } else if (UNLOCK == command) {
_dataCache.unlock(path); _clipCache.unlock(path);
} else if (FLUSH == command) { } else if (FLUSH == command) {
flushResources(false); flushResources(false);
@@ -337,25 +337,19 @@ public class SoundManager
} }
// get the sound data from our LRU cache // get the sound data from our LRU cache
Object[] stuff = getAudioData(path); ClipInfo info = getClipData(path);
if (stuff == null) { if (info == null) {
return; // borked! return; // borked!
} }
try { try {
DataLine.Info info = (DataLine.Info) stuff[0]; Clip clip = (Clip) AudioSystem.getLine(info.info);
AudioInputStream stream = (AudioInputStream) stuff[1]; clip.open(info.stream);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
SoundRecord rec = new SoundRecord(path, clip); SoundRecord rec = new SoundRecord(path, clip);
rec.start(_clipVol); rec.start(_clipVol);
_activeClips.add(rec); _activeClips.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 + "].");
@@ -414,8 +408,8 @@ public class SoundManager
try { try {
_sequencer.setSequence(getResource(path)); _sequencer.setSequence(getResource(path));
_sequencer.start(); _sequencer.start();
//updateMusicVolume();
_midiStack.addFirst(path); _midiStack.addFirst(path);
updateMusicVolume();
} catch (InvalidMidiDataException imda) { } catch (InvalidMidiDataException imda) {
Log.warning("Invalid midi data, not playing [path=" + path + "]."); Log.warning("Invalid midi data, not playing [path=" + path + "].");
@@ -481,7 +475,7 @@ public class SoundManager
} else { } else {
int setting = (int) (_musicVol * 127.0); int setting = (int) (_musicVol * 127.0);
for (int ii=0; ii < _midiChannels.length; ii++) { for (int ii=0; ii < _midiChannels.length; ii++) {
_midiChannels[ii].controlChange(7, setting); _midiChannels[ii].controlChange(MIDI_VOLUME_CONTROL, setting);
} }
} }
} }
@@ -489,15 +483,13 @@ public class SoundManager
// documentation inherited from interface MetaEventListener // documentation inherited from interface MetaEventListener
public void meta (MetaMessage msg) public void meta (MetaMessage msg)
{ {
if (msg.getType() == MIDI_END_OF_TRACK) {
// loop that puppy
// _sequencer.stop();
// _sequencer.setTickPosition(0);
_sequencer.start();
}
//
// Log.info("meta message: " + msg.getType() + ", msg=" + // Log.info("meta message: " + msg.getType() + ", msg=" +
// new String(msg.getData())); // new String(msg.getData()));
if (msg.getType() == MIDI_END_OF_TRACK) {
// loop that puppy
playSequence((String) _midiStack.removeFirst());
}
} }
/** /**
@@ -523,20 +515,27 @@ public class SoundManager
/** /**
* Get the audio data for the specified path. * Get the audio data for the specified path.
*/ */
protected Object[] getAudioData (String path) protected ClipInfo getClipData (String path)
{ {
Object[] stuff = (Object[]) _dataCache.get(path); ClipInfo info = (ClipInfo) _clipCache.get(path);
if (stuff != null) { if (info != null) {
return stuff; // we are re-using an old stream, make sure to rewind it.
try {
info.stream.reset();
} catch (IOException ioe) {
Log.warning("Couldn't reset audio stream! " +
"(this shouldn't happen)");
}
return info;
} }
try { try {
AudioInputStream stream = AudioSystem.getAudioInputStream( AudioInputStream stream = AudioSystem.getAudioInputStream(
getResource(path)); getResource(path));
DataLine.Info info = new DataLine.Info( DataLine.Info dinfo = new DataLine.Info(
Clip.class, stream.getFormat()); Clip.class, stream.getFormat());
stuff = new Object[] { info, stream }; info = new ClipInfo(dinfo, stream);
_dataCache.put(path, stuff); _clipCache.put(path, info);
} catch (UnsupportedAudioFileException uafe) { } catch (UnsupportedAudioFileException uafe) {
Log.warning("Unsupported sound format [path=" + path + ", e=" + Log.warning("Unsupported sound format [path=" + path + ", e=" +
@@ -547,7 +546,7 @@ public class SoundManager
ioe + "]."); ioe + "].");
} }
return stuff; return info;
} }
/** /**
@@ -766,6 +765,24 @@ public class SoundManager
protected Clip _clip; protected Clip _clip;
} }
/**
* A wee helper class that holds clip information in our cache.
*/
protected static class ClipInfo
{
/** The information needed to construct a clip from the stream. */
public DataLine.Info info;
/** The data to be used to make the clip. */
public AudioInputStream stream;
public ClipInfo (DataLine.Info info, AudioInputStream stream)
{
this.info = info;
this.stream = stream;
}
}
/** /**
* Every 3 seconds we look for sounds that haven't been used for 4 and * Every 3 seconds we look for sounds that haven't been used for 4 and
* free them up. * free them up.
@@ -792,7 +809,7 @@ public class SoundManager
protected float _clipVol = 1f, _musicVol = 1f; protected float _clipVol = 1f, _musicVol = 1f;
/** The cache of recent audio clips . */ /** The cache of recent audio clips . */
protected LockableLRUHashMap _dataCache = new LockableLRUHashMap(10); protected LockableLRUHashMap _clipCache = new LockableLRUHashMap(10);
/** The clips that are currently active. */ /** The clips that are currently active. */
protected ArrayList _activeClips = new ArrayList(); protected ArrayList _activeClips = new ArrayList();
@@ -822,6 +839,9 @@ public class SoundManager
/** This is apparently the midi code for end of track. Wack. */ /** This is apparently the midi code for end of track. Wack. */
protected static final int MIDI_END_OF_TRACK = 47; protected static final int MIDI_END_OF_TRACK = 47;
/** The midi control for volume is 7. Ooooooo. */
protected static final int MIDI_VOLUME_CONTROL = 7;
/** The queue size at which we start to ignore requests to play sounds. */ /** The queue size at which we start to ignore requests to play sounds. */
protected static final int MAX_QUEUE_SIZE = 100; protected static final int MAX_QUEUE_SIZE = 100;