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-29 11:06:05 +08:00
|
|
|
void AudioCodec::OnInputReady(std::function<bool()> callback) {
|
|
|
|
|
on_input_ready_ = callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioCodec::OnOutputReady(std::function<bool()> callback) {
|
|
|
|
|
on_output_ready_ = callback;
|
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 duration = 30;
|
|
|
|
|
int input_frame_size = input_sample_rate_ / 1000 * duration * input_channels_;
|
|
|
|
|
|
|
|
|
|
data.resize(input_frame_size);
|
|
|
|
|
int samples = Read(data.data(), data.size());
|
|
|
|
|
if (samples > 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2024-11-14 23:15:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IRAM_ATTR bool AudioCodec::on_sent(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx) {
|
|
|
|
|
auto audio_codec = (AudioCodec*)user_ctx;
|
2024-11-29 11:06:05 +08:00
|
|
|
if (audio_codec->output_enabled_ && audio_codec->on_output_ready_) {
|
|
|
|
|
return audio_codec->on_output_ready_();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IRAM_ATTR bool AudioCodec::on_recv(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx) {
|
|
|
|
|
auto audio_codec = (AudioCodec*)user_ctx;
|
|
|
|
|
if (audio_codec->input_enabled_ && audio_codec->on_input_ready_) {
|
|
|
|
|
return audio_codec->on_input_ready_();
|
|
|
|
|
}
|
2024-11-14 23:15:43 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioCodec::Start() {
|
2024-11-15 04:44:53 +08:00
|
|
|
Settings settings("audio", false);
|
|
|
|
|
output_volume_ = settings.GetInt("output_volume", output_volume_);
|
|
|
|
|
|
2024-11-29 11:06:05 +08:00
|
|
|
// 注册音频数据回调
|
|
|
|
|
i2s_event_callbacks_t rx_callbacks = {};
|
|
|
|
|
rx_callbacks.on_recv = on_recv;
|
|
|
|
|
i2s_channel_register_event_callback(rx_handle_, &rx_callbacks, this);
|
|
|
|
|
|
|
|
|
|
i2s_event_callbacks_t tx_callbacks = {};
|
|
|
|
|
tx_callbacks.on_sent = on_sent;
|
|
|
|
|
i2s_channel_register_event_callback(tx_handle_, &tx_callbacks, this);
|
2024-11-14 23:15:43 +08:00
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(i2s_channel_enable(tx_handle_));
|
|
|
|
|
ESP_ERROR_CHECK(i2s_channel_enable(rx_handle_));
|
|
|
|
|
|
|
|
|
|
EnableInput(true);
|
|
|
|
|
EnableOutput(true);
|
|
|
|
|
}
|
|
|
|
|
|
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::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");
|
|
|
|
|
}
|