15static constexpr uint32_t BUFFER_DURATION_MS = 16;
20 input_stream_info.
get_channels(), this->target_sample_rate_);
35 "Resampler Microphone:\n"
36 " Target Sample Rate: %" PRIu32
" Hz\n"
99 input_stream_info.
get_channels(), this->target_sample_rate_);
101 auto resampler = make_unique<esp_audio_libs::resampler::Resampler>(
104 esp_audio_libs::resampler::ResamplerConfiguration resample_config = {
105 .source_sample_rate =
static_cast<float>(input_stream_info.
get_sample_rate()),
112 .subsample_interpolate =
false,
113 .number_of_taps = this->
taps_,
114 .number_of_filters = this->
filters_,
117 if (!resampler->initialize(resample_config)) {
118 ESP_LOGE(TAG,
"Not enough memory to resample");
144 const size_t input_bytes_per_frame = input_stream_info.
frames_to_bytes(1);
149 const uint8_t *input = data.data();
151 while (input_frames > 0) {
157 esp_audio_libs::resampler::ResamplerResults results = this->
resampler_->resample(
158 input, this->
output_buffer_.data(), std::min(input_frames, max_input_frames), max_output_frames, 0.0f);
160 input += results.frames_used * input_bytes_per_frame;
161 input_frames -= results.frames_used;
163 if (results.frames_generated > 0) {
166 }
else if (results.frames_used == 0) {
void mark_failed()
Mark this component as failed.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
size_t ms_to_bytes(uint32_t ms) const
Converts duration to bytes.
size_t frames_to_bytes(uint32_t frames) const
Converts frames to bytes.
uint8_t get_bits_per_sample() const
uint32_t ms_to_samples(uint32_t ms) const
Converts duration to samples.
uint32_t bytes_to_frames(size_t bytes) const
Convert bytes to frames.
uint8_t get_channels() const
uint32_t ms_to_frames(uint32_t ms) const
Converts duration to frames.
uint32_t get_sample_rate() const
audio::AudioStreamInfo audio_stream_info_
CallbackManager< void(const std::vector< uint8_t > &)> data_callbacks_
void add_data_callback(F &&data_callback)
audio::AudioStreamInfo get_audio_stream_info()
Gets the AudioStreamInfo of the data after processing.
bool init_resampler_(const audio::AudioStreamInfo &input_stream_info)
Sets up the resampler for the given input format.
void process_audio_(const std::vector< uint8_t > &data)
Resamples a chunk of source audio and passes it to the data callbacks. Source microphone task only.
microphone::MicrophoneSource * source_
uint8_t active_listeners_
audio::AudioStreamInfo input_stream_info_
std::unique_ptr< esp_audio_libs::resampler::Resampler > resampler_
uint32_t target_sample_rate_
void dump_config() override
std::vector< uint8_t > output_buffer_
ESPHOME_LOG_TAG(TAG, "resampler.microphone")