⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audio_ctrl.c

📁 sample on embedded linux
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <hi_common_api.h> #include "audio_comm.h"HI_S32 Sample_Attribute(void){    HI_S32 s32Ret;    AUDIO_CH_ATTR_S Ao_attr_set,Ao_attr_get,Ai_attr_set,Ai_attr_get;;    /*Step 1: Set the AI attribute */    Ai_attr_set.enBitWidth     = AUDIO_BIT_WIDTH_16;    Ai_attr_set.enMode         = AUDIO_MODE_I2S_SLAVE;    Ai_attr_set.enSampleRate   = AUDIO_SAMPLE_RATE_8;    /* Just set the attribute of AI channel 0 */    s32Ret = HI_AENC_SetAIAttr(0,&Ai_attr_set);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);        /*Step 2: Get the attribute of AI channel 0 */    s32Ret =HI_AENC_GetAIAttr(0,&Ai_attr_get);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Check the attribute */    if((AUDIO_BIT_WIDTH_16    == (Ai_attr_get.enBitWidth ))\       &&(AUDIO_MODE_I2S_SLAVE== (Ai_attr_get.enMode))\       &&(AUDIO_SAMPLE_RATE_8 == (Ai_attr_get.enSampleRate)))    {        printf("AENC_GetAIAttr Done\n");    }    else    {        printf("AENC_GetAIAttr Failure\n");        return HI_FAILURE;    }        /*Step 3: Set the AO attribute */    Ao_attr_set.enBitWidth     = AUDIO_BIT_WIDTH_16;    Ao_attr_set.enMode         = AUDIO_MODE_I2S_SLAVE;    Ao_attr_set.enSampleRate   = AUDIO_SAMPLE_RATE_8;    /* Just set the attribute of AO channel 0 */    s32Ret = HI_ADEC_SetAOAttr(0,&Ao_attr_set);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);        /*Step 4: Get the attribute of AO channel 0 */    s32Ret =HI_ADEC_GetAOAttr(0,&Ao_attr_get);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Check the attribute */    if((AUDIO_BIT_WIDTH_16      == (Ao_attr_get.enBitWidth ))\       &&(AUDIO_MODE_I2S_SLAVE  == (Ao_attr_get.enMode))\       &&(AUDIO_SAMPLE_RATE_8   == (Ao_attr_get.enSampleRate)))    {        printf("ADEC_GetAOAttr Done\n");    }    else    {        printf("ADEC_GetAOAttr Failure\n");        return HI_FAILURE;    }        return s32Ret;}HI_S32 Sample_LocalLoop(void){    AENC_STREAM_S   AencStream;    AENC_CHN        AencChn;    ADEC_CHN        AdecChn;    HI_S32          s32Ret;    /* Set the sample rate of audio AD and SIO as 8K.*/    SetADSampleRate(SET_8K_SAMPLERATE);    SetAiSampleRate(0, AUDIO_SAMPLE_RATE_8);        /* Create the audio encoder channel */    s32Ret = HI_AENC_CreateCH(0, AUDIO_CODEC_FORMAT_G726, HI_TRUE, &AencChn);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Start the audio encoder channel */    s32Ret = HI_AENC_StartCH(AencChn, 0);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Create the audio decoder channel */    s32Ret = HI_ADEC_CreateCH(0, AUDIO_CODEC_FORMAT_G726, HI_TRUE, &AdecChn);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Start the audio decoder channel */    s32Ret = HI_ADEC_StartCH(AdecChn, 0);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Enable the AI2AO local loop */    s32Ret = HI_AI_EnableAI2AO(0, 0);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    #if 0    /* If you want get AENC stream, just do it. */    while(1)    {        s32Ret = HI_AENC_GetStream(AencChn, &AencStream);        ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);        //TODO: Do anything...        s32Ret = HI_AENC_ReleaseStream(AencChn, &AencStream);        ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    }    #endif     /* Or, you can just to sleep. */    sleep(60);    /* Stop the audio decoder channel */    s32Ret = HI_ADEC_StopCH(AdecChn);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Stop the audio encoder channel */    s32Ret = HI_AENC_StopCH(AencChn);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Destroy the audio decoder channel */    s32Ret = HI_ADEC_DestroyCH(AdecChn, HI_FALSE);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Destroy the audio encoder channel */    s32Ret = HI_AENC_DestroyCH(AencChn, HI_FALSE);    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    return HI_SUCCESS;}HI_S32 main(){    HI_S32 s32Ret;        /* Open the audio encoder */    s32Ret = HI_AENC_Open();    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Open the audio decoder */    s32Ret = HI_ADEC_Open();    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Check the attribute operation function */    s32Ret = Sample_Attribute();    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Run the local loop function */    s32Ret = Sample_LocalLoop();    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Close the audio decoder */    s32Ret = HI_ADEC_Close();    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);    /* Close the audio encoder */    s32Ret = HI_AENC_Close();    ASSERT_RETURN((HI_SUCCESS == s32Ret), s32Ret);        return HI_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -