From b95a50dec69be207e9815b3302f73fd61334544a Mon Sep 17 00:00:00 2001 From: Louis Dalibard Date: Thu, 16 Jan 2025 22:19:56 +0100 Subject: [PATCH] yes --- config/config.go | 5 +- main.go | 3 + signalprocessing/signalprocessing.go | 14 ++ .../mjibson/go-dsp/window/window.go | 152 ++++++++++++++++++ vendor/modules.txt | 1 + 5 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 signalprocessing/signalprocessing.go create mode 100644 vendor/github.com/mjibson/go-dsp/window/window.go diff --git a/config/config.go b/config/config.go index 9e35efd..e83a58b 100644 --- a/config/config.go +++ b/config/config.go @@ -76,6 +76,8 @@ var watcher *fsnotify.Watcher var SingleChannel bool = false +var HannWindow []float64 + func Init() { configPath := configdir.LocalConfig("ontake", "xyosc") err := configdir.MakePath(configPath) // Ensure it exists. @@ -101,8 +103,9 @@ func Init() { decoder := yaml.NewDecoder(fh) decoder.Decode(&Config) - SingleChannel = Config.DefaultToSingleChannel + } + SingleChannel = Config.DefaultToSingleChannel // Get pywal accent color watcher, err = fsnotify.NewWatcher() diff --git a/main.go b/main.go index 6a3ecb1..309fe8f 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "xyosc/icons" "xyosc/media" "xyosc/particles" + "xyosc/signalprocessing" "fmt" @@ -26,6 +27,7 @@ import ( "github.com/hajimehoshi/ebiten/v2/vector" "github.com/mjibson/go-dsp/fft" + "github.com/mjibson/go-dsp/window" ) type Game struct { @@ -99,6 +101,7 @@ func (g *Game) Draw(screen *ebiten.Image) { binary.Read(audio.SampleRingBuffer, binary.NativeEndian, &AX) binary.Read(audio.SampleRingBuffer, binary.NativeEndian, &AY) } + FFTBuffer = window.Apply(FFTBuffer, signalprocessing.HannWindow) X := fft.FFTReal(FFTBuffer) r, θ := cmplx.Polar(X[1]) maxR := r diff --git a/signalprocessing/signalprocessing.go b/signalprocessing/signalprocessing.go new file mode 100644 index 0000000..9553163 --- /dev/null +++ b/signalprocessing/signalprocessing.go @@ -0,0 +1,14 @@ +package signalprocessing + +import ( + "xyosc/audio" + "xyosc/config" + + "github.com/mjibson/go-dsp/window" +) + +var HannWindow []float64 + +func Init() { + HannWindow = window.Hann(config.Config.ReadBufferSize / audio.SampleSizeInBytes / 2) +} diff --git a/vendor/github.com/mjibson/go-dsp/window/window.go b/vendor/github.com/mjibson/go-dsp/window/window.go new file mode 100644 index 0000000..25632d9 --- /dev/null +++ b/vendor/github.com/mjibson/go-dsp/window/window.go @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2012 Matt Jibson + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +// Package window provides window functions for digital signal processing. +package window + +import ( + "math" +) + +// Apply applies the window windowFunction to x. +func Apply(x []float64, windowFunction func(int) []float64) { + for i, w := range windowFunction(len(x)) { + x[i] *= w + } +} + +// Rectangular returns an L-point rectangular window (all values are 1). +func Rectangular(L int) []float64 { + r := make([]float64, L) + + for i := range r { + r[i] = 1 + } + + return r +} + +// Hamming returns an L-point symmetric Hamming window. +// Reference: http://www.mathworks.com/help/signal/ref/hamming.html +func Hamming(L int) []float64 { + r := make([]float64, L) + + if L == 1 { + r[0] = 1 + } else { + N := L - 1 + coef := math.Pi * 2 / float64(N) + for n := 0; n <= N; n++ { + r[n] = 0.54 - 0.46*math.Cos(coef*float64(n)) + } + } + + return r +} + +// Hann returns an L-point Hann window. +// Reference: http://www.mathworks.com/help/signal/ref/hann.html +func Hann(L int) []float64 { + r := make([]float64, L) + + if L == 1 { + r[0] = 1 + } else { + N := L - 1 + coef := 2 * math.Pi / float64(N) + for n := 0; n <= N; n++ { + r[n] = 0.5 * (1 - math.Cos(coef*float64(n))) + } + } + + return r +} + +// Bartlett returns an L-point Bartlett window. +// Reference: http://www.mathworks.com/help/signal/ref/bartlett.html +func Bartlett(L int) []float64 { + r := make([]float64, L) + + if L == 1 { + r[0] = 1 + } else { + N := L - 1 + coef := 2 / float64(N) + n := 0 + for ; n <= N/2; n++ { + r[n] = coef * float64(n) + } + for ; n <= N; n++ { + r[n] = 2 - coef*float64(n) + } + } + + return r +} + +// FlatTop returns an L-point flat top window. +// Reference: http://www.mathworks.com/help/signal/ref/flattopwin.html +func FlatTop(L int) []float64 { + const ( + alpha0 = float64(0.21557895) + alpha1 = float64(0.41663158) + alpha2 = float64(0.277263158) + alpha3 = float64(0.083578947) + alpha4 = float64(0.006947368) + ) + + r := make([]float64, L) + + if L == 1 { + r[0] = 1 + return r + } + + N := L - 1 + coef := 2 * math.Pi / float64(N) + + for n := 0; n <= N; n++ { + factor := float64(n) * coef + + term0 := alpha0 + term1 := alpha1 * math.Cos(factor) + term2 := alpha2 * math.Cos(2*factor) + term3 := alpha3 * math.Cos(3*factor) + term4 := alpha4 * math.Cos(4*factor) + + r[n] = term0 - term1 + term2 - term3 + term4 + } + + return r +} + +// Blackman returns an L-point Blackman window +// Reference: http://www.mathworks.com/help/signal/ref/blackman.html +func Blackman(L int) []float64 { + r := make([]float64, L) + if L == 1 { + r[0] = 1 + } else { + N := L - 1 + for n := 0; n <= N; n++ { + const term0 = 0.42 + term1 := -0.5 * math.Cos(2*math.Pi*float64(n)/float64(N)) + term2 := 0.08 * math.Cos(4*math.Pi*float64(n)/float64(N)) + r[n] = term0 + term1 + term2 + } + } + return r +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 13df1c0..f3531bc 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -108,6 +108,7 @@ github.com/leberKleber/go-mpris ## explicit github.com/mjibson/go-dsp/dsputils github.com/mjibson/go-dsp/fft +github.com/mjibson/go-dsp/window # github.com/smallnest/ringbuffer v0.0.0-20241129171057-356c688ba81d ## explicit; go 1.19 github.com/smallnest/ringbuffer