[Video and Audio Data Processing] 截取部分PCM16LE单声道音频采样数据
程序员文章站
2022-07-06 10:19:15
...
0. 代码
extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#endif
}
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}
/**
* Cut a 16LE PCM single channel file.
* @param url Location of PCM file.
* @param start_num start point
* @param dur_num how much point to cut
*/
int simplest_pcm16le_cut_singlechannel(const char* url, int start_num, int dur_num) {
FILE* fp = fopen(url, "rb+");
FILE* fp1 = fopen("output_cut.pcm", "wb+");
FILE* fp_stat = fopen("output_cut.txt", "wb+");
unsigned char* sample = (unsigned char*)malloc(2);
int cnt = 0;
while (!feof(fp)) {
fread(sample, 1, 2, fp);
if (cnt > start_num && cnt <= (start_num + dur_num)) {
fwrite(sample, 1, 2, fp1);
short samplenum = sample[1];
samplenum = samplenum * 256;
//获取高8位的采样值
samplenum = samplenum + sample[0];
//获取整体的采样值
fprintf(fp_stat, "%6d,", samplenum);
//以%6d的格式,向输出流fp_stat写入samplenum的数据
if (cnt % 10 == 0)
fprintf(fp_stat, "\n", samplenum);
//以\n的格式-换行,向输出流fp_stat写入samplenum的数据
}
cnt++;
}
free(sample);
fclose(fp);
fclose(fp1);
fclose(fp_stat);
return 0;
}
int main()
{
simplest_pcm16le_cut_singlechannel("drum.pcm", 2360, 120);
return 0;
}
音源获取:https://github.com/leixiaohua1020/simplest_mediadata_test
1. 运行效果
原始音频文件:
处理之后的截取的音频文件:
输出的 采样数据:
参考链接:
https://blog.csdn.net/leixiaohua1020/article/details/50534316
上一篇: 网页版WebQQ即将停止服务
下一篇: MongoDB常用命令小结