diff --git a/extension/mic-asp-sdk/srcs/mic_asp.c b/extension/mic-asp-sdk/srcs/mic_asp.c index 4ed63cde..209c32a3 100644 --- a/extension/mic-asp-sdk/srcs/mic_asp.c +++ b/extension/mic-asp-sdk/srcs/mic_asp.c @@ -4,38 +4,60 @@ #include #include #include +#include static PCMContainer_t g_pcmParam; static apr_aiwrap_t* g_aprHandle; static void* g_libInterface; +static pthread_t g_voiceThread; +static int g_isStopVoice; #define MAX_BUF_TIME 128000 #define NUM_CHANNELS 8 -#define SAMPLE_RATE 16000 +#define SAMPLE_RATE 16000 +#define BUFFER_SIZE (8 * 4 * 16000) -#define DECL_API_PROC(name) static Proc_##name _##name = NULL; -#define LOAD_API_PROC(name) _##name = (Proc_##name)dlsym(g_aprHandle, #name); +#define DECL_API_PROC(name) static Proc_##name _##name = NULL; +#define LOAD_API_PROC(name) _##name = (Proc_##name)dlsym(g_aprHandle, #name); API_LIST_SDK(DECL_API_PROC); +static void* voiceCB(void* arg) { + int len; + unsigned char* pBufData = (unsigned char*)malloc(BUFFER_SIZE); + + while (pBufData && g_isStopVoice) { + usleep(1000); + len = alsa_read_tdmin_pcm(&g_pcmParam); + memcpy(pBufData, g_pcmParam.data_buf, g_pcmParam.chunk_bytes); + + if (len > 0 && len <= BUFFER_SIZE) { + _apr_aiwrap_write(g_aprHandle, pBufData, len); + } + } + + g_isStopVoice = 2; + pthread_detach(pthread_self()); + + return NULL; +} void mic_asp_uninit() { - if(g_aprHandle) { + if (g_aprHandle) { _apr_aiwrap_stop(g_aprHandle); _apr_aiwrap_destory(g_aprHandle); dlclose(g_libInterface); - } + } alsa_tdmin_uninit(&g_pcmParam); } - int mic_asp_init(MicDataCallback cb) { int err; g_libInterface = dlopen("/usr/lib64/libaiwrap_6mic_c500.so", RTLD_LAZY); - if(NULL == g_libInterface) { + if (NULL == g_libInterface) { return -1; } @@ -44,28 +66,53 @@ int mic_asp_init(MicDataCallback cb) { API_LIST_SDK(LOAD_API_PROC); err = alsa_tdmin_init(&g_pcmParam, SAMPLE_RATE, NUM_CHANNELS, MAX_BUF_TIME); - if (err != 0) { + if (err != 0) { return -2; } err = _apr_aiwrap_create(&g_aprHandle, "/data/res/conf_6mic_c500_nr_release.ini", "/data/res/auth.param"); - if (err != 0) { + if (err != 0) { return -3; } err = _apr_aiwrap_cb_set(g_aprHandle, "presswk", (void*)cb, 0); - if(err !=0){ + if (err != 0) { return -4; } + err = _apr_aiwrap_param_set(g_aprHandle, "cae_dso", "/usr/lib64/lib6mic_circle.so"); + if (err != 0) { + return -5; + } + return 0; } int mic_asp_start() { - return _apr_aiwrap_start(g_aprHandle); + int err = _apr_aiwrap_start(g_aprHandle); + + if(err != 0) { + return -1; + } + + g_isStopVoice = 0; + + if (pthread_create(&g_voiceThread, NULL, voiceCB, NULL) == -1) { + return -2; + } + + return 0; } int mic_asp_stop() { + g_isStopVoice = 1; + + while(g_isStopVoice != 2) { + usleep(1000); + } + + g_isStopVoice = 0; + return _apr_aiwrap_stop(g_aprHandle); } \ No newline at end of file