将PCM16LE双声道音频采样数据转换为PCM8音频采样数据

master
dezhihuang 2017-11-12 21:14:30 +08:00
parent b941d84992
commit c03d8e84d0
2 changed files with 178 additions and 0 deletions

View File

@ -542,6 +542,105 @@ int main()
从源代码可以看出本程序只采样了每个声道偶数点的样值。处理完成后原本22秒左右的音频变成了11秒左右。音频的播放速度提高了2倍音频的音调也变高了很多。
<br />
<br />
## 将PCM16LE双声道音频采样数据转换为PCM8音频采样数据
> 注本文中声音样值的采样频率一律是44100Hz采样格式一律为16LE。“16”代表采样位数是16bit。由于1Byte=8bit所以一个声道的一个采样值占用2Byte。“LE”代表Little Endian代表2 Byte采样值的存储方式为高位存在高地址中。
``` C++
//
//本程序中的函数可以通过计算的方式将PCM16LE双声道数据16bit的采样位数转换为8bit。
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int pcm16le_to_pcm8(const char *file)
{
if (file == NULL) {
printf("原始PCM文件为空\n");
return 0;
}
FILE *fp = fopen(file, "rb+");
if (fp == NULL) {
printf("原始PCM文件打开失败\n");
return 0;
}
FILE *fp1 = fopen("./output/pcm16le_to_pcm8.pcm", "wb+");
if (fp1 == NULL) {
printf("文件打开或创建失败!\n");
return 0;
}
unsigned char buf[4] = {0};
while ( !feof(fp) ) {
//从文件中读取一次采样值因为是16位的所以需读取4个字节
//左右声道采样值间隔存储,前两个字节为左声道采样值,后两个字节为右声道采样值
fread(buf, 1, 4, fp);
//将前两个字节(左声道采样值)强制转换为 short类型因为short类型长度为两个字节
short *sample = (short *)buf;
//右移8位相当于除以256(2的8次方)
//将pcm16(short类型)的值以256为除数取模作为pcm8的采样值
unsigned char pcm8 = (*sample) >> 8;
//因为short类型的范围为-32768~32767经过上一步获得的结果为-128~127
//所以转成unsigned char需要加上128unsigned char类型的范围为0~255
pcm8 = pcm8 + 128;
//写入左声道的采样值
fwrite(&pcm8, 1, 1, fp1);
//将前两个字节(右声道采样值)强制转换为 short类型
sample = (short *)(buf + 2);
pcm8 = (*sample) >> 8;
//-128~127 => 0~128
pcm8 = pcm8 + 128;
//写入右声道的采样值
fwrite(&pcm8, 1, 1, fp1);
}
fclose(fp);
fclose(fp1);
return 1;
}
int main()
{
char file[] = "./mediadata/NocturneNo2inEflat_44.1k_s16le.pcm";
if (pcm16le_to_pcm8(file)) {
printf("操作成功!\n");
} else {
printf("操作失败!\n");
}
return 0;
}
```
PCM16LE格式的采样数据的取值范围是-32768到32767而PCM8格式的采样数据的取值范围是0到255。所以PCM16LE转换到PCM8需要经过两个步骤第一步是将-32768到32767的16bit有符号数值转换为-128到127的8bit有符号数值第二步是将-128到127的8bit有符号数值转换为0到255的8bit无符号数值。在本程序中16bit采样数据是通过short类型变量存储的而8bit采样数据是通过unsigned char类型存储的。
<br />
<br />
<br />
<br />

79
pcm16le_to_pcm8.cpp Normal file
View File

@ -0,0 +1,79 @@
//
//本程序中的函数可以通过计算的方式将PCM16LE双声道数据16bit的采样位数转换为8bit。
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int pcm16le_to_pcm8(const char *file)
{
if (file == NULL) {
printf("原始PCM文件为空\n");
return 0;
}
FILE *fp = fopen(file, "rb+");
if (fp == NULL) {
printf("原始PCM文件打开失败\n");
return 0;
}
FILE *fp1 = fopen("./output/pcm16le_to_pcm8.pcm", "wb+");
if (fp1 == NULL) {
printf("文件打开或创建失败!\n");
return 0;
}
unsigned char buf[4] = {0};
while ( !feof(fp) ) {
//从文件中读取一次采样值因为是16位的所以需读取4个字节
//左右声道采样值间隔存储,前两个字节为左声道采样值,后两个字节为右声道采样值
fread(buf, 1, 4, fp);
//将前两个字节(左声道采样值)强制转换为 short类型因为short类型长度为两个字节
short *sample = (short *)buf;
//右移8位相当于除以256(2的8次方)
//将pcm16(short类型)的值以256为除数取模作为pcm8的采样值
unsigned char pcm8 = (*sample) >> 8;
//因为short类型的范围为-32768~32767经过上一步获得的结果为-128~127
//所以转成unsigned char需要加上128unsigned char类型的范围为0~255
pcm8 = pcm8 + 128;
//写入左声道的采样值
fwrite(&pcm8, 1, 1, fp1);
//将前两个字节(右声道采样值)强制转换为 short类型
sample = (short *)(buf + 2);
pcm8 = (*sample) >> 8;
//-128~127 => 0~128
pcm8 = pcm8 + 128;
//写入右声道的采样值
fwrite(&pcm8, 1, 1, fp1);
}
fclose(fp);
fclose(fp1);
return 1;
}
int main()
{
char file[] = "./mediadata/NocturneNo2inEflat_44.1k_s16le.pcm";
if (pcm16le_to_pcm8(file)) {
printf("操作成功!\n");
} else {
printf("操作失败!\n");
}
return 0;
}