ESPHome 2025.5.0
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 <functional>
11#include <vector>
12
13namespace esphome {
14namespace microphone {
15
16static const int32_t MAX_GAIN_FACTOR = 64;
17
19 /*
20 * @brief Helper class that handles converting raw microphone data to a requested format.
21 * Components requesting microphone audio should register a callback through this class instead of registering a
22 * callback directly with the microphone if a particular format is required.
23 *
24 * Raw microphone data may have a different number of bits per sample and number of channels than the requesting
25 * component needs. This class handles the conversion by:
26 * - Internally adds a callback to receive the raw microphone data
27 * - The ``process_audio_`` handles the raw data
28 * - Only the channels set in the ``channels_`` bitset are passed through
29 * - Passed through samples have the bits per sample converted
30 * - A gain factor is optionally applied to increase the volume - audio may clip!
31 * - The processed audio is passed to the callback of the component requesting microphone data
32 * - It tracks an internal enabled state, so it ignores raw microphone data when the component requesting
33 * microphone data is not actively requesting audio.
34 *
35 * Note that this class cannot convert sample rates!
36 */
37 public:
38 MicrophoneSource(Microphone *mic, uint8_t bits_per_sample, int32_t gain_factor, bool passive)
39 : mic_(mic), bits_per_sample_(bits_per_sample), gain_factor_(gain_factor), passive_(passive) {}
40
48 void add_channel(uint8_t channel) { this->channels_.set(channel); }
49
50 void add_data_callback(std::function<void(const std::vector<uint8_t> &)> &&data_callback);
51
52 void set_gain_factor(int32_t gain_factor) { this->gain_factor_ = clamp<int32_t>(gain_factor, 1, MAX_GAIN_FACTOR); }
53 int32_t get_gain_factor() { return this->gain_factor_; }
54
59
60 void start();
61 void stop();
62 bool is_passive() const { return this->passive_; }
63 bool is_running() const { return (this->mic_->is_running() && (this->enabled_ || this->passive_)); }
64 bool is_stopped() const { return !this->is_running(); };
65
66 protected:
67 void process_audio_(const std::vector<uint8_t> &data, std::vector<uint8_t> &filtered_data);
68
69 std::shared_ptr<std::vector<uint8_t>> processed_samples_;
70
73 std::bitset<8> channels_;
74 int32_t gain_factor_;
75 bool enabled_{false};
76 bool passive_; // Only pass audio if ``mic_`` is already running
77};
78
79} // namespace microphone
80} // namespace esphome
void set_gain_factor(int32_t gain_factor)
MicrophoneSource(Microphone *mic, uint8_t bits_per_sample, int32_t gain_factor, bool passive)
std::shared_ptr< std::vector< uint8_t > > processed_samples_
void add_channel(uint8_t channel)
Enables a channel to be processed through the callback.
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 process_audio_(const std::vector< uint8_t > &data, std::vector< uint8_t > &filtered_data)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101