I have searched almost SO & few forums, but unable to figure out. I have not coded this from scratch. I got the code and I am trying to modify as per my requirement.
So firstly credits to original coder. I am ref this : http://www3.nd.edu/~dthain/courses/cse20211/fall2013/wavfile/
Here he is creating the sine wave for just 1 second. But I am unable to surpass this point. I need to play it for atleast 20seconds.
Here is my code. Hope to get some pointer/help. Thanks in advance. Again thanks to original coder.
WAVFILE_SAMPLES_PER_SECOND = 44100
example.c file
#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;
}
wave.h file
#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
The file is created successfully. But I am unable to play it in any player. Can any one help. Thanks again
With the original code, to get 20 seconds you just need to change the line
const int NUM_SAMPLES = (WAVFILE_SAMPLES_PER_SECOND*2);
to
const int NUM_SAMPLES = (WAVFILE_SAMPLES_PER_SECOND*20);