Why this code (sinewave generator) doesn't work?

Audio software developers forum.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Post Reply
lussiano
Posts: 2
Joined: Sat Nov 28, 2009 2:16 pm
Operating System: Please select

Why this code (sinewave generator) doesn't work?

Post by lussiano » Sat Nov 28, 2009 2:43 pm

The code below is supposed to generate a 4-seconds long wave file of a sinewave at 440 Hz frequency, 16 bits per sample and at 44100 Hz sampling rate.
The code compiles fine and I run it in Windows console with the command "wave.exe > sine440.wav".
However, when I play the resulting file I hear a horrible amount of noise mixed with the sine tune. I've generated a wave file with those specifications in Audacity and compared the files: the filesizes and headers are identical. So, the problem is in my samples values.

If anyone could help me with this, I'd be very grateful.

Code: Select all

#include<math.h>

#define SAMPLE_RATE 44100
#define BITS_PER_SAMPLE 16
#define TIME 4
#define FREQ 440
#define PI 3.14159

void putint(unsigned int x)
{
     unsigned int y = x;
     
     if(x <= 0xFFFF) {
          putchar(x & 0x00FF);
          x = x & 0xFF00;
          x = x >> 8;
          putchar(x);
     }
     else {
          putchar(x & 0x000000FF);
          x = x & 0x0000FF00;
          x = x >> 8;
          putchar(x);
          x = y;
          x = x & 0x00FF0000;
          x = x >> 16;
          putchar(x);
          x = y;
          x = x & 0xFF000000;
          x = x >> 24;
          putchar(x);
     }
}

int main()
{
    int datasize = SAMPLE_RATE * BITS_PER_SAMPLE * TIME / 8; // size of waveform data in bytes
    int filesize = datasize + 44, i; // file size in bytes
    short sample;
    double angle = 0.0;
    
    putchar('R'), putchar('I'), putchar('F'), putchar('F'); // 4 bytes - "RIFF"
    putint(filesize - 8); // 4 bytes - size of waveform chunk
    putchar('W'), putchar('A'), putchar('V'), putchar('E'); // 4 bytes - "WAVE"
    putchar('f'), putchar('m'), putchar('t'), putchar(' '); // 4 bytes - "fmt "
    putint(16), putint(0); // 4 bytes - size of format chunk(16 bytes)
    putint(1); // 2 bytes - wf.wFormatTag = WAVE_FORMAT_PCM = 1
    putint(1); // 2 bytes - wf.nChannels
    putint(SAMPLE_RATE), putint(0); // 4 bytes - wf.nSamplesPerSec
    putint(SAMPLE_RATE * BITS_PER_SAMPLE / 8); // 4 bytes - wf.nAvgBytesPerSec
    putint(BITS_PER_SAMPLE/8); // 2 bytes - wf.nBlockAlign
    putint(BITS_PER_SAMPLE); // 2 bytes - wf.wBitsPerSample
    putchar('d'), putchar('a'), putchar('t'), putchar('a'); // 4 bytes - "data"
    putint(datasize); // 4 bytes - size of waveform data
    
    for(i = 0; i < datasize; i += 2) 
    {
        sample = (short)(32767 * sin(angle));
        putchar(sample & 0x00FF);
        sample = sample & 0xFF00;
        sample = sample >> 8;
        putchar(sample);
        angle += 2 * PI * FREQ / SAMPLE_RATE;
        if(angle > 2 * PI)
            angle -=  2 * PI;
    }
    
    return 0;
}
Last edited by lussiano on Sat Nov 28, 2009 9:30 pm, edited 1 time in total.

steve
Site Admin
Posts: 81609
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Why this code (sinewave generator) doesn't work?

Post by steve » Sat Nov 28, 2009 6:27 pm

lussiano wrote:This code
What code?
Why not use Audacity as your sine wave generator?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

lussiano
Posts: 2
Joined: Sat Nov 28, 2009 2:16 pm
Operating System: Please select

Re: Why this code (sinewave generator) doesn't work?

Post by lussiano » Sat Nov 28, 2009 9:44 pm

Sorry, I'd forgotten to post the code, but there it is now. In fact, my real goal is learning audio programming and synthesis using C/C++. Generate a sinewave is just the first step to that but it´s a decisive one, so I'm putting a great effort into it.

Thanks for your post and please keep helping!

steve
Site Admin
Posts: 81609
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Why this code (sinewave generator) doesn't work?

Post by steve » Sun Nov 29, 2009 7:06 pm

I can't help you with the code - I don't know C/C++, but if you import the tone into Audacity and zoom in close, you may be able to "see" the noise (for example if the tone is clipped or if there is a discontinuity as it passes zero) which in turn may help you to find the cause. Also, you can use Analyze > Plot Spectrum which may help you determine what the problem is.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply