ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
microphone_source.h
Go to the documentation of this file.
1#pragma once
2
3#include "microphone.h"
4
6
7#include <bitset>
8#include <cstddef>
9#include <cstdint>
10#include <vector>
11
12namespace esphome::microphone {
13
14static const int32_t MAX_GAIN_FACTOR = 64;
15
17 /*
18 * @brief Helper class that handles converting raw microphone data to a requested format.
19 * Components requesting microphone audio should register a callback through this class instead of registering a
20 * callback directly with the microphone if a particular format is required.
21 *
22 * Raw microphone data may have a different number of bits per sample and number of channels than the requesting
23 * component needs. This class handles the conversion by:
24 * - Internally adds a callback to receive the raw microphone data
25 * - The ``process_audio_`` handles the raw data
26 * - Only the channels set in the ``channels_`` bitset are passed through
27 * - Passed through samples have the bits per sample converted
28 * - A gain factor is optionally applied to increase the volume - audio may clip!
29 * - The processed audio is passed to the callback of the component requesting microphone data
30 * - It tracks an internal enabled state, so it ignores raw microphone data when the component requesting
31 * microphone data is not actively requesting audio.
32 *
33 * Note that this class cannot convert sample rates!
34 */
35 public:
36 MicrophoneSource(Microphone *mic, uint8_t bits_per_sample, int32_t gain_factor, bool passive)
37 : mic_(mic), bits_per_sample_(bits_per_sample), gain_factor_(gain_factor), passive_(passive) {}
38
46 void add_channel(uint8_t channel) { this->channels_.set(channel); }
47
48 template<typename F> void add_data_callback(F &&data_callback) {
49 this->mic_->add_data_callback([this, data_callback](const std::vector<uint8_t> &data) {
50 if (this->enabled_ || this->passive_) {
51 if (this->processed_samples_.use_count() == 0) {
52 // Create vector if its unused
53 this->processed_samples_ = std::make_shared<std::vector<uint8_t>>();
54 }
55
56 // Take temporary ownership of samples vector to avoid deallocation before the callback finishes
57 std::shared_ptr<std::vector<uint8_t>> output_samples = this->processed_samples_;
58 this->process_audio_(data, *output_samples);
59 data_callback(*output_samples);
60 }
61 });
62 }
63
64 void set_gain_factor(int32_t gain_factor) { this->gain_factor_ = clamp<int32_t>(gain_factor, 1, MAX_GAIN_FACTOR); }
65 int32_t get_gain_factor() { return this->gain_factor_; }
66
71
72 void start();
73 void stop();
74 bool is_passive() const { return this->passive_; }
75 bool is_running() const { return (this->mic_->is_running() && (this->enabled_ || this->passive_)); }
76 bool is_stopped() const { return !this->is_running(); };
77
78 protected:
79 void process_audio_(const std::vector<uint8_t> &data, std::vector<uint8_t> &filtered_data);
80
81 std::shared_ptr<std::vector<uint8_t>> processed_samples_;
82
85 std::bitset<8> channels_;
86 int32_t gain_factor_;
87 bool enabled_{false};
88 bool passive_; // Only pass audio if ``mic_`` is already running
89};
90
91} // namespace esphome::microphone
void add_data_callback(F &&data_callback)
Definition microphone.h:23
void set_gain_factor(int32_t gain_factor)
MicrophoneSource(Microphone *mic, uint8_t bits_per_sample, int32_t gain_factor, bool passive)
void add_data_callback(F &&data_callback)
std::shared_ptr< std::vector< uint8_t > > processed_samples_
void add_channel(uint8_t channel)
Enables a channel to be processed through the callback.
audio::AudioStreamInfo get_audio_stream_info()
Gets the AudioStreamInfo of the data after processing.
void process_audio_(const std::vector< uint8_t > &data, std::vector< uint8_t > &filtered_data)