Add audio visualization feature for audio-only mode
- Implement AudioVisualizerController for real-time audio capture and spectrum analysis - Add audio-only visualization mode in Settings > Synchronization - Auto-enable RGB sync animation when audio mode is active - Support pause/resume recording with proper audio visualizer cleanup - Add RECORD_AUDIO permission to AndroidManifest - Generate visual frames from 64-band audio spectrum with gradient coloring
This commit is contained in:
1 parent
65b6fbc009
commit
3aa9ba4b22
6 files changed
+258
No files matched your search
@@ -10,6 +10,7 @@
|
|||||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
|
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import com.hyperion.grabber.common.util.BorderProcessor;
|
|||||||
import com.hyperion.grabber.common.util.HyperionGrabberOptions;
|
import com.hyperion.grabber.common.util.HyperionGrabberOptions;
|
||||||
import com.hyperion.grabber.common.util.AnimationSyncController;
|
import com.hyperion.grabber.common.util.AnimationSyncController;
|
||||||
import com.hyperion.grabber.common.util.Preferences;
|
import com.hyperion.grabber.common.util.Preferences;
|
||||||
|
import com.hyperion.grabber.common.util.AudioVisualizerController;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
@@ -60,6 +61,9 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
|||||||
|
|
||||||
private AnimationSyncController mAnimationSync;
|
private AnimationSyncController mAnimationSync;
|
||||||
private Preferences mPreferences;
|
private Preferences mPreferences;
|
||||||
|
private AudioVisualizerController mAudioVisualizer;
|
||||||
|
private boolean mAudioOnlyMode = false;
|
||||||
|
private boolean mAnimationAutoEnabled = false;
|
||||||
|
|
||||||
private final Runnable mCaptureRunnable = new Runnable() {
|
private final Runnable mCaptureRunnable = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@@ -123,12 +127,20 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
|||||||
mPreferences = new Preferences(context);
|
mPreferences = new Preferences(context);
|
||||||
int frameDelay = mPreferences.getInt(R.string.pref_key_frame_delay, 0);
|
int frameDelay = mPreferences.getInt(R.string.pref_key_frame_delay, 0);
|
||||||
boolean enableAnimation = mPreferences.getBoolean(R.string.pref_key_enable_animation, false);
|
boolean enableAnimation = mPreferences.getBoolean(R.string.pref_key_enable_animation, false);
|
||||||
|
mAudioOnlyMode = mPreferences.getBoolean(R.string.pref_key_audio_only_mode, false);
|
||||||
|
|
||||||
mAnimationSync = new AnimationSyncController(frameDelay, enableAnimation);
|
mAnimationSync = new AnimationSyncController(frameDelay, enableAnimation);
|
||||||
|
|
||||||
|
if (mAudioOnlyMode) {
|
||||||
|
mAudioVisualizer = new AudioVisualizerController();
|
||||||
|
mAnimationAutoEnabled = true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
mAnimationSync = new AnimationSyncController(0, false);
|
mAnimationSync = new AnimationSyncController(0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG) Log.d(TAG, "Capture: " + mCaptureWidth + "x" + mCaptureHeight + " @ " + mFrameRate + "fps");
|
if (DEBUG) Log.d(TAG, "Capture: " + mCaptureWidth + "x" + mCaptureHeight + " @ " + mFrameRate + "fps");
|
||||||
|
if (mAudioOnlyMode) Log.d(TAG, "Audio-only visualization mode enabled");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
init();
|
init();
|
||||||
@@ -177,11 +189,28 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
|||||||
mRunning = true;
|
mRunning = true;
|
||||||
setCapturing(true);
|
setCapturing(true);
|
||||||
mFrameCount = 0;
|
mFrameCount = 0;
|
||||||
|
|
||||||
|
if (mAudioOnlyMode && mAudioVisualizer != null) {
|
||||||
|
mAudioVisualizer.start();
|
||||||
|
if (mAnimationSync != null && mAnimationAutoEnabled) {
|
||||||
|
mAnimationSync.setAnimationEnabled(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mCaptureHandler.post(mCaptureRunnable);
|
mCaptureHandler.post(mCaptureRunnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void captureFrame() {
|
private void captureFrame() {
|
||||||
long frameStart = System.nanoTime();
|
long frameStart = System.nanoTime();
|
||||||
|
|
||||||
|
if (mAudioOnlyMode && mAudioVisualizer != null) {
|
||||||
|
generateAudioVisualization();
|
||||||
|
long captureTime = System.nanoTime() - frameStart;
|
||||||
|
mLastCaptureTimeNs = captureTime;
|
||||||
|
updateLoadTracking(captureTime);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Image img = null;
|
Image img = null;
|
||||||
try {
|
try {
|
||||||
img = mImageReader.acquireLatestImage();
|
img = mImageReader.acquireLatestImage();
|
||||||
@@ -208,6 +237,27 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
|||||||
updateLoadTracking(captureTime);
|
updateLoadTracking(captureTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void generateAudioVisualization() {
|
||||||
|
if (mAudioVisualizer == null) return;
|
||||||
|
|
||||||
|
int[] spectrum = mAudioVisualizer.getFullSpectrum();
|
||||||
|
int[] visualization = AudioVisualizerController.generateAudioVisualization(
|
||||||
|
mCaptureWidth, mCaptureHeight, spectrum);
|
||||||
|
|
||||||
|
byte[] rgb = getRgbBuffer(mCaptureWidth, mCaptureHeight);
|
||||||
|
|
||||||
|
for (int i = 0; i < visualization.length && i < rgb.length; i++) {
|
||||||
|
rgb[i] = (byte) (visualization[i] & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mAnimationSync != null) {
|
||||||
|
rgb = mAnimationSync.applyAnimationToFrame(rgb);
|
||||||
|
}
|
||||||
|
|
||||||
|
mListener.sendFrame(rgb, mCaptureWidth, mCaptureHeight);
|
||||||
|
mRgbBufferIndex = (mRgbBufferIndex + 1) % RGB_BUFFER_RING_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
private void updateLoadTracking(long captureTimeNs) {
|
private void updateLoadTracking(long captureTimeNs) {
|
||||||
if (captureTimeNs > (long)(mFrameIntervalMs * 1_000_000 * DEADLINE_MISS_RATIO)) {
|
if (captureTimeNs > (long)(mFrameIntervalMs * 1_000_000 * DEADLINE_MISS_RATIO)) {
|
||||||
@@ -389,6 +439,12 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
|||||||
mRunning = false;
|
mRunning = false;
|
||||||
setCapturing(false);
|
setCapturing(false);
|
||||||
|
|
||||||
|
if (mAudioVisualizer != null) {
|
||||||
|
mAudioVisualizer.stop();
|
||||||
|
mAudioVisualizer.release();
|
||||||
|
mAudioVisualizer = null;
|
||||||
|
}
|
||||||
|
|
||||||
if (mCaptureHandler != null) {
|
if (mCaptureHandler != null) {
|
||||||
mCaptureHandler.removeCallbacksAndMessages(null);
|
mCaptureHandler.removeCallbacksAndMessages(null);
|
||||||
}
|
}
|
||||||
@@ -427,6 +483,9 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
|
|||||||
if (DEBUG) Log.i(TAG, "Pausing");
|
if (DEBUG) Log.i(TAG, "Pausing");
|
||||||
mRunning = false;
|
mRunning = false;
|
||||||
setCapturing(false);
|
setCapturing(false);
|
||||||
|
if (mAudioVisualizer != null) {
|
||||||
|
mAudioVisualizer.stop();
|
||||||
|
}
|
||||||
if (mCaptureHandler != null) {
|
if (mCaptureHandler != null) {
|
||||||
mCaptureHandler.removeCallbacksAndMessages(null);
|
mCaptureHandler.removeCallbacksAndMessages(null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,187 @@
|
|||||||
|
package com.hyperion.grabber.common.util;
|
||||||
|
|
||||||
|
import android.media.AudioFormat;
|
||||||
|
import android.media.AudioRecord;
|
||||||
|
import android.media.MediaRecorder;
|
||||||
|
import android.util.Log;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||||
|
|
||||||
|
public class AudioVisualizerController {
|
||||||
|
private static final String TAG = "AudioVisualizer";
|
||||||
|
private static final int SAMPLE_RATE = 16000;
|
||||||
|
private static final int BUFFER_SIZE = 2048;
|
||||||
|
private static final int SPECTRUM_BANDS = 64;
|
||||||
|
|
||||||
|
private AudioRecord audioRecord;
|
||||||
|
private Thread captureThread;
|
||||||
|
private final AtomicBoolean isCapturing = new AtomicBoolean(false);
|
||||||
|
private final AtomicIntegerArray spectrumData = new AtomicIntegerArray(SPECTRUM_BANDS);
|
||||||
|
private volatile int[] audioWaveform = new int[BUFFER_SIZE];
|
||||||
|
|
||||||
|
public AudioVisualizerController() {
|
||||||
|
initAudioRecord();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initAudioRecord() {
|
||||||
|
try {
|
||||||
|
int bufferSize = AudioRecord.getMinBufferSize(
|
||||||
|
SAMPLE_RATE,
|
||||||
|
AudioFormat.CHANNEL_IN_MONO,
|
||||||
|
AudioFormat.ENCODING_PCM_16BIT);
|
||||||
|
|
||||||
|
audioRecord = new AudioRecord(
|
||||||
|
MediaRecorder.AudioSource.MIC,
|
||||||
|
SAMPLE_RATE,
|
||||||
|
AudioFormat.CHANNEL_IN_MONO,
|
||||||
|
AudioFormat.ENCODING_PCM_16BIT,
|
||||||
|
Math.max(bufferSize, BUFFER_SIZE * 2));
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Failed to initialize AudioRecord", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
if (isCapturing.getAndSet(true)) return;
|
||||||
|
if (audioRecord == null) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
audioRecord.startRecording();
|
||||||
|
captureThread = new Thread(this::captureAudio, "AudioCapture");
|
||||||
|
captureThread.setDaemon(true);
|
||||||
|
captureThread.start();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Failed to start audio capture", e);
|
||||||
|
isCapturing.set(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
if (!isCapturing.getAndSet(false)) return;
|
||||||
|
|
||||||
|
if (audioRecord != null) {
|
||||||
|
try {
|
||||||
|
audioRecord.stop();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Error stopping audio record", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (captureThread != null) {
|
||||||
|
try {
|
||||||
|
captureThread.join(500);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void captureAudio() {
|
||||||
|
if (audioRecord == null) return;
|
||||||
|
|
||||||
|
short[] buffer = new short[BUFFER_SIZE];
|
||||||
|
|
||||||
|
while (isCapturing.get()) {
|
||||||
|
try {
|
||||||
|
int readSize = audioRecord.read(buffer, 0, BUFFER_SIZE);
|
||||||
|
if (readSize > 0) {
|
||||||
|
processAudio(buffer, readSize);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Error reading audio", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processAudio(short[] buffer, int size) {
|
||||||
|
int[] waveform = new int[size];
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
waveform[i] = Math.abs(buffer[i]);
|
||||||
|
}
|
||||||
|
audioWaveform = waveform;
|
||||||
|
|
||||||
|
computeSpectrum(buffer, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void computeSpectrum(short[] buffer, int size) {
|
||||||
|
int[] spectrum = new int[SPECTRUM_BANDS];
|
||||||
|
|
||||||
|
for (int band = 0; band < SPECTRUM_BANDS; band++) {
|
||||||
|
int bandWidth = size / SPECTRUM_BANDS;
|
||||||
|
int startIdx = band * bandWidth;
|
||||||
|
int endIdx = startIdx + bandWidth;
|
||||||
|
|
||||||
|
long sum = 0;
|
||||||
|
for (int i = startIdx; i < endIdx && i < size; i++) {
|
||||||
|
sum += Math.abs(buffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int avgMagnitude = (int) (sum / bandWidth);
|
||||||
|
spectrum[band] = Math.min(255, avgMagnitude / 128);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < SPECTRUM_BANDS; i++) {
|
||||||
|
spectrumData.set(i, spectrum[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSpectrumBand(int band) {
|
||||||
|
if (band < 0 || band >= SPECTRUM_BANDS) return 0;
|
||||||
|
return spectrumData.get(band);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getFullSpectrum() {
|
||||||
|
int[] result = new int[SPECTRUM_BANDS];
|
||||||
|
for (int i = 0; i < SPECTRUM_BANDS; i++) {
|
||||||
|
result[i] = spectrumData.get(i);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getWaveform() {
|
||||||
|
return audioWaveform;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWaveformSample(int index) {
|
||||||
|
if (index < 0 || index >= audioWaveform.length) return 0;
|
||||||
|
return audioWaveform[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void release() {
|
||||||
|
stop();
|
||||||
|
if (audioRecord != null) {
|
||||||
|
try {
|
||||||
|
audioRecord.release();
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Error releasing audio record", e);
|
||||||
|
}
|
||||||
|
audioRecord = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCapturing() {
|
||||||
|
return isCapturing.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] generateAudioVisualization(int width, int height, int[] spectrum) {
|
||||||
|
int[] visualization = new int[width * height * 3];
|
||||||
|
int bandWidth = Math.max(1, width / spectrum.length);
|
||||||
|
|
||||||
|
for (int y = 0; y < height; y++) {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
int band = Math.min(x / bandWidth, spectrum.length - 1);
|
||||||
|
int intensity = spectrum[band];
|
||||||
|
|
||||||
|
float normalizedHeight = (float) y / height;
|
||||||
|
int value = (int) (intensity * (1.0f - normalizedHeight));
|
||||||
|
|
||||||
|
int idx = (y * width + x) * 3;
|
||||||
|
visualization[idx] = Math.min(255, value);
|
||||||
|
visualization[idx + 1] = Math.min(255, value * 2 / 3);
|
||||||
|
visualization[idx + 2] = Math.min(255, value / 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return visualization;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,4 +45,7 @@
|
|||||||
|
|
||||||
<string name="pref_key_enable_animation" translatable="false">pref_key_enable_animation</string>
|
<string name="pref_key_enable_animation" translatable="false">pref_key_enable_animation</string>
|
||||||
<bool name="pref_default_enable_animation" translatable="false">false</bool>
|
<bool name="pref_default_enable_animation" translatable="false">false</bool>
|
||||||
|
|
||||||
|
<string name="pref_key_audio_only_mode" translatable="false">pref_key_audio_only_mode</string>
|
||||||
|
<bool name="pref_default_audio_only_mode" translatable="false">false</bool>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -77,4 +77,6 @@
|
|||||||
<string name="pref_summary_frame_delay">Delay frame updates in milliseconds to sync with RGB devices (-100 to +100ms)</string>
|
<string name="pref_summary_frame_delay">Delay frame updates in milliseconds to sync with RGB devices (-100 to +100ms)</string>
|
||||||
<string name="pref_title_enable_animation">Enable Update Animation</string>
|
<string name="pref_title_enable_animation">Enable Update Animation</string>
|
||||||
<string name="pref_summary_enable_animation">Sends a synchronized animation pattern to help align RGB timing</string>
|
<string name="pref_summary_enable_animation">Sends a synchronized animation pattern to help align RGB timing</string>
|
||||||
|
<string name="pref_title_audio_only_mode">Audio Visualization Mode</string>
|
||||||
|
<string name="pref_summary_audio_only_mode">Capture internal audio and visualize as gradient with audio-reactive sections</string>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -140,6 +140,12 @@
|
|||||||
android:title="@string/pref_title_enable_animation"
|
android:title="@string/pref_title_enable_animation"
|
||||||
android:summary="@string/pref_summary_enable_animation" />
|
android:summary="@string/pref_summary_enable_animation" />
|
||||||
|
|
||||||
|
<CheckBoxPreference
|
||||||
|
android:defaultValue="@bool/pref_default_audio_only_mode"
|
||||||
|
android:key="@string/pref_key_audio_only_mode"
|
||||||
|
android:title="@string/pref_title_audio_only_mode"
|
||||||
|
android:summary="@string/pref_summary_audio_only_mode" />
|
||||||
|
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
<PreferenceCategory
|
<PreferenceCategory
|
||||||
|
|||||||
Reference in new issue
Block a user