pcm_Stream_deal.c 3.6 KB
#include <stdio.h>

#include "AudioDeal/FF_Statement.h"

#include "../Alsa/pcm_AlsaPlay.h"
#include "AudioDeal/AudioDeal.h"
#include "../Filter/FF_Filter.h"
#include "../Resample/pcm_Resample.h"
#include "JZsdkLib.h"

#include "./AudioStreamDeal.h"

#define MEM_BLOCK_LEN (4096) //设置内存块处理长度为4096

/***************************
 * 
 *  pcm数据池子,pcm数据的接入接口
 * 
 * ************/
int PCM_PooL_Interface_PcmData(struct AudioDealInfo *AD_Info,unsigned int in_sampleRate,  AVChannelLayout in_ch_layout, enum AVSampleFormat in_format , unsigned char* data, int dataSize)
{   
    T_JZsdkReturnCode ret;

    //这段代码遍历 data,将连续的 0x00 0x00 替换为 0x00 0x01。这可能是为了确保音频数据中没有静默的连续帧,或者是为了满足某种特定的播放要求。
    for(int i=0; i<dataSize; i+=2)
    {
        if(i+1 < dataSize && data[i]==0x00 && data[i+1]==0x00)
        {
            data[i+1] = 0x01; // MSB,LSB
        }
    }

    //分配了两个 AVFrame 对象,一个用于临时存储重采样后的数据(temp_frame),另一个用于从过滤器中获取数据(eq_frame)。
    AVFrame *temp_frame = av_frame_alloc();
    AVFrame *eq_frame = av_frame_alloc();

    //如果此次输入的音频数据不同于采样的当前设置,重设采样
    FF_Resample_Reset(AD_Info, in_sampleRate, in_ch_layout, in_format);

    //检查滤波器
    FF_Filter_Init(AD_Info, 0x00);

    int out_nb_samples = 0; //重采样输出的数据长度
    int UnDeal_DataSize = dataSize; //未处理的数据长度

    while( (UnDeal_DataSize > 0) && (AD_Info->AudioDeal_ResampleAndFilter_Execute_Flag == JZ_FLAGCODE_ON))
    {
        int dealLen = 0;
        if (UnDeal_DataSize > MEM_BLOCK_LEN)
        {
            dealLen = MEM_BLOCK_LEN;
            UnDeal_DataSize = UnDeal_DataSize  - dealLen;
        }
        else
        {
            dealLen = UnDeal_DataSize;
            UnDeal_DataSize = UnDeal_DataSize  - dealLen;                
        }
        
        //重采样  //仅处理一半样本量
        void *resampledData = FF_Resample_Send_And_Get_ResampleData(AD_Info, &data, dealLen/2, &out_nb_samples);
        
        //如果滤波打开
        //滤波处理
        if(AD_Info->FilterMode != JZ_FLAGCODE_OFF)
        {
            temp_frame->data[0] = (unsigned char*)resampledData;
            temp_frame->nb_samples = out_nb_samples;

            //将临时帧 放入 均衡滤波器
            FF_Filter_push_frame_to_fliter(AD_Info, temp_frame);

            while(AD_Info->AudioDeal_ResampleAndFilter_Execute_Flag == JZ_FLAGCODE_ON)
            {
                //得到滤波器输出的音频帧 eq_frame
                int fret = FF_Filter_get_frame_from_filter(AD_Info, eq_frame);
                if (fret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
                {
                    break;
                }

                //播放改滤波后的帧
                Pcm_AlsaPlay(AD_Info, (unsigned char*)eq_frame->data[0], eq_frame->nb_samples);

                av_frame_unref(eq_frame);
            }
        
            av_frame_unref(temp_frame);
        }
        else //不滤波
        {
            //直接播放
            //JZSDK_LOG_INFO("播放 %d 数据",out_nb_samples);
            Pcm_AlsaPlay(AD_Info ,resampledData, out_nb_samples);
        }

        //先改用保存
        //AudioDeal_Test_DataSave((unsigned char *)resampledData, out_nb_samples, ResampleInfo->Out_SampleRate, 1, 16); 

        FF_Resample_freeReasmpleData(resampledData);

        data += dealLen;
    }


    av_frame_free(&temp_frame);
    av_frame_free(&eq_frame);
}