当前位置: 首页> 财经> 访谈 > 音频demo:将PCM数据左右声道分离

音频demo:将PCM数据左右声道分离

时间:2025/7/10 7:12:59来源:https://blog.csdn.net/weixin_44498318/article/details/140273588 浏览次数:0次

1、README

a. 编译
$ make 	# or `make CC=your-corosscompile-gcc`
b. 使用

注意:分离左右声道的前提是输入的PCM文件(数据)是双声道。

$ ./pcm_channels_split
Usage: ./pcm_channels_split in.pcm out_l.pcm out_r.pcm
examples:./pcm_channels_split ./audio/test_22050_16_2.pcm ./test_22050_16_1-l.pcm ./test_22050_16_1-r.pcm./pcm_channels_split ./audio/test_44100_16_2.pcm ./test_44100_16_1-l.pcm ./test_44100_16_1-r.pcm
c. 附录(demo目录架构)
.
├── audio
│   ├── test_22050_16_1-l.pcm
│   ├── test_22050_16_1-r.pcm
│   ├── test_22050_16_2.pcm
│   ├── test_44100_16_1-l.pcm
│   ├── test_44100_16_1-r.pcm
│   └── test_44100_16_2.pcm
├── docs
│   └── PCM音频数据 - 简书.mhtml
├── main.c
├── Makefile
└── README.md

2、主要代码片段

main.c
#include <stdio.h>
#include <stdlib.h>int pcm_s16le_split(const char* file, const char* out_lfile, const char* out_rfile);int main(int argc, char *argv[])
{if (argc != 4){	printf("Usage: %s in.pcm out_l.pcm out_r.pcm\n", argv[0]);printf("examples: \n""\t %s ./audio/test_22050_16_2.pcm ./test_22050_16_1-l.pcm ./test_22050_16_1-r.pcm\n""\t %s ./audio/test_44100_16_2.pcm ./test_44100_16_1-l.pcm ./test_44100_16_1-r.pcm\n",argv[0], argv[0]);return -1;}/* 注意:分离左右声道的前提是输入的PCM文件(数据)是双声道 */pcm_s16le_split(argv[1], argv[2], argv[3]);return 0;
}int pcm_s16le_split(const char* file, const char* out_lfile, const char* out_rfile)
{FILE *fp = fopen(file, "rb+");if (fp == NULL) {printf("open %s failed\n", file);return -1;}FILE *fp1 = fopen(out_lfile, "wb+");if (fp1 == NULL) {printf("open %s failed\n", out_lfile);return -1;}FILE *fp2 = fopen(out_rfile, "wb+");if (fp2 == NULL) {printf("open %s failed\n", out_rfile);return -1;}char * sample = (char *)malloc(4);while(1) {int readbytes = fread(sample, 1, 4, fp);if(readbytes <= 0){// 没有数据就不要写了break;}// Lfwrite(sample, 1, 2, fp1);// Rfwrite(sample + 2, 1, 2, fp2);}free(sample);fclose(fp);fclose(fp1);fclose(fp2);return 0;
}

3、demo下载地址(任选一个)

  • https://download.csdn.net/download/weixin_44498318/89525128

  • https://gitee.com/linriming/audio_pcm_channels_split.git

  • https://github.com/linriming20/audio_pcm_channels_split.git

关键字:音频demo:将PCM数据左右声道分离

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: