Share My Creation B4A Audio Equalizer

Full Explanation of the Audio Equalizer + BassBoost + Virtualizer + Presets + Visualizer (B4XPages + ExoPlayer)​

This code implements a complete audio processing page using B4XPages. It includes:

  • SimpleExoPlayer audio playback
  • Android AudioFX Equalizer (5 bands)
  • BassBoost
  • Virtualizer
  • Preset system (Pop, Rock, Techno, Vocal Booster, etc.)
  • Winamp‑style visualizer simulation
  • Automatic persistence using KeyValueStore
  • Dynamic initialization based on the audio session ID
It is designed to be easy to integrate, modify, and extend.

1. Page Initialization​

In B4XPage_Created, the code:

  • loads the layout
  • initializes KeyValueStore
  • configures the ComboBox
  • groups the 5 equalizer sliders into a list
  • assigns each slider a Tag representing its band index
  • initializes the visualizer arrays
  • starts a timer for the visualizer (≈30 FPS)
  • initializes ExoPlayer and prepares a sample MP3
This creates the foundation for the equalizer and visualizer.

2. Dynamic Equalizer Initialization​

Android’s audio effects (Equalizer, BassBoost, Virtualizer) must be created using the audio session ID of the player.

When ExoPlayer is ready:
B4A:
Dim AudioSessionId As Int = nativePlayer.RunMethod("getAudioSessionId", Null)
If the ID is valid, the code initializes:
B4A:
EQ.InitializeNewInstance("android.media.audiofx.Equalizer", Array(0, SessionId))
BassBoost.InitializeNewInstance("android.media.audiofx.BassBoost", Array(0, SessionId))
Virtualizer.InitializeNewInstance("android.media.audiofx.Virtualizer", Array(0, SessionId))

Then:
  • retrieves the min/max band levels
  • configures each slider’s range
  • restores saved values from KeyValueStore
  • applies the saved equalizer settings
This ensures the equalizer is fully synchronized with the audio engine.

3. Equalizer Sliders​

Each equalizer slider has a Tag containing its band index.

When a slider changes:
B4A:
Dim BandIndex As Int = sb.Tag
Dim TargetMilliBels As Short = Value + EQMinLevel
EQ.RunMethod("setBandLevel", Array(AsShort(BandIndex), TargetMilliBels))
The value is also saved:
B4A:
kvs.Put("Band_" & BandIndex, Value)
This makes the equalizer responsive and persistent.

BassBoost & Virtualizer​

These effects use the same pattern:
B4A:
BassBoost.RunMethod("setStrength", Array(AsShort(Value)))
Virtualizer.RunMethod("setStrength", Array(AsShort(Value)))
Both values are saved in KeyValueStore.

5. Preset System​

The ComboBox contains several audio profiles:
  • Flat
  • Pop
  • Rock
  • Techno / Dance
  • Vocal Booster
Each preset defines:
  • 5 equalizer band values
  • BassBoost strength
  • Virtualizer strength
The preset is applied using:
B4A:
EQ.RunMethod("setBandLevel", Array(AsShort(i), AsShort(GainMilliBels)))
sb.Value = SliderValue
This updates both the audio engine and the UI.

6. Winamp‑Style Visualizer​

The visualizer is a simulation, not a real FFT. It uses:
  • 32 bars
  • random values influenced by equalizer energy
  • peak‑hold effect
  • decay over time
  • a timer running at ~30 FPS
Each timer tick:
  1. Updates bar values
  2. Updates peak values
  3. Draws bars and peaks on a B4XCanvas
  4. Clears and redraws the panel
This produces a smooth, animated visualizer similar to classic Winamp skins.

7. Automatic Persistence (KeyValueStore)
All user settings are saved:
  • equalizer band levels
  • BassBoost strength
  • Virtualizer strength
On startup, the values are restored and applied automatically.
This makes the audio page behave like a real equalizer app.

8. Summary for B4X Developers​

This code demonstrates:
  • how to use ExoPlayer with B4XPages
  • how to initialize Android AudioFX correctly
  • how to build a multi‑band equalizer
  • how to implement BassBoost and Virtualizer
  • how to create a preset system
  • how to draw a custom visualizer
  • how to persist settings with KeyValueStore
It can be used as:
  • a base for a full audio player
  • a learning example for AudioFX
  • a visualizer demo
  • an equalizer module for existing apps

Required libraries
lib.png


Have fun
 

Attachments

  • Screenshot.png
    Screenshot.png
    41.6 KB · Views: 22
  • Equalizer.zip
    13.2 KB · Views: 15
Top