Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f94851a0 authored by Harry Youd's avatar Harry Youd Committed by Luca Stefani
Browse files

Visualizer: Adjust for volume level

Volume levels are taken into account when attaching to sessionId=0
(system mixer output). Account for this by querying volume and
multiplying the reciprocal of the percentage. Unfortunately, there is
nothing that can be done about volume=0

We could refactor to attach to specific audio sessions, but this limits
us to one stream only and adds complexity

Change-Id: I7ccf2852a5018a72eabf0c7dbdd2f7242eac58ce
parent 4c5a5d87
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.audiofx.Visualizer;
import android.os.AsyncTask;
import android.os.UserHandle;
@@ -45,6 +46,7 @@ public class VisualizerView extends View
    private static final String LOCKSCREEN_VISUALIZER_ENABLED =
            "lineagesecure:" + LineageSettings.Secure.LOCKSCREEN_VISUALIZER_ENABLED;

    private AudioManager mAudioManager;
    private Paint mPaint;
    private Visualizer mVisualizer;
    private ObjectAnimator mVisualizerColorAnimator;
@@ -80,7 +82,10 @@ public class VisualizerView extends View
                mValueAnimators[i].cancel();
                rfk = fft[i * 2 + 2];
                ifk = fft[i * 2 + 3];
                magnitude = rfk * rfk + ifk * ifk;
                int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                int volumeFactor = (currentVolume != 0 ? (maxVolume / currentVolume) : 1);
                magnitude = (rfk * rfk + ifk * ifk) * volumeFactor * volumeFactor;
                dbValue = magnitude > 0 ? (int) (10 * Math.log10(magnitude)) : 0;

                mValueAnimators[i].setFloatValues(mFFTPoints[i * 4 + 1],
@@ -143,6 +148,8 @@ public class VisualizerView extends View
    public VisualizerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mAudioManager = context.getSystemService(AudioManager.class);

        mColor = Color.TRANSPARENT;

        mPaint = new Paint();