ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
sound_level.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
8
12
13namespace esphome {
14namespace sound_level {
15
17 public:
18 void dump_config() override;
19 void setup() override;
20 void loop() override;
21
22 float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
23
24 void set_measurement_duration(uint32_t measurement_duration_ms) {
25 this->measurement_duration_ms_ = measurement_duration_ms;
26 }
28 this->microphone_source_ = microphone_source;
29 }
30 void set_peak_sensor(sensor::Sensor *peak_sensor) { this->peak_sensor_ = peak_sensor; }
31 void set_rms_sensor(sensor::Sensor *rms_sensor) { this->rms_sensor_ = rms_sensor; }
32
34 void start();
35
37 void stop();
38
39 protected:
42 bool start_();
43
45 void stop_();
46
48
51
52 std::unique_ptr<audio::AudioSourceTransferBuffer> audio_buffer_;
53 std::weak_ptr<RingBuffer> ring_buffer_;
54
55 int32_t squared_peak_{0};
57 uint32_t sample_count_{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 sound_level
73} // namespace esphome
74#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:1618
Base-class for all sensors.
Definition sensor.h:47
void set_peak_sensor(sensor::Sensor *peak_sensor)
Definition sound_level.h:30
void start()
Starts the MicrophoneSource to start measuring sound levels.
void set_rms_sensor(sensor::Sensor *rms_sensor)
Definition sound_level.h:31
std::unique_ptr< audio::AudioSourceTransferBuffer > audio_buffer_
Definition sound_level.h:52
void set_measurement_duration(uint32_t measurement_duration_ms)
Definition sound_level.h:24
microphone::MicrophoneSource * microphone_source_
Definition sound_level.h:47
void stop()
Stops the MicrophoneSource.
std::weak_ptr< RingBuffer > ring_buffer_
Definition sound_level.h:53
void set_microphone_source(microphone::MicrophoneSource *microphone_source)
Definition sound_level.h:27
float get_setup_priority() const override
Definition sound_level.h:22
void stop_()
Internal start command that, if necessary, allocates audio_buffer_ and a ring buffer which / audio_bu...
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:43
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5