0
$\begingroup$

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 BYTES_IN_HEADER (11*sizeof(int))
#define BYTES_IN_BUFFER (NUMSAMPLESC*sizeof(short) + BYTES_IN_HEADER)

#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;

 memcpy(buffer, wavHeader, BYTES_IN_HEADER);

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.

$\endgroup$
  • $\begingroup$ 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... $\endgroup$ – pichenettes Jul 9 '13 at 12:17
  • $\begingroup$ @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. $\endgroup$ – toby Jul 9 '13 at 12:23
1
$\begingroup$

OK, I figured out what the problem was using a hex editor. The compiler on Windows was seemingly introducing random 0x0D throughout the file. That's because of the mode I was opening the file with, "w". fwrite converted every instance of '\n' into "\r\n". All I had to do was change the file opening mode to "wb+", and it sounds exactly like the linked audio file, after changing the carrier amplitude to 2e5.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.