From 8cdbfb676ea31c6719e6f6e8ae7a44e060a5987f Mon Sep 17 00:00:00 2001
From: Sven Gothel
Date: Sun, 25 Aug 2013 02:47:49 +0200
Subject: GLMediaPlayer/AudioSink: Add set[Audio]Volume(float
v)/get[Audio]Volume() allowing to change the audio volume.
---
.../com/jogamp/opengl/util/av/AudioSink.java | 22 +++++++++-
.../com/jogamp/opengl/util/av/GLMediaPlayer.java | 23 +++++++++-
.../android/av/AndroidGLMediaPlayerAPI14.java | 16 +++++++
.../jogamp/opengl/openal/av/ALAudioSink.java | 31 +++++++++++++-
.../jogamp/opengl/util/av/GLMediaPlayerImpl.java | 49 +++++++++++++++++++++-
.../jogamp/opengl/util/av/JavaSoundAudioSink.java | 14 +++++++
.../jogamp/opengl/util/av/NullAudioSink.java | 16 ++++++-
7 files changed, 163 insertions(+), 8 deletions(-)
(limited to 'src/jogl')
diff --git a/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java b/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java
index 2b8da8af9..7f477a57d 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java
@@ -201,13 +201,31 @@ public interface AudioSink {
/**
* Sets the playback speed.
*
- * Play speed is set to normal, i.e. 1.0f
- * if abs(1.0f - rate) < 0.01f
to simplify test.
+ * To simplify test, play speed is normalized, i.e.
+ *
+ * 1.0f
: if Math.abs(1.0f - rate) < 0.01f
+ *
*
* @return true if successful, otherwise false, i.e. due to unsupported value range of implementation.
*/
public boolean setPlaySpeed(float s);
+ /** Returns the volume. */
+ public float getVolume();
+
+ /**
+ * Sets the volume [0f..1f].
+ *
+ * To simplify test, volume is normalized, i.e.
+ *
+ * 0.0f
: if Math.abs(v) < 0.01f
+ * 1.0f
: if Math.abs(1.0f - v) < 0.01f
+ *
+ *
+ * @return true if successful, otherwise false, i.e. due to unsupported value range of implementation.
+ */
+ public boolean setVolume(float v);
+
/**
* Returns the preferred {@link AudioDataFormat} by this sink.
*
diff --git a/src/jogl/classes/com/jogamp/opengl/util/av/GLMediaPlayer.java b/src/jogl/classes/com/jogamp/opengl/util/av/GLMediaPlayer.java
index 02fbd721c..6235bdeb0 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/av/GLMediaPlayer.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/av/GLMediaPlayer.java
@@ -350,15 +350,34 @@ public interface GLMediaPlayer extends TextureSequence {
/**
* Sets the playback speed.
*
- * Play speed is set to normal, i.e. 1.0f
- * if abs(1.0f - rate) < 0.01f
to simplify test.
+ * To simplify test, play speed is normalized, i.e.
+ *
+ * 1.0f
: if Math.abs(1.0f - rate) < 0.01f
+ *
*
* @return true if successful, otherwise false, i.e. due to unsupported value range of implementation.
*/
public boolean setPlaySpeed(float rate);
+ /** Returns the playback speed. */
public float getPlaySpeed();
+ /**
+ * Sets the audio volume, [0f..1f].
+ *
+ * To simplify test, volume is normalized, i.e.
+ *
+ * 0.0f
: if Math.abs(v) < 0.01f
+ * 1.0f
: if Math.abs(1.0f - v) < 0.01f
+ *
+ *
+ * @return true if successful, otherwise false, i.e. due to unsupported value range of implementation.
+ */
+ public boolean setAudioVolume(float v);
+
+ /** Returns the audio volume. */
+ public float getAudioVolume();
+
/**
* Lifecycle: {@link State#Paused} -> {@link State#Playing}
*/
diff --git a/src/jogl/classes/jogamp/opengl/android/av/AndroidGLMediaPlayerAPI14.java b/src/jogl/classes/jogamp/opengl/android/av/AndroidGLMediaPlayerAPI14.java
index 63d9c8d22..3ff156ae3 100644
--- a/src/jogl/classes/jogamp/opengl/android/av/AndroidGLMediaPlayerAPI14.java
+++ b/src/jogl/classes/jogamp/opengl/android/av/AndroidGLMediaPlayerAPI14.java
@@ -98,6 +98,22 @@ public class AndroidGLMediaPlayerAPI14 extends GLMediaPlayerImpl {
@Override
protected final boolean setPlaySpeedImpl(float rate) {
+ // FIXME
+ return false;
+ }
+
+ @Override
+ protected final boolean setAudioVolumeImpl(float v) {
+ if(null != mp) {
+ try {
+ mp.setVolume(v, v);
+ return true;
+ } catch (IllegalStateException ise) {
+ if(DEBUG) {
+ ise.printStackTrace();
+ }
+ }
+ }
return false;
}
diff --git a/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java b/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java
index f9ca0c028..d3964a2cf 100644
--- a/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java
+++ b/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java
@@ -58,7 +58,8 @@ public class ALAudioSink implements AudioSink {
/** Playback speed, range [0.5 - 2.0], default 1.0. */
private float playSpeed;
-
+ private float volume = 1.0f;
+
static class ALAudioFrame extends AudioFrame {
private final int alBuffer;
@@ -685,6 +686,34 @@ public class ALAudioSink implements AudioSink {
return false;
}
+ @Override
+ public final float getVolume() {
+ return volume;
+ }
+
+ @Override
+ public final boolean setVolume(float v) {
+ if( !initialized || null == chosenFormat ) {
+ return false;
+ }
+ lockContext();
+ try {
+ if( Math.abs(v) < 0.01f ) {
+ v = 0.0f;
+ } else if( Math.abs(1.0f - v) < 0.01f ) {
+ v = 1.0f;
+ }
+ if( 0.0f <= v && v <= 1.0f ) { // OpenAL limits
+ volume = v;
+ al.alSourcef(alSource[0], AL.AL_GAIN, v);
+ return true;
+ }
+ } finally {
+ unlockContext();
+ }
+ return false;
+ }
+
@Override
public final void flush() {
if( !initialized || null == chosenFormat ) {
diff --git a/src/jogl/classes/jogamp/opengl/util/av/GLMediaPlayerImpl.java b/src/jogl/classes/jogamp/opengl/util/av/GLMediaPlayerImpl.java
index 238595d45..05cc997ca 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/GLMediaPlayerImpl.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/GLMediaPlayerImpl.java
@@ -88,6 +88,7 @@ public abstract class GLMediaPlayerImpl implements GLMediaPlayer {
protected URI streamLoc = null;
protected volatile float playSpeed = 1.0f;
+ protected float audioVolume = 1.0f;
/** Shall be set by the {@link #initStreamImpl(int, int)} method implementation. */
protected int vid = GLMediaPlayer.STREAM_ID_AUTO;
@@ -362,7 +363,6 @@ public abstract class GLMediaPlayerImpl implements GLMediaPlayer {
@Override
public final boolean setPlaySpeed(float rate) {
synchronized( stateLock ) {
- final State preState = state;
final float preSpeed = playSpeed;
boolean res = false;
if(State.Uninitialized != state ) {
@@ -377,7 +377,7 @@ public abstract class GLMediaPlayerImpl implements GLMediaPlayer {
}
}
}
- if(DEBUG) { System.err.println("setPlaySpeed("+rate+"): "+preState+"/"+preSpeed+" -> "+state+"/"+playSpeed+", "+toString()); }
+ if(DEBUG) { System.err.println("setPlaySpeed("+rate+"): "+state+", "+preSpeed+" -> "+playSpeed+", "+toString()); }
return res;
}
}
@@ -397,6 +397,51 @@ public abstract class GLMediaPlayerImpl implements GLMediaPlayer {
return true;
}
+ @Override
+ public final float getAudioVolume() {
+ getAudioVolumeImpl();
+ return audioVolume;
+ }
+ /**
+ * Override if not using AudioSink, or AudioSink's {@link AudioSink#getVolume()} is not sufficient!
+ */
+ protected void getAudioVolumeImpl() {
+ if( null != audioSink ) {
+ audioVolume = audioSink.getVolume();
+ }
+ }
+
+ @Override
+ public boolean setAudioVolume(float v) {
+ synchronized( stateLock ) {
+ final float preVolume = audioVolume;
+ boolean res = false;
+ if(State.Uninitialized != state ) {
+ if( Math.abs(v) < 0.01f ) {
+ v = 0.0f;
+ } else if( Math.abs(1.0f - v) < 0.01f ) {
+ v = 1.0f;
+ }
+ if( setAudioVolumeImpl(v) ) {
+ audioVolume = v;
+ res = true;
+ }
+ }
+ if(DEBUG) { System.err.println("setAudioVolume("+v+"): "+state+", "+preVolume+" -> "+audioVolume+", "+toString()); }
+ return res;
+ }
+ }
+ /**
+ * Override if not using AudioSink, or AudioSink's {@link AudioSink#setVolume(float)} is not sufficient!
+ */
+ protected boolean setAudioVolumeImpl(float v) {
+ if( null != audioSink ) {
+ return audioSink.setVolume(v);
+ }
+ // still true, even if audioSink rejects command ..
+ return true;
+ }
+
@Override
public final void initStream(URI streamLoc, int vid, int aid, int reqTextureCount) throws IllegalStateException, IllegalArgumentException {
synchronized( stateLock ) {
diff --git a/src/jogl/classes/jogamp/opengl/util/av/JavaSoundAudioSink.java b/src/jogl/classes/jogamp/opengl/util/av/JavaSoundAudioSink.java
index b46b64748..b1b9c1b6c 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/JavaSoundAudioSink.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/JavaSoundAudioSink.java
@@ -36,6 +36,7 @@ public class JavaSoundAudioSink implements AudioSink {
private AudioDataFormat chosenFormat = null;
private volatile boolean playRequested = false;
+ private float volume = 1.0f;
static {
boolean ok = false;
@@ -62,6 +63,19 @@ public class JavaSoundAudioSink implements AudioSink {
return false; // FIXME
}
+ @Override
+ public final float getVolume() {
+ // FIXME
+ return volume;
+ }
+
+ @Override
+ public final boolean setVolume(float v) {
+ // FIXME
+ volume = v;
+ return true;
+ }
+
@Override
public AudioDataFormat getPreferredFormat() {
return DefaultFormat;
diff --git a/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java b/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java
index 067322819..83799b074 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java
@@ -14,7 +14,8 @@ public class NullAudioSink implements AudioSink {
private volatile float playSpeed = 1.0f;
private volatile boolean playRequested = false;
-
+ private float volume = 1.0f;
+
@Override
public final float getPlaySpeed() { return playSpeed; }
@@ -27,6 +28,19 @@ public class NullAudioSink implements AudioSink {
return true;
}
+ @Override
+ public final float getVolume() {
+ // FIXME
+ return volume;
+ }
+
+ @Override
+ public final boolean setVolume(float v) {
+ // FIXME
+ volume = v;
+ return true;
+ }
+
@Override
public AudioDataFormat getPreferredFormat() {
return DefaultFormat;
--
cgit v1.2.3