Skip to content

Commit 4ebfca3

Browse files
committed
timer: add timer to force end stream
Add a timer that sends MSG_STOP N seconds after starting, where N is the value of CONFIG_WILLOW_STREAM_TIMEOUT. We don't need this for the following case, audio_rec_cfg_t.wakeup_time already takes care of it: I (00:16:15.053) WILLOW: AUDIO_REC_WAKEUP_START I (00:16:25.053) WILLOW: AUDIO_REC_WAKEUP_END Default to 5s which should be long enough for most commands. And max timeout 30s which matches Whisper's default max without chunking. Closes #18.
1 parent 5c3775c commit 4ebfca3

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

main/Kconfig.projbuild

+7
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ config WILLOW_RECORD_BUFFER
200200
help
201201
Custom record buffer for timing and latency
202202

203+
config WILLOW_STREAM_TIMEOUT
204+
int "Maximum stream duration"
205+
range 1 30
206+
default 5
207+
help
208+
Stop stream N seconds after wake event to avoid endless stream when VAD END doesn't trigger.
209+
203210
config WILLOW_DEBUG_MEM
204211
bool "Debug Willow memory"
205212
help

main/main.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757

5858
#define I2S_PORT I2S_NUM_0
5959

60-
static bool recording = false;
60+
bool recording = false;
61+
6162
static bool stream_to_api = false;
6263
static int total_write = 0;
6364

@@ -118,6 +119,9 @@ static esp_err_t cb_ar_event(audio_rec_evt_t are, void *data)
118119
switch (are) {
119120
case AUDIO_REC_VAD_END:
120121
ESP_LOGI(TAG, "AUDIO_REC_VAD_END");
122+
if (esp_timer_is_active(hdl_sess_timer)) {
123+
esp_timer_stop(hdl_sess_timer);
124+
}
121125
break;
122126
case AUDIO_REC_VAD_START:
123127
ESP_LOGI(TAG, "AUDIO_REC_VAD_START");
@@ -127,6 +131,10 @@ static esp_err_t cb_ar_event(audio_rec_evt_t are, void *data)
127131
msg = MSG_START;
128132
#endif
129133
xQueueSend(q_rec, &msg, 0);
134+
if (esp_timer_is_active(hdl_sess_timer)) {
135+
esp_timer_stop(hdl_sess_timer);
136+
}
137+
esp_timer_start_once(hdl_sess_timer, CONFIG_WILLOW_STREAM_TIMEOUT * 1000 * 1000);
130138
break;
131139
case AUDIO_REC_COMMAND_DECT:
132140
// Multinet timeout
@@ -808,6 +816,7 @@ void app_main(void)
808816
init_buttons();
809817
init_input_key_service();
810818
init_lvgl_touch();
819+
init_session_timer();
811820
init_timer();
812821
init_ap_to_api();
813822
start_rec();

main/shared.h

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#define WAKENET_NAME "wn9_alexa"
44

5+
bool recording;
6+
57
esp_periph_set_handle_t hdl_pset;
68

79
extern QueueHandle_t q_rec;

main/timer.c

+22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
#include "driver/timer.h"
2+
#include "esp_log.h"
23
#include "freertos/FreeRTOS.h"
34
#include "freertos/queue.h"
45

6+
#include "shared.h"
57
#include "tasks.h"
68
#include "timer.h"
79

810
#define TIMER_DIV (16)
911
#define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIV)
1012

13+
esp_timer_handle_t hdl_sess_timer = NULL;
1114
xQueueHandle hdl_q_timer = NULL;
1215

16+
static void cb_session_timer(void *data)
17+
{
18+
if (recording) {
19+
ESP_LOGI(TAG, "session timer expired - forcing end stream");
20+
int msg = MSG_STOP;
21+
xQueueSend(q_rec, &msg, 0);
22+
}
23+
}
24+
1325
static bool IRAM_ATTR cb_timer_isr(void *data)
1426
{
1527
BaseType_t hiprio_woken = pdFALSE;
@@ -45,4 +57,14 @@ void reset_timer(bool pause)
4557
timer_pause(TIMER_GROUP_0, TIMER_0);
4658
}
4759
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
60+
}
61+
62+
esp_err_t init_session_timer(void)
63+
{
64+
const esp_timer_create_args_t cfg_et = {
65+
.callback = &cb_session_timer,
66+
.name = "session_timer",
67+
};
68+
69+
return esp_timer_create(&cfg_et, &hdl_sess_timer);
4870
}

main/timer.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
esp_timer_handle_t hdl_sess_timer;
12
xQueueHandle hdl_q_timer;
23

4+
esp_err_t init_session_timer(void);
35
void init_timer(void);
46
void reset_timer(bool pause);

0 commit comments

Comments
 (0)