我几乎搜索了 SO 和几个论坛,但无法弄清楚。我没有从头开始编码。我得到了代码,我正在尝试根据我的要求进行修改。
所以首先归功于原始编码器。我参考这个: http ://www3.nd.edu/~dthain/courses/cse20211/fall2013/wavfile/
在这里,他只创建了 1 秒的正弦波。但我无法超越这一点。我需要播放至少 20 秒。
这是我的代码。希望得到一些指针/帮助。提前致谢。再次感谢原始编码器。
WAVFILE_SAMPLES_PER_SECOND = 44100
例子.c 文件
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include "wavfile.h"
const int NUM_SAMPLES = (WAVFILE_SAMPLES_PER_SECOND*2);
int main()
{
unsigned long waveform[NUM_SAMPLES*4]; // here I am changing it to 4
double frequency = 440;
int volume = 255;
unsigned long int length = NUM_SAMPLES*4; // here I am changing it to 4
// For one second the length = 88200
unsigned long int i;
for(i=0;i<length;i++) {
long double t = (long double) i / WAVFILE_SAMPLES_PER_SECOND;
waveform[i] = volume*sin(frequency*t*2*M_PI);
}
printf("length=%d\n",length);
FILE * f = wavfile_open("sound.wav");
if(!f) {
printf("couldn't open sound.wav for writing: %s",strerror(errno));
return 1;
}
wavfile_write(f,waveform,length);
wavfile_close(f);
return 0;
}
波.h 文件
#ifndef WAVFILE_H
#define WAVFILE_H
#include <stdio.h>
#include <inttypes.h>
FILE * wavfile_open( const char *filename );
void wavfile_write( FILE *file, short data[], int length );
void wavfile_close( FILE * file );
#define WAVFILE_SAMPLES_PER_SECOND 44100
#endif
文件创建成功。但我无法在任何播放器中播放它。任何人都可以帮助。再次感谢