const int32_t NUMBER_BASE = 10;
const int32_t CHANNEL_COUNT = 2;
const int32_t SAMPLERATE_16000 = 16000;
-const uint32_t AI_VOICE_BUFFER_SIZE = 0x800; // 2KB buffer for AI voice
-const uint32_t AI_VOICE_QUEUE_SIZE = 10; // 队列大小
+const uint32_t AI_VOICE_BUFFER_SIZE = 0x1000; // 2KB buffer for AI voice
+const uint32_t AI_VOICE_QUEUE_SIZE = 20; // 队列大小
// 定义音频数据回调函数类型
* @param size 音频数据大小
* @return 0 成功,-1 失败
*/
+#ifdef BOARD_LEINIAO
int32_t AI_WriteAudioData(const uint8_t* data, uint32_t size)
{
if (data == nullptr || size == 0) {
if (ret != 0) {
// MEDIA_ERR_LOG("Failed to write audio data to output stream, ret=%d", ret);
// return -1;
- usleep(1000*500);
+ usleep(30*size);
goto AI_WRITE;
}
// MEDIA_INFO_LOG("Successfully wrote %u bytes of audio data to output stream", size);
return 0;
}
+#else
+int32_t AI_WriteAudioData(const uint8_t* data, uint32_t size)
+{
+ if (data == nullptr || size == 0) {
+ MEDIA_ERR_LOG("Invalid audio data parameters");
+ return -1;
+ }
+ AI_WRITE:
+ if(g_aiVoiceContext->interruptFlag)
+ return -1;
+ // 获取全局互斥锁(自动初始化)
+ MediaMutexHandle mutex = GetGlobalMutex();
+ if (mutex == nullptr) {
+ MEDIA_ERR_LOG("Failed to get global mutex");
+ return -1;
+ }
+
+ // 检查AI语音上下文是否存在
+ MediaMutexLock(mutex);
+ if (g_aiVoiceContext == nullptr) {
+ MEDIA_ERR_LOG("AI voice context is null, please start AI voice first");
+ MediaMutexUnLock(mutex);
+ return -1;
+ }
+ MediaMutexUnLock(g_aiVoiceContextMutex);
+ // 写入音频数据到输出流
+ int32_t ret = AudioStreamOutStreamWrite(data, size);
+ usleep(50*size);
+ if (ret != 0) {
+ // MEDIA_ERR_LOG("Failed to write audio data to output stream, ret=%d", ret);
+ // return -1;
+ usleep(1000*500);
+ goto AI_WRITE;
+ }
+
+ // MEDIA_INFO_LOG("Successfully wrote %u bytes of audio data to output stream", size);
+ return 0;
+}
+#endif
/**
* @brief 检查AI语音服务是否正在运行
* @return true 正在运行,false 未运行
}
total_written += written;
#endif
- AI_SendAudioDataToQueue((const uint8_t*)client_data.responseBuf, client_data.contentBlockLen);
+ ret = AI_SendAudioDataToQueue((const uint8_t*)client_data.responseBuf, client_data.contentBlockLen);
+ if (ret != 0)
+ {
+ printf("download mp3 file advanced: write failed1, ret=%d\n", ret);
+ // 写失败重新写
+ LOS_Msleep(100);
+ ret = AI_SendAudioDataToQueue((const uint8_t*)client_data.responseBuf, (uint32_t)client_data.contentBlockLen);
+ if (ret != 0)
+ {
+ printf("download mp3 file advanced: write failed2, ret=%d\n", ret);
+ LOS_Msleep(100);
+ ret = AI_SendAudioDataToQueue((const uint8_t*)client_data.responseBuf, (uint32_t)client_data.contentBlockLen);
+ if (ret != 0)
+ {
+ printf("download mp3 file advanced: write failed3, ret=%d\n", ret);
+ }
+ }
+ }
}
else
{
void ln_ai_record_stop(void)
{
+ g_is_ai_record_start = 0;
if (g_aiCtx_fp)
{
fclose(g_aiCtx_fp);
g_aiCtx_fp = NULL;
}
- g_is_ai_record_start = 0;
}
// 雷鸟http服务初始化