Files
beatglow/app/shader/effect.go
2026-10-29 17:29:54 +01:00

469 lines
11 KiB
Go

package shader
import "math"
// Effect defines the interface for shader effects.
type Effect interface {
Apply(x, y, width, height int, samples []float64) (r, g, b float64)
}
type AudioIntensityBarsEffect struct{}
func (e *AudioIntensityBarsEffect) Apply(x, y, width, height int, sample []float64) (r, g, b float64) {
// Calculate the average amplitude across all channels
amplitude := 0.0
for _, sample := range sample {
amplitude += math.Abs(sample)
}
amplitude /= float64(len(sample))
// Map the amplitude to bar height
barHeight := int(amplitude * float64(height))
// If the current y is within the bar height, light up the pixel
if y < barHeight {
intensity := 1.0 - (float64(y) / float64(barHeight))
r = intensity
g = intensity
b = intensity
}
return r, g, b
}
type StereoColorGradientEffect struct{}
func (e *StereoColorGradientEffect) Apply(x, y, _, _ int, sample []float64) (r, g, b float64) {
left := math.Abs(sample[0])
right := math.Abs(sample[1])
r = left
g = right
b = (left + right) / 2.0
return r, g, b
}
type StereoRippleEffect struct{}
func (e *StereoRippleEffect) Apply(x, y, width, height int, sample []float64) (r, g, b float64) {
centerX := width / 2
centerY := height / 2
dx := float64(x - centerX)
dy := float64(y - centerY)
distance := math.Sqrt(dx*dx + dy*dy)
// Use the difference between left and right channels for ripple intensity
ripple := math.Sin(distance*0.1 - (sample[0]-sample[1])*10.0)
ripple = (ripple + 1.0) / 2.0 // Normalize to [0, 1]
r = ripple
g = ripple * 0.5
b = ripple * 0.2
return r, g, b
}
type StereoSpectrumBarsEffect struct{}
func (e *StereoSpectrumBarsEffect) Apply(x, y, width, height int, sample []float64) (r, g, b float64) {
barWidth := 10
barIndex := x / barWidth
// Use left channel for even bars, right channel for odd bars
var amplitude float64
if barIndex%2 == 0 {
amplitude = math.Abs(sample[0])
} else {
amplitude = math.Abs(sample[1])
}
barHeight := int(amplitude * float64(barWidth))
if y < barHeight {
r = 1.0
g = 0.5
b = 0.0
}
return r, g, b
}
type ChannelSeparationEffect struct {
}
func (e *ChannelSeparationEffect) Apply(x, y, width, _ int, sample []float64) (r, g, b float64) {
// Split the screen vertically for stereo separation
if len(sample) > 1 {
if x < width/2 {
// Left channel controls the left side
r = math.Abs(sample[0])
g = 0.0
b = 0.0
} else {
// Right channel controls the right side
r = 0.0
g = math.Abs(sample[1])
b = 0.0
}
} else {
// Mono audio: use the same value for both sides
r = math.Abs(sample[0])
g = math.Abs(sample[0])
b = math.Abs(sample[0])
}
return r, g, b
}
type StereoWaveformEffect struct{}
func (e *StereoWaveformEffect) Apply(x, y, _, _ int, sample []float64) (r, g, b float64) {
// Map the audio sample to the y-axis for each channel
if len(sample) > 1 {
leftY := int((sample[0] + 1.0) / 2.0 * float64(64))
rightY := int((sample[1] + 1.0) / 2.0 * float64(64))
// Left channel (red)
if math.Abs(float64(y)-float64(leftY)) < 2 {
r = 1.0
}
// Right channel (green)
if math.Abs(float64(y)-float64(rightY)) < 2 {
g = 1.0
}
} else {
// Mono audio: single waveform
waveY := int((sample[0] + 1.0) / 2.0 * float64(64))
if math.Abs(float64(y)-float64(waveY)) < 2 {
r = 1.0
g = 1.0
b = 1.0
}
}
return r, g, b
}
type StereoSpectrumEffect struct {
BarHeight int
}
func (e *StereoSpectrumEffect) Apply(x, y, _, _ int, sample []float64) (r, g, b float64) {
// Divide the screen into horizontal bars for each channel
barHeight := e.BarHeight
if barHeight < 1 {
barHeight = 8
}
barIndex := y / barHeight
if len(sample) > 1 {
if barIndex%2 == 0 {
// Left channel (red)
r = math.Abs(sample[0])
} else {
// Right channel (green)
g = math.Abs(sample[1])
}
} else {
// Mono audio: use the same value for all bars
r = math.Abs(sample[0])
g = math.Abs(sample[0])
b = math.Abs(sample[0])
}
return r, g, b
}
type StereoRadialEffect struct {
}
func (e *StereoRadialEffect) Apply(x, y, width, height int, sample []float64) (r, g, b float64) {
// Center of the screen
centerX := width / 2
centerY := height / 2
// Distance and angle from the center
dx := float64(x - centerX)
dy := float64(y - centerY)
distance := math.Sqrt(dx*dx + dy*dy)
angle := math.Atan2(dy, dx)
if len(sample) > 1 {
// Left channel controls the top half, right channel controls the bottom half
if angle > 0 {
r = math.Abs(sample[0]) * (1.0 - distance/128.0)
} else {
g = math.Abs(sample[1]) * (1.0 - distance/128.0)
}
} else {
// Mono audio: uniform radial effect
r = math.Abs(sample[0]) * (1.0 - distance/128.0)
g = math.Abs(sample[0]) * (1.0 - distance/128.0)
b = math.Abs(sample[0]) * (1.0 - distance/128.0)
}
return r, g, b
}
type StereoPulseEffect struct{}
func (e *StereoPulseEffect) Apply(x, y, _, _ int, sample []float64) (r, g, b float64) {
if len(sample) > 1 {
// Left channel controls horizontal pulsation
r = 0.5 + 0.5*math.Sin(float64(x)*0.1+sample[0]*10.0)
// Right channel controls vertical pulsation
g = 0.5 + 0.5*math.Sin(float64(y)*0.1+sample[1]*10.0)
} else {
// Mono audio: uniform pulsation
pulse := 0.5 + 0.5*math.Sin(float64(x+y)*0.1+sample[0]*10.0)
r = pulse
g = pulse
b = pulse
}
return r, g, b
}
type StereoSwirlEffect struct {
}
func (e *StereoSwirlEffect) Apply(x, y, width, height int, sample []float64) (r, g, b float64) {
// Center of the screen
centerX := width / 2
centerY := height / 2
// Distance and angle from the center
dx := float64(x - centerX)
dy := float64(y - centerY)
angle := math.Atan2(dy, dx)
if len(sample) > 1 {
// Left channel controls clockwise swirl, right channel controls counterclockwise swirl
swirl := math.Sin(angle+sample[0]*10.0) + math.Cos(angle-sample[1]*10.0)
r = (swirl + 1.0) / 2.0
g = (1.0 - swirl) / 2.0
b = 0.5
} else {
// Mono audio: uniform swirl
swirl := math.Sin(angle + sample[0]*10.0)
r = (swirl + 1.0) / 2.0
g = (1.0 - swirl) / 2.0
b = 0.5
}
return r, g, b
}
type StereoMirrorEffect struct {
Width int
}
func (e *StereoMirrorEffect) Apply(x, y, _, _ int, sample []float64) (r, g, b float64) {
if len(sample) > 1 {
if x < e.Width/2 {
// Left channel controls the left side
r = math.Abs(sample[0])
} else {
// Right channel controls the mirrored right side
r = math.Abs(sample[1])
}
} else {
// Mono audio: uniform mirror
r = math.Abs(sample[0])
}
g = r
b = r
return r, g, b
}
type PulsatingCirclesEffect struct {
}
func (e *PulsatingCirclesEffect) Apply(x, y, width, height int, sample []float64) (r, g, b float64) {
// Center of the screen
centerX := width / 2
centerY := height / 2
// Distance from the center
dx := float64(x - centerX)
dy := float64(y - centerY)
distance := math.Sqrt(dx*dx + dy*dy)
// Calculate the average amplitude across all channels
amplitude := 0.0
for _, sample := range sample {
amplitude += math.Abs(sample)
}
amplitude /= float64(len(sample))
// Map the amplitude to the circle radius
radius := amplitude * 64.0
// Light up pixels near the circle boundary
if math.Abs(distance-radius) < 2 {
intensity := 1.0
r = intensity
g = intensity
b = intensity
}
return r, g, b
}
type AudioNoiseEffect struct{}
func (e *AudioNoiseEffect) Apply(x, y, _, _ int, sample []float64) (r, g, b float64) {
// Calculate the average amplitude across all channels
amplitude := 0.0
for _, sample := range sample {
amplitude += math.Abs(sample)
}
amplitude /= float64(len(sample))
// Generate noise based on the amplitude
noise := math.Sin(float64(x*y)) * amplitude
intensity := (noise + 1.0) / 2.0
r = intensity
g = intensity
b = intensity
return r, g, b
}
type AudioRipplesEffect struct{}
func (e *AudioRipplesEffect) Apply(x, y, width, height int, sample []float64) (r, g, b float64) {
// Center of the screen
centerX := width / 2
centerY := height / 2
// Distance from the center
dx := float64(x - centerX)
dy := float64(y - centerY)
distance := math.Sqrt(dx*dx + dy*dy)
// Calculate the average amplitude across all channels
amplitude := 0.0
for _, sample := range sample {
amplitude += math.Abs(sample)
}
amplitude /= float64(len(sample))
// Create ripples based on the amplitude
ripple := math.Sin(distance*0.1 - amplitude*10.0)
intensity := (ripple + 1.0) / 2.0
r = intensity
g = intensity
b = intensity
return r, g, b
}
type AudioScanlinesEffect struct{}
func (e *AudioScanlinesEffect) Apply(x, y, _, _ int, sample []float64) (r, g, b float64) {
// Calculate the average amplitude across all channels
amplitude := 0.0
for _, sample := range sample {
amplitude += math.Abs(sample)
}
amplitude /= float64(len(sample))
// Alternate scanlines based on the amplitude
if y%2 == 0 {
intensity := amplitude
r = intensity
g = intensity
b = intensity
} else {
intensity := 1.0 - amplitude
r = intensity
g = intensity
b = intensity
}
return r, g, b
}
type MoireRotatingZoomerEffect struct {
Time float64 // Time counter for animation
}
func (e *MoireRotatingZoomerEffect) Apply(x, y, width, height int, sample []float64) (r, g, b float64) {
// Center of the screen
centerX := float64(width) / 2.0
centerY := float64(height) / 2.0
// Normalized coordinates relative to the center
dx := float64(x) - centerX
dy := float64(y) - centerY
// Calculate the average amplitude across all channels
amplitude := 0.0
for _, sample := range sample {
amplitude += math.Abs(sample)
}
amplitude /= float64(len(sample))
// Use amplitude to modulate rotation speed and zoom level
rotationSpeed := 0.5 + amplitude*2.0 // Speed increases with amplitude
zoomLevel := 1.0 + amplitude*2.0 // Zoom increases with amplitude
// Update time for animation
e.Time += 0.01 * rotationSpeed
// Rotate and zoom the coordinates
angle := e.Time
cosA := math.Cos(angle)
sinA := math.Sin(angle)
rotatedX := dx*cosA - dy*sinA
rotatedY := dx*sinA + dy*cosA
// Apply zoom
rotatedX /= zoomLevel
rotatedY /= zoomLevel
// Create a Moiré pattern using sine waves
moire := math.Sin(rotatedX*0.1) * math.Sin(rotatedY*0.1)
// Map the Moiré pattern to gray scale intensity
intensity := (moire + 1.0) / 2.0 // Normalize to [0, 1]
r = intensity
g = intensity
b = intensity
return r, g, b
}
type RadialAudioIntensityEffect struct{}
func (e *RadialAudioIntensityEffect) Apply(x, y, width, height int, audioSample []float64) (r, g, b float64) {
// Center of the screen
centerX := float64(width) / 2.0
centerY := float64(height) / 2.0
// Distance from the center
dx := float64(x) - centerX
dy := float64(y) - centerY
distance := math.Sqrt(dx*dx + dy*dy)
// Calculate the average amplitude across all channels
amplitude := 0.0
for _, sample := range audioSample {
amplitude += math.Abs(sample)
}
amplitude /= float64(len(audioSample))
// Map the amplitude to the radial intensity
intensity := amplitude * (1.0 - distance/float64(width/2))
r = intensity
g = intensity
b = intensity
return r, g, b
}
var (
_ Effect = (*AudioIntensityBarsEffect)(nil)
_ Effect = (*AudioNoiseEffect)(nil)
_ Effect = (*AudioRipplesEffect)(nil)
_ Effect = (*AudioScanlinesEffect)(nil)
_ Effect = (*MoireRotatingZoomerEffect)(nil)
_ Effect = (*PulsatingCirclesEffect)(nil)
_ Effect = (*RadialAudioIntensityEffect)(nil)
_ Effect = (*StereoColorGradientEffect)(nil)
_ Effect = (*StereoMirrorEffect)(nil)
_ Effect = (*StereoPulseEffect)(nil)
_ Effect = (*StereoRadialEffect)(nil)
_ Effect = (*StereoSpectrumBarsEffect)(nil)
_ Effect = (*StereoSwirlEffect)(nil)
_ Effect = (*StereoWaveformEffect)(nil)
)