Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ jobs:
- name: Configure CMake
run: |
mkdir -p build && cd build
cmake -G Ninja -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} ..
cmake -G Ninja \
-DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \
-DBUILD_TESTING=ON \
-DENABLE_COVERAGE=ON \
..

- name: Build project
run: |
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ set(MATRIX_SOURCES

set(MATRIX_HEADERS
src/main.h
src/math_utils.h
src/tracyprofiler.h
src/stb_image.h
${kissfft_SOURCE_DIR}/kiss_fft.h
Expand Down
31 changes: 1 addition & 30 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "main.h"
#include "math_utils.h"

#include <regex>

Expand All @@ -19,7 +20,6 @@

#include <algorithm>
#include <chrono>
#include <cmath>
#include <memory>

#define SMOOTHING_TIME_CONSTANT (0.5f) // default 0.8
Expand Down Expand Up @@ -673,35 +673,6 @@ GLuint CVisualizationMatrix::CreateTexture(const std::string& file, GLint intern
return texture;
}

float CVisualizationMatrix::BlackmanWindow(float in, size_t i, size_t length)
{
constexpr double alpha = 0.16;
constexpr double a0 = 0.5 * (1.0 - alpha);
constexpr double a1 = 0.5;
constexpr double a2 = 0.5 * alpha;

const float x = static_cast<float>(i) / static_cast<float>(length);
return in * static_cast<float>(a0 - a1 * std::cos(2.0 * M_PI * x) + a2 * std::cos(4.0 * M_PI * x));
}

void CVisualizationMatrix::SmoothingOverTime(std::vector<float>& outputBuffer, const std::vector<float>& lastOutputBuffer,
kiss_fft_cpx* inputBuffer, size_t length, float smoothingTimeConstant, unsigned int fftSize)
{
for (size_t i = 0; i < length; i++)
{
const kiss_fft_cpx c = inputBuffer[i];
const float magnitude = std::sqrt(c.r * c.r + c.i * c.i) / static_cast<float>(fftSize);
outputBuffer[i] = smoothingTimeConstant * lastOutputBuffer[i] + (1.0f - smoothingTimeConstant) * magnitude;
}
}

float CVisualizationMatrix::LinearToDecibels(float linear)
{
if (!linear)
return -1000.0f;
return 20.0f * std::log10(linear);
}

int CVisualizationMatrix::DetermineBitsPrecision()
{
m_state.fbwidth = 32;
Expand Down
10 changes: 1 addition & 9 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <glm/gtc/type_ptr.hpp>

#include <kissfft/kiss_fft.h>
#include "math_utils.h"

#include <vector>
#include <string>
Expand Down Expand Up @@ -56,18 +57,9 @@ class ATTRIBUTE_HIDDEN CVisualizationMatrix
GLuint CreateTexture(GLint format, unsigned int w, unsigned int h, const GLvoid* data);
GLuint CreateTexture(const GLvoid* data, GLint format, unsigned int w, unsigned int h, GLint internalFormat, GLint scaling, GLint repeat);
GLuint CreateTexture(const std::string& file, GLint internalFormat, GLint scaling, GLint repeat);
float BlackmanWindow(float in, size_t i, size_t length);
void SmoothingOverTime(std::vector<float>& outputBuffer, const std::vector<float>& lastOutputBuffer,
kiss_fft_cpx* inputBuffer, size_t length, float smoothingTimeConstant, unsigned int fftSize);
float LinearToDecibels(float linear);
int DetermineBitsPrecision();
bool UpdateAlbumart();
void GatherDefines();

// FFT configuration
static constexpr size_t AUDIO_BUFFER = 1024;
static constexpr size_t NUM_BANDS = AUDIO_BUFFER / 2;

// Audio processing
kiss_fft_cfg m_kissCfg;
std::vector<GLubyte> m_audioData;
Expand Down
62 changes: 62 additions & 0 deletions src/math_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2005-2021 Team Kodi <https://kodi.tv>
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSE.md for more information.
*/

#pragma once

#include <cmath>
#include <vector>

// FFT configuration constants
static constexpr size_t AUDIO_BUFFER = 1024;
static constexpr size_t NUM_BANDS = AUDIO_BUFFER / 2;

struct kiss_fft_cpx
{
float r;
float i;
};

/**
* Apply Blackman window function to a sample.
* The Blackman window reduces spectral leakage in FFT analysis.
*/
inline float BlackmanWindow(float in, size_t i, size_t length)
{
constexpr double alpha = 0.16;
constexpr double a0 = 0.5 * (1.0 - alpha);
constexpr double a1 = 0.5;
constexpr double a2 = 0.5 * alpha;

const float x = static_cast<float>(i) / static_cast<float>(length);
return in * static_cast<float>(a0 - a1 * std::cos(2.0 * M_PI * x) + a2 * std::cos(4.0 * M_PI * x));
}

/**
* Convert linear amplitude to decibels.
* Returns -1000 dB for zero input (effectively negative infinity).
*/
inline float LinearToDecibels(float linear)
{
if (!linear)
return -1000.0f;
return 20.0f * std::log10(linear);
}

/**
* Apply exponential smoothing to magnitude values over time.
* This smooths the FFT output to reduce flickering in the visualization.
*/
inline void SmoothingOverTime(std::vector<float>& outputBuffer, const std::vector<float>& lastOutputBuffer,
kiss_fft_cpx* inputBuffer, size_t length, float smoothingTimeConstant, unsigned int fftSize)
{
for (size_t i = 0; i < length; i++)
{
const kiss_fft_cpx c = inputBuffer[i];
const float magnitude = std::sqrt(c.r * c.r + c.i * c.i) / static_cast<float>(fftSize);
outputBuffer[i] = smoothingTimeConstant * lastOutputBuffer[i] + (1.0f - smoothingTimeConstant) * magnitude;
}
}
52 changes: 5 additions & 47 deletions tests/test_visualization_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,8 @@
#include <cmath>
#include <vector>

// Include the functions to test from main.cpp
// We need to extract the pure functions that don't depend on OpenGL/Kodi

// --- Copied from main.cpp for testing ---

#define AUDIO_BUFFER (1024)
#define NUM_BANDS (AUDIO_BUFFER / 2)

struct kiss_fft_cpx
{
float r;
float i;
};

// BlackmanWindow function (copied from main.cpp)
float BlackmanWindow(float in, size_t i, size_t length)
{
double alpha = 0.16;
double a0 = 0.5 * (1.0 - alpha);
double a1 = 0.5;
double a2 = 0.5 * alpha;

float x = (float)i / (float)length;
return in * (a0 - a1 * cos(2.0 * M_PI * x) + a2 * cos(4.0 * M_PI * x));
}

// LinearToDecibels function (copied from main.cpp)
float LinearToDecibels(float linear)
{
if (!linear)
return -1000;
return 20 * log10f(linear);
}

// SmoothingOverTime function (copied from main.cpp)
void SmoothingOverTime(float* outputBuffer, float* lastOutputBuffer, kiss_fft_cpx* inputBuffer, size_t length, float smoothingTimeConstant, unsigned int fftSize)
{
for (size_t i = 0; i < length; i++)
{
kiss_fft_cpx c = inputBuffer[i];
float magnitude = sqrt(c.r * c.r + c.i * c.i) / (float)fftSize;
outputBuffer[i] = smoothingTimeConstant * lastOutputBuffer[i] + (1.0 - smoothingTimeConstant) * magnitude;
}
}
// Include the actual math utilities
#include "../src/math_utils.h"

// --- Tests ---

Expand Down Expand Up @@ -152,7 +110,7 @@ TEST_F(VisualizationMatrixTest, SmoothingOverTime_ZeroInput)
inputBuffer[i].i = 0.0f;
}

SmoothingOverTime(outputBuffer.data(), lastOutputBuffer.data(), inputBuffer.data(), NUM_BANDS, 0.5f, AUDIO_BUFFER);
SmoothingOverTime(outputBuffer, lastOutputBuffer, inputBuffer.data(), NUM_BANDS, 0.5f, AUDIO_BUFFER);

for (size_t i = 0; i < NUM_BANDS; i++)
{
Expand All @@ -174,7 +132,7 @@ TEST_F(VisualizationMatrixTest, SmoothingOverTime_NonZeroInput)
inputBuffer[i].i = 0.0f;
}

SmoothingOverTime(outputBuffer.data(), lastOutputBuffer.data(), inputBuffer.data(), NUM_BANDS, 0.0f, AUDIO_BUFFER);
SmoothingOverTime(outputBuffer, lastOutputBuffer, inputBuffer.data(), NUM_BANDS, 0.0f, AUDIO_BUFFER);

for (size_t i = 0; i < NUM_BANDS; i++)
{
Expand All @@ -198,7 +156,7 @@ TEST_F(VisualizationMatrixTest, SmoothingOverTime_FullSmoothing)
}

// With smoothingTimeConstant = 1.0, output should be: 1.0 * lastOutputBuffer[i] + 0 * magnitude = lastOutputBuffer[i]
SmoothingOverTime(outputBuffer.data(), lastOutputBuffer.data(), inputBuffer.data(), NUM_BANDS, 1.0f, AUDIO_BUFFER);
SmoothingOverTime(outputBuffer, lastOutputBuffer, inputBuffer.data(), NUM_BANDS, 1.0f, AUDIO_BUFFER);

for (size_t i = 0; i < NUM_BANDS; i++)
{
Expand Down
Loading