|
|
|
|
@@ -1,4 +1,5 @@
|
|
|
|
|
#include <BuiltinLed.h>
|
|
|
|
|
#include <TcpTransport.h>
|
|
|
|
|
#include <TlsTransport.h>
|
|
|
|
|
#include <Ml307SslTransport.h>
|
|
|
|
|
#include <WifiConfigurationAp.h>
|
|
|
|
|
@@ -153,6 +154,14 @@ void Application::Start() {
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "ML307 IMEI: %s", ml307_at_modem_.GetImei().c_str());
|
|
|
|
|
ESP_LOGI(TAG, "ML307 ICCID: %s", ml307_at_modem_.GetIccid().c_str());
|
|
|
|
|
|
|
|
|
|
// If low power, the material ready event will be triggered by the modem because of a reset
|
|
|
|
|
ml307_at_modem_.OnMaterialReady([this]() {
|
|
|
|
|
ESP_LOGI(TAG, "ML307 material ready");
|
|
|
|
|
Schedule([this]() {
|
|
|
|
|
SetChatState(kChatStateIdle);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
#else
|
|
|
|
|
// Try to connect to WiFi, if failed, launch the WiFi configuration AP
|
|
|
|
|
auto& wifi_station = WifiStation::GetInstance();
|
|
|
|
|
@@ -433,7 +442,9 @@ void Application::AudioEncodeTask() {
|
|
|
|
|
auto protocol = AllocateBinaryProtocol(opus, opus_size);
|
|
|
|
|
Schedule([this, protocol, opus_size]() {
|
|
|
|
|
if (ws_client_ && ws_client_->IsConnected()) {
|
|
|
|
|
ws_client_->Send(protocol, sizeof(BinaryProtocol) + opus_size, true);
|
|
|
|
|
if (!ws_client_->Send(protocol, sizeof(BinaryProtocol) + opus_size, true)) {
|
|
|
|
|
ESP_LOGE(TAG, "Failed to send audio data");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
heap_caps_free(protocol);
|
|
|
|
|
});
|
|
|
|
|
@@ -443,7 +454,7 @@ void Application::AudioEncodeTask() {
|
|
|
|
|
audio_decode_queue_.pop_front();
|
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
|
|
int frame_size = opus_decode_sample_rate_ / 1000 * opus_duration_ms_;
|
|
|
|
|
int frame_size = opus_decode_sample_rate_ * opus_duration_ms_ / 1000;
|
|
|
|
|
packet->pcm.resize(frame_size);
|
|
|
|
|
|
|
|
|
|
int ret = opus_decode(opus_decoder_, packet->opus.data(), packet->opus.size(), packet->pcm.data(), frame_size, 0);
|
|
|
|
|
@@ -539,6 +550,7 @@ void Application::SetDecodeSampleRate(int sample_rate) {
|
|
|
|
|
opus_decode_sample_rate_ = sample_rate;
|
|
|
|
|
opus_decoder_ = opus_decoder_create(opus_decode_sample_rate_, 1, NULL);
|
|
|
|
|
if (opus_decode_sample_rate_ != CONFIG_AUDIO_OUTPUT_SAMPLE_RATE) {
|
|
|
|
|
ESP_LOGI(TAG, "Resampling audio from %d to %d", opus_decode_sample_rate_, CONFIG_AUDIO_OUTPUT_SAMPLE_RATE);
|
|
|
|
|
opus_resampler_.Configure(opus_decode_sample_rate_, CONFIG_AUDIO_OUTPUT_SAMPLE_RATE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -549,11 +561,16 @@ void Application::StartWebSocketClient() {
|
|
|
|
|
delete ws_client_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string url = CONFIG_WEBSOCKET_URL;
|
|
|
|
|
std::string token = "Bearer " + std::string(CONFIG_WEBSOCKET_ACCESS_TOKEN);
|
|
|
|
|
#ifdef CONFIG_USE_ML307
|
|
|
|
|
ws_client_ = new WebSocket(new Ml307SslTransport(ml307_at_modem_, 0));
|
|
|
|
|
#else
|
|
|
|
|
ws_client_ = new WebSocket(new TlsTransport());
|
|
|
|
|
if (url.find("wss://") == 0) {
|
|
|
|
|
ws_client_ = new WebSocket(new TlsTransport());
|
|
|
|
|
} else {
|
|
|
|
|
ws_client_ = new WebSocket(new TcpTransport());
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
ws_client_->SetHeader("Authorization", token.c_str());
|
|
|
|
|
ws_client_->SetHeader("Device-Id", SystemInfo::GetMacAddress().c_str());
|
|
|
|
|
@@ -617,7 +634,16 @@ void Application::StartWebSocketClient() {
|
|
|
|
|
if (text != NULL) {
|
|
|
|
|
ESP_LOGI(TAG, ">> %s", text->valuestring);
|
|
|
|
|
}
|
|
|
|
|
} else if (strcmp(type->valuestring, "llm") == 0) {
|
|
|
|
|
auto emotion = cJSON_GetObjectItem(root, "emotion");
|
|
|
|
|
if (emotion != NULL) {
|
|
|
|
|
ESP_LOGD(TAG, "EMOTION: %s", emotion->valuestring);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ESP_LOGW(TAG, "Unknown message type: %s", type->valuestring);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ESP_LOGE(TAG, "Missing message type, data: %s", data);
|
|
|
|
|
}
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
}
|
|
|
|
|
@@ -639,7 +665,7 @@ void Application::StartWebSocketClient() {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!ws_client_->Connect(CONFIG_WEBSOCKET_URL)) {
|
|
|
|
|
if (!ws_client_->Connect(url.c_str())) {
|
|
|
|
|
ESP_LOGE(TAG, "Failed to connect to websocket server");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|