Skip to content

Commit 1b3d0bb

Browse files
committed
fixed vizuaizer source and currently improving flashlight viz
1 parent d04e11d commit 1b3d0bb

19 files changed

Lines changed: 615 additions & 443 deletions

File tree

TODO.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ with gemini of similar
4343
* **Make the haptic amplitude mode resubmit a oneshot haptic all the time, even if it doesn't change!!!** currently it doesn't resubmit when the vibration amplitude doesn't change!
4444
* make the oneshot duration slightly longer
4545
* enhance the built-in switches by adding an X (cross) or a done (✅) in them, like the battery guru's settings switches, and also with bouncy animations and nice haptics
46-
* Fix microphone latency
47-
* decrease the decay for the android built in vizualiser (make the decay 60 fps even if the incoming data is 20fps)
4846
* Add m3e split row selectors
4947
* Typography menu
5048
* Idle breathing
@@ -68,9 +66,12 @@ with gemini of similar
6866
---
6967

7068
## Done
69+
* Fix microphone latency
70+
* decrease the decay for the android built in vizualiser (make the decay 60 fps even if the incoming data is 20fps)
7171
* Add a disclaimer popup on first use of the media projection source that we do not record the screen, even if it looks like it.
7272
* Track preset usage time instead
7373
* Implement weekly leaderboard and usage statistics
74+
* --- 3.2.1
7475
* Refine beat detection engine haptics
7576
* Fix the amplitude haptics (Aleks)
7677
* fix the app crash

app/src/main/java/com/better/nothing/music/vizualizer/logic/AudioProcessor.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public class AudioProcessor {
2424
private float[] hann;
2525
private DoubleFFT_1D fft;
2626

27+
// Improved Autogain state
28+
private float mRunningMax = 0.05f;
29+
private float mTargetPeak = 0.18f;
30+
private float mAutoGain = 1.0f;
31+
private boolean mAutoGainEnabled = false;
32+
33+
private static final float DECAY_FAST = 0.999f;
34+
private static final float DECAY_SLOW = 0.9998f;
35+
private static final float GAIN_SMOOTHING = 0.15f; // Alpha for gain smoothing
36+
2737
public AudioProcessor() {
2838
updateFFTSize(); // Default
2939
}
@@ -33,7 +43,8 @@ public void updateFFTSize() {
3343
}
3444

3545
public void updateFFTSize(int sampleRate) {
36-
int newFftSize = 4096; // Fixed size for temporal snappiness
46+
// Reduced from 4096 to 2048 to improve temporal responsiveness and reduce window latency.
47+
int newFftSize = 2048;
3748

3849
if (this.fftSize == newFftSize && this.fft != null && this.sampleRate == sampleRate) {
3950
return;
@@ -58,6 +69,18 @@ public float getHzPerBin() {
5869
return hzPerBin;
5970
}
6071

72+
public int getFFTSize() {
73+
return fftSize;
74+
}
75+
76+
public void setAutoGainEnabled(boolean enabled) {
77+
this.mAutoGainEnabled = enabled;
78+
if (!enabled) {
79+
mAutoGain = 1.0f;
80+
mRunningMax = 0.05f;
81+
}
82+
}
83+
6184
public AudioFrameResult processAudioFrame(short[] hopBuffer, VisualizerConfig config, FrequencyRange hapticRange) {
6285
// Fill ring buffer
6386
for (short value : hopBuffer) {
@@ -87,7 +110,25 @@ public AudioFrameResult processAudioFrame(short[] hopBuffer, VisualizerConfig co
87110
// Amplify high frequencies: linear boost from 1.0x at 0Hz to ~4.0x at 20kHz
88111
float freq = i * hzPerBin;
89112
float boost = 1f + (freq / 15000f) * 5f;
90-
magnitude[i] = mag * boost;
113+
float rawMag = mag * boost;
114+
115+
if (mAutoGainEnabled) {
116+
// Update running max with adaptive decay: faster if we're way above target, slower if we're below
117+
float decay = rawMag > mRunningMax ? 0.95f : DECAY_SLOW;
118+
mRunningMax = Math.max(mRunningMax * decay, rawMag);
119+
120+
if (mRunningMax > 0.0001f) {
121+
float desiredGain = mTargetPeak / Math.max(mRunningMax, 0.005f);
122+
// Clamp gain to reasonable range [0.5, 20.0] for high quality normalization
123+
desiredGain = Math.max(0.5f, Math.min(20.0f, desiredGain));
124+
125+
// Smooth the gain changes to prevent flickering
126+
mAutoGain = (mAutoGain * (1f - GAIN_SMOOTHING)) + (desiredGain * GAIN_SMOOTHING);
127+
}
128+
magnitude[i] = rawMag * mAutoGain;
129+
} else {
130+
magnitude[i] = rawMag;
131+
}
91132
}
92133

93134
// Compute magnitudes

0 commit comments

Comments
 (0)