application.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef _APPLICATION_H_
  2. #define _APPLICATION_H_
  3. #include <freertos/FreeRTOS.h>
  4. #include <freertos/event_groups.h>
  5. #include <freertos/task.h>
  6. #include <esp_timer.h>
  7. #include <string>
  8. #include <mutex>
  9. #include <list>
  10. #include <opus_encoder.h>
  11. #include <opus_decoder.h>
  12. #include <opus_resampler.h>
  13. #include "protocol.h"
  14. #include "ota.h"
  15. #include "background_task.h"
  16. #if CONFIG_USE_WAKE_WORD_DETECT
  17. #include "wake_word_detect.h"
  18. #endif
  19. #if CONFIG_USE_AUDIO_PROCESSOR
  20. #include "audio_processor.h"
  21. #endif
  22. #define SCHEDULE_EVENT (1 << 0)
  23. #define AUDIO_INPUT_READY_EVENT (1 << 1)
  24. #define AUDIO_OUTPUT_READY_EVENT (1 << 2)
  25. enum DeviceState {
  26. kDeviceStateUnknown,
  27. kDeviceStateStarting,
  28. kDeviceStateWifiConfiguring,
  29. kDeviceStateIdle,
  30. kDeviceStateConnecting,
  31. kDeviceStateListening,
  32. kDeviceStateSpeaking,
  33. kDeviceStateUpgrading,
  34. kDeviceStateActivating,
  35. kDeviceStateFatalError
  36. };
  37. #define OPUS_FRAME_DURATION_MS 60
  38. class Application {
  39. public:
  40. static Application& GetInstance() {
  41. static Application instance;
  42. return instance;
  43. }
  44. // 删除拷贝构造函数和赋值运算符
  45. Application(const Application&) = delete;
  46. Application& operator=(const Application&) = delete;
  47. void Start();
  48. DeviceState GetDeviceState() const { return device_state_; }
  49. bool IsVoiceDetected() const { return voice_detected_; }
  50. void Schedule(std::function<void()> callback);
  51. void SetDeviceState(DeviceState state);
  52. void Alert(const char* status, const char* message, const char* emotion = "", const std::string_view& sound = "");
  53. void DismissAlert();
  54. void AbortSpeaking(AbortReason reason);
  55. void ToggleChatState();
  56. void StartListening();
  57. void StopListening();
  58. void UpdateIotStates();
  59. void Reboot();
  60. void WakeWordInvoke(const std::string& wake_word);
  61. void PlaySound(const std::string_view& sound);
  62. void ShowBatteryLevel(const std::string& batterylevel);
  63. void CheckAsrPRO();
  64. bool CanEnterSleepMode();
  65. private:
  66. Application();
  67. ~Application();
  68. #if CONFIG_USE_WAKE_WORD_DETECT
  69. WakeWordDetect wake_word_detect_;
  70. #endif
  71. #if CONFIG_USE_AUDIO_PROCESSOR
  72. AudioProcessor audio_processor_;
  73. #endif
  74. Ota ota_;
  75. std::mutex mutex_;
  76. std::list<std::function<void()>> main_tasks_;
  77. std::unique_ptr<Protocol> protocol_;
  78. EventGroupHandle_t event_group_ = nullptr;
  79. esp_timer_handle_t clock_timer_handle_ = nullptr;
  80. volatile DeviceState device_state_ = kDeviceStateUnknown;
  81. bool keep_listening_ = false;
  82. bool aborted_ = false;
  83. bool voice_detected_ = false;
  84. int clock_ticks_ = 0;
  85. // Audio encode / decode
  86. BackgroundTask* background_task_ = nullptr;
  87. std::chrono::steady_clock::time_point last_output_time_;
  88. std::list<std::vector<uint8_t>> audio_decode_queue_;
  89. std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
  90. std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
  91. int opus_decode_sample_rate_ = -1;
  92. OpusResampler input_resampler_;
  93. OpusResampler reference_resampler_;
  94. OpusResampler output_resampler_;
  95. void MainLoop();
  96. void InputAudio();
  97. void OutputAudio();
  98. void ResetDecoder();
  99. void SetDecodeSampleRate(int sample_rate);
  100. void CheckNewVersion();
  101. void ShowActivationCode();
  102. void OnClockTimer();
  103. };
  104. #endif // _APPLICATION_H_