Skip to content

Commit

Permalink
Merge pull request #44 from gnumpi/fix_mic_resampler
Browse files Browse the repository at this point in the history
Fix using the resampler within the mic pipeline
  • Loading branch information
gnumpi authored Jun 18, 2024
2 parents ac25907 + acbc87a commit 78ea8bd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
14 changes: 13 additions & 1 deletion esphome/components/adf_pipeline/adf_audio_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool ADFResampler::init_adf_elements_(){
.dest_rate = this->dst_rate_,
.dest_bits = 16,
.dest_ch = this->dst_num_channels_,
.src_bits = 16,
.src_bits = this->src_bit_depth_,
.mode = RESAMPLE_DECODE_MODE,
.max_indata_bytes = RSP_FILTER_BUFFER_BYTE,
.out_len_bytes = RSP_FILTER_BUFFER_BYTE,
Expand All @@ -50,6 +50,7 @@ bool ADFResampler::init_adf_elements_(){
}

void ADFResampler::on_settings_request(AudioPipelineSettingsRequest &request){

bool settings_changed = false;
if( request.sampling_rate > -1 ){
if( request.sampling_rate != this->src_rate_ )
Expand Down Expand Up @@ -86,6 +87,15 @@ void ADFResampler::on_settings_request(AudioPipelineSettingsRequest &request){
}
}

if ( request.final_bit_depth > -1 && request.final_bit_depth != 16 ){
request.bit_depth = 16;
request.final_bit_depth = 16;
request.requested_by = this;
this->pipeline_->request_settings(request);
}



if( this->sdk_resampler_ && settings_changed)
{
if( audio_element_get_state(this->sdk_resampler_) == AEL_STATE_RUNNING)
Expand All @@ -110,6 +120,8 @@ void ADFResampler::on_settings_request(AudioPipelineSettingsRequest &request){
resample_info.src_bits = this->src_bit_depth_;
resample_info.dest_bits = 16;
}

esph_log_d(TAG, "Current settings: SRC: rate: %d, ch: %d bits: %d, DST: rate: %d, ch: %d, bits %d", this->src_rate_, this->src_num_channels_, this->src_bit_depth_, this->dst_rate_, this->dst_num_channels_, 16);
}

} // namespace esp_adf
Expand Down
3 changes: 3 additions & 0 deletions esphome/components/adf_pipeline/adf_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ bool ADFPipeline::request_settings(AudioPipelineSettingsRequest &request) {
if (*it != request.requested_by) {
(*it)->on_settings_request(request);
}
else {
break;
}
}
return !request.failed;
}
Expand Down
12 changes: 12 additions & 0 deletions esphome/components/adf_pipeline/microphone/esp_adf_microphone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void ADFMicrophone::dump_config() {
}

void ADFMicrophone::start() {
esph_log_d(TAG, "start request while ine state %d", this->state_);
if ( this->state_ == microphone::STATE_RUNNING){
return;
}
Expand Down Expand Up @@ -58,6 +59,17 @@ size_t ADFMicrophone::read(int16_t *buf, size_t len) {
}
size_t bytes_read = 0;
bytes_read = this->pcm_stream_.stream_read_bytes((char *) buf, len);
if ( bytes_read && this->gain_log2_ > 0){
size_t samples_read = bytes_read / sizeof(int16_t);
std::vector<int16_t> samples;
samples.resize(samples_read);
for (size_t i = 0; i < samples_read; i++) {
int32_t temp = (buf)[i] << this->gain_log2_ ;
samples[i] = (int16_t) clamp<int32_t>(temp, INT16_MIN, INT16_MAX );
}
memcpy(buf, samples.data(), samples_read * sizeof(int16_t));
}

return bytes_read;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ADFMicrophone : public microphone::Microphone, public ADFPipelineControlle
protected:
void on_pipeline_state_change(PipelineState state) override;

uint8_t gain_log2_{3};
uint8_t gain_log2_{0};
PCMSink pcm_stream_;
};

Expand Down
9 changes: 9 additions & 0 deletions esphome/components/i2s_audio/adf_pipeline/adf_i2s_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ bool ADFElementI2SIn::init_adf_elements_() {
sdk_audio_elements_.push_back(this->adf_i2s_stream_reader_);
sdk_element_tags_.push_back("i2s_in");

this->valid_settings_ = false;
return true;
};

Expand All @@ -86,6 +87,14 @@ void ADFElementI2SIn::clear_adf_elements_(){
this->sdk_audio_elements_.clear();
this->sdk_element_tags_.clear();
this->uninstall_i2s_driver();
this->valid_settings_ = false;
}

bool ADFElementI2SIn::prepare_elements(bool initial_call){
if( initial_call ){
this->valid_settings_ = false;
}
return ADFPipelineElement::prepare_elements(initial_call);
}


Expand Down
2 changes: 1 addition & 1 deletion esphome/components/i2s_audio/adf_pipeline/adf_i2s_in.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ADFElementI2SIn : public I2SReader, public ADFPipelineSourceElement, publi
const std::string get_name() override { return "I2S_Reader"; }
void dump_config() override { this->dump_i2s_settings(); }
bool is_ready() override;

bool prepare_elements(bool initial_call) override;
protected:

bool valid_settings_{false};
Expand Down

0 comments on commit 78ea8bd

Please sign in to comment.