[Android] libvlcjni: add equalizer API

Sébastien Toque git at videolan.org
Wed Aug 7 10:18:16 CEST 2013


vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Mon Aug  5 16:13:55 2013 +0200| [8075748013500b6d58b6ba9d71bb64eeaa4c5d27] | committer: Sébastien Toque

libvlcjni: add equalizer API

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=8075748013500b6d58b6ba9d71bb64eeaa4c5d27
---

 vlc-android/jni/Android.mk                         |    2 +-
 vlc-android/jni/libvlcjni-equalizer.c              |  120 ++++++++++++++++++++
 vlc-android/src/org/videolan/libvlc/LibVLC.java    |   25 ++++
 vlc-android/src/org/videolan/vlc/AudioService.java |    3 +
 .../vlc/gui/video/VideoPlayerActivity.java         |    1 +
 5 files changed, 150 insertions(+), 1 deletion(-)

diff --git a/vlc-android/jni/Android.mk b/vlc-android/jni/Android.mk
index 55ea452..0331647 100644
--- a/vlc-android/jni/Android.mk
+++ b/vlc-android/jni/Android.mk
@@ -3,7 +3,7 @@ include $(CLEAR_VARS)
 
 LOCAL_MODULE    := libvlcjni
 
-LOCAL_SRC_FILES := libvlcjni.c libvlcjni-util.c libvlcjni-track.c aout.c vout.c
+LOCAL_SRC_FILES := libvlcjni.c libvlcjni-util.c libvlcjni-track.c aout.c vout.c libvlcjni-equalizer.c
 LOCAL_SRC_FILES += thumbnailer.c pthread-condattr.c pthread-rwlocks.c pthread-once.c eventfd.c sem.c
 LOCAL_SRC_FILES += pipe2.c
 LOCAL_SRC_FILES += wchar/wcpcpy.c
