Versatile Java game engine with pluggable backends (this was used in Rogue, I think)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
1.8 KiB

10 years ago
package mightypork.gamecore.audio.players;
import mightypork.gamecore.audio.IAudio;
10 years ago
import mightypork.gamecore.audio.Volume;
import mightypork.utils.interfaces.Destroyable;
/**
* Base of an audio player
*
10 years ago
* @author Ondřej Hruška (MightyPork)
*/
public abstract class AudioPlayer implements Destroyable {
10 years ago
/** the track */
private final IAudio audio;
10 years ago
/** base gain for sfx */
private double baseGain;
10 years ago
/** base pitch for sfx */
private double basePitch;
10 years ago
/** dedicated volume control */
private final Volume gainMultiplier;
10 years ago
/**
* @param track audio resource
* @param volume colume control
*/
public AudioPlayer(IAudio track, Volume volume)
{
10 years ago
this.audio = track;
10 years ago
if (volume == null) volume = new Volume(1D);
10 years ago
this.gainMultiplier = volume;
}
10 years ago
@Override
public void destroy()
{
audio.destroy();
}
10 years ago
/**
* @return audio resource
*/
protected IAudio getAudio()
10 years ago
{
return audio;
}
10 years ago
/**
* Get play gain, computed based on volume and given multiplier
*
10 years ago
* @param multiplier extra volume adjustment
* @return computed gain
*/
protected double computeGain(double multiplier)
{
return baseGain * gainMultiplier.get() * multiplier;
}
10 years ago
/**
* Get pitch
*
10 years ago
* @param multiplier pitch adjustment
* @return computed pitch
*/
protected double computePitch(double multiplier)
{
return basePitch * multiplier;
}
10 years ago
/**
* Get if audio is valid
*
10 years ago
* @return is valid
*/
protected boolean hasAudio()
{
return (audio != null);
}
/**
* Set base gain. 1 is original volume, 0 is silence.
*
* @param gain base gain
*/
public void setGain(double gain)
{
this.baseGain = gain;
}
10 years ago
/**
* Set base pitch. 1 is original pitch, less is deeper, more is higher.
*
* @param pitch base pitch
10 years ago
*/
public void setPitch(double pitch)
10 years ago
{
this.basePitch = pitch;
10 years ago
}
10 years ago
}