libMusicForge
Loading...
Searching...
No Matches
musicforge.analysis.analyzer.MusicAnalyzer Class Reference

Analyze normalized mono PCM using a Hann-windowed real FFT. More...

Inheritance diagram for musicforge.analysis.analyzer.MusicAnalyzer:
musicforge.analysis.interfaces.MusicAnalyzerIf

Public Member Functions

None __init__ (self, *, int fft_size=2048, int band_count=24)
 Initialize the configured instance.
bool is_zeroized (self)
 Report whether ambient calibration is active.
None start_zeroize (self)
 Begin collecting ambient noise frames.
None finish_zeroize (self)
 Commit collected ambient noise calibration; requires at least one frame.
None clear_zeroize (self)
 Discard calibration and reset adaptive state.
MusicAnalysisState analyze (self, Sequence[float] samples, int sample_rate_hz)
 Analyze normalized mono PCM into spectrum and activity estimates.

Public Attributes

 fft_size = fft_size
 band_count = band_count

Protected Member Functions

float _beat (self, np.ndarray magnitude, float duration_s)
np.ndarray _gate_ambient (self, np.ndarray magnitude)
np.ndarray _gate_spectral_leakage (self, np.ndarray magnitude)
None _reset_adaptive_state (self)
float _normalized_band (self, np.ndarray magnitude, np.ndarray frequencies, float low_hz, float high_hz, str key)
tuple[float,...] _spectrum (self, np.ndarray magnitude, np.ndarray frequencies, int sample_rate_hz)
float _activity (self, str name, float raw, float transient_weight)
PercussionActivity _percussion (self, np.ndarray magnitude, np.ndarray frequencies)

Static Protected Member Functions

float _band_rms (np.ndarray magnitude, np.ndarray frequencies, float low_hz, float high_hz)

Protected Attributes

 _window = np.hanning(fft_size).astype(np.float32)
 _band_peaks = np.full(band_count, 1e-6, dtype=np.float32)
dict _summary_peaks = {"bass": 1e-6, "mid": 1e-6, "treble": 1e-6}
dict _activity_peaks = {name: 1e-6 for name in activity_names}
dict _previous_activity = {name: 0.0 for name in activity_names}
np.ndarray|None _previous_magnitude = None
float _beat_envelope = 0.0
float _flux_average = 0.0
float _beat_age = 1.0
bool _zeroize_collecting = False
list _zeroize_frames = []
np.ndarray|None _noise_floor = None

Static Protected Attributes

float _ZEROIZE_PERCENTILE = 75.0
float _ZEROIZE_MARGIN_DB = 3.0
float _SPECTRAL_RELATIVE_FLOOR_DB = -42.0

Detailed Description

Analyze normalized mono PCM using a Hann-windowed real FFT.

Constructor & Destructor Documentation

◆ __init__()

None musicforge.analysis.analyzer.MusicAnalyzer.__init__ ( self,
* ,
int fft_size = 2048,
int band_count = 24 )

Initialize the configured instance.

Parameters
selfCurrent instance.
fft_sizeFFT length, a power of two of at least 256.
band_countNumber of spectrum bands, at least three.

Member Function Documentation

◆ _activity()

float musicforge.analysis.analyzer.MusicAnalyzer._activity ( self,
str name,
float raw,
float transient_weight )
protected

◆ _band_rms()

float musicforge.analysis.analyzer.MusicAnalyzer._band_rms ( np.ndarray magnitude,
np.ndarray frequencies,
float low_hz,
float high_hz )
staticprotected

◆ _beat()

float musicforge.analysis.analyzer.MusicAnalyzer._beat ( self,
np.ndarray magnitude,
float duration_s )
protected
Estimate onsets via positive spectral flux, not tempo or instrument identity.

◆ _gate_ambient()

np.ndarray musicforge.analysis.analyzer.MusicAnalyzer._gate_ambient ( self,
np.ndarray magnitude )
protected

◆ _gate_spectral_leakage()

np.ndarray musicforge.analysis.analyzer.MusicAnalyzer._gate_spectral_leakage ( self,
np.ndarray magnitude )
protected
Suppress bins far below the strongest spectral component.

A Hann window greatly reduces FFT leakage, but it does not eliminate it.
Without a floor, adaptive per-band normalization can promote tiny leakage
in an otherwise empty band to full-scale activity on the first frame.

◆ _normalized_band()

float musicforge.analysis.analyzer.MusicAnalyzer._normalized_band ( self,
np.ndarray magnitude,
np.ndarray frequencies,
float low_hz,
float high_hz,
str key )
protected

◆ _percussion()

