1.创建一个控制台工程。
2.把libsndfile-1.lib文件拷贝到工程里面。在工程选项里面添加lib文件。
3.拷贝头文件sndfile.h到工程目录,添加到工程。
4.编写如下代码:
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include "sndfile.h"
-
-
void save(short *b1, int n);
-
int main(int argc,char *argv[])
-
{
-
SF_INFO sf_info;
-
SNDFILE *snd_file;
-
SNDFILE *fpOut;
-
SF_INFO sf_info_out;
-
short *buf1;
-
//float *buf1;
-
sf_count_t cout;
-
-
sf_info.format = 0;
-
snd_file = sf_open(argv[1],SFM_READ,&sf_info);
-
-
-
printf ("Using %s.\n", sf_version_string ()) ;
-
printf("File Name : %s\n", argv[1]);
-
printf("Sample Rate : %d\n", sf_info.samplerate);
-
printf("Channels : %d\n", sf_info.channels);
-
printf("Sections : %d\n", sf_info.sections );
-
printf("Frames : %d\n", (int)sf_info.frames );
-
-
buf1 = (short *)malloc(sf_info.frames *sizeof(short)*2);
-
-
sf_info_out.channels = sf_info.channels;
-
sf_info_out.samplerate = sf_info.samplerate;
-
sf_info_out.frames = sf_info.frames;
-
sf_info_out.format = (SF_FORMAT_WAV|SF_FORMAT_PCM_16|SF_ENDIAN_LITTLE);
-
fpOut = sf_open(argv[2],SFM_WRITE,&sf_info_out);
-
if(fpOut == NULL)
-
{
-
printf("open out file failed\n");
-
exit(1);
-
}
-
-
while( sf_read_short(snd_file, buf1, 480) == 480)
-
{
-
sf_write_short(fpOut,buf1,480);
-
}
-
-
-
free(buf1);
-
sf_close(snd_file);
-
sf_close(fpOut);
-
return 0;
-
}
编译通过
运行的时候,提示缺少dll文件,把libsndfile-1.dll拷贝到程序运行的目录。生成的wav文件播放起来声音正常。
阅读(3229) | 评论(0) | 转发(0) |