From 0de52016830e72786dd712d846481712d6ff4096 Mon Sep 17 00:00:00 2001 From: xufuji456 Date: Wed, 23 Mar 2022 14:18:54 +0800 Subject: [PATCH] Feature: add AudioEffect of LoudnessEnhancer --- .../controller/AudioEffectController.kt | 57 ++++++++++++++----- .../ffmpeg/activity/AudioEffectActivity.kt | 41 +++---------- 2 files changed, 52 insertions(+), 46 deletions(-) diff --git a/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt b/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt index 49ae39d..f72ebc1 100644 --- a/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt +++ b/AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt @@ -4,7 +4,9 @@ import android.R import android.content.Context import android.media.audiofx.BassBoost import android.media.audiofx.Equalizer +import android.media.audiofx.LoudnessEnhancer import android.media.audiofx.PresetReverb +import android.os.Build import android.util.Log import android.util.Pair import android.view.View @@ -12,6 +14,7 @@ import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.SeekBar import android.widget.Spinner +import androidx.annotation.RequiresApi import com.frank.androidmedia.listener.AudioEffectCallback import java.util.ArrayList @@ -27,26 +30,27 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) { val TAG: String = AudioEffectController::class.java.simpleName } - private var bands: Short = 0 + private var mBands: Short = 0 private var minEQLevel: Short = 0 private var mEqualizer: Equalizer? = null - private var mPresetReverb: PresetReverb? = null - private val reverbValues = ArrayList() - private var mBass: BassBoost? = null + private var mPresetReverb: PresetReverb? = null + private var mLoudnessEnhancer: LoudnessEnhancer? = null private var mAudioEffectCallback: AudioEffectCallback? = null - private val presetReverb = arrayOf("None", "SmallRoom", "MediumRoom", "LargeRoom", - "MediumHall", "LargeHall", "Plate") + private val presetReverb = arrayOf("None", "SmallRoom", "MediumRoom", + "LargeRoom", "MediumHall", "LargeHall", "Plate") init { mAudioEffectCallback = audioEffectCallback } /** - * Setup AudioEffect of Equalizer, which has centerFrequency、band、bandLevel + * Setup AudioEffect of Equalizer, which uses to adjust the gain of frequency. + * There are key params of band、bandLevel、centerFrequency in Equalizer. + * The frequency ranges from ultra-low freq、low freq、middle freq、high freq、ultra-high freq. */ fun setupEqualizer(audioSessionId: Int) { val equalizerList = ArrayList>() @@ -55,8 +59,8 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) { // band level: min and max minEQLevel = mEqualizer!!.bandLevelRange[0]//min level val maxEQLevel = mEqualizer!!.bandLevelRange[1] // max level - bands = mEqualizer!!.numberOfBands - for (i in 0 until bands) { + mBands = mEqualizer!!.numberOfBands + for (i in 0 until mBands) { val centerFreq = (mEqualizer!!.getCenterFreq(i.toShort()) / 1000).toString() + " Hz" val pair = Pair.create(centerFreq, mEqualizer!!.getBandLevel(i.toShort()) - minEQLevel) equalizerList.add(pair) @@ -68,18 +72,19 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) { * Setup preset style, which associates to Equalizer */ fun setupPresetStyle(context: Context, spinnerStyle: Spinner) { + val mReverbValues = ArrayList() for (i in 0 until mEqualizer!!.numberOfPresets) { - reverbValues.add(mEqualizer!!.getPresetName(i.toShort())) + mReverbValues.add(mEqualizer!!.getPresetName(i.toShort())) } - spinnerStyle.adapter = ArrayAdapter(context, R.layout.simple_spinner_item, reverbValues) + spinnerStyle.adapter = ArrayAdapter(context, R.layout.simple_spinner_item, mReverbValues) spinnerStyle.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(arg0: AdapterView<*>, arg1: View, arg2: Int, arg3: Long) { try { mEqualizer!!.usePreset(arg2.toShort()) val seekBarList: List? = mAudioEffectCallback?.getSeeBarList() - if (bands > 0 && seekBarList != null && mEqualizer != null) { - for (band in 0 until bands) { + if (mBands > 0 && seekBarList != null && mEqualizer != null) { + for (band in 0 until mBands) { seekBarList[band].progress = mEqualizer!!.getBandLevel(band.toShort()) - minEQLevel } } @@ -115,6 +120,31 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) { }) } + /** + * Setup AudioEffect of LoudnessEnhancer, which use + */ + @RequiresApi(Build.VERSION_CODES.KITKAT) + fun setLoudnessEnhancer(audioSessionId: Int, barEnhancer: SeekBar) { + mLoudnessEnhancer = LoudnessEnhancer(audioSessionId) + mLoudnessEnhancer!!.enabled = true + // Unit: mB + mLoudnessEnhancer!!.setTargetGain(500) + barEnhancer.max = 1000 + barEnhancer.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + if (fromUser) { + mLoudnessEnhancer!!.setTargetGain(progress) + } + } + + override fun onStartTrackingTouch(seekBar: SeekBar?) { + } + + override fun onStopTrackingTouch(seekBar: SeekBar?) { + } + }) + } + fun onEqualizerProgress(index: Int, progress: Int) { mEqualizer!!.setBandLevel(index.toShort(), (progress + minEQLevel).toShort()) } @@ -123,6 +153,7 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) { mBass?.release() mEqualizer?.release() mPresetReverb?.release() + mLoudnessEnhancer?.release() } diff --git a/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt index c89bf5e..99c8a23 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt @@ -10,7 +10,6 @@ import android.util.Log import android.util.Pair import android.view.View import android.widget.* -import androidx.annotation.RequiresApi import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.frank.androidmedia.controller.AudioEffectController @@ -41,12 +40,11 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac private var spinnerReverb: Spinner? = null private var barBassBoost: SeekBar? = null private var equalizerAdapter: EqualizerAdapter? = null - private var loudnessEnhancer: LoudnessEnhancer? = null private var barEnhancer: SeekBar? = null private var visualizerView: VisualizerView? = null private var mVisualizer: AudioVisualizer? = null - private var audioEffectController: AudioEffectController? = null + private var mAudioEffectController: AudioEffectController? = null private val permissions = arrayOf( Manifest.permission.WRITE_EXTERNAL_STORAGE, @@ -54,11 +52,11 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac Manifest.permission.MODIFY_AUDIO_SETTINGS) private val onPreparedListener = MediaPlayer.OnPreparedListener { - audioEffectController = AudioEffectController(this) - audioEffectController?.setupEqualizer(mPlayer!!.audioSessionId) - audioEffectController?.setupPresetStyle(this@AudioEffectActivity, spinnerStyle!!) - audioEffectController?.setupBassBoost(mPlayer!!.audioSessionId, barBassBoost!!) - setLoudnessEnhancer() + mAudioEffectController = AudioEffectController(this) + mAudioEffectController?.setupEqualizer(mPlayer!!.audioSessionId) + mAudioEffectController?.setupPresetStyle(this@AudioEffectActivity, spinnerStyle!!) + mAudioEffectController?.setupBassBoost(mPlayer!!.audioSessionId, barBassBoost!!) + mAudioEffectController?.setLoudnessEnhancer(mPlayer!!.audioSessionId, barEnhancer!!) setupVisualizer() mPlayer!!.start() @@ -111,29 +109,7 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac } override fun onProgress(index: Int, progress: Int) { - audioEffectController?.onEqualizerProgress(index, progress) - } - - @RequiresApi(Build.VERSION_CODES.KITKAT) - private fun setLoudnessEnhancer() { - loudnessEnhancer = LoudnessEnhancer(mPlayer!!.audioSessionId) - loudnessEnhancer!!.enabled = true - // Unit: mB - loudnessEnhancer!!.setTargetGain(500) - barEnhancer!!.max = 1000 - barEnhancer!!.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { - override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - if (fromUser) { - loudnessEnhancer!!.setTargetGain(progress) - } - } - - override fun onStartTrackingTouch(seekBar: SeekBar?) { - } - - override fun onStopTrackingTouch(seekBar: SeekBar?) { - } - }) + mAudioEffectController?.onEqualizerProgress(index, progress) } private fun setupVisualizer() { @@ -176,8 +152,7 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac override fun onDestroy() { super.onDestroy() - audioEffectController?.release() - loudnessEnhancer?.release() + mAudioEffectController?.release() releaseVisualizer() mPlayer?.release() }