ESPHome 2026.5.1
Loading...
Searching...
No Matches
sound_level.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
9
12
13namespace esphome::sound_level {
14
16 public:
17 void dump_config() override;
18 void setup() override;
19 void loop() override;
20
21 float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
22
23 void set_measurement_duration(uint32_t measurement_duration_ms) {
24 this->measurement_duration_ms_ = measurement_duration_ms;
25 }
27 this->microphone_source_ = microphone_source;
28 }
29 void set_peak_sensor(sensor::Sensor *peak_sensor) { this->peak_sensor_ = peak_sensor; }
30 void set_rms_sensor(sensor::Sensor *rms_sensor) { this->rms_sensor_ = rms_sensor; }
31
33 void start();
34
36 void stop();
37
38 protected:
42 bool start_();
43
45 void stop_();
46
48
51
52 std::unique_ptr<audio::RingBufferAudioSource> audio_source_;
53 std::weak_ptr<ring_buffer::RingBuffer> ring_buffer_;
54
55 int32_t squared_peak_{0};
58
60};
61
62template<typename... Ts> class StartAction : public Action<Ts...>, public Parented<SoundLevelComponent> {
63 public:
64 void play(const Ts &...x) override { this->parent_->start(); }
65};
66
67template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<SoundLevelComponent> {
68 public:
69 void play(const Ts &...x) override { this->parent_->stop(); }
70};
71
72} // namespace esphome::sound_level
73
74#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:1861
Base-class for all sensors.
Definition sensor.h:47
void set_peak_sensor(sensor::Sensor *peak_sensor)
Definition sound_level.h:29
void start()
Starts the MicrophoneSource to start measuring sound levels.
void set_rms_sensor(sensor::Sensor *rms_sensor)
Definition sound_level.h:30
void set_measurement_duration(uint32_t measurement_duration_ms)
Definition sound_level.h:23
std::weak_ptr< ring_buffer::RingBuffer > ring_buffer_
Definition sound_level.h:53
microphone::MicrophoneSource * microphone_source_
Definition sound_level.h:47
void stop()
Stops the MicrophoneSource.
void set_microphone_source(microphone::MicrophoneSource *microphone_source)
Definition sound_level.h:26
float get_setup_priority() const override
Definition sound_level.h:21
void stop_()
Internal start command that, if necessary, allocates a ring buffer and a zero-copy RingBufferAudioSou...
std::unique_ptr< audio::RingBufferAudioSource > audio_source_
Definition sound_level.h:52
void play(const Ts &...x) override
Definition sound_level.h:64
void play(const Ts &...x) override
Definition sound_level.h:69
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:55
static void uint32_t
uint16_t x
Definition tt21100.cpp:5