application.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. bool CanEnterSleepMode();
  63. private:
  64. Application();
  65. ~Application();
  66. #if CONFIG_USE_WAKE_WORD_DETECT
  67. WakeWordDetect wake_word_detect_;
  68. #endif
  69. #if CONFIG_USE_AUDIO_PROCESSOR
  70. AudioProcessor audio_processor_;
  71. #endif
  72. Ota ota_;
  73. std::mutex mutex_;
  74. std::list<std::function<void()>> main_tasks_;
  75. std::unique_ptr<Protocol> protocol_;
  76. EventGroupHandle_t event_group_ = nullptr;
  77. esp_timer_handle_t clock_timer_handle_ = nullptr;
  78. volatile DeviceState device_state_ = kDeviceStateUnknown;
  79. bool keep_listening_ = false;
  80. bool aborted_ = false;
  81. bool voice_detected_ = false;
  82. int clock_ticks_ = 0;
  83. // Audio encode / decode
  84. BackgroundTask* background_task_ = nullptr;
  85. std::chrono::steady_clock::time_point last_output_time_;
  86. std::list<std::vector<uint8_t>> audio_decode_queue_;
  87. std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
  88. std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
  89. int opus_decode_sample_rate_ = -1;
  90. OpusResampler input_resampler_;
  91. OpusResampler reference_resampler_;
  92. OpusResampler output_resampler_;
  93. void MainLoop();
  94. void InputAudio();
  95. void OutputAudio();
  96. void ResetDecoder();
  97. void SetDecodeSampleRate(int sample_rate);
  98. void CheckNewVersion();
  99. void ShowActivationCode();
  100. void OnClockTimer();
  101. //我添加的
  102. std::string last_displayed_message_;
  103. };
  104. #endif // _APPLICATION_H_