Files
xiaozhi-esp32/main/application.h

97 lines
2.4 KiB
C
Raw Normal View History

2024-08-31 18:00:23 +08:00
#ifndef _APPLICATION_H_
#define _APPLICATION_H_
#include <freertos/FreeRTOS.h>
2024-10-03 06:39:22 +08:00
#include <freertos/event_groups.h>
#include <freertos/task.h>
#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-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-10-03 06:39:22 +08:00
#ifdef CONFIG_USE_AFE_SR
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-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-14 14:58:03 +08:00
kChatStateWakeWordDetected,
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-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-10-31 05:57:13 +08:00
void AbortSpeaking();
2024-11-06 06:18:56 +08:00
void ToggleChatState();
2024-09-10 05:58:56 +08:00
// 删除拷贝构造函数和赋值运算符
Application(const Application&) = delete;
Application& operator=(const Application&) = delete;
2024-08-31 18:00:23 +08:00
private:
2024-09-10 05:58:56 +08:00
Application();
~Application();
2024-10-03 06:39:22 +08:00
#ifdef CONFIG_USE_AFE_SR
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::condition_variable_any cv_;
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-10-03 06:39:22 +08:00
bool skip_to_end_ = false;
2024-08-31 18:00:23 +08:00
// Audio encode / decode
2024-10-03 06:39:22 +08:00
TaskHandle_t audio_encode_task_ = nullptr;
2024-08-31 18:00:23 +08:00
StaticTask_t audio_encode_task_buffer_;
StackType_t* audio_encode_task_stack_ = nullptr;
2024-10-03 06:39:22 +08:00
std::list<std::vector<int16_t>> audio_encode_queue_;
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-09-01 13:24:45 +08:00
void SetDecodeSampleRate(int sample_rate);
void CheckNewVersion();
2024-10-03 06:39:22 +08:00
2024-08-31 18:00:23 +08:00
void AudioEncodeTask();
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_