11namespace sound_level {
13static const char *
const TAG =
"sound_level";
15static const uint32_t AUDIO_BUFFER_DURATION_MS = 30;
16static const uint32_t RING_BUFFER_DURATION_MS = 120;
19static const double MAX_SAMPLE_SQUARED_DENOMINATOR = INT16_MIN * INT16_MIN;
22 ESP_LOGCONFIG(TAG,
"Sound Level Component:");
31 std::shared_ptr<RingBuffer> temp_ring_buffer = this->
ring_buffer_.lock();
34 temp_ring_buffer->write((
void *) data.data(), data.size());
91 const uint32_t samples_in_window =
93 const uint32_t samples_available_to_process =
95 const uint32_t samples_to_process = std::min(samples_in_window - this->
sample_count_, samples_available_to_process);
98 const int16_t *audio_data =
reinterpret_cast<const int16_t *
>(this->
audio_buffer_->get_buffer_start());
101 for (uint32_t i = 0; i < samples_to_process; ++i) {
103 int32_t squared_sample =
static_cast<int32_t
>(audio_data[i]) *
static_cast<int32_t
>(audio_data[i]);
124 const float peak_db = 10.0f * log10(
static_cast<float>(this->
squared_peak_) / MAX_SAMPLE_SQUARED_DENOMINATOR);
132 const double rms_db = 10.0 * log10((this->
squared_samples_sum_ / MAX_SAMPLE_SQUARED_DENOMINATOR) /
133 static_cast<double>(samples_in_window));
145 ESP_LOGW(TAG,
"Can't start the microphone in passive mode");
153 ESP_LOGW(TAG,
"Can't stop microphone in passive mode");
159bool SoundLevelComponent::start_() {
174 std::shared_ptr<RingBuffer> temp_ring_buffer =
176 if (temp_ring_buffer.use_count() == 0) {
void status_clear_error()
void status_momentary_error(const std::string &name, uint32_t length=5000)
bool status_has_warning() const
bool status_has_error() const
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
static std::unique_ptr< RingBuffer > create(size_t len)
static std::unique_ptr< AudioSourceTransferBuffer > create(size_t buffer_size)
Creates a new source transfer buffer.
size_t ms_to_bytes(uint32_t ms) const
Converts duration to bytes.
uint32_t ms_to_samples(uint32_t ms) const
Converts duration to samples.
size_t samples_to_bytes(uint32_t samples) const
Converts samples to bytes.
uint32_t bytes_to_samples(size_t bytes) const
Convert bytes to samples.
void add_data_callback(std::function< void(const std::vector< uint8_t > &)> &&data_callback)
audio::AudioStreamInfo get_audio_stream_info()
Gets the AudioStreamInfo of the data after processing.
void publish_state(float state)
Publish a new state to the front-end.
void dump_config() override
sensor::Sensor * rms_sensor_
void start()
Starts the MicrophoneSource to start measuring sound levels.
std::unique_ptr< audio::AudioSourceTransferBuffer > audio_buffer_
uint32_t measurement_duration_ms_
microphone::MicrophoneSource * microphone_source_
void stop()
Stops the MicrophoneSource.
std::weak_ptr< RingBuffer > ring_buffer_
sensor::Sensor * peak_sensor_
void stop_()
Internal start command that, if necessary, allocates audio_buffer_ and a ring buffer which / audio_bu...
uint64_t squared_samples_sum_
Providing packet encoding functions for exchanging data with a remote host.