Fixed some audio issues, refactoring

master
Ondřej Hruška 11 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 * Create effect resource
* *
* @param resource resource path * @param resource resource path
* @param pitch default pitch (1 = unchanged)
* @param gain default gain (0-1)
* @return player * @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) * Register loop resource (music / effect loop)
* *
* @param resource resource path * @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 * @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); final LoopPlayer p = new LoopPlayer(createAudioResource(resource), loopsVolume);
p.setFadeTimes(fadeIn, fadeOut);
loopPlayers.add(p); loopPlayers.add(p);
return 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 * 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 * @param res a resource name
* @return the resource * @return the resource
* @throws IllegalArgumentException if resource is already registered * @throws IllegalArgumentException if resource is already registered
*/ */
protected DeferredAudio createAudio(String res) protected final DeferredAudio createAudioResource(String res)
{ {
final DeferredAudio a = doCreateResource(res); final DeferredAudio a = doCreateResource(res);
App.bus().send(new ResourceLoadRequest(a)); 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 * @param res resource path
* @return Deferred Audio * @return Deferred Audio

@ -27,22 +27,22 @@ public abstract class DeferredAudio extends BaseDeferredResource implements IAud
@Override @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 @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 @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(); void stop();
@ -54,46 +54,55 @@ public interface IAudio extends Destroyable {
/** /**
* Play as sound effect at listener position * Play as sound effect at listener position
* *
* @param pitch pitch (1 = default) * @param gain gain
* @param gain gain (0-1) * @param pitch pitch
* @param loop looping * @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
* @param gain gain (0-1) * @param pitch pitch
* @param loop looping * @param loop looping
* @param x * @param x
* @param y * @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
* @param gain gain (0-1) * @param pitch pitch
* @param loop looping * @param loop looping
* @param x * @param x
* @param y * @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 * Play as sound effect at given position
* *
* @param pitch pitch (1 = default) * @param gain gain
* @param gain gain (0-1) * @param pitch pitch
* @param loop looping * @param loop looping
* @param pos coord * @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.EffectPlayer;
import mightypork.gamecore.audio.players.LoopPlayer; import mightypork.gamecore.audio.players.LoopPlayer;
import mightypork.gamecore.core.App;
/** /**
@ -24,13 +23,11 @@ public class SoundRegistry {
* Register effect resource * Register effect resource
* *
* @param key sound key * @param key sound key
* @param resource resource path * @param effect the effect to add (Obtained from audio module)
* @param pitch default pitch (1 = unchanged)
* @param gain default gain (0-1)
*/ */
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) * Register loop resource (music / effect loop)
* *
* @param key sound key * @param key sound key
* @param resource resource path * @param loop the loop to add (Obtained from audio module)
* @param pitch default pitch (1 = unchanged)
* @param gain default gain (0-1)
* @param fadeIn default time for fadeIn
* @param fadeOut default time for fadeOut
*/ */
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; package mightypork.gamecore.audio.players;
import mightypork.gamecore.audio.DeferredAudio; import mightypork.gamecore.audio.IAudio;
import mightypork.gamecore.audio.Volume; import mightypork.gamecore.audio.Volume;
import mightypork.utils.interfaces.Destroyable; import mightypork.utils.interfaces.Destroyable;
/** /**
* Basic abstract player * Base of an audio player
* *
* @author Ondřej Hruška (MightyPork) * @author Ondřej Hruška (MightyPork)
*/ */
public abstract class BaseAudioPlayer implements Destroyable { public abstract class AudioPlayer implements Destroyable {
/** the track */ /** the track */
private final DeferredAudio audio; private final IAudio audio;
/** base gain for sfx */ /** base gain for sfx */
private final double baseGain; private double baseGain;
/** base pitch for sfx */ /** base pitch for sfx */
private final double basePitch; private double basePitch;
/** dedicated volume control */ /** dedicated volume control */
private final Volume gainMultiplier; private final Volume gainMultiplier;
@ -28,17 +28,12 @@ public abstract class BaseAudioPlayer implements Destroyable {
/** /**
* @param track audio resource * @param track audio resource
* @param basePitch base pitch (pitch multiplier)
* @param baseGain base gain (volume multiplier)
* @param volume colume control * @param volume colume control
*/ */
public BaseAudioPlayer(DeferredAudio track, double basePitch, double baseGain, Volume volume) public AudioPlayer(IAudio track, Volume volume)
{ {
this.audio = track; this.audio = track;
this.baseGain = baseGain;
this.basePitch = basePitch;
if (volume == null) volume = new Volume(1D); if (volume == null) volume = new Volume(1D);
this.gainMultiplier = volume; this.gainMultiplier = volume;
@ -55,7 +50,7 @@ public abstract class BaseAudioPlayer implements Destroyable {
/** /**
* @return audio resource * @return audio resource
*/ */
protected DeferredAudio getAudio() protected IAudio getAudio()
{ {
return audio; 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; package mightypork.gamecore.audio.players;
import mightypork.gamecore.audio.DeferredAudio; import mightypork.gamecore.audio.IAudio;
import mightypork.gamecore.audio.Volume; import mightypork.gamecore.audio.Volume;
import mightypork.utils.math.constraints.vect.Vect; import mightypork.utils.math.constraints.vect.Vect;
@ -11,31 +11,29 @@ import mightypork.utils.math.constraints.vect.Vect;
* *
* @author Ondřej Hruška (MightyPork) * @author Ondřej Hruška (MightyPork)
*/ */
public class EffectPlayer extends BaseAudioPlayer { public class EffectPlayer extends AudioPlayer {
/** /**
* @param track audio resource * @param track audio resource
* @param basePitch base pitch (pitch multiplier)
* @param baseGain base gain (volume multiplier)
* @param volume volume control * @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 * Play at listener
* *
* @param pitch play pitch
* @param gain play gain * @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; 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) 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 * Play at given position
* *
* @param pitch play pitch
* @param gain play gain * @param gain play gain
* @param pitch play pitch
* @param pos play position * @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; 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) * @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 */ /** animator for fade in and fade out */
private final NumAnimated fadeAnim = new NumAnimated(0); private final NumAnimated fadeAnim = new NumAnimated(0);
@ -32,13 +32,11 @@ public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable
/** /**
* @param track audio resource * @param track audio resource
* @param basePitch base pitch (pitch multiplier)
* @param baseGain base gain (volume multiplier)
* @param volume volume control * @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; paused = true;
} }
@ -59,8 +57,8 @@ public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable
private void initLoop() private void initLoop()
{ {
if (hasAudio()) { if (hasAudio() && !getAudio().isActive()) {
getAudio().play(computePitch(1), computeGain(1), true); getAudio().play(computeGain(1), computePitch(1), true);
getAudio().pauseLoop(); 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 @Override
public void resume() public void resume()
{ {

@ -272,7 +272,7 @@ public abstract class GraphicsModule extends BackendModule {
* @param path path to texture * @param path path to texture
* @return the deferred font * @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 * @param path path to font, or font name in the system
* @return the deferred font * @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(); 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.setFilter(filter);
texture.setWrap(wrap); texture.setWrap(wrap);

Loading…
Cancel
Save