I have a simple program in C to generate a square wave at a specific frequency, sample-rate, and amplitude, which works fine. However, if I comment out the relevant line and uncomment the new sine-wave line, it generates my sine wave, but also creates a lot of noise when I process the output into Audacity or SoX.
However, a log file containing the data in my generated buffer reads as expected, with signed values within the amplitude, oscillating in time (samples) relative to the specified frequency, with no unexpected values or noise...
That might lead me to think something is wrong with the sample-rate or some other setting (endianness, mono/stereo, data type, etc.), but as I said before, the square wave has no such troubles. Also I ensured that all calculation was done using a double-precision float type so that I know integer coercion isn't my problem. I am stumped. Any help or insight is welcome.
Here's what I have:
#define pi 3.1415926
int main()
{
FILE *fer=fopen("log.txt","w");
double f=440; //freq
double a=1000; //amplitude
double p=44100/f; //period
int16_t *b=malloc(sizeof(int16_t)*44100);
if(!b)return 1;
for(int i=0;i<44100;i++)
{
double ll=sin( (2.0L*pi/p)*i ) * a; //SINE WAVE
// double ll=fmod(i,p)<p/2?a:-a; //SQUARE WAVE
b[i]= ll;
fprintf(fer,"%i\n",b[i]);
}
fwrite(b,sizeof(int16_t),44100,stdout);
free(b);
fclose(fer);
}
0x0D
was converted, when it was one byte of, say0x140D
16-bit value. Open a file in binary mode. I don't understand why you are sending binary data tostdout
. Presumably you redirect the output. Have a look at the resultant file size, is it larger than expected? - Weather Vane0x8000
? - Weather Vanestdout
wasn't a binary stream. Now I feel dumb. I suppose you could create an answer if you want, or I can delete the question since it's a duplicate. Also, I'm not sure if I understand the second question. Perhaps Audacity gleans the offset value from the encoding (signed 16-bit, 32-bit float, etc.) - same for SoX - C. Dunn