3 Star 0 Fork 2

Runkey/doorbell

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sample_audio.c 16.46 KB
一键复制 编辑 原始数据 按行查看 历史
root 提交于 2017-05-14 15:01 . init
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
/******************************************************************************
A simple program of Hisilicon HI3518 audio input/output/encoder/decoder implementation.
Copyright (C), 2010-2012, Hisilicon Tech. Co., Ltd.
******************************************************************************
Modification: 2012-7-3 Created
******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include "sample_comm.h"
#include "acodec.h"
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* End of #ifdef __cplusplus */
#define SAMPLE_AUDIO_PTNUMPERFRM 320
static PAYLOAD_TYPE_E gs_enPayloadType = PT_ADPCMA;
static HI_BOOL gs_bMicIn = HI_TRUE;
static HI_BOOL gs_bAiAnr = HI_FALSE;
static HI_BOOL gs_bAioReSample = HI_FALSE;
static HI_BOOL gs_bUserGetMode = HI_FALSE;
static AUDIO_RESAMPLE_ATTR_S *gs_pstAiReSmpAttr = NULL;
static AUDIO_RESAMPLE_ATTR_S *gs_pstAoReSmpAttr = NULL;
#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[128];
/* create file for save stream*/
sprintf(aszFileName, "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[128];
/* create file for save stream*/
sprintf(aszFileName, "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(AIO_ATTR_S *pstAioAttr)
{
HI_S32 s32Ret;
AUDIO_DEV AoDev = 0;
AO_CHN AoChn = 0;
ADEC_CHN AdChn = 0;
FILE *pfd = NULL;
if (NULL == pstAioAttr)
{
printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "NULL pointer");
return HI_FAILURE;
}
s32Ret = SAMPLE_COMM_AUDIO_CfgAcodec(pstAioAttr, gs_bMicIn);
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;
}
s32Ret = SAMPLE_COMM_AUDIO_StartAo(AoDev, AoChn, pstAioAttr, gs_pstAoReSmpAttr);
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();
SAMPLE_COMM_AUDIO_DestoryTrdFileAdec(AdChn);
SAMPLE_COMM_AUDIO_StopAo(AoDev, AoChn, gs_bAioReSample);
SAMPLE_COMM_AUDIO_StopAdec(AdChn);
SAMPLE_COMM_AUDIO_AoUnbindAdec(AoDev, AoChn, AdChn);
return HI_SUCCESS;
}
/******************************************************************************
* function : Ai -> Aenc -> file
* -> Adec -> Ao
******************************************************************************/
HI_S32 SAMPLE_AUDIO_AiAenc(AIO_ATTR_S *pstAioAttr)
{
HI_S32 i, s32Ret;
AUDIO_DEV AiDev = 0;
AI_CHN AiChn;
AUDIO_DEV AoDev = 0;
AO_CHN AoChn = 0;
ADEC_CHN AdChn = 0;
HI_S32 s32AiChnCnt;
HI_S32 s32AencChnCnt;
AENC_CHN AeChn;
HI_BOOL bSendAdec = HI_TRUE;
FILE *pfd = NULL;
/* config ai aenc dev attr */
if (NULL == pstAioAttr)
{
printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "NULL pointer");
return HI_FAILURE;
}
/********************************************
step 1: config audio codec
********************************************/
s32Ret = SAMPLE_COMM_AUDIO_CfgAcodec(pstAioAttr, gs_bMicIn);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_DBG(s32Ret);
return HI_FAILURE;
}
/********************************************
step 2: start Ai
********************************************/
s32AiChnCnt = pstAioAttr->u32ChnCnt;
s32AencChnCnt = 1;//s32AiChnCnt;
s32Ret = SAMPLE_COMM_AUDIO_StartAi(AiDev, s32AiChnCnt, pstAioAttr, gs_bAiAnr, gs_pstAiReSmpAttr);
if (s32Ret != HI_SUCCESS)
{
SAMPLE_DBG(s32Ret);
return HI_FAILURE;
}
/********************************************
step 3: start Aenc
********************************************/
s32Ret = SAMPLE_COMM_AUDIO_StartAenc(s32AencChnCnt, gs_enPayloadType);
if (s32Ret != HI_SUCCESS)
{
SAMPLE_DBG(s32Ret);
return HI_FAILURE;
}
/********************************************
step 4: Aenc bind Ai Chn
********************************************/
for (i=0; i<s32AencChnCnt; i++)
{
AeChn = i;
AiChn = 0;
if (HI_TRUE == gs_bUserGetMode)
{
s32Ret = SAMPLE_COMM_AUDIO_CreatTrdAiAenc(AiDev, AiChn, AeChn);
if (s32Ret != HI_SUCCESS)
{
SAMPLE_DBG(s32Ret);
return HI_FAILURE;
}
}
else
{
s32Ret = SAMPLE_COMM_AUDIO_AencBindAi(AiDev, AiChn, AeChn);
if (s32Ret != HI_SUCCESS)
{
SAMPLE_DBG(s32Ret);
return s32Ret;
}
}
printf("Ai(%d,%d) bind to AencChn:%d ok!\n",AiDev , AiChn, AeChn);
}
/********************************************
step 5: start Adec & Ao. ( if you want )
********************************************/
if (HI_TRUE == bSendAdec)
{
s32Ret = SAMPLE_COMM_AUDIO_StartAdec(AdChn, gs_enPayloadType);
if (s32Ret != HI_SUCCESS)
{
SAMPLE_DBG(s32Ret);
return HI_FAILURE;
}
s32Ret = SAMPLE_COMM_AUDIO_StartAo(AoDev, AoChn, pstAioAttr, gs_pstAoReSmpAttr);
if (s32Ret != HI_SUCCESS)
{
SAMPLE_DBG(s32Ret);
return HI_FAILURE;
}
pfd = SAMPLE_AUDIO_OpenAencFile(AdChn, gs_enPayloadType);
if (!pfd)
{
SAMPLE_DBG(HI_FAILURE);
return HI_FAILURE;
}
s32Ret = SAMPLE_COMM_AUDIO_CreatTrdAencAdec(AeChn, AdChn, pfd);
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;
}
printf("bind adec:%d to ao(%d,%d) ok \n", AdChn, AoDev, AoChn);
}
printf("\nplease press twice ENTER to exit this sample\n");
return 0;
getchar();
getchar();
/********************************************
step 6: exit the process
********************************************/
if (HI_TRUE == bSendAdec)
{
SAMPLE_COMM_AUDIO_DestoryTrdAencAdec(AdChn);
SAMPLE_COMM_AUDIO_StopAo(AoDev, AoChn, gs_bAioReSample);
SAMPLE_COMM_AUDIO_StopAdec(AdChn);
SAMPLE_COMM_AUDIO_AoUnbindAdec(AoDev, AoChn, AdChn);
}
for (i=0; i<s32AencChnCnt; i++)
{
AeChn = i;
AiChn = i;
if (HI_TRUE == gs_bUserGetMode)
{
SAMPLE_COMM_AUDIO_DestoryTrdAi(AiDev, AiChn);
}
else
{
SAMPLE_COMM_AUDIO_AencUnbindAi(AiDev, AiChn, AeChn);
}
}
SAMPLE_COMM_AUDIO_StopAenc(s32AencChnCnt);
SAMPLE_COMM_AUDIO_StopAi(AiDev, s32AiChnCnt, gs_bAiAnr, gs_bAioReSample);
return HI_SUCCESS;
}
/******************************************************************************
* function : Ai -> Ao
******************************************************************************/
HI_S32 SAMPLE_AUDIO_AiAo(AIO_ATTR_S *pstAioAttr)
{
HI_S32 s32Ret, s32AiChnCnt;
AUDIO_DEV AiDev = 0;
AI_CHN AiChn = 0;
AUDIO_DEV AoDev = 0;
AO_CHN AoChn = 0;
/* config aio dev attr */
if (NULL == pstAioAttr)
{
printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "NULL pointer");
return HI_FAILURE;
}
/* config audio codec */
s32Ret = SAMPLE_COMM_AUDIO_CfgAcodec(pstAioAttr, gs_bMicIn);
if (HI_SUCCESS != s32Ret)
{
SAMPLE_DBG(s32Ret);
return HI_FAILURE;
}
/* enable AI channle */
s32AiChnCnt = pstAioAttr->u32ChnCnt;
s32Ret = SAMPLE_COMM_AUDIO_StartAi(AiDev, s32AiChnCnt, pstAioAttr, gs_bAiAnr, gs_pstAiReSmpAttr);
if (s32Ret != HI_SUCCESS)
{
SAMPLE_DBG(s32Ret);
return HI_FAILURE;
}
/* enable AO channle */
pstAioAttr->u32ChnCnt = 2;
s32Ret = SAMPLE_COMM_AUDIO_StartAo(AoDev, AoChn, pstAioAttr, gs_pstAoReSmpAttr);
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);
printf("\nplease press twice ENTER to exit this sample\n");
while (1)
sleep(1);
getchar();
getchar();
if (HI_TRUE == gs_bUserGetMode)
{
SAMPLE_COMM_AUDIO_DestoryTrdAi(AiDev, AiChn);
}
else
{
SAMPLE_COMM_AUDIO_AoUnbindAi(AiDev, AiChn, AoDev, AoChn);
}
SAMPLE_COMM_AUDIO_StopAi(AiDev, s32AiChnCnt, gs_bAiAnr, gs_bAioReSample);
SAMPLE_COMM_AUDIO_StopAo(AoDev, AoChn, gs_bAioReSample);
return HI_SUCCESS;
}
HI_VOID SAMPLE_AUDIO_Usage(void)
{
printf("\n/************************************/\n");
printf("press sample command as follows!\n");
printf("1: send audio frame to AENC channel form AI, save them\n");
printf("2: read audio stream from file,decode and send AO\n");
printf("3: start AI to AO loop\n");
printf("q: quit whole audio sample\n\n");
printf("sample command:");
}
/******************************************************************************
* function : to process abnormal case
******************************************************************************/
void SAMPLE_AUDIO_HandleSig(HI_S32 signo)
{
if (SIGINT == signo || SIGTSTP == signo)
{
SAMPLE_COMM_SYS_Exit();
printf("\033[0;31mprogram exit abnormally!\033[0;39m\n");
}
exit(0);
}
/******************************************************************************
* function : main
******************************************************************************/
HI_S32 audio_main(int argc, char *argv[])
{
char ch;
HI_S32 s32Ret= HI_SUCCESS;
VB_CONF_S stVbConf;
AIO_ATTR_S stAioAttr;
/* arg 1 is audio payload type */
#if 0
if (argc >= 2)
{
gs_enPayloadType = atoi(argv[1]);
if (gs_enPayloadType != PT_G711A && gs_enPayloadType != PT_G711U &&\
gs_enPayloadType != PT_ADPCMA && gs_enPayloadType != PT_G726 &&\
gs_enPayloadType != PT_LPCM)
{
printf("payload type invalid!\n");
printf("\nargv[1]:%d is payload type ID, suport such type:\n", gs_enPayloadType);
printf("%d:g711a, %d:g711u, %d:adpcm, %d:g726, %d:lpcm\n",
PT_G711A, PT_G711U, PT_ADPCMA, PT_G726, PT_LPCM);
return HI_FAILURE;
}
}
#endif
gs_enPayloadType = PT_LPCM;
/* init stAio. all of cases will use it */
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 = 1;
stAioAttr.u32FrmNum = 30;
stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM;
stAioAttr.u32ChnCnt = 2;
stAioAttr.u32ClkSel = 1;
/* config ao resample attr if needed */
if (HI_TRUE == gs_bAioReSample)
{
AUDIO_RESAMPLE_ATTR_S stAiReSampleAttr;
AUDIO_RESAMPLE_ATTR_S stAoReSampleAttr;
stAioAttr.enSamplerate = AUDIO_SAMPLE_RATE_32000;
stAioAttr.u32PtNumPerFrm = SAMPLE_AUDIO_PTNUMPERFRM * 4;
/* ai 32k -> 8k */
stAiReSampleAttr.u32InPointNum = SAMPLE_AUDIO_PTNUMPERFRM * 4;
stAiReSampleAttr.enInSampleRate = AUDIO_SAMPLE_RATE_32000;
stAiReSampleAttr.enReSampleType = AUDIO_RESAMPLE_4X1;
gs_pstAiReSmpAttr = &stAiReSampleAttr;
/* ao 8k -> 32k */
stAoReSampleAttr.u32InPointNum = SAMPLE_AUDIO_PTNUMPERFRM;
stAoReSampleAttr.enInSampleRate = AUDIO_SAMPLE_RATE_8000;
stAoReSampleAttr.enReSampleType = AUDIO_RESAMPLE_1X4;
gs_pstAoReSmpAttr = &stAoReSampleAttr;
}
else
{
gs_pstAiReSmpAttr = NULL;
gs_pstAoReSmpAttr = NULL;
}
/* resample and anr should be user get mode */
gs_bUserGetMode = (HI_TRUE == gs_bAioReSample || HI_TRUE == gs_bAiAnr) ? HI_TRUE : HI_FALSE;
signal(SIGINT, SAMPLE_AUDIO_HandleSig);
signal(SIGTERM, SAMPLE_AUDIO_HandleSig);
///memset(&stVbConf, 0, sizeof(VB_CONF_S));
//s32Ret = SAMPLE_COMM_SYS_Init(&stVbConf);
//if (HI_SUCCESS != s32Ret)
{
// printf("%s: system init failed with %d!\n", __FUNCTION__, s32Ret);
//return HI_FAILURE;
}
//SAMPLE_AUDIO_Usage();
//s32Ret = SAMPLE_AUDIO_AiAo(&stAioAttr);
SAMPLE_AUDIO_AiAenc(&stAioAttr);
return 0;
while ((ch = getchar()) != 'q')
{
switch (ch)
{
case '1':
{
s32Ret = SAMPLE_AUDIO_AiAenc(&stAioAttr);/* send audio frame to AENC channel form AI, save them*/
break;
}
case '2':
{
s32Ret = SAMPLE_AUDIO_AdecAo(&stAioAttr);/* read audio stream from file,decode and send AO*/
break;
}
case '3':
{
s32Ret = SAMPLE_AUDIO_AiAo(&stAioAttr);/* AI to AO*/
break;
}
default:
{
SAMPLE_AUDIO_Usage();
break;
}
}
if (s32Ret != HI_SUCCESS)
{
break;
}
}
SAMPLE_COMM_SYS_Exit();
return HI_SUCCESS;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/RunKey/doorbell.git
[email protected]:RunKey/doorbell.git
RunKey
doorbell
doorbell
master

搜索帮助