Skip to content

Commit

Permalink
timer: add timer to force end stream
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stintel authored and kristiankielhofner committed May 26, 2023
1 parent 8c8ad13 commit 7da6d73
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
7 changes: 7 additions & 0 deletions main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ config WILLOW_RECORD_BUFFER
help
Custom record buffer for timing and latency

config WILLOW_STREAM_TIMEOUT
int "Maximum stream duration"
range 1 30
default 5
help
Stop stream N seconds after wake event to avoid endless stream when VAD END doesn't trigger.

config WILLOW_DEBUG_MEM
bool "Debug Willow memory"
help
Expand Down
11 changes: 10 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@

#define I2S_PORT I2S_NUM_0

static bool recording = false;
bool recording = false;

static bool stream_to_api = false;
static int total_write = 0;

Expand Down Expand Up @@ -118,6 +119,9 @@ static esp_err_t cb_ar_event(audio_rec_evt_t are, void *data)
switch (are) {
case AUDIO_REC_VAD_END:
ESP_LOGI(TAG, "AUDIO_REC_VAD_END");
if (esp_timer_is_active(hdl_sess_timer)) {
esp_timer_stop(hdl_sess_timer);
}
break;
case AUDIO_REC_VAD_START:
ESP_LOGI(TAG, "AUDIO_REC_VAD_START");
Expand All @@ -127,6 +131,10 @@ static esp_err_t cb_ar_event(audio_rec_evt_t are, void *data)
msg = MSG_START;
#endif
xQueueSend(q_rec, &msg, 0);
if (esp_timer_is_active(hdl_sess_timer)) {
esp_timer_stop(hdl_sess_timer);
}
esp_timer_start_once(hdl_sess_timer, CONFIG_WILLOW_STREAM_TIMEOUT * 1000 * 1000);
break;
case AUDIO_REC_COMMAND_DECT:
// Multinet timeout
Expand Down Expand Up @@ -808,6 +816,7 @@ void app_main(void)
init_buttons();
init_input_key_service();
init_lvgl_touch();
init_session_timer();
init_timer();
init_ap_to_api();
start_rec();
Expand Down
2 changes: 2 additions & 0 deletions main/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#define WAKENET_NAME "wn9_alexa"

bool recording;

esp_periph_set_handle_t hdl_pset;

extern QueueHandle_t q_rec;
Expand Down
22 changes: 22 additions & 0 deletions main/timer.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#include "driver/timer.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"

#include "shared.h"
#include "tasks.h"
#include "timer.h"

#define TIMER_DIV (16)
#define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIV)

esp_timer_handle_t hdl_sess_timer = NULL;
xQueueHandle hdl_q_timer = NULL;

static void cb_session_timer(void *data)
{
if (recording) {
ESP_LOGI(TAG, "session timer expired - forcing end stream");
int msg = MSG_STOP;
xQueueSend(q_rec, &msg, 0);
}
}

static bool IRAM_ATTR cb_timer_isr(void *data)
{
BaseType_t hiprio_woken = pdFALSE;
Expand Down Expand Up @@ -45,4 +57,14 @@ void reset_timer(bool pause)
timer_pause(TIMER_GROUP_0, TIMER_0);
}
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
}

esp_err_t init_session_timer(void)
{
const esp_timer_create_args_t cfg_et = {
.callback = &cb_session_timer,
.name = "session_timer",
};

return esp_timer_create(&cfg_et, &hdl_sess_timer);
}
2 changes: 2 additions & 0 deletions main/timer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
esp_timer_handle_t hdl_sess_timer;
xQueueHandle hdl_q_timer;

esp_err_t init_session_timer(void);
void init_timer(void);
void reset_timer(bool pause);

0 comments on commit 7da6d73

Please sign in to comment.