ACMX 2.139.0
Dual-Backend Real-Time GPU Video Synthesis
Loading...
Searching...
No Matches
SpectrumTexture Class Reference

Manages a 1D OpenGL texture that holds the FFT frequency-magnitude spectrum. More...

Public Member Functions

void bind (int unit=SPECTRUM_TEXTURE_UNIT) const
 Bind the spectrum texture to a specific texture unit.
void cleanup ()
 Delete the OpenGL texture and reset state.
void init ()
 Create the 1D texture and set its sampling parameters.
 SpectrumTexture (acmx2::audio::AudioAnalyzer &analyzer)
void update ()
 Compute a fresh FFT and upload the magnitudes into the texture.
void update (float scale)
 ~SpectrumTexture ()

Static Public Attributes

static constexpr int SPECTRUM_TEXTURE_UNIT = 9
 Texture unit reserved for the spectrum (units 0–8 are taken).

Private Attributes

acmx2::audio::AudioAnalyzer & analyzer
int bins = 0
 Number of texels (== FFT_SIZE / 2).
std::vector< float > scaled_buf
 Scratch buffer for sensitivity-scaled magnitudes.
GLuint textureID = 0
 OpenGL name for the 1D texture.

Detailed Description

Manages a 1D OpenGL texture that holds the FFT frequency-magnitude spectrum.

What this class does

Every audio frame, the RtAudio callback captures raw PCM samples into a thread-safe analyzer buffer. On the render thread, update() computes a radix-2 FFT and uploads the resulting magnitude array into a GL_TEXTURE_1D so that any GLSL shader can sample it.

How the 1D texture works

A 1D texture is like a single-row image. Each texel (pixel) stores one float — the energy at that frequency bin. The texture is FFT_SIZE/2 texels wide (256 by default) because a real-valued FFT is symmetric and only the first half carries unique information.

The internal format is GL_R32F (one 32-bit float per texel, red channel only). In GLSL you read it with:

uniform sampler1D spectrum; // bound to texture unit 9
float energy = texture(spectrum, x).r; // x in [0,1]

where x = 0.0 is the DC bin and x = 1.0 is the Nyquist frequency.

Texture parameters

  • GL_LINEAR filtering — the GPU interpolates between adjacent bins, giving smooth results even when the shader samples at non-integer frequency positions.
  • GL_CLAMP_TO_EDGE wrapping — lookups outside [0,1] clamp to the nearest edge bin instead of wrapping or returning black.

Texture unit

The spectrum is bound to GL_TEXTURE9 (unit 9). Units 0 is the main video frame, units 1–8 are the temporal frame cache, so 9 is the first free slot.

RAII

init() creates the texture; cleanup() deletes it. The destructor calls cleanup() automatically, so you never leak GPU resources.

See also
acmx2::audio::AudioAnalyzer

Definition at line 2812 of file ACMX2/acmx.cpp.

Constructor & Destructor Documentation

◆ SpectrumTexture()

SpectrumTexture::SpectrumTexture ( acmx2::audio::AudioAnalyzer & analyzer)
inlineexplicit

Definition at line 2814 of file ACMX2/acmx.cpp.

References analyzer.

◆ ~SpectrumTexture()

SpectrumTexture::~SpectrumTexture ( )
inline

Definition at line 2918 of file ACMX2/acmx.cpp.

References cleanup().

Member Function Documentation

◆ bind()

void SpectrumTexture::bind ( int unit = SPECTRUM_TEXTURE_UNIT) const
inline

Bind the spectrum texture to a specific texture unit.

The caller passes a unit index (e.g. 9) and the corresponding GL_TEXTURE9 unit is activated. After this call the shader uniform spectrum should be set to the same unit index via glUniform1i(loc, unit).

Parameters
unitTexture unit index (0-based). Default is 9.

Definition at line 2900 of file ACMX2/acmx.cpp.

References SPECTRUM_TEXTURE_UNIT, and textureID.

◆ cleanup()

void SpectrumTexture::cleanup ( )
inline

Delete the OpenGL texture and reset state.

Safe to call more than once — the texture ID is checked before deletion and zeroed afterwards.

Definition at line 2911 of file ACMX2/acmx.cpp.

References textureID.

Referenced by ACView::~ACView(), and ~SpectrumTexture().

◆ init()

void SpectrumTexture::init ( )
inline

Create the 1D texture and set its sampling parameters.

Allocates a GL_TEXTURE_1D of width FFT_SIZE / 2 (256 texels) with the GL_R32F internal format (one float per texel). The initial texel data is nullptr — the texture is filled on the first update() call.

The two filter parameters (GL_LINEAR) tell the GPU to linearly interpolate when a shader samples between two bins, which avoids visible staircase artefacts. GL_CLAMP_TO_EDGE prevents wrap- around artefacts if a shader accidentally samples outside [0,1].

Definition at line 2829 of file ACMX2/acmx.cpp.

References bins, FFT_SIZE, and textureID.

◆ update() [1/2]

void SpectrumTexture::update ( )
inline

Compute a fresh FFT and upload the magnitudes into the texture.

This is the per-frame call that bridges the audio and GPU worlds:

  1. AudioAnalyzer reads the latest PCM snapshot, applies a Hann window, runs the radix-2 FFT, and writes the magnitudes.
  2. glTexSubImage1D() copies those magnitudes into the existing texture without reallocating — much cheaper than glTexImage1D every frame.

Call this once per frame from the render thread, before binding the texture for shader use.

Definition at line 2867 of file ACMX2/acmx.cpp.

References analyzer, bins, acmx2::audio::AudioAnalyzer::compute_spectrum(), acmx2::audio::AudioAnalyzer::spectrum(), and textureID.

◆ update() [2/2]

void SpectrumTexture::update ( float scale)
inline

Member Data Documentation

◆ analyzer

acmx2::audio::AudioAnalyzer& SpectrumTexture::analyzer
private

Definition at line 2924 of file ACMX2/acmx.cpp.

Referenced by SpectrumTexture(), update(), and update().

◆ bins

int SpectrumTexture::bins = 0
private

Number of texels (== FFT_SIZE / 2).

Definition at line 2926 of file ACMX2/acmx.cpp.

Referenced by init(), update(), and update().

◆ scaled_buf

std::vector<float> SpectrumTexture::scaled_buf
private

Scratch buffer for sensitivity-scaled magnitudes.

Definition at line 2927 of file ACMX2/acmx.cpp.

Referenced by update().

◆ SPECTRUM_TEXTURE_UNIT

int SpectrumTexture::SPECTRUM_TEXTURE_UNIT = 9
staticconstexpr

Texture unit reserved for the spectrum (units 0–8 are taken).

Definition at line 2921 of file ACMX2/acmx.cpp.

Referenced by bind(), ShaderLibrary::update(), ShaderLibrary::updateShaderUniforms(), and ShaderLibrary::updateShaderUniforms2D().

◆ textureID

GLuint SpectrumTexture::textureID = 0
private

OpenGL name for the 1D texture.

Definition at line 2925 of file ACMX2/acmx.cpp.

Referenced by bind(), cleanup(), init(), update(), and update().


The documentation for this class was generated from the following file: