乒乓Buffer

#define LEN_PING_PANG          (48000)  //1 seconds
typedef struct S_PINGPANG
{
    void* p1 ;
    void* p2 ;
    int  position;
}PINGPANG;
PINGPANG * Ping;

这里是结构和变量的定义

 

    Ping = malloc(sizeof(PINGPANG)) ;
    Ping->p1 = malloc(LEN_PING_PANG) ;
    Ping->p2 = malloc(LEN_PING_PANG) ;
    Ping->position=0;

初始化

    if (Ping != NULL){
        free(Ping->p1) ;
        free(Ping->p2) ;
        free(Ping) ;
    }

释放资源

void* savePingPang(void* p, int len) {
    if (len<=0) {
        if (Ping->position < LEN_PING_PANG) {
            memset(Ping->p1+Ping->position,'\0',LEN_PING_PANG-Ping->position);
            Ping->position=0;
            return Ping->p1 ;
        }
        else  {
            memset(Ping->p2+Ping->position-LEN_PING_PANG,'\0',2*LEN_PING_PANG-Ping->position);
            Ping->position=0;
            return Ping->p2 ;
        }
    }
    if (Ping->position + len < LEN_PING_PANG){
        memcpy(Ping->p1+Ping->position,p,len);
        Ping->position+=len;
        return NULL;
    }else if(Ping->position + len < (LEN_PING_PANG*2)){
        if (Ping->position < LEN_PING_PANG) {
            int byte_p1 = LEN_PING_PANG - Ping->position ;
            memcpy(Ping->p1+Ping->position,p,byte_p1) ;
            memcpy(Ping->p2,p+byte_p1,len-byte_p1);
            Ping->position+=len;
            return Ping->p1;
        }else{
            memcpy(Ping->p2+Ping->position-LEN_PING_PANG,p,len);
            Ping->position+=len;
            return NULL;
        }
    }else{
        int byte_p2=LEN_PING_PANG*2-Ping->position;
        memcpy(Ping->p2+Ping->position-LEN_PING_PANG,p,byte_p2) ;
        memcpy(Ping->p1,p+byte_p2,len-byte_p2);
        Ping->position=Ping->position+len-LEN_PING_PANG-LEN_PING_PANG;
        return Ping->p2;
    }
}
    void* buffer = savePingPang(ReSampleBuffer,lenOutput*sizeof(short)) ;
    if (buffer != NULL)
        WriteAudioData((void *)userContext, (void *)buffer,LEN_PING_PANG);

实现和调用

posted @ 2020-04-18 11:23  shinedream  阅读(859)  评论(0编辑  收藏  举报