音频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下载地址(任选一个)

相关推荐
布拉德很帅27 分钟前
android系统使用FFmpeng集成OpenSL音频录制和播放
音视频
硅谷秋水1 小时前
MAPLE:编码从自我为中心的视频中学习的灵巧机器人操作先验
人工智能·机器学习·计算机视觉·机器人·音视频
TSINGSEE4 小时前
跨平台嵌入式音视频开发指南:EasyRTC音视频通话的多场景适配与AI扩展能力
人工智能·音视频·webrtc·智能家居
科技小E12 小时前
5G时代,视频分析设备平台EasyCVR实现通信基站远程安全便捷管控
大数据·网络·人工智能·音视频·安防监控
小白教程15 小时前
如何处理Python爬取视频时的反爬机制?
开发语言·python·音视频·python爬虫
Luke Ewin15 小时前
一个基于OpenAI Whisper开发的音视频字幕文件生成工具
人工智能·whisper·音视频·语音识别·asr·语音转写·视频字幕生成
WDeLiang18 小时前
音频基础概念
音视频
Enougme20 小时前
python-将文本生成音频
python·音视频·语音识别
hjjdebug1 天前
最简单的使用SDL2 播放原始音频数据程序
ffmpeg·音视频