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. | |
Manages a 1D OpenGL texture that holds the FFT frequency-magnitude spectrum.
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.
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:
where x = 0.0 is the DC bin and x = 1.0 is the Nyquist frequency.
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.
init() creates the texture; cleanup() deletes it. The destructor calls cleanup() automatically, so you never leak GPU resources.
Definition at line 2812 of file ACMX2/acmx.cpp.
|
inlineexplicit |
Definition at line 2814 of file ACMX2/acmx.cpp.
References analyzer.
|
inline |
Definition at line 2918 of file ACMX2/acmx.cpp.
References cleanup().
|
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).
| unit | Texture unit index (0-based). Default is 9. |
Definition at line 2900 of file ACMX2/acmx.cpp.
References SPECTRUM_TEXTURE_UNIT, and textureID.
|
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().
|
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.
|
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:
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.
|
inline |
Definition at line 2877 of file ACMX2/acmx.cpp.
References analyzer, bins, acmx2::audio::AudioAnalyzer::compute_spectrum(), scaled_buf, acmx2::audio::AudioAnalyzer::spectrum(), and textureID.
|
private |
Definition at line 2924 of file ACMX2/acmx.cpp.
Referenced by SpectrumTexture(), update(), and update().
|
private |
Number of texels (== FFT_SIZE / 2).
Definition at line 2926 of file ACMX2/acmx.cpp.
|
private |
Scratch buffer for sensitivity-scaled magnitudes.
Definition at line 2927 of file ACMX2/acmx.cpp.
Referenced by update().
|
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().
|
private |