音频文件pcm的转换

本例子中的音频文件都是 44100hz,双声道,16bit的
雷宵骅关于pcm转换的博客:https://blog.csdn.net/leixiaohua1020/article/details/50534316
资源文件:链接: https://pan.baidu.com/s/1-dEwS9D15Yi2pNP2Wea5sw 提取码: ja7i
一,分离PCM16LE双声道音频采样数据的左声道和右声道

void getLeftAndRright(uint8_t *pcm, uint8_t *pcm_out_left, uint8_t *pcm_out_right, int len){
    int16_t *p0 = (int16_t *)pcm;
    int16_t *p1 = (int16_t *)pcm_out_left;
    int16_t *p2 = (int16_t *)pcm_out_right;
    for (int i = 0; i < len/2; i++)
    {
        if (i % 2 == 0)//左声道
        {
            *(p1 + i/2) = *(p0 + i);
        }
        else//右声道
        {
            *(p2 + i / 2) = *(p0 + i);
        }

    }
}
int main(int argc, char **argv){
    FILE *fp = fopen("NocturneNo2inEflat_44.1k_s16le.pcm","rb+");
    FILE *fp1 = fopen("outpcmleft.pcm","wb+");
    FILE *fp2 = fopen("outpcmright.pcm", "wb+");
    
    int len = 44100 * 2 * 2 * 60;
    unsigned char *sample = (unsigned char *)malloc(len);
    int i = 0;
    while (!feof(fp))
    {
        int size = fread(sample + i,1,4,fp);
        if (size <= 0)
        {
            break;
        }
        i += 4;
    }
    len = i;
    fclose(fp);
    unsigned char *sample_left = (unsigned char *)malloc(len);
    unsigned char *sample_right = (unsigned char *)malloc(len);
    getLeftAndRright(sample,sample_left,sample_right,len);
    fwrite(sample_left, 1, len/2, fp1);
    fwrite(sample_right, 1, len/2, fp2);
    free(sample);
    free(sample_left);
    free(sample_right);
    fclose(fp1);
    fclose(fp2);
    return getchar();
}

二,将PCM16LE双声道音频采样数据中左声道的音量降到1/4

void getLeftHalf(uint8_t *pcm,uint8_t *pcm_out,int len){
    int16_t *p0 = (int16_t *)pcm;
    int16_t *p1 = (int16_t *)pcm_out;
    for (int i = 0; i < len/2; i++)
    {
        if (i % 2 == 0)//左声道
        {
            short samplenum = *(p0 + i);
            samplenum = samplenum / 4;
            *(p1 + i) = samplenum;
        }
        else//右声道
        {
            *(p1 + i) = *(p0 + i);
        }
    }
}
int main(int argc, char **argv){
    FILE *fp = fopen("NocturneNo2inEflat_44.1k_s16le.pcm","rb+");
    FILE *fp1 = fopen("outpcmlefthalf.pcm","wb+");
    
    int len = 44100 * 2 * 2 * 60;
    unsigned char *sample = (unsigned char *)malloc(len);
    int i = 0;
    while (!feof(fp))
    {
        int size = fread(sample + i,1,4,fp);
        if (size <= 0)
        {
            break;
        }
        i += 4;
    }
    len = i;
    fclose(fp);
    unsigned char *sample_out = (unsigned char *)malloc(len);
    getLeftHalf(sample, sample_out,len);
    fwrite(sample_out, 1, len, fp1);
    free(sample);
    free(sample_out);
    fclose(fp1);
    return getchar();
}

三,将PCM16LE双声道音频采样数据的声音速度提高一倍

void getDoubleSpeed(uint8_t *pcm, uint8_t *pcm_out, int len){
    int16_t *p0 = (int16_t *)pcm;
    int16_t *p1 = (int16_t *)pcm_out;
    for (int i = 0; i < len/4; i++)
    {
        if (i %2 == 0){
            *(p1 + i) = *(p0 + i * 2);
            *(p1 + i + 1) = *(p0 + i * 2 + 1);
        }
    }
}
int main(int argc, char **argv){
    FILE *fp = fopen("NocturneNo2inEflat_44.1k_s16le.pcm","rb+");
    FILE *fp1 = fopen("outpcmdoublespeed.pcm","wb+");
    
    int len = 44100 * 2 * 2 * 60;
    unsigned char *sample = (unsigned char *)malloc(len);
    int i = 0;
    while (!feof(fp))
    {
        int size = fread(sample + i,1,4,fp);
        if (size <= 0)
        {
            break;
        }
        i += 4;
    }
    len = i;
    fclose(fp);
    unsigned char *sample_out = (unsigned char *)malloc(len);
    getDoubleSpeed(sample, sample_out,len);
    fwrite(sample_out, 1, len/2, fp1);
    free(sample);
    free(sample_out);
    fclose(fp1);
    return getchar();
}

四,将PCM16LE双声道音频采样数据转换为PCM8音频采样数据

