# FM synthesis fuzzy sound

I'm trying to reproduce this !!!WARNING LOUDNESS!!! sound in C. In the website, it says to use Carrier: 500 Hz, modulator frequency: 1 Hz, modulator index: 500, however I'm getting lots of clicks and fuzzing (along with the right sound) with this code:

#include <stdio.h>
#include <math.h>
#include <string.h>

#define DURATION    20
#define RATE        44100
#define NUMCHANS 1

#define NUMSAMPLES  (DURATION*RATE)
#define NUMSAMPLESC (NUMSAMPLES*NUMCHANS)

#define DELTAT (1.0/RATE)

#define M_PI 3.14159265359

static const int wavHeader[] = {
0x46464952,
NUMSAMPLESC*2+36,
0x45564157,
0x20746D66,
16,
1|(NUMCHANS<<16),
RATE,
RATE*NUMCHANS*sizeof(short),
(NUMCHANS*sizeof(short))|((8*sizeof(short))<<16),
0x61746164,
NUMSAMPLES*sizeof(short)
};

static short buffer[BYTES_IN_BUFFER/sizeof(short)];

int main(void) {

unsigned int i;
double t = 0.0;
FILE *fp;

for(i = BYTES_IN_HEADER/sizeof(short); i < BYTES_IN_BUFFER/sizeof(short); i++, t += DELTAT) {
buffer[i] = 100 * sin(2*M_PI*500*t + 500*sin(t*2*M_PI*1));
}

fp = fopen("test.wav", "w");
fwrite(buffer, 1, BYTES_IN_BUFFER, fp);

fclose(fp);

return 0;
}


I have tried using single precision floats, reversing endianness, but with the same result. The only thing that seems to remove the clicks and fuzzing to a certain extent (but they're still there) is if i copy the lower 8 bits of each sample into the upper 8 bits.

My sound card is integrated from a P43DE3 motherboard and can handle audio CDs well, and my OS is Windows 8.

How do I reproduce that sound without noise?

EDIT

It seems to produce the correct output when I run it on a Mac. Perhaps and endianness error? Will investigate further.

• Your code works on my machine without modifications - but produces a low amplitude signal (expected because the amplitude of the modulated sine wave is 100... while the range of shorts is -32768..32767). Maybe post of .wav file of what you get on your end... – pichenettes Jul 9 '13 at 12:17
• @pichenettes Thanks for such a quick response. This is what I'm getting: dropbox.com/s/u6ofcne06oltq5r/test.wav Tested in Windows Media Player and Foobar2000. – toby Jul 9 '13 at 12:23