From 1be717f362353e558a260329545cebcb8efddcc1 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 12 Aug 2010 20:54:12 +0000 Subject: [PATCH] Notify sounds (via their groups) when the base gain changes. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@980 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/java/com/threerings/openal/Sound.java | 16 +++++++--- .../com/threerings/openal/SoundGroup.java | 20 +++++++++++++ .../com/threerings/openal/SoundManager.java | 29 +++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/java/com/threerings/openal/Sound.java b/src/java/com/threerings/openal/Sound.java index 4a811ae2..a6126eaa 100644 --- a/src/java/com/threerings/openal/Sound.java +++ b/src/java/com/threerings/openal/Sound.java @@ -89,10 +89,8 @@ public class Sound */ public void setGain (float gain) { - if (_source != null) { - _source.setGain(gain * _group.getBaseGain()); - } _gain = gain; + updateSourceGain(); } /** @@ -366,7 +364,7 @@ public class Sound // configure the source with our ephemera _source.setPosition(_px, _py, _pz); _source.setVelocity(_vx, _vy, _vz); - _source.setGain(_gain * _group.getBaseGain()); + updateSourceGain(); _source.setSourceRelative(_sourceRelative); _source.setMinGain(_minGain); _source.setMaxGain(_maxGain); @@ -389,6 +387,16 @@ public class Sound return true; } + /** + * Updates the source gain according to our configured gain and the base gain. + */ + protected void updateSourceGain () + { + if (_source != null) { + _source.setGain(_gain * _group.getBaseGain()); + } + } + /** * Called by the {@link SoundGroup} when it wants to reclaim our source. * diff --git a/src/java/com/threerings/openal/SoundGroup.java b/src/java/com/threerings/openal/SoundGroup.java index 944d1010..9dd65e36 100644 --- a/src/java/com/threerings/openal/SoundGroup.java +++ b/src/java/com/threerings/openal/SoundGroup.java @@ -69,6 +69,9 @@ public class SoundGroup pooled.source.delete(); } _sources.clear(); + + // remove from the manager + _manager.removeGroup(this); } /** @@ -91,6 +94,9 @@ public class SoundGroup _manager = manager; _provider = provider; + // register with the manager + _manager.addGroup(this); + // if we were unable to initialize the sound system at all, just // stop here and we'll behave as if we have no available sources if (!_manager.isInitialized()) { @@ -139,6 +145,20 @@ public class SoundGroup return _manager.getBaseGain(); } + /** + * Called by the manager when the base gain has changed. + */ + protected void baseGainChanged (float gain) + { + // notify any sound currently holding a source + for (int ii = 0, nn = _sources.size(); ii < nn; ii++) { + Sound holder = _sources.get(ii).holder; + if (holder != null) { + holder.updateSourceGain(); + } + } + } + /** Used to track which sources are in use. */ protected static class PooledSource { diff --git a/src/java/com/threerings/openal/SoundManager.java b/src/java/com/threerings/openal/SoundManager.java index 58c0c164..40e99ca5 100644 --- a/src/java/com/threerings/openal/SoundManager.java +++ b/src/java/com/threerings/openal/SoundManager.java @@ -23,6 +23,7 @@ package com.threerings.openal; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.nio.IntBuffer; @@ -114,7 +115,15 @@ public class SoundManager */ public void setBaseGain (float gain) { + if (_baseGain == gain) { + return; + } _baseGain = gain; + + // alert the groups + for (int ii = 0, nn = _groups.size(); ii < nn; ii++) { + _groups.get(ii).baseGainChanged(gain); + } } /** @@ -316,6 +325,23 @@ public class SoundManager _streams.remove(stream); } + /** + * Adds a group to the list maintained by the manager. Called by groups when they are created. + */ + protected void addGroup (SoundGroup group) + { + _groups.add(group); + } + + /** + * Removes a group from the list maintained by the manager. Called by groups when they are + * disposed. + */ + protected void removeGroup (SoundGroup group) + { + _groups.remove(group); + } + /** * Called when a source has been finalized. */ @@ -406,6 +432,9 @@ public class SoundManager /** The list of active streams. */ protected ArrayList _streams = Lists.newArrayList(); + /** The list of active groups. */ + protected List _groups = Lists.newArrayList(); + /** The list of sources to be deleted. */ protected int[] _finalizedSources;