Thread: generating audio in C

  1. #16
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If you don't mind using the system speaker...

    Beep(freq, duration); from windows.h would be suffice.

    Portable? No. Cross compiler compatible? Yes (at least for windows compilers).

  2. #17
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    What about using a sine curve? Sound waveforms for a set frequency use sine curves. If your audio file is 44,100 Hz sample rate, the typical used with audio CD's, you'd have a full loop every 44,100 samples at 1 Hz. Frequency is how many loops you can get in that 44,100 samples. For 500 Hz pitch, you'd need 88.2 samples per loop. Note that samples divided by pitch gives the span between wave crests. To reproduce this sample 500 Hz wave, you'd need to determine the location of each sample and a basic sine curve answers this. You'd need to cover 360 degrees in a span of 88.2 samples This makes each sample cover 360/88.2 or 4.08163265 degrees per sample. Starting the loop at 0 degrees, you increment based on that and multiply this result by the range of the samples, like 32767 for a 16-bit audio file (which is what audio CD's use. The first is obvious - 0. The next is the sine of 4.08163265 degrees, or 0.07117769. Multiply this by 32767 and that's the position of that sample. Increment the angle for the next sample and keep going for as long as you want. That's the basic pure tone.

    I don't know what a squarewave is, but if it's just basic linear increments, a similar system applies but no sine curves are used. Determining the span between crests is still the same. Instead of using a sine curve, you'd have the top part of the wave as one quarter of the way through, or 22.05 samples then the bottom part as 3/4 of the way through, or 66.15 samples (for the same 500 Hz sound).

    Hopefully this will give some insight to help write your own function if nothing else is available or works for you.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    May 2007
    Posts
    12

    Post

    Quote Originally Posted by zacs7 View Post
    If you don't mind using the system speaker...

    Beep(freq, duration); from windows.h would be suffice.

    Portable? No. Cross compiler compatible? Yes (at least for windows compilers).
    when you say portable what do you mean?

    is this 'beep' similar to the sound() and nosound()

    see i came up with something using he sound(), delay() and nosound(). but it just seemed to produce a one second note and not more than one note. this was the code which i attempted:

    Code:
    void 	my_sounds(void)
     {
    	sound(500);		/* make a sound at 500 Hz.*/
    	delay(2000);		/* pause for 2000 milliseconds = 2 seconds  */
    	sound(1000);                          /* generate next sound at 1000Hz */
    	delay(2000);
    	sound(1500);
    	delay(2000);
    	sound(2000);
    	delay(2000);
    	nosound();		/* turn off sound	*/
     }
    do you know what is the best way to produce sequence of notes in C is?

    many thanks

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Google is your friend:
    http://www.harmony-central.com/Compu...gramming/#more

    Also, I'd check out code depot places like Sourceforge.net, etc, and see what someone might have going there that you could learn from.

    Good luck!

  6. #21
    Registered User
    Join Date
    May 2007
    Posts
    12
    Quote Originally Posted by Adak View Post
    Google is your friend:
    http://www.harmony-central.com/Compu...gramming/#more

    Also, I'd check out code depot places like Sourceforge.net, etc, and see what someone might have going there that you could learn from.

    Good luck!
    yeah google is normally my friend but not on this, i tried the harmony-central site before i came to this board.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  2. Audio Programming
    By dwalters in forum Tech Board
    Replies: 0
    Last Post: 02-07-2008, 04:20 PM
  3. Audio Drivers
    By Tonto in forum Tech Board
    Replies: 8
    Last Post: 08-30-2006, 09:07 PM
  4. audio programming (from scratch)
    By simpleid in forum C Programming
    Replies: 6
    Last Post: 07-26-2006, 09:32 AM
  5. Port Audio
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 12-07-2005, 11:43 AM