Skip to content

AudioService

Source: AudioService.h

AudioService

class AudioService
src/core/services/AudioService.h:45

Inherits: MoonModule

Acquires an audio source and publishes a frame: a level, sixteen bands, and the peak.

The producer half of the audio-reactive pipeline, which every audio effect reads. Named for what it does, acquisition plus analysis, rather than for one source.

AudioService card

Public Attributes

uint8_t prevBands_ = {} : the previous block's bands, the flux's reference

BandConditioner cond_ : the per-band floor and peak tables, learned live

LevelConditioner levelCond_ : the same, for the overall level (VU) in automatic mode

OnsetDetector onset_ : the hit decision, with its running mean and refractory

uint8_t device = 0 : Which capture device to open, where the host offers a choice.

uint8_t micMode = 0 : Which kind of microphone is wired, the two-wire kind having no clocks to set.

int8_t sckPin = -1 : the bit clock, or -1 while unset

int8_t wsPin = -1 : the word select, or -1 while unset

int8_t sdPin = -1 : the data line, or -1 while unset

int8_t mclkPin = -1 : The master clock a converter may need, a self-clocked part leaving it unset.

uint8_t sampleRateSel = 2 : Which of the standard rates to run at, a choice rather than a free number.

uint8_t floor = 100 : The silence threshold: below it a band reads as nothing.

uint8_t gain = 128 : The window's width, a higher value running the display hotter.

uint8_t levels = 1 : Who sets the display window: the two sliders, or the learner.

uint8_t simulate = 0 : Which synthesized pattern to produce: a plausible song, or a deterministic march.

uint8_t mode = 0 : The source: a synthesized signal, the network, or its own input.

bool send = false : Whether to broadcast the local analysis, which only the local mode can do.

uint16_t syncPort = WLED_SYNC_PORT : The port both directions use, which must match on both ends.

Public Methods

inline void finishBands() : Finish a block of raw bands:

virtual inline ModuleRole role() const override : A service, so the container accepts it as a child.

inline uint8_t sync() const : What the sync machinery does:

inline uint32_t sampleRate() const : The selected rate in hertz.

virtual inline void defineControls() override : Declare the mode, then only the controls that mode needs.

virtual inline bool affectsPrepare(const char * name) const override : Which changes rebuild the channel, rebind the socket, or re-toggle the rows.

virtual inline void prepare() override : Claim the frame seat, then acquire only the hardware the current mode needs.

virtual inline void setup() override : One-time wiring only: the acquire and the election live in the build.

virtual inline void release() override : Free the peripheral and the socket, and vacate the seat for any survivor.

inline const AudioFrame * audioFrame() const : The latest analyzed frame, always valid, so a consumer without a mic reads silence.

inline bool syncOpenForTest() const : Whether the sync socket is open.

inline bool micStatusStaleForTest() const : Whether a mic diagnosis is outstanding, which suppresses the sync line while it is.

inline void setMicStatusStaleForTest(bool stale) : Set the flag a local-mode diagnosis would have set, so leaving that mode can be tested anywhere.

inline const char * syncStatusForTest() const : The sync state as the card shows it, which is the reported state itself.

inline uint32_t syncSendCountForTest() const : How many sends have gone out.

virtual inline void tick() override : Produce this tick's frame, from the network, a synthesizer, or the local input.

inline void synthesizeFrame(bool sweep) : Fill frame_ with a synthesized signal.

virtual inline void tick1s() override : The once-a-second tick for housekeeping, which by default ticks the children.

Public Static Attributes

constexpr size_t kBlock = 512 : The block and transform size, a power of two, around twenty milliseconds of audio.

constexpr size_t kMag = / 2 : How many magnitude bins that produces.

constexpr uint8_t kRatio = 4 : How much of a band's deviation to correct, short of the ratios that pump.

constexpr uint8_t kMaxGainDb = 24 : How far a band may be lifted, so a silent one is never amplified into its own noise.

constexpr uint8_t kSimMode = 0 : A synthesized signal, so a device demonstrates sound before anything is wired to it.

constexpr uint8_t kReceiveMode = 1 : Another device's analysis, over the network that has to exist to carry it.

constexpr uint8_t kLocalMode = ? 2 : 1 : This device's own microphone, which needs pins and a part to be connected to them.

constexpr uint16_t kSampleRates = {8000, 16000, 22050, 44100} : The rates the selector offers.

constexpr uint8_t kSampleRateCount = 4 : How many there are.

Public Static Methods

constexpr static inline constexpr uint32_t syncSendIntervalMsForTest() : How often a send may go out.

constexpr static inline constexpr uint32_t syncFallbackMsForTest() : How long a peer may be quiet before it is stale.

constexpr static inline constexpr uint32_t syncOpenRetryMsForTest() : How long a failed open waits.

static inline const AudioFrame * latestFrame() : The live frame every consumer reads, or silence where there is no source.

More info

The modes run simple to advanced

Simulate synthesizes a signal for a demo or a test. Receive is a sink a peer drives. Local runs the device's own input and analyzes it here. A device is exactly one of these, and changing it acquires or releases hardware live.

They are ordered by what each one needs: simulate needs nothing, receive needs a network, local needs a part wired to pins. So the default is the first entry rather than an index that depended on the platform, and a device demonstrates sound before anything is attached to it.

The pipeline

Each block: read, block the offset, measure the level, window, transform, map to bands. The high-pass conditions the block once, so both halves see the same cleaned signal. A block outlasts a tick, so a tick completing none re-publishes the last frame. The level is measured independently of the bands.

Only the read and the transform are platform code; the rest is host-tested maths. The scratch is fixed and resident, so a block allocates nothing.