diff --git a/vlc-android/jni/libvlcjni-equalizer.c b/vlc-android/jni/libvlcjni-equalizer.c
new file mode 100644
index 0000000..d11f24d
--- /dev/null
+++ b/vlc-android/jni/libvlcjni-equalizer.c
@@ -0,0 +1,120 @@
+/*****************************************************************************
+ * libvlcjni-equalizer.c
+ *****************************************************************************
+ * Copyright © 2010-2013 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include <vlc/vlc.h>
+#include <vlc_common.h>
+
+#include <jni.h>
+
+#include "utils.h"
+
+#define LOG_TAG "VLC/JNI/Equalizer"
+#include "log.h"
+
+/**
+ * return band list as float[]
+ */
+jfloatArray Java_org_videolan_libvlc_LibVLC_getBands(JNIEnv *env, jobject thiz)
+{
+    unsigned count = libvlc_audio_equalizer_get_band_count();
+    jfloatArray bands = (*env)->NewFloatArray(env, count);
+
+    for (unsigned i = 0; i < count; ++i)
+    {
+        jfloat band = libvlc_audio_equalizer_get_band_frequency(i);
+        (*env)->SetFloatArrayRegion(env, bands, i, 1, &band);
+    }
+    return bands;
+}
+
+/**
+ * return preset list as String[]
+ */
+jobjectArray Java_org_videolan_libvlc_LibVLC_getPresets(JNIEnv *env, jobject thiz)
+{
+    unsigned count = libvlc_audio_equalizer_get_preset_count();
+    jclass stringClass = (*env)->FindClass(env, "java/lang/String");
+    jobjectArray presets = (*env)->NewObjectArray(env, count, stringClass, NULL);
+
+    for (unsigned i = 0; i < count; ++i)
+    {
+        const char *name = libvlc_audio_equalizer_get_preset_name(i);
+        jstring jname = (*env)->NewStringUTF(env, name);
+        (*env)->SetObjectArrayElement(env, presets, i, jname);
+    }
+    return presets;
+}
+
+/**
+ * return preset n° <index> as float[] (first element is preamp, then bands)
+ */
+jfloatArray Java_org_videolan_libvlc_LibVLC_getPreset(JNIEnv *env, jobject thiz, jint index)
+{
+    unsigned count = libvlc_audio_equalizer_get_band_count();
+    jfloatArray array = (*env)->NewFloatArray(env, count + 1);
+    libvlc_equalizer_t *p_equalizer = libvlc_audio_equalizer_new_from_preset(index);
+    if (p_equalizer != NULL)
+    {
+        jfloat preamp = libvlc_audio_equalizer_get_preamp(p_equalizer);
+        (*env)->SetFloatArrayRegion(env, array, 0, 1, &preamp);
+
+        for (unsigned i = 0; i < count; ++i)
+        {
+            jfloat band = libvlc_audio_equalizer_get_amp_at_index(p_equalizer, i);
+            (*env)->SetFloatArrayRegion(env, array, i + 1, 1, &band);
+        }
+        libvlc_audio_equalizer_release(p_equalizer);
+    }
+    return array;
+}
+
+/**
+ * apply equalizer settings (param bands is float[] (first element is preamp, then bands))
+ */
+//"--audio-filter=equalizer", "--equalizer-bands=-3.5 -4.5 -1 0 0 5 8 8 8 8",
+jint Java_org_videolan_libvlc_LibVLC_setNativeEqualizer(JNIEnv *env, jobject thiz, jfloatArray bands)
+{
+    jint res = -1;
+    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
+    if (!mp)
+        return res;
+
+    if (bands == NULL)
+        return libvlc_media_player_set_equalizer(mp, NULL);
+
+    jfloat *cbands = (*env)->GetFloatArrayElements(env, bands, NULL);
+    if (cbands == NULL)
+        return res;
+
+    jsize input_count = (*env)->GetArrayLength(env, bands);
+    unsigned band_count = libvlc_audio_equalizer_get_band_count();
+    if (input_count == band_count+1) // first item is preamp
+    {
+        libvlc_equalizer_t *p_equalizer = libvlc_audio_equalizer_new();
+        libvlc_audio_equalizer_set_preamp(p_equalizer, cbands[0]);
+        for (unsigned i = 0; i < band_count; ++i)
+        {
+            libvlc_audio_equalizer_set_amp_at_index(p_equalizer, cbands[i+1], i);
+        }
+        res = libvlc_media_player_set_equalizer(mp, p_equalizer);
+        libvlc_audio_equalizer_release(p_equalizer);
+    }
+    return res;
+}
diff --git a/vlc-android/src/org/videolan/libvlc/LibVLC.java b/vlc-android/src/org/videolan/libvlc/LibVLC.java
index 92330fa..395e90c 100644
--- a/vlc-android/src/org/videolan/libvlc/LibVLC.java
+++ b/vlc-android/src/org/videolan/libvlc/LibVLC.java
@@ -61,6 +61,7 @@ public class LibVLC {
     private int deblocking = -1;
     private String chroma = "";
     private boolean verboseMode = true;
+    private float[] equalizer = null;
 
     /** Check in libVLC already initialized otherwise crash */
     private boolean mIsInitialized = false;
@@ -243,6 +244,22 @@ public class LibVLC {
         this.verboseMode = verboseMode;
     }
 
+    public float[] getEqualizer()
+    {
+        return equalizer;
+    }
+
+    public void setEqualizer(float[] equalizer)
+    {
+        this.equalizer = equalizer;
+        applyEqualizer();
+    }
+
+    public void applyEqualizer()
+    {
+        setNativeEqualizer(this.equalizer);
+    }
+
     /**
      * Initialize the libVLC class
      */
@@ -566,4 +583,12 @@ public class LibVLC {
     private native void setEventHandler(EventHandler eventHandler);
 
     private native void detachEventHandler();
+
+    private native float[] getBands();
+
+    private native String[] getPresets();
+
+    private native float[] getPreset(int index);
+
+    private native int setNativeEqualizer(float[] bands);
 }
diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index 72b117a..f72d651 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -692,6 +692,7 @@ public class AudioService extends Service {
             }
         }
 
+        mLibVLC.applyEqualizer();
         mHandler.sendEmptyMessage(SHOW_PROGRESS);
         setUpRemoteControlClient();
         showNotification();
@@ -735,6 +736,7 @@ public class AudioService extends Service {
         } else {
             mLibVLC.readMedia(mCurrentMedia.getLocation(), true);
         }
+        mLibVLC.applyEqualizer();
         mHandler.sendEmptyMessage(SHOW_PROGRESS);
         setUpRemoteControlClient();
         showNotification();
@@ -906,6 +908,7 @@ public class AudioService extends Service {
                 } else {
                     mLibVLC.readMedia(mCurrentMedia.getLocation(), noVideo);
                 }
+                mLibVLC.applyEqualizer();
                 setUpRemoteControlClient();
                 showNotification();
                 updateWidget(AudioService.this);
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index e9c175a..6e6bddd 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -1484,6 +1484,7 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
         } else if (mLocation != null && mLocation.length() > 0 && !dontParse) {
             savedIndexPosition = mLibVLC.readMedia(mLocation, false);
         }
+        mLibVLC.applyEqualizer();
 
         if (mLocation != null && mLocation.length() > 0 && !dontParse) {
             // restore last position



More information about the Android mailing list