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

1、README

a. 编译
bash 复制代码
$ make 	# or `make CC=your-corosscompile-gcc`
b. 使用

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

bash 复制代码
$ ./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目录架构)
bash 复制代码
.
├── 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
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;
		}

		// L
		fwrite(sample, 1, 2, fp1);
		// R
		fwrite(sample + 2, 1, 2, fp2);
	}
	free(sample);

	fclose(fp);
	fclose(fp1);
	fclose(fp2);
	return 0;
}

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

相关推荐
脑子缺根弦3 小时前
融合优势:SIP 广播对讲联动华为会议 全场景沟通响应提速
华为·音视频·广播对讲系统
肥or胖12 小时前
【FFmpeg 快速入门】本地播放器 项目
开发语言·qt·ffmpeg·音视频
DogDaoDao14 小时前
GitHub开源轻量级语音模型 Vui:重塑边缘智能语音交互的未来
大模型·github·音视频·交互·vui·语音模型·智能语音
张海森-1688201 天前
视频码率是什么?视频流分辨率 2688x1520_25fps采用 h264格式压缩,其码率为
音视频
DogDaoDao2 天前
视频HDR技术全解析:从原理到应用的深度探索
音视频·hdr·sdr·视频渲染·hdr10·视频hdr技术标准·人眼视觉系统
IoT砖家涂拉拉2 天前
萌宠语聊新模板!借助On-App AI降噪与音频处理技术,远程安抚宠物更轻松、更安心!
人工智能·ai·app·音视频·智能家居·智能硬件·宠物
AI视觉网奇3 天前
截取视频 截取mp4 视频缩放
音视频
DogDaoDao3 天前
2025年 GitHub 主流开源视频生成模型介绍
人工智能·深度学习·开源·大模型·github·音视频·视频生成
todoitbo3 天前
docker搭建freeswitch实现点对点视频,多人视频
docker·容器·音视频·freeswitch·视频聊天
菜包eo3 天前
3种添加视频水印的加密方式,守护视频安全!
安全·音视频