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>
|
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-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"
|
2024-10-03 06:39:22 +08:00
|
|
|
|
2025-02-08 13:56:27 +08:00
|
|
|
#if CONFIG_USE_AUDIO_PROCESSING
|
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
|
|
|
|
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
|
|
|
|
|
|
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-16 06:59:19 +08:00
|
|
|
void Alert(const std::string& status, const std::string& message, const std::string& emotion = "", const std::string& sound = "");
|
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();
|
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-02-08 13:56:27 +08:00
|
|
|
#if CONFIG_USE_AUDIO_PROCESSING
|
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_;
|
2024-12-04 02:12:20 +08:00
|
|
|
std::unique_ptr<Protocol> protocol_;
|
2024-08-31 18:00:23 +08:00
|
|
|
EventGroupHandle_t event_group_;
|
2025-01-05 19:34:28 +08:00
|
|
|
volatile DeviceState device_state_ = kDeviceStateUnknown;
|
2024-11-25 00:59:03 +08:00
|
|
|
bool keep_listening_ = false;
|
2024-11-29 11:06:05 +08:00
|
|
|
bool aborted_ = false;
|
2025-01-05 19:34:28 +08:00
|
|
|
bool voice_detected_ = 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
|
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_;
|
2024-12-04 02:12:20 +08:00
|
|
|
std::list<std::vector<uint8_t>> audio_decode_queue_;
|
2024-08-31 18:00:23 +08:00
|
|
|
|
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-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);
|
2024-09-15 14:03:11 +08:00
|
|
|
void CheckNewVersion();
|
2025-02-16 06:59:19 +08:00
|
|
|
void ShowActivationCode();
|
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_
|