/****************************************************************************** A simple program of Hisilicon mpp audio input/output/encoder/decoder implementation. Copyright (C), 2010-2021, Hisilicon Tech. Co., Ltd. ****************************************************************************** Modification: 2013-7 Created ******************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include "sample_comm.h" #include "acodec.h" #include "tlv320aic31.h" static PAYLOAD_TYPE_E gs_enPayloadType = PT_ADPCMA; //static HI_BOOL gs_bMicIn = HI_FALSE; static HI_BOOL gs_bAioReSample = HI_FALSE; static HI_BOOL gs_bUserGetMode = HI_FALSE; static HI_BOOL gs_bAoVolumeCtrl = HI_TRUE; static AUDIO_SAMPLE_RATE_E enInSampleRate = AUDIO_SAMPLE_RATE_BUTT; static AUDIO_SAMPLE_RATE_E enOutSampleRate = AUDIO_SAMPLE_RATE_BUTT; static HI_U32 u32AencPtNumPerFrm = 0; #define SAMPLE_DBG(s32Ret)\ do{\ printf("s32Ret=%#x,fuc:%s,line:%d\n", s32Ret, __FUNCTION__, __LINE__);\ }while(0) /****************************************************************************** * function : PT Number to String ******************************************************************************/ static char* SAMPLE_AUDIO_Pt2Str(PAYLOAD_TYPE_E enType) { if (PT_G711A == enType) { return "g711a"; } else if (PT_G711U == enType) { return "g711u"; } else if (PT_ADPCMA == enType) { return "adpcm"; } else if (PT_G726 == enType) { return "g726"; } else if (PT_LPCM == enType) { return "pcm"; } else { return "data"; } } /****************************************************************************** * function : Open Aenc File ******************************************************************************/ static FILE * SAMPLE_AUDIO_OpenAencFile(AENC_CHN AeChn, PAYLOAD_TYPE_E enType) { FILE* pfd; HI_CHAR aszFileName[FILE_NAME_LEN]; /* create file for save stream*/ snprintf(aszFileName, FILE_NAME_LEN, "audio_chn%d.%s", AeChn, SAMPLE_AUDIO_Pt2Str(enType)); pfd = fopen(aszFileName, "w+"); if (NULL == pfd) { printf("%s: open file %s failed\n", __FUNCTION__, aszFileName); return NULL; } printf("open stream file:\"%s\" for aenc ok\n", aszFileName); return pfd; } /****************************************************************************** * function : Open Adec File ******************************************************************************/ static FILE *SAMPLE_AUDIO_OpenAdecFile(ADEC_CHN AdChn, PAYLOAD_TYPE_E enType) { FILE* pfd; HI_CHAR aszFileName[FILE_NAME_LEN]; /* create file for save stream*/ snprintf(aszFileName, FILE_NAME_LEN ,"audio_chn%d.%s", AdChn, SAMPLE_AUDIO_Pt2Str(enType)); pfd = fopen(aszFileName, "rb"); if (NULL == pfd) { printf("%s: open file %s failed\n", __FUNCTION__, aszFileName); return NULL; } printf("open stream file:\"%s\" for adec ok\n", aszFileName); return pfd; } /****************************************************************************** * function : file -> Adec -> Ao ******************************************************************************/ HI_S32 SAMPLE_AUDIO_AdecAo(HI_VOID) { HI_S32 s32Ret; AUDIO_DEV AoDev = SAMPLE_AUDIO_AO_DEV; AO_CHN AoChn = 0; ADEC_CHN AdChn = 0; HI_S32 s32AoChnCnt; FILE* pfd = NULL; AIO_ATTR_S stAioAttr; #ifdef HI_ACODEC_TYPE_TLV320AIC31 stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_8000; stAioAttr.enBitwidth = AUDIO_BIT_WIDTH_16; stAioAttr.enWorkmode = AIO_MODE_I2S_MASTER; stAioAttr.enSoundmode = AUDIO_SOUND_MODE_MONO; stAioAttr.u32EXFlag = 0; stAioAttr.u32FrmNum = 30; stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM; stAioAttr.u32ChnCnt = 1; stAioAttr.u32ClkSel = 1; #else stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_8000; stAioAttr.enBitwidth = AUDIO_BIT_WIDTH_16; stAioAttr.enWorkmode = AIO_MODE_I2S_MASTER; stAioAttr.enSoundmode = AUDIO_SOUND_MODE_MONO; stAioAttr.u32EXFlag = 0; stAioAttr.u32FrmNum = 30; stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM; stAioAttr.u32ChnCnt = 1; stAioAttr.u32ClkSel = 0; #endif gs_bAioReSample = HI_FALSE; enInSampleRate = AUDIO_SAMPLE_RATE_BUTT; enOutSampleRate = AUDIO_SAMPLE_RATE_BUTT; s32Ret = SAMPLE_COMM_AUDIO_CfgAcodec(&stAioAttr); if (HI_SUCCESS != s32Ret) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } s32Ret = SAMPLE_COMM_AUDIO_StartAdec(AdChn, gs_enPayloadType); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } s32AoChnCnt = stAioAttr.u32ChnCnt; s32Ret = SAMPLE_COMM_AUDIO_StartAo(AoDev, s32AoChnCnt, &stAioAttr, enInSampleRate, gs_bAioReSample); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } s32Ret = SAMPLE_COMM_AUDIO_AoBindAdec(AoDev, AoChn, AdChn); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } pfd = SAMPLE_AUDIO_OpenAdecFile(AdChn, gs_enPayloadType); if (!pfd) { SAMPLE_DBG(HI_FAILURE); return HI_FAILURE; } s32Ret = SAMPLE_COMM_AUDIO_CreatTrdFileAdec(AdChn, pfd); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } printf("bind adec:%d to ao(%d,%d) ok \n", AdChn, AoDev, AoChn); printf("\nplease press twice ENTER to exit this sample\n"); getchar(); getchar(); s32Ret = SAMPLE_COMM_AUDIO_DestoryTrdFileAdec(AdChn); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } s32Ret = SAMPLE_COMM_AUDIO_StopAo(AoDev, s32AoChnCnt, gs_bAioReSample); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } s32Ret = SAMPLE_COMM_AUDIO_StopAdec(AdChn); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } s32Ret = SAMPLE_COMM_AUDIO_AoUnbindAdec(AoDev, AoChn, AdChn); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } return HI_SUCCESS; } /****************************************************************************** * function : Ai -> Aenc -> file * -> Adec -> Ao ******************************************************************************/ HI_S32 SAMPLE_AUDIO_AiAenc(HI_VOID) { HI_S32 i, s32Ret; AUDIO_DEV AiDev = SAMPLE_AUDIO_AI_DEV; AI_CHN AiChn; AUDIO_DEV AoDev = SAMPLE_AUDIO_AO_DEV; AO_CHN AoChn = 0; ADEC_CHN AdChn = 0; HI_S32 s32AiChnCnt; HI_S32 s32AoChnCnt; HI_S32 s32AencChnCnt; AENC_CHN AeChn; HI_BOOL bSendAdec = HI_TRUE; FILE *pfd = NULL; AIO_ATTR_S stAioAttr; #ifdef HI_ACODEC_TYPE_TLV320AIC31 stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_8000; stAioAttr.enBitwidth = AUDIO_BIT_WIDTH_16; stAioAttr.enWorkmode = AIO_MODE_I2S_MASTER; stAioAttr.enSoundmode = AUDIO_SOUND_MODE_MONO; stAioAttr.u32EXFlag = 0; stAioAttr.u32FrmNum = 30; stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM; stAioAttr.u32ChnCnt = 1; stAioAttr.u32ClkSel = 1; #else stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_8000; stAioAttr.enBitwidth = AUDIO_BIT_WIDTH_16; stAioAttr.enWorkmode = AIO_MODE_I2S_MASTER; stAioAttr.enSoundmode = AUDIO_SOUND_MODE_MONO; stAioAttr.u32EXFlag = 0; stAioAttr.u32FrmNum = 30; stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM; stAioAttr.u32ChnCnt = 1; stAioAttr.u32ClkSel = 0; #endif gs_bAioReSample = HI_FALSE; enInSampleRate = AUDIO_SAMPLE_RATE_BUTT; enOutSampleRate = AUDIO_SAMPLE_RATE_BUTT; u32AencPtNumPerFrm = stAioAttr.u32PtNumPerFrm; /******************************************** step 1: config audio codec ********************************************/ s32Ret = SAMPLE_COMM_AUDIO_CfgAcodec(&stAioAttr); if (HI_SUCCESS != s32Ret) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } /******************************************** step 2: start Ai ********************************************/ s32AiChnCnt = stAioAttr.u32ChnCnt; s32Ret = SAMPLE_COMM_AUDIO_StartAi(AiDev, s32AiChnCnt, &stAioAttr, enOutSampleRate, gs_bAioReSample, NULL); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } /******************************************** step 3: start Aenc ********************************************/ s32AencChnCnt = 1; s32Ret = SAMPLE_COMM_AUDIO_StartAenc(s32AencChnCnt, u32AencPtNumPerFrm, gs_enPayloadType); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } /******************************************** step 4: Aenc bind Ai Chn ********************************************/ for (i=0; i Ao(with fade in/out and volume adjust) ******************************************************************************/ HI_S32 SAMPLE_AUDIO_AiAo(HI_VOID) { HI_S32 s32Ret; HI_S32 s32AiChnCnt; HI_S32 s32AoChnCnt; AUDIO_DEV AiDev = SAMPLE_AUDIO_AI_DEV; AI_CHN AiChn = 0; AUDIO_DEV AoDev = SAMPLE_AUDIO_AO_DEV; AO_CHN AoChn = 0; AIO_ATTR_S stAioAttr; #ifdef HI_ACODEC_TYPE_TLV320AIC31 stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_8000; stAioAttr.enBitwidth = AUDIO_BIT_WIDTH_16; stAioAttr.enWorkmode = AIO_MODE_I2S_MASTER; stAioAttr.enSoundmode = AUDIO_SOUND_MODE_MONO; stAioAttr.u32EXFlag = 0; stAioAttr.u32FrmNum = 30; stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM; stAioAttr.u32ChnCnt = 1; stAioAttr.u32ClkSel = 1; #else // inner acodec stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_8000; stAioAttr.enBitwidth = AUDIO_BIT_WIDTH_16; stAioAttr.enWorkmode = AIO_MODE_I2S_MASTER; stAioAttr.enSoundmode = AUDIO_SOUND_MODE_MONO; stAioAttr.u32EXFlag = 0; stAioAttr.u32FrmNum = 30; stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM; stAioAttr.u32ChnCnt = 1; stAioAttr.u32ClkSel = 0; #endif /* config ao resample attr if needed */ if (HI_TRUE == gs_bAioReSample) { stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_32000; stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM * 4; /* ai 32k -> 8k */ enOutSampleRate = AUDIO_SAMPLE_RATE_8000; /* ao 8k -> 32k */ enInSampleRate = AUDIO_SAMPLE_RATE_8000; } else { enInSampleRate = AUDIO_SAMPLE_RATE_BUTT; enOutSampleRate = AUDIO_SAMPLE_RATE_BUTT; } /* resample and anr should be user get mode */ gs_bUserGetMode = (HI_TRUE == gs_bAioReSample) ? HI_TRUE : HI_FALSE; /* config audio codec */ s32Ret = SAMPLE_COMM_AUDIO_CfgAcodec(&stAioAttr); if (HI_SUCCESS != s32Ret) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } /* enable AI channle */ s32AiChnCnt = stAioAttr.u32ChnCnt; s32Ret = SAMPLE_COMM_AUDIO_StartAi(AiDev, s32AiChnCnt, &stAioAttr, enOutSampleRate, gs_bAioReSample, NULL); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } /* enable AO channle */ s32AoChnCnt = stAioAttr.u32ChnCnt; s32Ret = SAMPLE_COMM_AUDIO_StartAo(AoDev, s32AoChnCnt, &stAioAttr, enInSampleRate, gs_bAioReSample); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } /* bind AI to AO channle */ if (HI_TRUE == gs_bUserGetMode) { s32Ret = SAMPLE_COMM_AUDIO_CreatTrdAiAo(AiDev, AiChn, AoDev, AoChn); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } } else { s32Ret = SAMPLE_COMM_AUDIO_AoBindAi(AiDev, AiChn, AoDev, AoChn); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } } printf("ai(%d,%d) bind to ao(%d,%d) ok\n", AiDev, AiChn, AoDev, AoChn); if(HI_TRUE == gs_bAoVolumeCtrl) { s32Ret = SAMPLE_COMM_AUDIO_CreatTrdAoVolCtrl(AoDev); if(s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } } printf("\nplease press twice ENTER to exit this sample\n"); getchar(); getchar(); if(HI_TRUE == gs_bAoVolumeCtrl) { s32Ret = SAMPLE_COMM_AUDIO_DestoryTrdAoVolCtrl(AoDev); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } } if (HI_TRUE == gs_bUserGetMode) { s32Ret = SAMPLE_COMM_AUDIO_DestoryTrdAi(AiDev, AiChn); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } } else { s32Ret = SAMPLE_COMM_AUDIO_AoUnbindAi(AiDev, AiChn, AoDev, AoChn); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } } s32Ret = SAMPLE_COMM_AUDIO_StopAi(AiDev, s32AiChnCnt, gs_bAioReSample, HI_FALSE); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } s32Ret = SAMPLE_COMM_AUDIO_StopAo(AoDev, s32AoChnCnt, gs_bAioReSample); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } return HI_SUCCESS; } /****************************************************************************** * function : Ai -> Ao ******************************************************************************/ HI_S32 SAMPLE_AUDIO_AiVqeProcessAo(HI_VOID) { HI_S32 i, s32Ret; AUDIO_DEV AiDev = SAMPLE_AUDIO_AI_DEV; AUDIO_DEV AoDev = SAMPLE_AUDIO_AO_DEV; HI_S32 s32AiChnCnt; HI_S32 s32AoChnCnt; AIO_ATTR_S stAioAttr; AI_VQE_CONFIG_S stAiVqeAttr; #ifdef HI_ACODEC_TYPE_TLV320AIC31 stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_8000; stAioAttr.enBitwidth = AUDIO_BIT_WIDTH_16; stAioAttr.enWorkmode = AIO_MODE_I2S_MASTER; stAioAttr.enSoundmode = AUDIO_SOUND_MODE_MONO; stAioAttr.u32EXFlag = 0; stAioAttr.u32FrmNum = 30; stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM; stAioAttr.u32ChnCnt = 1; stAioAttr.u32ClkSel = 1; #else stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_8000; stAioAttr.enBitwidth = AUDIO_BIT_WIDTH_16; stAioAttr.enWorkmode = AIO_MODE_I2S_MASTER; stAioAttr.enSoundmode = AUDIO_SOUND_MODE_MONO; stAioAttr.u32EXFlag = 0; stAioAttr.u32FrmNum = 30; stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM; stAioAttr.u32ChnCnt = 1; stAioAttr.u32ClkSel = 0; #endif gs_bAioReSample = HI_FALSE; enInSampleRate = AUDIO_SAMPLE_RATE_BUTT; enOutSampleRate = AUDIO_SAMPLE_RATE_BUTT; stAiVqeAttr.s32WorkSampleRate = AUDIO_SAMPLE_RATE_8000; stAiVqeAttr.s32FrameSample = SAMPLE_AUDIO_PTNUMPERFRM; stAiVqeAttr.enWorkstate = VQE_WORKSTATE_COMMON; stAiVqeAttr.bAecOpen = HI_TRUE; stAiVqeAttr.stAecCfg.bUsrMode = HI_FALSE; stAiVqeAttr.stAecCfg.s8CngMode = AUDIO_AEC_MODE_CLOSE; stAiVqeAttr.bAgcOpen = HI_TRUE; stAiVqeAttr.stAgcCfg.bUsrMode = HI_FALSE; stAiVqeAttr.bAnrOpen = HI_TRUE; stAiVqeAttr.stAnrCfg.bUsrMode = HI_FALSE; stAiVqeAttr.bHpfOpen = HI_TRUE; stAiVqeAttr.stHpfCfg.enHpfFreq = AUDIO_HPF_FREQ_150; stAiVqeAttr.bRnrOpen = HI_FALSE; stAiVqeAttr.bEqOpen = HI_FALSE; /******************************************** step 1: config audio codec ********************************************/ s32Ret = SAMPLE_COMM_AUDIO_CfgAcodec(&stAioAttr); if (HI_SUCCESS != s32Ret) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } /******************************************** step 2: start Ai ********************************************/ s32AiChnCnt = stAioAttr.u32ChnCnt; s32Ret = SAMPLE_COMM_AUDIO_StartAi(AiDev, s32AiChnCnt, &stAioAttr, enOutSampleRate, gs_bAioReSample, &stAiVqeAttr); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } /******************************************** step 3: start Ao ********************************************/ s32AoChnCnt = stAioAttr.u32ChnCnt; s32Ret = SAMPLE_COMM_AUDIO_StartAo(AoDev, s32AoChnCnt, &stAioAttr, enInSampleRate, gs_bAioReSample); if (s32Ret != HI_SUCCESS) { SAMPLE_DBG(s32Ret); return HI_FAILURE; } /******************************************** step 4: Ao bind Ai Chn ********************************************/ for (i=0; i