n", sf_info.sections );
printf("Frames : %d\n", (int)sf_info.frames );
buf1 = (short *)malloc(sf_info.frames *sizeof(short));
buf2 = (double *)malloc(sf_info.frames *sizeof(double));
sf_readf_short(snd_file, buf1, sf_info.frames) ;
sf_seek (snd_file, 0, SEEK_SET) ;
sf_readf_double(snd_file, buf2, sf_info.frames) ;
save(buf1, buf2, sf_info.frames);
free(buf1);
free(buf2);
sf_close(snd_file);
return 0;
}
void save(short *b1, double *b2, int n)
{
int i;
FILE *fp1;
FILE *fp2;
fp1 = fopen("short.dat", "w");
fp2 = fopen("double.dat", "w");
for(i = 0; i< n; i++)
{
fprintf(fp1, "%d\n", (int)b1[i]);
fprintf(fp2, "%f\n", b2[i]);
}
fclose(fp1);
fclose(fp2);
}
#include
#include
#include
void save(short *b1, double *b2, int n);
int main(int argc, char * argv[])
{
SF_INFO sf_info;
SNDFILE *snd_file;
short *buf1;
double *buf2;
if(argc != 2)
{
exit(1);
}
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));
buf2 = (double *)malloc(sf_info.frames *sizeof(double));
sf_readf_short(snd_file, buf1, sf_info.frames) ;
sf_seek (snd_file, 0, SEEK_SET) ;
sf_readf_double(snd_file, buf2, sf_info.frames) ;
save(buf1, buf2, sf_info.frames);
free(buf1);
free(buf2);
sf_close(snd_file);
return 0;
}
void save(short *b1, double *b2, int n)
{
int i;
FILE *fp1;
FILE *fp2;
fp1 = fopen("short.dat", "w");
fp2 = fopen("double.dat", "w");
for(i = 0; i< n; i++)
{
fprintf(fp1, "%d\n", (int)b1[i]);
fprintf(fp2, "%f\n", b2[i]);
}
fclose(fp1);
fclose(fp2);
}
编译命令如下:
mingw32-gcc.exe -Wall -g -IC:\MinGW\msys\1.0\local\include C:\MinGW\msys\1.0\home\Administrator\wav_test\main.c -o bin\Debug\wav_test.exe C:\MinGW\msys\1.0\local\lib\libsndfile.dll.a
与之相对应的是写音频文件。
[cpp]
sf_count_t sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
sf_count_t sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
sf_count_t sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
sf_count_t sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
sf_count_t sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
sf_count_t sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
sf_count_t sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
sf_count_t sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
sf_count_t sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
sf_count_t sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
sf_count_t sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
sf_count_t sf_writef_int (SNDFILE *sndfile, int *p