Music volume adjusts the mod player.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1979 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2002-11-20 23:06:33 +00:00
parent 0c42e140b4
commit 4c19a90642
2 changed files with 95 additions and 64 deletions
@@ -1,5 +1,5 @@
//
// $Id: ModPlayer.java,v 1.1 2002/11/20 04:03:09 ray Exp $
// $Id: ModPlayer.java,v 1.2 2002/11/20 23:06:33 ray Exp $
package com.threerings.media;
@@ -26,6 +26,13 @@ public class ModPlayer
{
_rmgr = rmgr;
_smgr = smgr;
try {
_device = new NaryaSoundDevice();
} catch (OutputDeviceException ode) {
Log.warning("Unable to allocate sound channel for mod playing " +
"[e=" + ode + "].");
}
}
public void stop ()
@@ -35,16 +42,6 @@ public class ModPlayer
public void start (String set, String path)
{
PCM16StreamOutputDevice devvy = null;
try {
devvy = new JavaSoundOutputDevice(
new SS16LEAudioFormatConverter(), 44100, 1000);
} catch (OutputDeviceException ode) {
Log.warning("Oh we're fucked with the mod [e=" + ode + "].");
return;
}
Module module = null;
try {
module = ModuleLoader.read(
@@ -54,15 +51,13 @@ public class ModPlayer
return;
}
final PCM16StreamOutputDevice outdev = devvy;
final MicroMod mod = new MicroMod(
module, outdev, new LinearResampler());
module, _device, new LinearResampler());
_player = new Thread("narya mod player") {
public void run () {
outdev.start();
_device.start();
while ((_player == Thread.currentThread()) &&
(mod.getSequenceLoopCount() == 0)) {
@@ -73,7 +68,7 @@ public class ModPlayer
// WFCares
}
}
outdev.stop();
_device.stop();
_smgr.songStopEvent();
}
};
@@ -82,7 +77,38 @@ public class ModPlayer
_player.start();
}
/**
* Set the volume of the midiplayer.
* @param vol 0f - 1f (inclusive).
*/
public void setVolume (float vol)
{
_device.setVolume(vol);
}
/**
* A class that allows us to access the dataline so we can adjust
* the volume.
*/
protected static class NaryaSoundDevice extends JavaSoundOutputDevice
{
public NaryaSoundDevice ()
throws OutputDeviceException
{
super(new SS16LEAudioFormatConverter(), 44100, 1000);
}
/**
* Adjust the volume of the line that we're sending our mod data to.
*/
public void setVolume (float vol)
{
SoundManager.adjustVolume(sourceDataLine, vol);
}
}
protected Thread _player;
protected NaryaSoundDevice _device;
protected ResourceManager _rmgr;
protected SoundManager _smgr;
@@ -1,5 +1,5 @@
//
// $Id: SoundManager.java,v 1.22 2002/11/20 04:03:09 ray Exp $
// $Id: SoundManager.java,v 1.23 2002/11/20 23:06:33 ray Exp $
package com.threerings.media;
@@ -621,6 +621,7 @@ public class SoundManager
{
_mp3player = new MP3Manager(_rmgr, this);
_modplayer = new ModPlayer(_rmgr, this);
_modplayer.setVolume(_musicVol);
try {
Sequencer seq = MidiSystem.getSequencer();
@@ -690,6 +691,10 @@ public class SoundManager
*/
protected void updateMusicVolume ()
{
if (_modplayer != null) {
_modplayer.setVolume(_musicVol);
}
if (_midiChannels == null) {
Log.warning("Cannot modify music volume!");
@@ -800,6 +805,52 @@ public class SoundManager
_rmgr.getResource(key.set, key.path), BUFFER_SIZE));
}
// /**
// * Adjust the volume of this clip.
// */
// protected static void adjustVolumeIdeally (Line line, float volume)
// {
// if (line.isControlSupported(FloatControl.Type.VOLUME)) {
// FloatControl vol = (FloatControl)
// line.getControl(FloatControl.Type.VOLUME);
//
// float min = vol.getMinimum();
// float max = vol.getMaximum();
//
// float ourval = (volume * (max - min)) + min;
// Log.debug("adjust vol: [min=" + min + ", ourval=" + ourval +
// ", max=" + max + "].");
// vol.setValue(ourval);
//
// } else {
// // fall back
// adjustVolume(line, volume);
// }
// }
/**
* Use the gain control to implement volume.
*/
protected static void adjustVolume (Line line, float vol)
{
FloatControl control = (FloatControl)
line.getControl(FloatControl.Type.MASTER_GAIN);
// the only problem is that gain is specified in decibals,
// which is a logarithmic scale.
// Since we want max volume to leave the sample unchanged, our
// maximum volume translates into a 0db gain.
float gain;
if (vol == 0f) {
gain = control.getMinimum();
} else {
gain = (float) ((Math.log(vol) / Math.log(10.0)) * 20.0);
}
control.setValue(gain);
//Log.info("Set gain: " + gain);
}
/**
* A record to help us manage the use of sound resources.
* We don't free the resources associated with a clip immediately, because
@@ -855,7 +906,7 @@ public class SoundManager
*/
public void start (float volume)
{
adjustVolume(volume);
adjustVolume(_clip, volume);
_clip.start();
didStart();
}
@@ -948,52 +999,6 @@ public class SoundManager
return _length == AudioSystem.NOT_SPECIFIED;
}
// /**
// * Adjust the volume of this clip.
// */
// protected void adjustVolume (float volume)
// {
// if (_clip.isControlSupported(FloatControl.Type.VOLUME)) {
// FloatControl vol = (FloatControl)
// _clip.getControl(FloatControl.Type.VOLUME);
//
// float min = vol.getMinimum();
// float max = vol.getMaximum();
//
// float ourval = (volume * (max - min)) + min;
// Log.debug("adjust vol: [min=" + min + ", ourval=" + ourval +
// ", max=" + max + "].");
// vol.setValue(ourval);
//
// } else {
// // fall back
// adjustVolumeFallback(vol);
// }
// }
/**
* Use the gain control to implement volume.
*/
protected void adjustVolume (float vol)
{
FloatControl control = (FloatControl)
_clip.getControl(FloatControl.Type.MASTER_GAIN);
// the only problem is that gain is specified in decibals,
// which is a logarithmic scale.
// Since we want max volume to leave the sample unchanged, our
// maximum volume translates into a 0db gain.
float gain;
if (vol == 0f) {
gain = control.getMinimum();
} else {
gain = (float) ((Math.log(vol) / Math.log(10.0)) * 20.0);
}
control.setValue(gain);
//Log.info("Set gain: " + gain);
}
/** The timestamp of the moment this clip last stopped playing. */
protected long _stamp;