Files
xiaozhi-esp32/main/audio/audio_codec.cc

78 lines
1.8 KiB
C++
Raw Normal View History

2024-11-06 06:18:56 +08:00
#include "audio_codec.h"
#include "board.h"
2024-11-15 04:44:53 +08:00
#include "settings.h"
2024-11-06 06:18:56 +08:00
#include <esp_log.h>
#include <cstring>
2024-11-14 23:15:43 +08:00
#include <driver/i2s_common.h>
2024-11-06 06:18:56 +08:00
#define TAG "AudioCodec"
AudioCodec::AudioCodec() {
}
AudioCodec::~AudioCodec() {
}
2024-11-14 23:15:43 +08:00
void AudioCodec::OutputData(std::vector<int16_t>& data) {
2024-11-29 11:06:05 +08:00
Write(data.data(), data.size());
}
bool AudioCodec::InputData(std::vector<int16_t>& data) {
int samples = Read(data.data(), data.size());
if (samples > 0) {
return true;
}
return false;
2024-11-14 23:15:43 +08:00
}
void AudioCodec::Start() {
2024-11-15 04:44:53 +08:00
Settings settings("audio", false);
output_volume_ = settings.GetInt("output_volume", output_volume_);
2025-03-22 06:03:22 +08:00
if (output_volume_ <= 0) {
ESP_LOGW(TAG, "Output volume value (%d) is too small, setting to default (10)", output_volume_);
output_volume_ = 10;
}
2024-11-15 04:44:53 +08:00
if (tx_handle_ != nullptr) {
ESP_ERROR_CHECK(i2s_channel_enable(tx_handle_));
}
if (rx_handle_ != nullptr) {
ESP_ERROR_CHECK(i2s_channel_enable(rx_handle_));
}
2024-11-14 23:15:43 +08:00
EnableInput(true);
EnableOutput(true);
2025-03-22 06:03:22 +08:00
ESP_LOGI(TAG, "Audio codec started");
2024-11-14 23:15:43 +08:00
}
2024-11-06 06:18:56 +08:00
void AudioCodec::SetOutputVolume(int volume) {
output_volume_ = volume;
ESP_LOGI(TAG, "Set output volume to %d", output_volume_);
2024-11-15 04:44:53 +08:00
Settings settings("audio", true);
settings.SetInt("output_volume", output_volume_);
2024-11-06 06:18:56 +08:00
}
void AudioCodec::SetInputGain(float gain) {
input_gain_ = gain;
ESP_LOGI(TAG, "Set input gain to %.1f", input_gain_);
}
2024-11-06 06:18:56 +08:00
void AudioCodec::EnableInput(bool enable) {
if (enable == input_enabled_) {
return;
}
input_enabled_ = enable;
ESP_LOGI(TAG, "Set input enable to %s", enable ? "true" : "false");
}
void AudioCodec::EnableOutput(bool enable) {
if (enable == output_enabled_) {
return;
}
output_enabled_ = enable;
ESP_LOGI(TAG, "Set output enable to %s", enable ? "true" : "false");
}