Files
xiaozhi-esp32/main/application.h

103 lines
2.6 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>
2024-12-06 11:08:49 +08:00
#include <string>
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
2024-12-06 11:08:49 +08:00
#include <opus_encoder.h>
#include <opus_decoder.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 "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,
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;
}
// 删除拷贝构造函数和赋值运算符
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);
void Alert(const std::string& title, const std::string& message);
void AbortSpeaking(AbortReason reason);
2024-11-06 06:18:56 +08:00
void ToggleChatState();
void StartListening();
void StopListening();
2024-12-06 11:08:49 +08:00
void UpdateIotStates();
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_;
std::unique_ptr<Protocol> protocol_;
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;
bool keep_listening_ = false;
2024-11-29 11:06:05 +08:00
bool aborted_ = false;
2024-12-06 11:08:49 +08:00
std::string last_iot_states_;
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_;
std::list<std::vector<uint8_t>> audio_decode_queue_;
2024-08-31 18:00:23 +08:00
std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
2024-08-31 18:00:23 +08:00
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);
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_