Class thl::dsp::analysis::SpectrumAnalyser#

class SpectrumAnalyser : public thl::dsp::fft::StftAnalyser#

Inheritance diagram for thl::dsp::analysis::SpectrumAnalyser:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "1" [label="thl::dsp::analysis::SpectrumAnalyser" tooltip="thl::dsp::analysis::SpectrumAnalyser" fillcolor="#BFBFBF"] "2" [label="thl::dsp::fft::StftAnalyser" tooltip="thl::dsp::fft::StftAnalyser"] "1" -> "2" [dir=forward tooltip="public-inheritance"] }

Collaboration diagram for thl::dsp::analysis::SpectrumAnalyser:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "1" [label="thl::dsp::analysis::SpectrumAnalyser" tooltip="thl::dsp::analysis::SpectrumAnalyser" fillcolor="#BFBFBF"] "2" [label="thl::dsp::fft::StftAnalyser" tooltip="thl::dsp::fft::StftAnalyser"] "1" -> "2" [dir=forward tooltip="public-inheritance"] }

The curve of a spectrum analyser display: an EQ’s analyser, a mastering meter.

Frames come from an StftAnalyser (fed with push(), or drained from a SampleProbe with read_from()). Every frame makes one curve of num_points() values in dB, at frequencies() spaced evenly on a log axis from min_frequency to max_frequency:

  • The channels are averaged in power (a stereo signal reads as its mean).

  • Without smoothing, a point is the largest bin between it and its neighbours (a sine keeps its level however many bins fall on one point); where a point lies between two bins, the power between them. With fractional-octave smoothing, a point is the mean power over its band (a sine is spread over it).

  • Tilt: tilt_db_per_octave added per octave around 1 kHz; 4.5 dB/octave (the default) is how the ear weighs loudness, and shows pink noise about flat.

  • Speed: a point rises at once and falls by release_db_per_second at most (the display’s ballistics). Freeze: it does not fall at all, so the curve holds the maximum.

  • Below floor_db everything reads floor_db (the display’s range). Scaled as StftAnalyser: a full-scale sine at a bin’s frequency reads 0 dB, tilt 0.

Threads: everything on one thread, not the audio thread: curve_db() and frequencies() are views of what push(), read_from() and the setters rewrite (set_resolution() reallocates them). A UI on another thread copies the curve on this one. prepare() and set_resolution() allocate; push(), read_from(), the other setters and curve_db() do not.

Public Types

enum class Resolution : uint8_t#

The FFT size: Low 1024, Medium 2048, High 4096 (the default), Maximum 8192 points.

More points: finer at low frequencies, but fewer frames a second (a slower attack).

Values:

enumerator Low#
enumerator Medium#
enumerator High#
enumerator Maximum#

Public Functions

bool prepare(double sample_rate, size_t num_channels, const Settings &settings)#

Frames of the resolution’s size, four a frame (75 % overlap), for num_channels channels.

A maximum frequency above Nyquist is clamped to it. at sample_rate. False, and unprepared, for settings it cannot use.

bool prepare(double sample_rate, size_t num_channels)#

prepare() with the default Settings.

inline const Settings &settings() const#
bool set_resolution(Resolution resolution)#

Re-prepares at another FFT size (allocates); the curve starts over.

void set_tilt(float db_per_octave)#

Applies to the curve at once, frozen or not.

void set_smoothing(float octaves)#

The curve starts over (a band’s maximum and its mean do not convert).

void set_release(float db_per_second)#
inline void set_freeze(bool freeze)#

Frozen, the curve only rises; unfrozen it falls again from the maximum it held.

void set_floor(float floor_db)#
void read_from(thl::dsp::probe::SampleProbe &probe)#

Drains probe (its one reader) and feeds what it held.

After a discontinuity the frames start over (no frame spans the gap); the curve goes on falling from where it was. The probe must have num_channels() channels: otherwise nothing is read (an assert in debug builds).

inline std::span<const float> frequencies() const#

The frequency (Hz) of every point.

inline std::span<const float> curve_db() const#

The curve in dB, as of the last frame (floor_db() before the first).

inline size_t num_frames() const#

Frames made since prepare().

Protected Functions

virtual void process_spectrum(size_t channel, std::span<const std::complex<float>> bins) override#

One frame of channel channel: num_bins() bins, scaled as described above.

Called for every channel of a frame in turn.

struct Settings#

Public Members

Resolution m_resolution = Resolution::High#
Window m_window = Window::Hann#

BlackmanHarris for a range beyond 90 dB.

size_t m_num_points = 256#
float m_min_frequency = 20.0f#
float m_max_frequency = 20000.0f#
float m_tilt_db_per_octave = 4.5f#
float m_smoothing_octaves = 0.0f#

0 off, else e.g. 1/3, 1/6, 1/12, 1/24

float m_release_db_per_second = 30.0f#

the speed

float m_floor_db = -120.0f#
bool m_freeze = false#