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>
|
2025-02-24 14:41:34 +08:00
|
|
|
|
#include <esp_timer.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>
|
2025-04-13 11:47:15 +08:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <condition_variable>
|
2025-04-29 18:17:08 +08:00
|
|
|
|
#include <memory>
|
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-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 "ota.h"
|
2024-11-29 11:06:05 +08:00
|
|
|
|
#include "background_task.h"
|
2025-04-29 18:17:08 +08:00
|
|
|
|
#include "audio_processor.h"
|
2024-10-03 06:39:22 +08:00
|
|
|
|
|
2025-03-04 05:30:35 +08:00
|
|
|
|
#if CONFIG_USE_WAKE_WORD_DETECT
|
2024-11-05 20:15:00 +08:00
|
|
|
|
#include "wake_word_detect.h"
|
2025-03-04 05:30:35 +08:00
|
|
|
|
#endif
|
2024-10-03 06:39:22 +08:00
|
|
|
|
|
2024-11-29 11:06:05 +08:00
|
|
|
|
#define SCHEDULE_EVENT (1 << 0)
|
2025-05-27 05:44:46 +08:00
|
|
|
|
#define SEND_AUDIO_EVENT (1 << 1)
|
2025-05-26 14:30:44 +08:00
|
|
|
|
#define CHECK_NEW_VERSION_DONE_EVENT (1 << 2)
|
2024-08-31 18:00:23 +08:00
|
|
|
|
|
2025-01-05 19:34:28 +08:00
|
|
|
|
enum DeviceState {
|
|
|
|
|
|
kDeviceStateUnknown,
|
|
|
|
|
|
kDeviceStateStarting,
|
|
|
|
|
|
kDeviceStateWifiConfiguring,
|
|
|
|
|
|
kDeviceStateIdle,
|
|
|
|
|
|
kDeviceStateConnecting,
|
|
|
|
|
|
kDeviceStateListening,
|
|
|
|
|
|
kDeviceStateSpeaking,
|
|
|
|
|
|
kDeviceStateUpgrading,
|
2025-02-16 06:59:19 +08:00
|
|
|
|
kDeviceStateActivating,
|
2025-01-05 19:34:28 +08:00
|
|
|
|
kDeviceStateFatalError
|
2024-08-31 18:00:23 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-11-15 04:44:53 +08:00
|
|
|
|
#define OPUS_FRAME_DURATION_MS 60
|
2025-05-26 14:30:44 +08:00
|
|
|
|
#define MAX_AUDIO_PACKETS_IN_QUEUE (2400 / OPUS_FRAME_DURATION_MS)
|
2024-11-15 04:44:53 +08:00
|
|
|
|
|
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();
|
2025-01-05 19:34:28 +08:00
|
|
|
|
DeviceState GetDeviceState() const { return device_state_; }
|
|
|
|
|
|
bool IsVoiceDetected() const { return voice_detected_; }
|
2024-10-29 00:22:29 +08:00
|
|
|
|
void Schedule(std::function<void()> callback);
|
2025-01-05 19:34:28 +08:00
|
|
|
|
void SetDeviceState(DeviceState state);
|
2025-02-19 23:54:59 +08:00
|
|
|
|
void Alert(const char* status, const char* message, const char* emotion = "", const std::string_view& sound = "");
|
2025-03-03 07:29:22 +08:00
|
|
|
|
void DismissAlert();
|
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-12-06 11:08:49 +08:00
|
|
|
|
void UpdateIotStates();
|
2025-02-16 06:59:19 +08:00
|
|
|
|
void Reboot();
|
2025-02-17 18:47:21 +08:00
|
|
|
|
void WakeWordInvoke(const std::string& wake_word);
|
2025-03-03 07:29:22 +08:00
|
|
|
|
void PlaySound(const std::string_view& sound);
|
2025-03-05 09:37:13 +08:00
|
|
|
|
bool CanEnterSleepMode();
|
2025-05-22 19:19:36 +08:00
|
|
|
|
void SendMcpMessage(const std::string& payload);
|
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();
|
|
|
|
|
|
|
2025-03-04 05:30:35 +08:00
|
|
|
|
#if CONFIG_USE_WAKE_WORD_DETECT
|
2024-10-03 06:39:22 +08:00
|
|
|
|
WakeWordDetect wake_word_detect_;
|
2025-03-04 05:30:35 +08:00
|
|
|
|
#endif
|
2025-04-29 18:17:08 +08:00
|
|
|
|
std::unique_ptr<AudioProcessor> audio_processor_;
|
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-12-04 02:12:20 +08:00
|
|
|
|
std::unique_ptr<Protocol> protocol_;
|
2025-02-24 14:41:34 +08:00
|
|
|
|
EventGroupHandle_t event_group_ = nullptr;
|
|
|
|
|
|
esp_timer_handle_t clock_timer_handle_ = nullptr;
|
2025-01-05 19:34:28 +08:00
|
|
|
|
volatile DeviceState device_state_ = kDeviceStateUnknown;
|
2025-03-30 09:07:08 +08:00
|
|
|
|
ListeningMode listening_mode_ = kListeningModeAutoStop;
|
2025-05-09 14:00:26 +08:00
|
|
|
|
#if CONFIG_USE_DEVICE_AEC || CONFIG_USE_SERVER_AEC
|
2025-03-30 09:07:08 +08:00
|
|
|
|
bool realtime_chat_enabled_ = true;
|
|
|
|
|
|
#else
|
|
|
|
|
|
bool realtime_chat_enabled_ = false;
|
|
|
|
|
|
#endif
|
2024-11-29 11:06:05 +08:00
|
|
|
|
bool aborted_ = false;
|
2025-01-05 19:34:28 +08:00
|
|
|
|
bool voice_detected_ = false;
|
2025-04-13 23:12:44 +08:00
|
|
|
|
bool busy_decoding_audio_ = false;
|
2025-03-04 06:27:11 +08:00
|
|
|
|
int clock_ticks_ = 0;
|
2025-03-30 09:07:08 +08:00
|
|
|
|
TaskHandle_t check_new_version_task_handle_ = nullptr;
|
2024-08-31 18:00:23 +08:00
|
|
|
|
|
|
|
|
|
|
// Audio encode / decode
|
2025-03-30 09:07:08 +08:00
|
|
|
|
TaskHandle_t audio_loop_task_handle_ = nullptr;
|
2025-01-13 06:49:01 +08:00
|
|
|
|
BackgroundTask* background_task_ = nullptr;
|
2024-11-29 11:06:05 +08:00
|
|
|
|
std::chrono::steady_clock::time_point last_output_time_;
|
2025-05-27 05:44:46 +08:00
|
|
|
|
std::list<AudioStreamPacket> audio_send_queue_;
|
2025-04-28 23:10:24 +08:00
|
|
|
|
std::list<AudioStreamPacket> audio_decode_queue_;
|
2025-04-13 11:47:15 +08:00
|
|
|
|
std::condition_variable audio_decode_cv_;
|
2024-08-31 18:00:23 +08:00
|
|
|
|
|
2025-05-16 18:46:20 +08:00
|
|
|
|
// 新增:用于维护音频包的timestamp队列
|
|
|
|
|
|
std::list<uint32_t> timestamp_queue_;
|
|
|
|
|
|
std::mutex timestamp_mutex_;
|
|
|
|
|
|
std::atomic<uint32_t> last_output_timestamp_ = 0;
|
|
|
|
|
|
|
2024-12-04 02:12:20 +08:00
|
|
|
|
std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
|
|
|
|
|
|
std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
|
2024-08-31 18:00:23 +08:00
|
|
|
|
|
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
|
|
|
|
|
2025-04-13 11:47:15 +08:00
|
|
|
|
void MainEventLoop();
|
2025-03-30 09:07:08 +08:00
|
|
|
|
void OnAudioInput();
|
|
|
|
|
|
void OnAudioOutput();
|
|
|
|
|
|
void ReadAudio(std::vector<int16_t>& data, int sample_rate, int samples);
|
2024-11-29 11:06:05 +08:00
|
|
|
|
void ResetDecoder();
|
2025-03-30 09:07:08 +08:00
|
|
|
|
void SetDecodeSampleRate(int sample_rate, int frame_duration);
|
2024-09-15 14:03:11 +08:00
|
|
|
|
void CheckNewVersion();
|
2025-02-16 06:59:19 +08:00
|
|
|
|
void ShowActivationCode();
|
2025-02-24 14:41:34 +08:00
|
|
|
|
void OnClockTimer();
|
2025-03-30 09:07:08 +08:00
|
|
|
|
void SetListeningMode(ListeningMode mode);
|
|
|
|
|
|
void AudioLoop();
|
2024-08-31 18:00:23 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _APPLICATION_H_
|