Python Streaming Types
The records of the incremental StreamAnalyzer and of the streaming equalizer and mastering chain, as returned by the classes on Python Streaming API. Batch analysis records are on Python Analysis Types.
Frame batches
StreamAnalyzer.read_frames returns a StreamFrames: a batch of analyzed frames. Matrices are band-major (bands, n_frames) arrays, the layout of the batch mel_spectrogram. A feature that is not computed is None, and the chord arrays follow compute_chroma. mel is linear Mel power, not dB. chord_root is the pitch class of each frame's chord, or -1 where none was detected; chord_quality is an ordinal into CHORD_QUALITIES.
read_quantized_frames_u8 and read_quantized_frames_i16 return the same shape packed into uint8 or int16 to cut bandwidth. Unlike StreamFrames, their mel is in dB, quantized over [mel_db_min, mel_db_max], and chord_confidence is quantized over [0, 1]. A StreamQuantizeConfig sets the ranges. Its defaults mirror sonare_stream_quantize_config_init; widen any range whose source values exceed the defaults, because the quantizers clamp normalized values to [0, 1], so a stream louder or quieter than the ranges saturates silently to the endpoints.
# A batch of analyzed frames from :class:`StreamAnalyzer`.
@dataclass(frozen=True, slots=True, eq=False)
class StreamFrames:
n_frames: int
timestamps_sec: NDArray[np.float32]
mel: NDArray[np.float32] | None
chroma: NDArray[np.float32] | None
onset_strength: NDArray[np.float32] | None
rms_energy: NDArray[np.float32]
spectral_centroid: NDArray[np.float32] | None
spectral_flatness: NDArray[np.float32] | None
chord_root: NDArray[np.int32] | None
chord_quality: NDArray[np.int32] | None
chord_confidence: NDArray[np.float32] | None
# Quantized (uint8) frame batch, shaped as :class:`StreamFrames`.
@dataclass(frozen=True, slots=True, eq=False)
class StreamFramesU8:
n_frames: int
timestamps_sec: NDArray[np.float32]
mel: NDArray[np.uint8] | None
chroma: NDArray[np.uint8] | None
onset_strength: NDArray[np.uint8] | None
rms_energy: NDArray[np.uint8]
spectral_centroid: NDArray[np.uint8] | None
spectral_flatness: NDArray[np.uint8] | None
chord_root: NDArray[np.int8] | None
chord_quality: NDArray[np.int8] | None
chord_confidence: NDArray[np.uint8] | None
# Quantized (int16) frame batch, shaped as :class:`StreamFrames`.
@dataclass(frozen=True, slots=True, eq=False)
class StreamFramesI16:
n_frames: int
timestamps_sec: NDArray[np.float32]
mel: NDArray[np.int16] | None
chroma: NDArray[np.int16] | None
onset_strength: NDArray[np.int16] | None
rms_energy: NDArray[np.int16]
spectral_centroid: NDArray[np.int16] | None
spectral_flatness: NDArray[np.int16] | None
chord_root: NDArray[np.int8] | None
chord_quality: NDArray[np.int8] | None
chord_confidence: NDArray[np.int16] | None
# Quantization ranges for the u8/i16 bandwidth-reduction read paths.
@dataclass(frozen=True, slots=True)
class StreamQuantizeConfig:
mel_db_min: float = -80.0
mel_db_max: float = 0.0
onset_max: float = 50.0
rms_max: float = 1.0
centroid_max: float = 11025.0Progressive estimates and counters
StreamStats comes from StreamAnalyzer.stats and carries counters plus a StreamProgressiveEstimate snapshot in estimate. The dropped_* counters report output frames and progression entries discarded at their caps: new frames at the pending-frame cap, and the oldest chord changes and bar chords at the progression caps. non_finite_discard_blocks counts blocks in which a non-finite input sample was replaced before it could reach the analyzer's recursive state. Nothing is missing from the output in that case: every estimate is produced as usual and simply stops describing the input, so this counter is the only report that the stream was degraded. The unit is one StreamAnalyzer.process call, never a sample.
A StreamProgressiveEstimate field is None until the analyzer has an estimate for it; current_bar and bar_duration_sec stay None until the BPM is stable. bpm_candidate_count is the number of tempo candidates the most recent BPM estimate chose from, 0 until one has run. updated is true when the key or BPM was re-estimated since the previous snapshot; one change sets it on exactly one snapshot however the caller chunks its input, and a call that produced no frame does not repeat it.
StreamChordChange and StreamBarChord spell name as the batch analysis spells a chord, and quality is a CHORD_QUALITIES token. For StreamBarChord, bar_index is the bar number, not the entry's position in the list: bars with no confident chord are not recorded and the oldest entries are dropped at the history cap, so group bars by pattern position with bar_index. start_sec is on the timeline of StreamFrames.timestamps_sec, and consecutive bars are bar_duration_sec apart rather than snapped to the analysis frame grid. In voted_pattern the fields shift meaning: bar_index is the pattern position, start_sec is unused, and a position no chord was voted for is None.
# Counters and the progressive estimate snapshot from :class:`StreamAnalyzer`.
@dataclass(frozen=True, slots=True)
class StreamStats:
total_frames: int
total_samples: int
duration_sec: float
pending_frames: int
dropped_output_frames: int
dropped_chord_progression_entries: int
dropped_bar_progression_entries: int
non_finite_discard_blocks: int
estimate: StreamProgressiveEstimate
# Progressive BPM, key and chord estimate.
@dataclass(frozen=True, slots=True)
class StreamProgressiveEstimate:
bpm: float | None
bpm_confidence: float | None
bpm_candidate_count: int
key: Key | None
chord: StreamChordChange | None
chord_progression: list[StreamChordChange]
bar_chord_progression: list[StreamBarChord]
current_bar: int | None
bar_duration_sec: float | None
voted_pattern: list[StreamBarChord | None]
pattern_length: int
detected_pattern_name: str | None
detected_pattern_score: float | None
all_pattern_scores: list[StreamPatternScore]
accumulated_sec: float
used_frames: int
updated: bool
# A chord change in the progression; ``name`` is spelled as the batch analysis spells a chord, and ``quality`` is a :data:`CHORD_QUALITIES` token.
@dataclass(frozen=True, slots=True)
class StreamChordChange:
root: PitchClass
quality: str
name: str
start_sec: float
confidence: float
# One bar of the beat-synchronized chord progression.
@dataclass(frozen=True, slots=True)
class StreamBarChord:
bar_index: int
root: PitchClass
quality: str
name: str
start_sec: float
confidence: float
# Match score of one chord pattern in a streaming analysis.
@dataclass(frozen=True, slots=True)
class StreamPatternScore:
name: str
score: floatStreaming equalizer and mastering records
The streaming mastering chain and equalizer return each processed block as a StereoBlock, a named tuple that unpacks as left, right:
left, right = chain.process_stereo(block_left, block_right)StreamingEqualizer.spectrum() returns an EqSpectrumSnapshot. pre_left and pre_right, and post_left and post_right, are the pre- and post-EQ waveform streams (uniformly decimated time-domain samples), so they are a scope feed rather than a spectral estimate. profile_db is the frequency-domain view: the post-EQ signal is Hann-windowed, transformed and its bin powers summed into 16 geometrically spaced bands covering 20 Hz to 20 kHz, in amplitude decibels relative to full scale, rising immediately and falling smoothly. band_gain_db is the current gain of each band, last_auto_gain_db the most recent automatic gain, and seq the snapshot sequence number.
# One processed stereo block; unpacks as ``left, right``.
class StereoBlock(NamedTuple):
left: NDArray[np.float32]
right: NDArray[np.float32]
# Realtime equalizer snapshot.
@dataclass(frozen=True, slots=True, eq=False)
class EqSpectrumSnapshot:
pre_left: NDArray[np.float32]
pre_right: NDArray[np.float32]
post_left: NDArray[np.float32]
post_right: NDArray[np.float32]
band_gain_db: NDArray[np.float32]
profile_db: NDArray[np.float32]
last_auto_gain_db: float
seq: int