Fixed some audio issues, refactoring

master
Ondřej Hruška 10 years ago
parent abd5bec682
commit 6d901ddfe5
  1. 25
      src/mightypork/gamecore/audio/AudioModule.java
  2. 12
      src/mightypork/gamecore/audio/DeferredAudio.java
  3. 41
      src/mightypork/gamecore/audio/IAudio.java
  4. 19
      src/mightypork/gamecore/audio/SoundRegistry.java
  5. 41
      src/mightypork/gamecore/audio/players/AudioPlayer.java
  6. 36
      src/mightypork/gamecore/audio/players/EffectPlayer.java
  7. 21
      src/mightypork/gamecore/audio/players/LoopPlayer.java
  8. 4
      src/mightypork/gamecore/graphics/GraphicsModule.java
  9. 2
      src/mightypork/gamecore/graphics/textures/TextureRegistry.java

@ -76,13 +76,11 @@ public abstract class AudioModule extends BackendModule implements Updateable {
* Create effect resource
*
* @param resource resource path
* @param pitch default pitch (1 = unchanged)
* @param gain default gain (0-1)
* @return player
*/
public EffectPlayer createEffect(String resource, double pitch, double gain)
public EffectPlayer createEffect(String resource)
{
return new EffectPlayer(createAudio(resource), pitch, gain, effectsVolume);
return new EffectPlayer(createAudioResource(resource), effectsVolume);
}
@ -90,16 +88,11 @@ public abstract class AudioModule extends BackendModule implements Updateable {
* Register loop resource (music / effect loop)
*
* @param resource resource path
* @param pitch default pitch (1 = unchanged)
* @param gain default gain (0-1)
* @param fadeIn default time for fadeIn
* @param fadeOut default time for fadeOut
* @return player
*/
public LoopPlayer createLoop(String resource, double pitch, double gain, double fadeIn, double fadeOut)
public LoopPlayer createLoop(String resource)
{
final LoopPlayer p = new LoopPlayer(createAudio(resource), pitch, gain, loopsVolume);
p.setFadeTimes(fadeIn, fadeOut);
final LoopPlayer p = new LoopPlayer(createAudioResource(resource), loopsVolume);
loopPlayers.add(p);
return p;
}
@ -107,13 +100,15 @@ public abstract class AudioModule extends BackendModule implements Updateable {
/**
* Create {@link DeferredAudio} for a resource, request deferred load and
* add to the resources list.
* add to the resources list.<br>
* The method is accessed only from within this class, since the sound
* registry holds players, not the resources themselves.
*
* @param res a resource name
* @return the resource
* @throws IllegalArgumentException if resource is already registered
*/
protected DeferredAudio createAudio(String res)
protected final DeferredAudio createAudioResource(String res)
{
final DeferredAudio a = doCreateResource(res);
App.bus().send(new ResourceLoadRequest(a));
@ -123,7 +118,9 @@ public abstract class AudioModule extends BackendModule implements Updateable {
/**
* Create a backend-specific deferred audio resource
* Create a backend-specific deferred audio resource.<br>
* The actual resource instance should be created here. Registering, loading
* etc. is handled higher.
*
* @param res resource path
* @return Deferred Audio

@ -27,22 +27,22 @@ public abstract class DeferredAudio extends BaseDeferredResource implements IAud
@Override
public void play(double pitch, double gain, boolean loop)
public void play(double gain, double pitch, boolean loop)
{
play(pitch, gain, loop, App.audio().getListenerPos());
play(gain, pitch, loop, App.audio().getListenerPos());
}
@Override
public void play(double pitch, double gain, boolean loop, double x, double y)
public void play(double gain, double pitch, boolean loop, double x, double y)
{
play(pitch, gain, loop, x, y, App.audio().getListenerPos().z());
play(gain, pitch, loop, x, y, App.audio().getListenerPos().z());
}
@Override
public void play(double pitch, double gain, boolean loop, Vect pos)
public void play(double gain, double pitch, boolean loop, Vect pos)
{
play(pitch, gain, loop, pos.x(), pos.y(), pos.z());
play(gain, pitch, loop, pos.x(), pos.y(), pos.z());
}
}

@ -34,7 +34,7 @@ public interface IAudio extends Destroyable {
/**
* Stop audio playback
* Stop audio playback, free source.
*/
void stop();
@ -54,46 +54,55 @@ public interface IAudio extends Destroyable {
/**
* Play as sound effect at listener position
*
* @param pitch pitch (1 = default)
* @param gain gain (0-1)
* @param gain gain
* @param pitch pitch
* @param loop looping
*/
void play(double pitch, double gain, boolean loop);
void play(double gain, double pitch, boolean loop);
/**
* Play as sound effect at given X-Y position
* Play as sound effect at given position
*
* @param pitch pitch (1 = default)
* @param gain gain (0-1)
* @param gain gain
* @param pitch pitch
* @param loop looping
* @param x
* @param y
* @param z
*/
void play(double pitch, double gain, boolean loop, double x, double y);
void play(double gain, double pitch, boolean loop, double x, double y, double z);
/**
* Play as sound effect at given position
* Play as sound effect at given X-Y position
*
* @param pitch pitch (1 = default)
* @param gain gain (0-1)
* @param gain gain
* @param pitch pitch
* @param loop looping
* @param x
* @param y
* @param z
*/
void play(double pitch, double gain, boolean loop, double x, double y, double z);
void play(double gain, double pitch, boolean loop, double x, double y);
/**
* Play as sound effect at given position
*
* @param pitch pitch (1 = default)
* @param gain gain (0-1)
* @param gain gain
* @param pitch pitch
* @param loop looping
* @param pos coord
*/
void play(double pitch, double gain, boolean loop, Vect pos);
void play(double gain, double pitch, boolean loop, Vect pos);
/**
* Check if this audio is currently active (ie. playing or paused, not
* stopped)
*
* @return is active
*/
boolean isActive();
}

@ -6,7 +6,6 @@ import java.util.Map;
import mightypork.gamecore.audio.players.EffectPlayer;
import mightypork.gamecore.audio.players.LoopPlayer;
import mightypork.gamecore.core.App;
/**
@ -24,13 +23,11 @@ public class SoundRegistry {
* Register effect resource
*
* @param key sound key
* @param resource resource path
* @param pitch default pitch (1 = unchanged)
* @param gain default gain (0-1)
* @param effect the effect to add (Obtained from audio module)
*/
public void addEffect(String key, String resource, double pitch, double gain)
public void addEffect(String key, EffectPlayer effect)
{
effects.put(key, App.audio().createEffect(resource, pitch, gain));
effects.put(key, effect);
}
@ -38,15 +35,11 @@ public class SoundRegistry {
* Register loop resource (music / effect loop)
*
* @param key sound key
* @param resource resource path
* @param pitch default pitch (1 = unchanged)
* @param gain default gain (0-1)
* @param fadeIn default time for fadeIn
* @param fadeOut default time for fadeOut
* @param loop the loop to add (Obtained from audio module)
*/
public void addLoop(String key, String resource, double pitch, double gain, double fadeIn, double fadeOut)
public void addLoop(String key, LoopPlayer loop)
{
loops.put(key, App.audio().createLoop(resource, pitch, gain, fadeIn, fadeOut));
loops.put(key, loop);
}

@ -1,26 +1,26 @@
package mightypork.gamecore.audio.players;
import mightypork.gamecore.audio.DeferredAudio;
import mightypork.gamecore.audio.IAudio;
import mightypork.gamecore.audio.Volume;
import mightypork.utils.interfaces.Destroyable;
/**
* Basic abstract player
* Base of an audio player
*
* @author Ondřej Hruška (MightyPork)
*/
public abstract class BaseAudioPlayer implements Destroyable {
public abstract class AudioPlayer implements Destroyable {
/** the track */
private final DeferredAudio audio;
private final IAudio audio;
/** base gain for sfx */
private final double baseGain;
private double baseGain;
/** base pitch for sfx */
private final double basePitch;
private double basePitch;
/** dedicated volume control */
private final Volume gainMultiplier;
@ -28,17 +28,12 @@ public abstract class BaseAudioPlayer implements Destroyable {
/**
* @param track audio resource
* @param basePitch base pitch (pitch multiplier)
* @param baseGain base gain (volume multiplier)
* @param volume colume control
*/
public BaseAudioPlayer(DeferredAudio track, double basePitch, double baseGain, Volume volume)
public AudioPlayer(IAudio track, Volume volume)
{
this.audio = track;
this.baseGain = baseGain;
this.basePitch = basePitch;
if (volume == null) volume = new Volume(1D);
this.gainMultiplier = volume;
@ -55,7 +50,7 @@ public abstract class BaseAudioPlayer implements Destroyable {
/**
* @return audio resource
*/
protected DeferredAudio getAudio()
protected IAudio getAudio()
{
return audio;
}
@ -97,10 +92,24 @@ public abstract class BaseAudioPlayer implements Destroyable {
/**
* force load the resource
* Set base gain. 1 is original volume, 0 is silence.
*
* @param baseGain base gain
*/
public void setGain(double baseGain)
{
this.baseGain = baseGain;
}
/**
* Set base pitch. 1 is original pitch, less is deeper, more is higher.
*
* @param basePitch base pitch
*/
public void load()
public void setPitch(double basePitch)
{
if (hasAudio()) audio.load();
this.basePitch = basePitch;
}
}

@ -1,7 +1,7 @@
package mightypork.gamecore.audio.players;
import mightypork.gamecore.audio.DeferredAudio;
import mightypork.gamecore.audio.IAudio;
import mightypork.gamecore.audio.Volume;
import mightypork.utils.math.constraints.vect.Vect;
@ -11,31 +11,29 @@ import mightypork.utils.math.constraints.vect.Vect;
*
* @author Ondřej Hruška (MightyPork)
*/
public class EffectPlayer extends BaseAudioPlayer {
public class EffectPlayer extends AudioPlayer {
/**
* @param track audio resource
* @param basePitch base pitch (pitch multiplier)
* @param baseGain base gain (volume multiplier)
* @param volume volume control
*/
public EffectPlayer(DeferredAudio track, double basePitch, double baseGain, Volume volume)
public EffectPlayer(IAudio track, Volume volume)
{
super(track, (float) basePitch, (float) baseGain, volume);
super(track, volume);
}
/**
* Play at listener
*
* @param pitch play pitch
* @param gain play gain
* @param pitch play pitch
*/
public void play(double pitch, double gain)
public void play(double gain, double pitch)
{
if (!hasAudio()) return;
getAudio().play(computePitch(pitch), computeGain(gain), false);
getAudio().play(computeGain(gain), computePitch(pitch), false);
}
@ -46,22 +44,34 @@ public class EffectPlayer extends BaseAudioPlayer {
*/
public void play(double gain)
{
play(1, gain);
play(gain, 1);
}
/**
* Play at given position
*
* @param gain play gain
* @param pos play position
*/
public void play(double gain, Vect pos)
{
play(gain, 1, pos);
}
/**
* Play at given position
*
* @param pitch play pitch
* @param gain play gain
* @param pitch play pitch
* @param pos play position
*/
public void play(double pitch, double gain, Vect pos)
public void play(double gain, double pitch, Vect pos)
{
if (!hasAudio()) return;
getAudio().play(computePitch(pitch), computeGain(gain), false, pos);
getAudio().play(computeGain(gain), computePitch(pitch), false, pos);
}
}

@ -13,7 +13,7 @@ import mightypork.utils.math.animation.NumAnimated;
*
* @author Ondřej Hruška (MightyPork)
*/
public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable {
public class LoopPlayer extends AudioPlayer implements Updateable, Pauseable {
/** animator for fade in and fade out */
private final NumAnimated fadeAnim = new NumAnimated(0);
@ -32,13 +32,11 @@ public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable
/**
* @param track audio resource
* @param basePitch base pitch (pitch multiplier)
* @param baseGain base gain (volume multiplier)
* @param volume volume control
*/
public LoopPlayer(DeferredAudio track, double basePitch, double baseGain, Volume volume)
public LoopPlayer(DeferredAudio track, Volume volume)
{
super(track, (float) basePitch, (float) baseGain, volume);
super(track, volume);
paused = true;
}
@ -59,8 +57,8 @@ public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable
private void initLoop()
{
if (hasAudio()) {
getAudio().play(computePitch(1), computeGain(1), true);
if (hasAudio() && !getAudio().isActive()) {
getAudio().play(computeGain(1), computePitch(1), true);
getAudio().pauseLoop();
}
}
@ -85,6 +83,15 @@ public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable
}
/**
* Alias to resume (more meaningful name)
*/
public void play()
{
resume();
}
@Override
public void resume()
{

@ -272,7 +272,7 @@ public abstract class GraphicsModule extends BackendModule {
* @param path path to texture
* @return the deferred font
*/
public abstract DeferredTexture createDeferredTexture(String path);
public abstract DeferredTexture createTextureResource(String path);
/**
@ -281,7 +281,7 @@ public abstract class GraphicsModule extends BackendModule {
* @param path path to font, or font name in the system
* @return the deferred font
*/
public abstract DeferredFont createDeferredFont(String path);
public abstract DeferredFont createFontResource(String path);
/**

@ -51,7 +51,7 @@ public class TextureRegistry {
{
if (key != null) if (textures.containsKey(key)) throw new KeyAlreadyExistsException();
final DeferredTexture texture = App.gfx().createDeferredTexture(resourcePath);
final DeferredTexture texture = App.gfx().createTextureResource(resourcePath);
texture.setFilter(filter);
texture.setWrap(wrap);

Loading…
Cancel
Save