Thread: generating audio in C

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    12

    generating audio in C

    Hi,

    Iam trying to design a small C programme which can generate a squarewave function and play a sequence of musical notes for 30 seconds. How would i go about doing this?

    Many Thanks

    teknologik

  2. #2
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    Under some compilers, there is a function called sound(). Note that this function is not portable, so don't rely on it. For audio, I would suggest a library such as SDL and/or SDL_Mixer.

    EDIT: The compiler I discovered the sound() function on is called DJGPP.
    Last edited by kawk; 05-09-2007 at 02:42 PM.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    12
    i know how to make the square wave using C its just im not sure how to generate a sequence of musical notes correctly.

    static char seq[]="CDEFGABAGFEDC"; but thats as far as i can get

    does anyone know how to generate a sequence of notes?

  4. #4
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    Well, presuming this is homework, I'd suggest looking into whatever your professor or tutor suggests . . .

    IF (and only if) your tutor/professor can't tell you what to use, try sound() and a lookup table of freqs.

    EDIT: If this isn't homework, I'm sorry . . .

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    12
    my school days have been and gone; so lucky for me this isnt homework other wise i would never get it handed in on time lol just trying to learn some C thats all, how would i go about making a look up table then

  6. #6
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    Well, I know that middle A is 440 . . . I have a lookup table around here somewhere . . .

  7. #7
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    Here you go:
    Code:
    #define _LC 130
    #define _LCS 137
    #define _LD 145
    #define _LDS 155
    #define _LE 166
    #define _LF 175
    #define _LFS 185
    #define _LG 195
    #define _LGS 207
    #define _LA 220
    #define _LAS 235
    #define _LB 245
    
    #define _MC 260
    #define _MCS 275
    #define _MD 290
    #define _MDS 311
    #define _ME 333
    #define _MF 350
    #define _MFS 370
    #define _MG 390
    #define _MGS 415
    #define _MA 440
    #define _MAS 470
    #define _MB 490
    
    #define _HC 520
    So, _LC is Low C, i.e. one octave below Middle C. _LCS is one semitone higher.

    BTW, I found this by playing them on my piano (which was out of tune at the time . . .) so they may be a little off. You do know that one octave higher is double the freq, right?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Googling for "piano note hertz" turned up this page: http://www.vibrationdata.com/piano.htm
    It has hertz values for different notes.

    [edit] Also, see this Wikipedia entry: http://en.wikipedia.org/wiki/Note
    Scroll down to Note frequency (hertz) for a formula for calculating frequency values. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    So, a simple program to play the above sequence you mentioned:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main() {
        int notes[14] = {_MC, _MD, _M, _ME, _MF, _MG, _LA, _LB, _LA, _LG, _LF, _LE, _LD, _LC};
        int x;
        for(x = 0; x < 14; x ++) {
            sound(notes[x]);
            delay(100); /* NOTE! THIS CRASHES WINDOWS 98 SOMETIMES */
        }
        sound(0);
        return 0;
    }
    Two things: I apoligize about the underscore leading the #defines, and this code _may_ not work, as I haven't compiled it yet . . .

    EDIT: Try sleep(), but I don't know if RHIDE/DJGPP supports it . . .

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    DJGPP's sound() function appears to be in pc.h: www.delorie.com/djgpp/doc/libc/libc_735.html

    That function looks very unportable. Perhaps there is a Windows function that would be more likely to work with the OP's existing compiler?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    I don't know, as I am suggesting a possible solution, not a rock-solid answer. Oops :P.
    Last edited by kawk; 05-09-2007 at 03:25 PM. Reason: Speling eror

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    12
    Quote Originally Posted by kawk View Post
    So, a simple program to play the above sequence you mentioned:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main() {
        int notes[14] = {_MC, _MD, _M, _ME, _MF, _MG, _LA, _LB, _LA, _LG, _LF, _LE, _LD, _LC};
        int x;
        for(x = 0; x < 14; x ++) {
            sound(notes[x]);
            delay(100); /* NOTE! THIS CRASHES WINDOWS 98 SOMETIMES */
        }
        sound(0);
        return 0;
    }
    Two things: I apoligize about the underscore leading the #defines, and this code _may_ not work, as I haven't compiled it yet . . .

    EDIT: Try sleep(), but I don't know if RHIDE/DJGPP supports it . . .
    hello kwak,

    the compiler which i am using Turbo C v 2.0.1 (i think) didnt seem to like the _ between the notes, is there anything you can suggest which may work?

    thanks

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the compiler which i am using Turbo C v 2.0.1
    Lemme guess, your OS is XP ?

    Would you buy a Ferrari, and replace the engine with a steam engine?
    Because that is what you've done by trying to use TC2.01 on a modern machine.
    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.

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    12
    yes the os is xp

  15. #15
    Registered User
    Join Date
    May 2007
    Posts
    12
    ok then salem what do you recommend a suitable compiler?

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