Thread: How to play a sound

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Question How to play a sound

    I was wondering if there was any way to make C++ emit sounds, and at different frequencies. What I want to do is be able to play a note for a specific period of time then play a different note (either higher or lower) for a different set period of time. I'm running on a Windows computer if that makes a difference.
    Thanks in advance to anyone that helps.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
    while (1)
      printf("\a");
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User credobyte's Avatar
    Join Date
    Jun 2009
    Posts
    1
    Quote Originally Posted by ಠ_ಠ View Post
    Code:
    while (1)
      printf("\a");
    Beep, beep .. That's not what he's looking for !

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    C++ provides nothing for this. There are a number of libraries out there written in C++ that can produce sounds of various frequencies however. You mentioned you are on windows...check out the direct sound API or just a standard win32 call (PlaySound()).

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by asofaihp View Post
    I was wondering if there was any way to make C++ emit sounds
    I thought it was the computer, or more specifically the sound card (or even more specifically, the speakers), which emitted sounds.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    You can play a beep, like : printf("%c",7) (ASCII code 7 is BELL, normally)

    But me too, I'm searching how to play a .wav instrument sound, to really "program" music rythm..maybe one could help me there ?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    PlaySound Windows API, or some media-based API such as Windows Media Foundations or DirectShow.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    winmm.lib in widows contains a function called PlaySound defied in mmsystem.h as mentioned above. It can play a sound file of upto 1 mb.

    Code:
    PlaySound(L"asynchronized.wav",NULL,SND_ASYNC|SND_FILENAME|SND_LOOP);
    Its not the best but it gets the job done, and to stop a sound

    Code:
    PlaySound(NULL,NULL,NULL);
    Spidey out!

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    External

    Im using linux but if u r not about timing u can use a system call of any external console player, under win too like :

    wait((void *)system("playwave test.wav"));

    where playwave could be any player found on net

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    A simple Google search reveals many sound APIs you can use.

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    I want to play sounds at some rythm, but i dont know how to time precisely the pause. Which lib could i use under linux ?

  12. #12
    i've lost my mind
    Join Date
    Jun 2008
    Posts
    26
    You're in the wrong place for this kind of knowledge, however lucky you I was strolling around here and can tell you exactly what to do.

    Download FMOD, that's an excellent library - just don't sell your software and your creations will be free to use and spread.

    A part of the FMOD API is routines to play user created buffers, which naturally you will write sinusoidal values to.

    Actually, FMOD comes with an example of user created sound within their API.

    FMOD is used in a lot of professional/commercial applications, it is fast and powerful as hell, you get all kinds of tools for DSP and effects, you can also do trivial things such as play files and read the audio buffers.

    I've been using FMOD for audio visualization projects and audio creation projects.

    FMOD.

    OpenAL may do the same, I've only used OpenAL to read audio buffers so I cannot say for sure what it is capable of, I assume mostly everything FMOD can do. I just prefer FMOD since I've adapted and grown used to it. :]

    I also recommend you sign up over at the FMOD or some audio programming forum, with questions related to these things you really want people used to the math and systems associated with this. This forum is more language specific than anything else.

    For general graphics or game programming GameDev is ideal, good community of experienced developers in those fields.

    When ever you venture in to new aspects of computer programming, it's really important to go to the right places otherwise you'll be frustratingly dealing with people who can't really say much on the subject, or you may just get nothing useful at all.

    Mastering a programming language is not mastering audio/visual programming. cboard is all about the language itself and its implementation.

    I hope this will help, I hope I didn't offend any of you cboard people with my bluntness, you guys know your ........ when it comes to the language itself. :]
    Last edited by gltiich; 09-02-2009 at 02:41 PM.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    OpenAL is perfectly suited for whatever you may need but FMOD will do the same.

    Mastering a programming language is not mastering audio/visual programming. cboard is all about the language itself and its implementation.
    Cboard covers more than just the language in its other forums such as Windows, Networking, Tech, Game programming, etc.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    So please do not feel discouraged of asking anything about anything that can be expressed in the C++ programming language. Chances are you will get an answer. And rarely will you get a bad final answer.

    The collective knowledge on these boards extends well far beyond the C and C++ programming languages rules, syntax and structure. It goes deeply into all the possible applications for the programming languages you can think of. Heck, it extends well over to other programming languages!

    I've seen all sorts of things being discussed here. From cryptography to artificial intelligence. From scripting languages like LUA, to the very backbone of every programming language, Assembly. From game projects to operating system kernels. From brilliant small algorithms that are the cells of programming life, to immense complex applications.
    Last edited by Mario F.; 09-02-2009 at 09:14 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by brewbuck View Post
    I thought it was the computer, or more specifically the sound card (or even more specifically, the speakers), which emitted sounds.
    well actually theres an on board piezo buzzer that can also emit sounds, you can even make it emit primitive audio by messing with the timer.

    OMFG I had to add piezo to google's dictionary............
    OMFG #2 i had to add google...................

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX Question
    By bladerunner627 in forum Game Programming
    Replies: 2
    Last Post: 04-04-2005, 11:55 AM
  2. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 01:58 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Gui Class With Tic Tac Toe
    By xxYukoxx in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2003, 04:28 PM
  5. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM

Tags for this Thread