ESPHome 2025.5.0
Loading...
Searching...
No Matches
sound_level.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
8
11
12namespace esphome {
13namespace 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:
41 bool start_();
42
44 void stop_();
45
47
50
51 std::unique_ptr<audio::AudioSourceTransferBuffer> audio_buffer_;
52 std::weak_ptr<RingBuffer> ring_buffer_;
53
54 int32_t squared_peak_{0};
56 uint32_t sample_count_{0};
57
59};
60
61template<typename... Ts> class StartAction : public Action<Ts...>, public Parented<SoundLevelComponent> {
62 public:
63 void play(Ts... x) override { this->parent_->start(); }
64};
65
66template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<SoundLevelComponent> {
67 public:
68 void play(Ts... x) override { this->parent_->stop(); }
69};
70
71} // namespace sound_level
72} // namespace esphome
73#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
Base-class for all sensors.
Definition sensor.h:57
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
std::unique_ptr< audio::AudioSourceTransferBuffer > audio_buffer_
Definition sound_level.h:51
void set_measurement_duration(uint32_t measurement_duration_ms)
Definition sound_level.h:23
microphone::MicrophoneSource * microphone_source_
Definition sound_level.h:46
void stop()
Stops the MicrophoneSource.
std::weak_ptr< RingBuffer > ring_buffer_
Definition sound_level.h:52
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 audio_buffer_ and a ring buffer which / audio_bu...
void play(Ts... x) override
Definition sound_level.h:63
void play(Ts... x) override
Definition sound_level.h:68
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.cpp:27
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5