pinebuds/services/app_ai/src/app_ai_if_thirdparty.cpp

112 lines
3.9 KiB
C++
Raw Normal View History

#include "app_ai_if_thirdparty.h"
2022-08-15 04:20:27 -05:00
#include "app_ai_if.h"
#include "app_ai_manager_api.h"
#include "app_ai_tws.h"
2022-08-15 04:20:27 -05:00
#include "app_bt_media_manager.h"
#include "app_thirdparty.h"
#include "cmsis_os.h"
#include "hal_trace.h"
#include "spp_api.h"
2022-08-15 04:20:27 -05:00
#ifdef __AI_VOICE__
#include "ai_control.h"
#include "ai_manager.h"
2022-08-15 04:20:27 -05:00
#include "ai_thread.h"
#include "app_ai_voice.h"
#endif
uint8_t
app_ai_if_thirdparty_mempool_buf[APP_AI_IF_THIRDPARTY_MEMPOOL_BUFFER_SIZE];
2022-08-15 04:20:27 -05:00
THIRDPARTY_AI_MEMPOOL_BUFFER_T app_ai_if_thirdparty_mempool_buf_t;
POSSIBLY_UNUSED void app_ai_if_thirdparty_mempool_init(void) {
memset((uint8_t *)app_ai_if_thirdparty_mempool_buf, 0,
APP_AI_IF_THIRDPARTY_MEMPOOL_BUFFER_SIZE);
2022-08-15 04:20:27 -05:00
app_ai_if_thirdparty_mempool_buf_t.buff = app_ai_if_thirdparty_mempool_buf;
app_ai_if_thirdparty_mempool_buf_t.buff_size_total =
APP_AI_IF_THIRDPARTY_MEMPOOL_BUFFER_SIZE;
app_ai_if_thirdparty_mempool_buf_t.buff_size_used = 0;
app_ai_if_thirdparty_mempool_buf_t.buff_size_free =
APP_AI_IF_THIRDPARTY_MEMPOOL_BUFFER_SIZE;
2022-08-15 04:20:27 -05:00
TRACE(3, "%s buf %p size %d", __func__, app_ai_if_thirdparty_mempool_buf,
APP_AI_IF_THIRDPARTY_MEMPOOL_BUFFER_SIZE);
2022-08-15 04:20:27 -05:00
}
void app_ai_if_thirdparty_mempool_deinit(void) {
TRACE(2, "%s size %d", __func__,
app_ai_if_thirdparty_mempool_buf_t.buff_size_total);
2022-08-15 04:20:27 -05:00
memset((uint8_t *)app_ai_if_thirdparty_mempool_buf_t.buff, 0,
app_ai_if_thirdparty_mempool_buf_t.buff_size_total);
2022-08-15 04:20:27 -05:00
app_ai_if_thirdparty_mempool_buf_t.buff_size_used = 0;
app_ai_if_thirdparty_mempool_buf_t.buff_size_free =
app_ai_if_thirdparty_mempool_buf_t.buff_size_total;
2022-08-15 04:20:27 -05:00
}
void app_ai_if_thirdparty_mempool_get_buff(uint8_t **buff, uint32_t size) {
uint32_t buff_size_free;
2022-08-15 04:20:27 -05:00
buff_size_free = app_ai_if_thirdparty_mempool_buf_t.buff_size_free;
2022-08-15 04:20:27 -05:00
if (size % 4) {
size = size + (4 - size % 4);
}
2022-08-15 04:20:27 -05:00
TRACE(3, "%s free %d to allocate %d", __func__, buff_size_free, size);
2022-08-15 04:20:27 -05:00
ASSERT(size <= buff_size_free, "[%s] size = %d > free size = %d", __func__,
size, buff_size_free);
2022-08-15 04:20:27 -05:00
*buff = app_ai_if_thirdparty_mempool_buf_t.buff +
app_ai_if_thirdparty_mempool_buf_t.buff_size_used;
2022-08-15 04:20:27 -05:00
app_ai_if_thirdparty_mempool_buf_t.buff_size_used += size;
app_ai_if_thirdparty_mempool_buf_t.buff_size_free -= size;
2022-08-15 04:20:27 -05:00
TRACE(3, "thirdparty allocate %d, now used %d left %d", size,
app_ai_if_thirdparty_mempool_buf_t.buff_size_used,
app_ai_if_thirdparty_mempool_buf_t.buff_size_free);
2022-08-15 04:20:27 -05:00
}
void app_ai_if_thirdparty_start_stream_by_app_audio_manager(void) {
2022-08-15 04:20:27 -05:00
#ifdef __THIRDPARTY
app_audio_manager_sendrequest(APP_BT_STREAM_MANAGER_START,
BT_STREAM_THIRDPARTY_VOICE, 0, 0);
2022-08-15 04:20:27 -05:00
#endif
}
void app_ai_if_thirdparty_stop_stream_by_app_audio_manager(void) {
2022-08-15 04:20:27 -05:00
#ifdef __THIRDPARTY
app_audio_manager_sendrequest(APP_BT_STREAM_MANAGER_STOP,
BT_STREAM_THIRDPARTY_VOICE, 0, 0);
2022-08-15 04:20:27 -05:00
#endif
}
int app_ai_if_thirdparty_event_handle(THIRDPARTY_FUNC_ID funcId,
THIRDPARTY_EVENT_TYPE event_type) {
int ret = 0;
2022-08-15 04:20:27 -05:00
#ifdef __THIRDPARTY
ret = app_thirdparty_specific_lib_event_handle(funcId, event_type);
2022-08-15 04:20:27 -05:00
#endif
return ret;
2022-08-15 04:20:27 -05:00
}
void app_ai_if_thirdparty_init(void) {
2022-08-15 04:20:27 -05:00
#if defined(__THIRDPARTY) && defined(__AI_VOICE__)
app_ai_set_use_thirdparty(true);
app_ai_if_thirdparty_mempool_init();
app_thirdparty_callback_init(THIRDPARTY_DATA_COME_CALLBACK,
app_ai_voice_thirdparty_data_come_callback);
app_thirdparty_callback_init(THIRDPARTY_WAKE_UP_CALLBACK,
app_ai_voice_thirdparty_wake_up_callback);
app_thirdparty_callback_init(THIRDPARTY_START_SPEECH_CALLBACK,
app_ai_voice_thirdparty_start_speech_callback);
app_thirdparty_callback_init(THIRDPARTY_STOP_SPEECH_CALLBACK,
app_ai_voice_thirdparty_stop_speech_callback);
app_ai_if_thirdparty_event_handle(THIRDPARTY_FUNC_KWS, THIRDPARTY_INIT);
2022-08-15 04:20:27 -05:00
#endif
}