音频文件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();
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容