diff --git a/common/src/main/AndroidManifest.xml b/common/src/main/AndroidManifest.xml index 5b3765e..a11f11b 100644 --- a/common/src/main/AndroidManifest.xml +++ b/common/src/main/AndroidManifest.xml @@ -10,6 +10,7 @@ + diff --git a/common/src/main/java/com/hyperion/grabber/HyperionScreenEncoder.java b/common/src/main/java/com/hyperion/grabber/HyperionScreenEncoder.java index c1ab7f3..cc6d385 100644 --- a/common/src/main/java/com/hyperion/grabber/HyperionScreenEncoder.java +++ b/common/src/main/java/com/hyperion/grabber/HyperionScreenEncoder.java @@ -20,6 +20,7 @@ import com.hyperion.grabber.common.util.BorderProcessor; import com.hyperion.grabber.common.util.HyperionGrabberOptions; import com.hyperion.grabber.common.util.AnimationSyncController; import com.hyperion.grabber.common.util.Preferences; +import com.hyperion.grabber.common.util.AudioVisualizerController; import java.nio.ByteBuffer; @@ -60,6 +61,9 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase { private AnimationSyncController mAnimationSync; private Preferences mPreferences; + private AudioVisualizerController mAudioVisualizer; + private boolean mAudioOnlyMode = false; + private boolean mAnimationAutoEnabled = false; private final Runnable mCaptureRunnable = new Runnable() { @Override @@ -123,12 +127,20 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase { mPreferences = new Preferences(context); int frameDelay = mPreferences.getInt(R.string.pref_key_frame_delay, 0); 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); + + if (mAudioOnlyMode) { + mAudioVisualizer = new AudioVisualizerController(); + mAnimationAutoEnabled = true; + } } else { mAnimationSync = new AnimationSyncController(0, false); } if (DEBUG) Log.d(TAG, "Capture: " + mCaptureWidth + "x" + mCaptureHeight + " @ " + mFrameRate + "fps"); + if (mAudioOnlyMode) Log.d(TAG, "Audio-only visualization mode enabled"); try { init(); @@ -177,11 +189,28 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase { mRunning = true; setCapturing(true); mFrameCount = 0; + + if (mAudioOnlyMode && mAudioVisualizer != null) { + mAudioVisualizer.start(); + if (mAnimationSync != null && mAnimationAutoEnabled) { + mAnimationSync.setAnimationEnabled(true); + } + } + mCaptureHandler.post(mCaptureRunnable); } private void captureFrame() { long frameStart = System.nanoTime(); + + if (mAudioOnlyMode && mAudioVisualizer != null) { + generateAudioVisualization(); + long captureTime = System.nanoTime() - frameStart; + mLastCaptureTimeNs = captureTime; + updateLoadTracking(captureTime); + return; + } + Image img = null; try { img = mImageReader.acquireLatestImage(); @@ -208,6 +237,27 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase { 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) { if (captureTimeNs > (long)(mFrameIntervalMs * 1_000_000 * DEADLINE_MISS_RATIO)) { @@ -389,6 +439,12 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase { mRunning = false; setCapturing(false); + if (mAudioVisualizer != null) { + mAudioVisualizer.stop(); + mAudioVisualizer.release(); + mAudioVisualizer = null; + } + if (mCaptureHandler != null) { mCaptureHandler.removeCallbacksAndMessages(null); } @@ -427,6 +483,9 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase { if (DEBUG) Log.i(TAG, "Pausing"); mRunning = false; setCapturing(false); + if (mAudioVisualizer != null) { + mAudioVisualizer.stop(); + } if (mCaptureHandler != null) { mCaptureHandler.removeCallbacksAndMessages(null); } diff --git a/common/src/main/java/com/hyperion/grabber/util/AudioVisualizerController.java b/common/src/main/java/com/hyperion/grabber/util/AudioVisualizerController.java new file mode 100644 index 0000000..91ff819 --- /dev/null +++ b/common/src/main/java/com/hyperion/grabber/util/AudioVisualizerController.java @@ -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; + } +} diff --git a/common/src/main/res/values/pref_values.xml b/common/src/main/res/values/pref_values.xml index fb6d157..c4af3eb 100644 --- a/common/src/main/res/values/pref_values.xml +++ b/common/src/main/res/values/pref_values.xml @@ -45,4 +45,7 @@ pref_key_enable_animation false + + pref_key_audio_only_mode + false diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index e2239ab..a34d0e3 100755 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -77,4 +77,6 @@ Delay frame updates in milliseconds to sync with RGB devices (-100 to +100ms) Enable Update Animation Sends a synchronized animation pattern to help align RGB timing + Audio Visualization Mode + Capture internal audio and visualize as gradient with audio-reactive sections diff --git a/common/src/main/res/xml/pref_general.xml b/common/src/main/res/xml/pref_general.xml index 88ea471..adf63a6 100644 --- a/common/src/main/res/xml/pref_general.xml +++ b/common/src/main/res/xml/pref_general.xml @@ -140,6 +140,12 @@ android:title="@string/pref_title_enable_animation" android:summary="@string/pref_summary_enable_animation" /> + +