audio_loop.c
来自「sample on embedded linux」· C语言 代码 · 共 222 行
C
222 行
#include <stdio.h>#include <unistd.h>#include "hi_common_api.h"#include "audio_comm.h"/************************************************************************ * Description for parament * * Parament 1: Loop times * * Parament 2: Loop type * * Parament 3: Codec type **************************************************************************/HI_S32 main(int argc, char *argv[]){ HI_S32 s32Ret, loop, loopMax, s32Flag; ADEC_CHN decChnID, decChnID2; AENC_CHN encChnID, encChnID2; AENC_STREAM_S Stream; HI_U8 u8Buf[4480]; AUDIO_RAWDATA_S rawData; AUDIO_CODEC_FORMAT_E enType = AUDIO_CODEC_FORMAT_G711A; /* You must prepare buffer for raw data */ rawData.ps16Addr = (HI_S16*)u8Buf; /* Get the codec type. The enum value should be input as number */ enType = (argc > 3) ? atoi(argv[3]) : AUDIO_CODEC_FORMAT_G711A; if(AUDIO_CODEC_FORMAT_G711A > enType || AUDIO_CODEC_FORMAT_BUTT <= enType) { printf("\n**Codec type error! The default type will be used!**\n\n"); enType = AUDIO_CODEC_FORMAT_G711A; } /* Get local loop type */ s32Flag = (argc > 2) ? atoi(argv[2]) : 1; /* Get loop times */ loopMax = (argc > 1) ? atoi(argv[1]) : 10000; printf("**************************************************************\n"); printf("./audio_loop [loop times] [loop type] [codec type]\n"); printf("./audio_loop %12d %11d %12d\n", loopMax, s32Flag, enType); printf("\n-------------------------------------------------------------\n"); printf("loop times: 0 ~ 4294967295\n"); printf("loop type : 0 ~ 1\n"); printf("codec type: 1 ~ 7[G711A, G711u, ADPCM, G726, AMR, AMRDTX, AAC]\n"); printf("**************************************************************\n"); fflush(stdout); /* Open the audio encoder and decoder */ s32Ret = HI_ADEC_Open(); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_AENC_Open(); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); if(AUDIO_CODEC_FORMAT_AAC == enType) { /* Set the sample rate of audio AD and SIO as 48K.*/ SetADSampleRate(SET_48K_SAMPLERATE); SetAiSampleRate(0, AUDIO_SAMPLE_RATE_48); } else { /* Set the sample rate of audio AD and SIO as 8K.*/ SetADSampleRate(SET_8K_SAMPLERATE); SetAiSampleRate(0, AUDIO_SAMPLE_RATE_8); } /* Create and start audio decoder channel */ s32Ret = HI_ADEC_CreateCH(0, enType, HI_TRUE, &decChnID); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_ADEC_StartCH(decChnID, 0); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); /* Create and start audio encoder channel */ s32Ret = HI_AENC_CreateCH(0, enType, HI_TRUE, &encChnID); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_AENC_StartCH(encChnID, 0); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); printf("Running please wait .........."); /* ** Build one AENC to ADEC local loop, and get the decoder data to ** play in decoder channel 1 */ if((s32Flag == 0) && (AUDIO_CODEC_FORMAT_AAC != enType)) { /* Create another ADEC channel 1 to play decoder data. */ s32Ret = HI_ADEC_CreateCH(1, enType, HI_TRUE, &decChnID2); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_ADEC_StartCH(decChnID2, 0); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); for(loop=0; loop<loopMax; loop++) { /* Get audio encode stream */ s32Ret = HI_AENC_GetStream(encChnID, &Stream); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); printf("\b\b\b\b\b\b\b\b\b\b%010d", Stream.u32FrameNum); fflush(stdout); /* Send stream to decoder channel 0 and play */ s32Ret = HI_ADEC_SendStream(decChnID, &Stream); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); /* Get the decode data from channel 0 */ s32Ret = HI_ADEC_GetStream(decChnID, &rawData); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); /* Send the decode data to channel 1 and play */ s32Ret = HI_ADEC_SendRawData(decChnID2, &rawData); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); } s32Ret = HI_ADEC_StopCH(decChnID2); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_ADEC_DestroyCH(decChnID2, HI_FALSE); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); } else if((s32Flag == 1) && (AUDIO_CODEC_FORMAT_AAC != enType)) { /* Create another ADEC channel 1 to play raw data. */ s32Ret = HI_ADEC_CreateCH(1, enType, HI_TRUE, &decChnID2); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_ADEC_StartCH(decChnID2, 0); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); /* Create another AENC channel 1 to get raw data. */ s32Ret = HI_AENC_CreateCH(1, enType, HI_TRUE, &encChnID2); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_AENC_StartCH(encChnID2, 0); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); for(loop=0; loop<loopMax; loop++) { /* Get encode data from AENC channel 0 */ s32Ret = HI_AENC_GetStream(encChnID, &Stream); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); printf("\b\b\b\b\b\b\b\b\b\b%010d", Stream.u32FrameNum); fflush(stdout); /* Send stream to decoder channel 0 and play */ s32Ret = HI_ADEC_SendStream(decChnID, &Stream); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); /* Get raw data from AENC channel 1 */ s32Ret = HI_AENC_GetRawData(encChnID2, &rawData); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); /* Send the raw data to channel 1 and play */ s32Ret = HI_ADEC_SendRawData(decChnID2, &rawData); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); } s32Ret = HI_AENC_StopCH(encChnID2); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_AENC_DestroyCH(encChnID2, HI_FALSE); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_ADEC_StopCH(decChnID2); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_ADEC_DestroyCH(decChnID2, HI_FALSE); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); } else if(AUDIO_CODEC_FORMAT_AAC == enType) /* ** AAC encoder is stereo, It will use AI channel 0 and 1 ** AAC decoder will use AO channel 0 and 1 */ { for(loop=0; loop<loopMax; loop++) { /* Get audio encode stream */ s32Ret = HI_AENC_GetStream(encChnID, &Stream); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); printf("\b\b\b\b\b\b\b\b\b\b%010d", Stream.u32FrameNum); fflush(stdout); /* Send stream to decoder channel 0 and play */ s32Ret = HI_ADEC_SendStream(decChnID, &Stream); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); } } else { printf("Loop type error!\n"); } s32Ret = HI_AENC_StopCH(encChnID); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_AENC_DestroyCH(encChnID, HI_FALSE); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_ADEC_StopCH(decChnID); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_ADEC_DestroyCH(decChnID, HI_FALSE); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_ADEC_Close(); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); s32Ret = HI_AENC_Close(); ASSERT_RETURN((HI_SUCCESS==s32Ret), s32Ret); return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?