72 lines
2.0 KiB
C++
72 lines
2.0 KiB
C++
#include "wake_word.h"
|
|
|
|
#include <esp_log.h>
|
|
#include <esp_wn_iface.h>
|
|
#include <esp_wn_models.h>
|
|
#include <model_path.h>
|
|
#include <cstring>
|
|
|
|
#define TAG "WakeWord"
|
|
|
|
bool WakeWordDetector::Initialize()
|
|
{
|
|
srmodel_list_t* models = esp_srmodel_init("model");
|
|
if (!models) {
|
|
ESP_LOGE(TAG, "Failed to init SR models");
|
|
return false;
|
|
}
|
|
|
|
// Find the custom wake word model (你好播乐 = ni3hao3bo1le4)
|
|
char* model_name = esp_srmodel_filter(models, ESP_WN_PREFIX, "ni3hao3bo1le4");
|
|
if (!model_name) {
|
|
// Fallback: use any available wake word model
|
|
model_name = esp_srmodel_filter(models, ESP_WN_PREFIX, NULL);
|
|
}
|
|
if (!model_name) {
|
|
ESP_LOGE(TAG, "No wake word model found");
|
|
return false;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Using wake word model: %s", model_name);
|
|
|
|
const esp_wn_iface_t* iface = esp_wn_handle_from_name(model_name);
|
|
if (!iface) {
|
|
ESP_LOGE(TAG, "Failed to get WakeNet interface");
|
|
return false;
|
|
}
|
|
|
|
wakenet_iface_ = iface;
|
|
wakenet_handle_ = iface->create(model_name, DET_MODE_90);
|
|
if (!wakenet_handle_) {
|
|
ESP_LOGE(TAG, "Failed to create WakeNet instance");
|
|
return false;
|
|
}
|
|
|
|
chunk_size_ = iface->get_samp_chunksize(static_cast<model_iface_data_t*>(wakenet_handle_));
|
|
int sample_rate = iface->get_samp_rate(static_cast<model_iface_data_t*>(wakenet_handle_));
|
|
ESP_LOGI(TAG, "WakeNet ready: chunk_size=%d, sample_rate=%d", chunk_size_, sample_rate);
|
|
|
|
initialized_ = true;
|
|
return true;
|
|
}
|
|
|
|
void WakeWordDetector::SetCallback(DetectedCallback callback)
|
|
{
|
|
callback_ = callback;
|
|
}
|
|
|
|
void WakeWordDetector::Feed(const int16_t* samples, int count)
|
|
{
|
|
if (!initialized_ || !wakenet_iface_ || !wakenet_handle_) return;
|
|
|
|
auto* iface = static_cast<const esp_wn_iface_t*>(wakenet_iface_);
|
|
int result = iface->detect(static_cast<model_iface_data_t*>(wakenet_handle_), const_cast<int16_t*>(samples));
|
|
|
|
if (result > 0) {
|
|
ESP_LOGI(TAG, "Wake word detected!");
|
|
if (callback_) {
|
|
callback_();
|
|
}
|
|
}
|
|
}
|