Thread: Tone generation

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Question Tone generation

    Hello,

    As this neither language- nor platform-specific, I'll post this here. I'm looking into making a tone generator program. As far as I can tell, this involves generating sine waves at different frequencies and amplitudes, a bit like so:-
    Code:
    int i;
    double scale;
    unsigned char ucBuffer[48000]; //48KHz sampling rate, 8-bit mono
    unsigned char *pData;
    
    scale = (2 * PI) / 48000;
    pData = ucBuffer;
    for (i=0;i<48000;i++)
    	*(pData++) = (unsigned char)sin(i * scale);
    However, when I pass this to an output device, I either hear nothing at all or some weird "clicking" or "popping". Am I going deaf?

    I know the code above is too simple to produce anything meaningful, but I'm heading in the right direction, aren't I?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >(unsigned char)sin(i * scale);

    You are using an unsigned char to store your value. Note that the maximum value of the sine-wave is 1 and the minimum is -1. Values between will be rounded off to 0 or 1, because the variable is unsigned.

    Try this: use a char instead of an unsigned char and give the sine-wave an amplitude larger than 1. Perhaps you could also do some debugging by writing the signal to a file so that you can see which values were calculated.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Ah yes, forgot about that, thanks Shiro

    It does generate a sine wave "sound" now, at least after multiplying it with the upper positive and negative limit of a char (127). Now if I can work out how to stop this infernal "popping" after each play (I've even tried splitting the data into two half-second buffers, but to no avail...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Labyrinth generation
    By guesst in forum Game Programming
    Replies: 16
    Last Post: 10-09-2008, 06:02 PM
  3. Diablo's random level generation
    By glo in forum Game Programming
    Replies: 7
    Last Post: 07-19-2008, 03:04 AM
  4. C Program with Tone Generation. HELP
    By LaVieEnRose in forum C Programming
    Replies: 1
    Last Post: 11-22-2007, 08:37 PM
  5. Grid generation from scattered points?
    By canada-paul in forum C Programming
    Replies: 6
    Last Post: 12-12-2003, 05:46 PM