Files
xiaozhi-esp32/main/application.h
ooxxU 6b2752a498 外接唤醒模组的支持,可以有多种自定义唤醒词,进行唤醒小智,ESP32(非C3,非S3) 面包板上已支持 (#172)
* 外接唤醒模组的支持,可以有多种自定义唤醒词,进行唤醒小智
ESP32(非C3,非S3) 面包板上已支持

* Update application.cc

外接唤醒模组的支持,好处是可以有多种自定义唤醒词,进行唤醒小智
唤醒模组需要一个GPIO Pin,设置成输出模式+高电平
对该Pin进行唤醒设置,1秒内的低电平脉冲,也就是小智的一个Click
可以参考 ESP32 面包板中的 asr_button_ 按钮的功能函数调用
本人测试采用ASR-ProV1.0版本的唤醒模组,测试内容包括:自定义唤醒词,唤醒词打断,唤醒词回应
此代码兼容其他型号的唤醒模组,并没做限制模组型号,方便大家使用
Modify By MarsBear

* Update esp32_bread_board.cc

---------

Co-authored-by: Xiaoxia <terrence@tenclass.com>
2025-02-17 18:47:21 +08:00

112 lines
3.0 KiB
C++

#ifndef _APPLICATION_H_
#define _APPLICATION_H_
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <freertos/task.h>
#include <string>
#include <mutex>
#include <list>
#include <opus_encoder.h>
#include <opus_decoder.h>
#include <opus_resampler.h>
#include "protocol.h"
#include "ota.h"
#include "background_task.h"
#if CONFIG_USE_AUDIO_PROCESSING
#include "wake_word_detect.h"
#include "audio_processor.h"
#endif
#define SCHEDULE_EVENT (1 << 0)
#define AUDIO_INPUT_READY_EVENT (1 << 1)
#define AUDIO_OUTPUT_READY_EVENT (1 << 2)
enum DeviceState {
kDeviceStateUnknown,
kDeviceStateStarting,
kDeviceStateWifiConfiguring,
kDeviceStateIdle,
kDeviceStateConnecting,
kDeviceStateListening,
kDeviceStateSpeaking,
kDeviceStateUpgrading,
kDeviceStateActivating,
kDeviceStateFatalError
};
#define OPUS_FRAME_DURATION_MS 60
class Application {
public:
static Application& GetInstance() {
static Application instance;
return instance;
}
// 删除拷贝构造函数和赋值运算符
Application(const Application&) = delete;
Application& operator=(const Application&) = delete;
void Start();
DeviceState GetDeviceState() const { return device_state_; }
bool IsVoiceDetected() const { return voice_detected_; }
void Schedule(std::function<void()> callback);
void SetDeviceState(DeviceState state);
void Alert(const std::string& status, const std::string& message, const std::string& emotion = "", const std::string& sound = "");
void AbortSpeaking(AbortReason reason);
void ToggleChatState();
void StartListening();
void StopListening();
void UpdateIotStates();
void Reboot();
void WakeWordInvoke(const std::string& wake_word);
private:
Application();
~Application();
#if CONFIG_USE_AUDIO_PROCESSING
WakeWordDetect wake_word_detect_;
AudioProcessor audio_processor_;
#endif
Ota ota_;
std::mutex mutex_;
std::list<std::function<void()>> main_tasks_;
std::unique_ptr<Protocol> protocol_;
EventGroupHandle_t event_group_;
volatile DeviceState device_state_ = kDeviceStateUnknown;
bool keep_listening_ = false;
bool aborted_ = false;
bool voice_detected_ = false;
std::string last_iot_states_;
// Audio encode / decode
BackgroundTask* background_task_ = nullptr;
std::chrono::steady_clock::time_point last_output_time_;
std::list<std::vector<uint8_t>> audio_decode_queue_;
std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
int opus_decode_sample_rate_ = -1;
OpusResampler input_resampler_;
OpusResampler reference_resampler_;
OpusResampler output_resampler_;
void MainLoop();
void InputAudio();
void OutputAudio();
void ResetDecoder();
void SetDecodeSampleRate(int sample_rate);
void CheckNewVersion();
void ShowActivationCode();
void PlayLocalFile(const char* data, size_t size);
};
#endif // _APPLICATION_H_