PCM音频文件(.wav)压缩成GSM6.10(.wav)
来源:互联网 发布:淘宝发货单怎么打印 编辑:程序博客网 时间:2024/04/29 14:32
GSM的输入是帧数据,一帧(20毫秒)由采样频率为8 kHz的带符号的160个样本组成,每个样本为13位或者16位的线性PCM码。GSM编码器可把一帧(160×16位)的数据压缩成260位的GSM帧,压缩后的数据率为1625字节,相当于13 kbps。由于260位不是8位的整数倍,因此编码器输出的GSM帧为264位的线性PCM码。采样频率为8 kHz、每个样本为16位的未压缩的话音数据率为128 kbps,使用GSM压缩后的数据率为: (264bit×8000样本/s) / 160样本 = 13.2 kbps
GSM的压缩比:128:13.2 = 9.7,近似于10:1。
void setGsmHeader( FILE *outFile, ULONG dataSize)
{
ULONG flen = 0;
INTELADPCM_HEADER gsmhead;
gsmhead.riff.chunkid = WAV_ID_RIFF;//4BYTE
int gsmDataSize = 0;
if(dataSize % 640 == 0) {
gsmDataSize = (((int)dataSize/640) * 65 + 1) & 0xFFFFFFFE;
}
else {
gsmDataSize = (((int)dataSize/640 + 1) * 65 + 1) & 0xFFFFFFFE;
}
gsmhead.riff.chunksize = gsmDataSize + sizeof(INTELADPCM_HEADER) - 8;
gsmhead.riff.wave_id = WAV_ID_WAVE;//4BYTE
gsmhead.chunkid = WAV_ID_FMT;//4BYTE
gsmhead.chunksize = 0x14;//2BYTE
gsmhead.wformattag = 0x31;//2BYTE
gsmhead.nchannels = 1;//2BYTE
gsmhead.nsamplespersec = 8000;//4BYTE
gsmhead.navgbytespersec = 1625;//4BYTE
gsmhead.nblockalign = 0x41;//2BYTE
gsmhead.wbitspersample = 0;//2BYTE
gsmhead.cbsize = 2;//2BYTE
gsmhead.wsamplesperblock = 0x140;// 2BYTE:320
gsmhead.fact.chunkid = WAV_ID_FACT;
gsmhead.fact.chunksize = 4;
gsmhead.fact.datalength = dataSize;
gsmhead.data.ID = WAV_ID_DATA;
gsmhead.data.Size = gsmDataSize;
fwrite( &gsmhead, sizeof(gsmhead), 1, outFile);
}
int gsmEncoder(char* strFilePath,
char* strOriginalPCM,
char* strCompressFileName){
FILE *sfp;
FILE *dfp;
gsm handle;
long samples;
long total_out;
gsm_signal linear[GSM_BLOCK_SIZE];
gsm_frame frame;
int out_size;
int rc;
// 圧縮するファイルのパスとファイル名を連接する。
char fileFullPath[FILE_NAME_LEN] = {'0',};
fileFullPathCat(fileFullPath, strFilePath, strOriginalPCM);
sfp = fopen(fileFullPath, "rb");
if (!sfp)
{
perror("open fails");
exit(1);
}
/* Read .wav file header info and calulate output size */
WAVEFMT waveFmt;
checkWaveFileFormat(sfp, &waveFmt);
samples = waveFmt.dataLength;
// 圧縮されたファイルのパスとファイル名を連接する。
memset(fileFullPath, 0, sizeof(fileFullPath));
fileFullPathCat(fileFullPath, strFilePath, strCompressFileName);
dfp = fopen(fileFullPath, "wb+");
/* Create the GSM codec object and option it for wave framing */
handle = gsm_create();
if (!handle)
{
perror("cannot create gsm codec");
exit(1);
}
(void )gsm_option(handle, GSM_OPT_WAV49, &F_wav_fmt);
/* Write the .wav file header */
setGsmHeader(dfp, samples);
/* Compress the audio */
total_out = 0;
while (samples > 0)
{
/* Read two frames worth of samples and convert to linear */
rc = fread(linear, (size_t )1, sizeof (linear), sfp);
if (rc < 0)
{
perror("error reading input");
exit(1);
}
samples -= rc;
if (rc < sizeof (linear))
{
memset((char *)linear + rc, LINEAR_ZERO, sizeof (linear) - rc);
}
/* Encode the even half and write short (32-byte) frame */
gsm_encode(handle, &linear[0], frame);
out_size = sizeof (frame) - 1;
rc = fwrite(frame, (size_t )1, out_size, dfp);
if (rc != out_size)
{
perror("error writing output");
exit(1);
}
total_out += rc;
/* Encode the odd half and write long (33-byte) frame */
gsm_encode(handle, &linear[160], frame);
out_size = sizeof (frame);
rc = fwrite(frame, (size_t )1, out_size, dfp);
if (rc != out_size)
{
perror("error writing output");
exit(1);
}
total_out += rc;
}
/* Pad output to even number of bytes */
if (total_out & 0x1)
{
frame[0] = 0x00;
rc = fwrite(frame, (size_t )1, 1, dfp);
if (rc != 1)
{
perror("error writing output");
exit(1);
}
total_out += rc;
}
/* Clean up */
gsm_destroy(handle);
return 0;
}
- PCM音频文件(.wav)压缩成GSM6.10(.wav)
- PCM音频文件(.wav)压缩成ADPCM(.wav)
- PCM音频文件(.wav)压缩成ADPCM(.wav) ,wav文件分析,wav 文件格式
- 音频文件pcm转换wav
- C#WAV音频文件转化PCM数据文件PCM转化WAV音频
- PCM 封装成 WAV
- wav音频文件
- WAV音频文件
- WAV && PCM
- pcm 格式 转换成 wav
- pcm 格式 转换成 wav
- FFMPEG mp3->wav wav->pcm
- wav音频文件的结构
- 读取*.wav音频文件
- 微软Wav音频文件解析
- WAV音频文件格式
- 读取*.wav音频文件
- OpenAL播放WAV音频文件
- linux rstatd安装
- 宽字符与Unicode
- JQuery 封装脚本系列之单选框(Radio)
- C#操作Excel(一)
- Second-price Auction
- PCM音频文件(.wav)压缩成GSM6.10(.wav)
- 读取某一文件夹下的所有文件
- 开发者不可不知的PHP框架深度解析
- ReportViewer的动态绑定
- windows.h winsock2.h winsock.h的关系
- 如何利用JDownloads在首页导航栏中添加一个链接
- Oracle创建表空间/表分区
- 挫折不是惩罚,而是学习的机会
- test