0

I am merging two pcm data and the resultant pcm is having additional noise of grrrrrrrrrr. My code is :

int main(void)
{

    FILE *rCAudio;
    FILE *raudio;
    FILE *wtest;

    rCAudio=fopen("Audio1.pcm","rb");        //Reading first pcm file
    if(rCAudio==NULL)
        cout<<"Errr";


    raudio=fopen("Audio2.pcm","rb");        //Reading second pcm file
    if(raudio==NULL)
        cout<<"Errr";

    fopen_s(&wtest,"AudioMerge.pcm","ab"); // Writing final pcm file

    short* first= new short[1792];;
    short* second= new short[1792];;
    short* merge = new short[1792];
    short sample1,sample2;

    while(1)
    {
        fread(first,2,1792,rCAudio);
        fread(second,2,1792,raudio);

        for(int j=0;j<1792;j++)
        {
            sample1 = first[j];
            sample2 = second[j];
            int mixedi=(int)sample1 + (int)sample2;
            if (mixedi>32767) mixedi=32767;
            if (mixedi<-32768) mixedi=-32768;

            merge[j] =(short)mixedi;
        }
        fwrite(merge,2,1972,wtest);
    }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423

1 Answers1

0

I got the solution , the problem was: I have written Audio1.pcm with 4096 bytes at a time in BYTE and Audio2.pcm with 4096 bytes at a time in BYTE. But i was reading 1972 bytes at a time in short.

So i corrected it by reading 4096 bytes at a time in BYTE and save by third merge file with 4096 bytes at a time in BYTE.