PercussionActivity musicforge.analysis.analyzer.MusicAnalyzer._percussion ( self,
np.ndarray magnitude,
np.ndarray frequencies )
protected

◆ _reset_adaptive_state()

None musicforge.analysis.analyzer.MusicAnalyzer._reset_adaptive_state ( self)
protected

◆ _spectrum()

tuple[float, ...] musicforge.analysis.analyzer.MusicAnalyzer._spectrum ( self,
np.ndarray magnitude,
np.ndarray frequencies,
int sample_rate_hz )
protected

◆ analyze()

MusicAnalysisState musicforge.analysis.analyzer.MusicAnalyzer.analyze ( self,
Sequence[float] samples,
int sample_rate_hz )

Analyze normalized mono PCM into spectrum and activity estimates.

Parameters
selfCurrent instance.
samplesNormalized mono PCM samples.
sample_rate_hzSample rate in hertz.
Returns
Complete analysis snapshot.

Reimplemented from musicforge.analysis.interfaces.MusicAnalyzerIf.

◆ clear_zeroize()

None musicforge.analysis.analyzer.MusicAnalyzer.clear_zeroize ( self)

Discard calibration and reset adaptive state.

Parameters
selfCurrent instance.

Reimplemented from musicforge.analysis.interfaces.MusicAnalyzerIf.

◆ finish_zeroize()

None musicforge.analysis.analyzer.MusicAnalyzer.finish_zeroize ( self)

Commit collected ambient noise calibration; requires at least one frame.

Parameters
selfCurrent instance.

Reimplemented from musicforge.analysis.interfaces.MusicAnalyzerIf.

◆ is_zeroized()

bool musicforge.analysis.analyzer.MusicAnalyzer.is_zeroized ( self)

Report whether ambient calibration is active.

Parameters
selfCurrent instance.
Returns
True if calibration is active.

Reimplemented from musicforge.analysis.interfaces.MusicAnalyzerIf.

◆ start_zeroize()

None musicforge.analysis.analyzer.MusicAnalyzer.start_zeroize ( self)

Begin collecting ambient noise frames.

Parameters
selfCurrent instance.

Reimplemented from musicforge.analysis.interfaces.MusicAnalyzerIf.

Member Data Documentation

◆ _activity_peaks

dict musicforge.analysis.analyzer.MusicAnalyzer._activity_peaks = {name: 1e-6 for name in activity_names}
protected

◆ _band_peaks

musicforge.analysis.analyzer.MusicAnalyzer._band_peaks = np.full(band_count, 1e-6, dtype=np.float32)
protected

◆ _beat_age

float musicforge.analysis.analyzer.MusicAnalyzer._beat_age = 1.0
protected

◆ _beat_envelope

float musicforge.analysis.analyzer.MusicAnalyzer._beat_envelope = 0.0
protected

◆ _flux_average

float musicforge.analysis.analyzer.MusicAnalyzer._flux_average = 0.0
protected

◆ _noise_floor

np.ndarray | None musicforge.analysis.analyzer.MusicAnalyzer._noise_floor = None
protected

◆ _previous_activity

dict musicforge.analysis.analyzer.MusicAnalyzer._previous_activity = {name: 0.0 for name in activity_names}
protected

◆ _previous_magnitude

np.ndarray | None musicforge.analysis.analyzer.MusicAnalyzer._previous_magnitude = None
protected

◆ _SPECTRAL_RELATIVE_FLOOR_DB

float musicforge.analysis.analyzer.MusicAnalyzer._SPECTRAL_RELATIVE_FLOOR_DB = -42.0
staticprotected

◆ _summary_peaks

dict musicforge.analysis.analyzer.MusicAnalyzer._summary_peaks = {"bass": 1e-6, "mid": 1e-6, "treble": 1e-6}
protected

◆ _window

musicforge.analysis.analyzer.MusicAnalyzer._window = np.hanning(fft_size).astype(np.float32)
protected

◆ _zeroize_collecting

bool musicforge.analysis.analyzer.MusicAnalyzer._zeroize_collecting = False
protected

◆ _zeroize_frames

list musicforge.analysis.analyzer.MusicAnalyzer._zeroize_frames = []
protected

◆ _ZEROIZE_MARGIN_DB

float musicforge.analysis.analyzer.MusicAnalyzer._ZEROIZE_MARGIN_DB = 3.0
staticprotected

◆ _ZEROIZE_PERCENTILE

float musicforge.analysis.analyzer.MusicAnalyzer._ZEROIZE_PERCENTILE = 75.0
staticprotected

◆ band_count

musicforge.analysis.analyzer.MusicAnalyzer.band_count = band_count

◆ fft_size

musicforge.analysis.analyzer.MusicAnalyzer.fft_size = fft_size

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