void getPcm16To8(uint8_t *pcm, uint8_t *pcm_out, int len){
    int16_t *p0 = (int16_t *)pcm;
    for (int i = 0; i < len/2; i++)
    {
        short sample16 = *(p0 + i);
        char sample8 = sample16 >> 8;
        *(pcm_out + i) = sample8 + 128;
    }
}
int main(int argc, char **argv){
    FILE *fp = fopen("NocturneNo2inEflat_44.1k_s16le.pcm","rb+");
    FILE *fp1 = fopen("outpcm16to8.pcm","wb+");
    
    int len = 44100 * 2 * 2 * 60;
    unsigned char *sample = (unsigned char *)malloc(len);
    int i = 0;
    while (!feof(fp))
    {
        int size = fread(sample + i,1,4,fp);
        if (size <= 0)
        {
            break;
        }
        i += 4;
    }
    len = i;
    fclose(fp);
    unsigned char *sample_out = (unsigned char *)malloc(len);
    getPcm16To8(sample, sample_out,len);
    fwrite(sample_out, 1, len/2, fp1);
    free(sample);
    free(sample_out);
    fclose(fp1);
    return getchar();
}

五,将从PCM16LE单声道音频采样数据中截取一部分数据

void getPcmCut(uint8_t *pcm, uint8_t *pcm_out, uint8_t *pcm_cut, int len, int start, int size){
    int16_t *p0 = (int16_t *)pcm;
    int16_t *p1 = (int16_t *)pcm_out;
    int16_t *p2 = (int16_t *)pcm_cut;
    for (int  i = 0; i < len/2; i++)
    {
        if (i > start && i <=(start + size)){
            *(p1 + i - start - 1) = *(p0 + i);
            short *sample = (p0 + i);
            printf("%d---%x---%d--%x---%d---%x\n",*sample,sample, sample[1], &sample[1], sample[0], &sample[0]);
            *(p2 + i - start - 1) = *sample;
        }
    }
}
int main(int argc, char **argv){
    FILE *fp = fopen("outpcmleft.pcm", "rb+");
    FILE *fp1 = fopen("outpcmcut.pcm", "wb+");
    FILE *fp2 = fopen("outpcmcut.txt", "wb+");
    int len = 44100 * 2 * 2 * 60;
    unsigned char *sample = (unsigned char *)malloc(len);
    int i = 0;
    while (!feof(fp))
    {
        int size = fread(sample + i, 1, 4, fp);
        if (size <= 0)
        {
            break;
        }
        i += 4;
    }
    len = i;
    fclose(fp);
    unsigned char *sample_out = (unsigned char *)malloc(len);
    unsigned char *sample_cut = (unsigned char *)malloc(len);
    getPcmCut(sample, sample_out, sample_cut, len, 732000, 20);
    for (int i = 0; i < 20; i++)
    {
        printf("%d+++++", *((short *)sample_cut + i));
        short samplenum = *((short *)sample_cut + i);
        fprintf(fp2, "%d ", samplenum);
    }
    fwrite(sample_out, 1, 20 * 2, fp1);

    free(sample);
    free(sample_out);
    free(sample_cut);
    fclose(fp1);
    fclose(fp2);
    return getchar();
}

六,两个音频文件混音

void mix(uint8_t *pcm1, uint8_t *pcm2, uint8_t *pcm_out, int len){
    int8_t *p1 = (int8_t *)pcm1;
    int8_t *p2 = (int8_t *)pcm2;
    int8_t *p3 = (int8_t *)pcm_out;
    for (int i = 0; i < len; i++)
    {
        int16_t s1 = *(p1 + i);
        int16_t s2 = *(p2 + i);
        *(p3 + i) = s1 / 2 + s2 / 2;
    }
}
int main(int argc, char **argv){
    int pcm1_len = 0, pcm2_len = 0;
    FILE *fp1 = fopen("sjlt_44.1k_s16le.pcm", "rb+");
    FILE *fp2 = fopen("gnzw_44.1K_s16le.pcm", "rb+");

    FILE *fp3 = fopen("mix.pcm", "wb+");
    int maxlen = 44100 * 2 * 2 * 60;
    unsigned char *samples1 = (unsigned char *)malloc(maxlen);
    unsigned char *samples2 = (unsigned char *)malloc(maxlen);
    int i = 0;
    int xcount = 0;
    while (!feof(fp1)) {

        int size = fread(samples1 + i, 1, 4, fp1); //∑µªÿ÷µ «≥…π¶∂¡µΩµƒ”––ßelementcount

        if (size <= 0) {
            break;
        }
        xcount++;
        i += 4;
    }
    pcm1_len = xcount * 4;
    i = 0; xcount = 0;
    while (!feof(fp2)) {

        int size = fread(samples2 + i, 1, 4, fp2);

        if (size <= 0) {
            break;
        }
        xcount++;
        i += 4;
    }
    pcm2_len = xcount * 4;
    fclose(fp1);
    fclose(fp2);
    int len = (pcm1_len > pcm2_len) ? pcm2_len : pcm1_len;
    unsigned char *samples3 = (unsigned char *)malloc(len);
    mix(samples1, samples2, samples3, len);

    fwrite(samples3, 1, len, fp3);

    fclose(fp3);
    free(samples1);
    free(samples2);
    free(samples3);
    printf("over");
    return getchar();
}

你可能感兴趣的:(音频文件pcm的转换)