Real-time CPU-based shader with audio reactivity. Available in both C++ (Windows) and Java (cross-platform).
Inspired by: Audio Reactive Shaders Tutorial and @xordev's graphics work
β οΈ Note: This is an experimental/learning implementation and not a perfect or production-ready shader. Performance and visual quality may vary.
This shader supports any WAV audio file. For best results, use bass-heavy electronic music.
π΅ Audio is embedded in the video.
π§ Track: Warriyo β Mortals (NoCopyrightSounds)
- Download music from official sources (music files not included in repo)
- Convert to WAV if needed:
ffmpeg -i input.mp3 -acodec pcm_s16le output.wav - Load in app with 'O' key
Note: This repository does not include copyrighted music. Users are responsible for ensuring they have rights to any audio files they use.
- Windows OS (uses Win32 API)
- Compiler: MinGW-w64 or Visual Studio
- Libraries: winmm, gdi32, comdlg32 (included with Windows SDK)
cd cpp
g++ -O3 -std=c++11 main.cpp -o shader.exe -lwinmm -lgdi32 -lcomdlg32 -mwindows- Create new Empty Project
- Add your
.cppfile to Source Files - Set configuration to Release and x64
- Build β Build Solution (
Ctrl+Shift+B) - Find
shader.exeinx64/Release/folder
@echo off
taskkill /F /IM shader.exe 2>nul
timeout /t 1 /nobreak >nul
g++ -O3 -std=c++11 main.cpp -o shader.exe -lwinmm -lgdi32 -lcomdlg32 -mwindows
if %errorlevel% == 0 (
echo Build successful!
shader.exe
)| Key | Action |
|---|---|
| O | Open WAV file |
| F | Toggle fullscreen |
| ESC | Exit fullscreen or close app |
| + | Increase FPS (+15) |
| - | Decrease FPS (-15) |
| [ | Decrease render scale (-0.1) |
| ] | Increase render scale (+0.1) |
- Format: WAV (PCM 16-bit)
- Channels: Mono or Stereo
- Sample Rate: Any (44100 Hz recommended)
- Start with 0.5x scale for good performance
- Lower scale = higher FPS
- Adjust FPS target based on your CPU
- Release builds run much faster than Debug
- Java 8+ (JDK or JRE)
- No external libraries needed
RealtimeShader.java # Main class
Vec2.java # (can be in same file)
Vec4.java # (can be in same file)
ShaderPanel.java # (can be in same file)
FileAudio.java # Audio playback class
cd javaAll classes can be in one file. Compile with:
javac RealtimeShader.java
java RealtimeShaderjavac *.java
java RealtimeShader# Compile
javac RealtimeShader.java
# Create JAR
jar cfe AudioShader.jar RealtimeShader *.class
# Run JAR
java -jar AudioShader.jar| Key | Action |
|---|---|
| O | Open audio file |
| F | Toggle fullscreen |
| ESC | Exit fullscreen or quit |
| + | Increase FPS (+15) |
| - | Decrease FPS (-15) |
| [ | Decrease render scale (-0.1) |
| ] | Increase render scale (+0.1) |
| 1 | Set FPS to 60 |
| 2 | Set FPS to 120 |
| 3 | Set FPS to 240 |
- File β Open Audio
- FPS β 60 / 120 / 240
- Scale β 0.25x / 0.5x / 0.75x / 1.0x
Java supports more formats via javax.sound.sampled:
- WAV (recommended)
- AIFF
- AU
- MP3 (with additional plugins)
- Use parallel streams (already enabled)
- Start with 0.5x scale
- Higher thread count = better performance
- Run with:
java -XX:+UseParallelGC RealtimeShader
- Fragment shader ported to CPU
- Multi-threaded rendering (one thread per row or parallel streams)
- Dynamic resolution based on scale setting
- Real-time frame generation at target FPS
- RMS (Root Mean Square) calculation for audio level
- Smoothing filter (92% old + 8% new) for stable animation
- Beat detection drives shader parameters:
- Iteration count
- Color intensity
- Pattern distortion
The shader creates fractal-like patterns using:
- Polar coordinate transformations
- Iterative sine/cosine feedback loops
- Exponential color mapping
- Hyperbolic tangent (tanh) for tone mapping
"Permission denied" error
# Kill running instance
taskkill /F /IM shader.exe
# Or compile to different name
g++ -O3 -std=c++11 main.cpp -o shader2.exe -lwinmm -lgdi32 -lcomdlg32 -mwindowsAudio keeps playing after closing
- Fixed in latest version with proper cleanup in
WM_CLOSEhandler
Choppy/laggy rendering
- Make sure you're using Release build, not Debug
- Lower the render scale with [ key
- Reduce FPS with - key
Audio doesn't play
# Check supported formats
java -cp . RealtimeShader
# If error occurs, convert audio to WAV PCM formatOutOfMemoryError
# Increase heap size
java -Xmx2G RealtimeShaderSlow performance
# Use parallel GC
java -XX:+UseParallelGC RealtimeShader
# Or G1GC for better latency
java -XX:+UseG1GC RealtimeShader- Resolution: 320Γ180 (scales with window)
- Render Scale: 0.5Γ (renders at 50% window size)
- FPS: 120 (C++), 120 (Java)
- Thread Count: CPU cores - 1 (C++), all cores (Java)
Both versions use precise frame timing:
- Sleep for most of frame time
- Busy-wait for last millisecond
- Ensures consistent frame rate
- C++: ~10-50 MB (depends on resolution)
- Java: ~100-200 MB (includes JVM overhead)
MIT License
Copyright (c) 2025
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Shader concept inspired by Audio Reactive Shaders Tutorial
- Graphics techniques from @xordev
- Built as a learning/experimental project
- Start at 0.5x scale for smooth 120 FPS
- Use bass-heavy music for best visual effects
- Toggle fullscreen (F) for immersive experience
- Adjust FPS to match your monitor refresh rate
- Experiment with scale - higher scale = more detail, lower FPS
Enjoy your audio-reactive visuals! π¨π΅
