2024-08-31 18:00:23 +08:00
|
|
|
#ifndef _APPLICATION_H_
|
|
|
|
|
#define _APPLICATION_H_
|
|
|
|
|
|
2024-11-05 16:50:29 +08:00
|
|
|
#include <freertos/FreeRTOS.h>
|
2024-10-03 06:39:22 +08:00
|
|
|
#include <freertos/event_groups.h>
|
|
|
|
|
#include <freertos/task.h>
|
2024-11-05 16:50:29 +08:00
|
|
|
#include <opus.h>
|
2024-08-31 18:00:23 +08:00
|
|
|
#include <mutex>
|
2024-09-03 13:57:18 +08:00
|
|
|
#include <list>
|
2024-10-03 06:39:22 +08:00
|
|
|
#include <condition_variable>
|
|
|
|
|
|
2024-11-05 20:15:00 +08:00
|
|
|
#include "opus_encoder.h"
|
|
|
|
|
#include "opus_resampler.h"
|
2024-11-05 16:50:29 +08:00
|
|
|
|
2024-11-14 23:15:43 +08:00
|
|
|
#include "protocol.h"
|
2024-11-05 20:15:00 +08:00
|
|
|
#include "display.h"
|
|
|
|
|
#include "board.h"
|
|
|
|
|
#include "ota.h"
|
2024-11-29 11:06:05 +08:00
|
|
|
#include "background_task.h"
|
2024-10-03 06:39:22 +08:00
|
|
|
|
2024-11-29 11:06:05 +08:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32S3
|
2024-11-05 20:15:00 +08:00
|
|
|
#include "wake_word_detect.h"
|
|
|
|
|
#include "audio_processor.h"
|
2024-10-03 06:39:22 +08:00
|
|
|
#endif
|
|
|
|
|
|
2024-11-29 11:06:05 +08:00
|
|
|
#define SCHEDULE_EVENT (1 << 0)
|
|
|
|
|
#define AUDIO_INPUT_READY_EVENT (1 << 1)
|
|
|
|
|
#define AUDIO_OUTPUT_READY_EVENT (1 << 2)
|
2024-08-31 18:00:23 +08:00
|
|
|
|
|
|
|
|
enum ChatState {
|
2024-10-29 00:22:29 +08:00
|
|
|
kChatStateUnknown,
|
2024-08-31 18:00:23 +08:00
|
|
|
kChatStateIdle,
|
|
|
|
|
kChatStateConnecting,
|
|
|
|
|
kChatStateListening,
|
|
|
|
|
kChatStateSpeaking,
|
2024-09-15 14:03:11 +08:00
|
|
|
kChatStateUpgrading
|
2024-08-31 18:00:23 +08:00
|
|
|
};
|
|
|
|
|
|
2024-11-15 04:44:53 +08:00
|
|
|
#define OPUS_FRAME_DURATION_MS 60
|
|
|
|
|
|
2024-08-31 18:00:23 +08:00
|
|
|
class Application {
|
|
|
|
|
public:
|
2024-09-10 05:58:56 +08:00
|
|
|
static Application& GetInstance() {
|
|
|
|
|
static Application instance;
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
2024-11-25 00:59:03 +08:00
|
|
|
// 删除拷贝构造函数和赋值运算符
|
|
|
|
|
Application(const Application&) = delete;
|
|
|
|
|
Application& operator=(const Application&) = delete;
|
2024-09-10 05:58:56 +08:00
|
|
|
|
2024-08-31 18:00:23 +08:00
|
|
|
void Start();
|
2024-10-29 00:22:29 +08:00
|
|
|
ChatState GetChatState() const { return chat_state_; }
|
|
|
|
|
void Schedule(std::function<void()> callback);
|
|
|
|
|
void SetChatState(ChatState state);
|
2024-10-30 06:58:29 +08:00
|
|
|
void Alert(const std::string&& title, const std::string&& message);
|
2024-11-25 00:59:03 +08:00
|
|
|
void AbortSpeaking(AbortReason reason);
|
2024-11-06 06:18:56 +08:00
|
|
|
void ToggleChatState();
|
2024-11-25 00:59:03 +08:00
|
|
|
void StartListening();
|
|
|
|
|
void StopListening();
|
2024-09-10 05:58:56 +08:00
|
|
|
|
2024-08-31 18:00:23 +08:00
|
|
|
private:
|
2024-09-10 05:58:56 +08:00
|
|
|
Application();
|
|
|
|
|
~Application();
|
|
|
|
|
|
2024-11-29 11:06:05 +08:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32S3
|
2024-10-03 06:39:22 +08:00
|
|
|
WakeWordDetect wake_word_detect_;
|
|
|
|
|
AudioProcessor audio_processor_;
|
2024-10-01 14:16:12 +08:00
|
|
|
#endif
|
2024-11-05 20:15:00 +08:00
|
|
|
Ota ota_;
|
2024-10-03 06:39:22 +08:00
|
|
|
std::mutex mutex_;
|
|
|
|
|
std::list<std::function<void()>> main_tasks_;
|
2024-11-14 23:15:43 +08:00
|
|
|
Protocol* protocol_ = nullptr;
|
2024-08-31 18:00:23 +08:00
|
|
|
EventGroupHandle_t event_group_;
|
2024-10-29 00:22:29 +08:00
|
|
|
volatile ChatState chat_state_ = kChatStateUnknown;
|
2024-11-25 00:59:03 +08:00
|
|
|
bool keep_listening_ = false;
|
2024-11-29 11:06:05 +08:00
|
|
|
bool aborted_ = false;
|
2024-08-31 18:00:23 +08:00
|
|
|
|
|
|
|
|
// Audio encode / decode
|
2024-11-29 11:06:05 +08:00
|
|
|
BackgroundTask background_task_;
|
|
|
|
|
std::chrono::steady_clock::time_point last_output_time_;
|
2024-11-14 23:15:43 +08:00
|
|
|
std::list<std::string> audio_decode_queue_;
|
2024-08-31 18:00:23 +08:00
|
|
|
|
|
|
|
|
OpusEncoder opus_encoder_;
|
|
|
|
|
OpusDecoder* opus_decoder_ = nullptr;
|
|
|
|
|
|
2024-11-06 06:18:56 +08:00
|
|
|
int opus_decode_sample_rate_ = -1;
|
2024-10-24 09:53:08 +08:00
|
|
|
OpusResampler input_resampler_;
|
2024-11-03 01:34:18 +08:00
|
|
|
OpusResampler reference_resampler_;
|
2024-10-24 09:53:08 +08:00
|
|
|
OpusResampler output_resampler_;
|
2024-10-01 14:16:12 +08:00
|
|
|
|
2024-10-03 06:39:22 +08:00
|
|
|
void MainLoop();
|
2024-11-29 11:06:05 +08:00
|
|
|
void InputAudio();
|
|
|
|
|
void OutputAudio();
|
|
|
|
|
void ResetDecoder();
|
2024-09-01 13:24:45 +08:00
|
|
|
void SetDecodeSampleRate(int sample_rate);
|
2024-09-15 14:03:11 +08:00
|
|
|
void CheckNewVersion();
|
2024-10-03 06:39:22 +08:00
|
|
|
|
2024-10-30 06:58:29 +08:00
|
|
|
void PlayLocalFile(const char* data, size_t size);
|
2024-08-31 18:00:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // _APPLICATION_H_
|