Thread: Trouble with Audio using C++

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile Trouble with Audio using C++

    Greetings my friends.

    I have been attempting to get this right for a while now, but so far no luck.
    I will begin by introducing my goal, then post the script I currently possess, and the errors I wish to fix.

    Goal:
    My goal is to make a simple program that plays an audio file of any extension.
    This audio file would be local and have a directory (say, my Desktop).

    Script:
    Code:
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <windows.h>
    #include <mmsystem.h>
    #pragma comment(lib,"winmm.lib");
    
    using namespace std;
    
    int main()
    {
        PlaySound("C:\\Users\\Main\\Desktop\\Sample.wav", NULL, SND_FILENAME);
        return 0;
    }
    The first limitation I am aware of with using PlaySound() is the file type can only be WAV.
    I'm okay with this limitation for now - I will investigate alternatives after I have at least one working basis to rely on.

    Error:
    ||undefined reference to `_PlaySoundA@12'|

    The compiler I am using is Code::Blocks 8.02

    Any help is appreciated!
    Thank you everyone

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Try adding libwinmm.a to your project.
    It should be in mingw's lib directory.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile

    Do you know which line I should add this too?
    You think you could repost the code with the proper additions?
    Sorry, I'm a beginner

    Thanks!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CPlus View Post
    Do you know which line I should add this too?
    You think you could repost the code with the proper additions?
    Sorry, I'm a beginner

    Thanks!
    It's not a code change. The appropriate library needs to be added to the project.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Hmm I'm confused

    You guys have a lot of knowledge, I'm sorry for wasting your time.
    I just wish I understood.

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    There is nothing wrong with your code. This is a linking error.

    It's been such a lot time since I've used C::B that I don't remember how you do it. Do a web search on it or mess around with the compiler settings or build options.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Ohh I see...I'll look into that right away

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Okay, so I found MingW in my computer.
    Do any of you know where to get the "libwinmm.a" file?

    I still have to figure out how to link it, but I'm researching that right now.

    Thanks

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    You *should* already have it in your mingw\lib directory.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Yes, this is strange. I guess I'm not doing it right

    I wish there were user-friendly tutorials on such fundamentals.
    Back to researching on the solution.

    EDIT:

    Nevermind! I found it!
    Great.
    Now to find how to link ... I switched to DevC++ now. Maybe it'll be easier.
    Last edited by CPlus; 02-08-2010 at 07:41 PM.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    My goal is to make a simple program that plays an audio file of any extension.
    That is not possible without paying a lot of money for something like the Miles Sound System from RAD game tools.

    Windows functions only play WAV files. If you write code that plays MP3 files then you could be reponsible for royalties and licensing fees. OpenAL can play any type of file but you must write the file format decoder. Ogg is becoming a widely used format and has many decoders already available so it can be used in OpenAL and other libraries. There are many many many types of sound files and mroe are added each day. There is no way that one library could support them all.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would just use DirectShow. Old and crude, perhaps, but it still does its job very well. A single decoder--ffdshow--can decode the most popular formats available and many more, and for nay a single penny.
    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.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    DirectShow does support MP3's but AFAIK it has been deprecated. Use at your own risk. It has, however, been integrated into the Platform SDK. Most of the things in the Platform SDK stick around for a very very long time even if they have long since been deprecated. So perhaps you could get away with using DirectShow to play MP3's if you so desire. It certainly does beat your other options. Not sure how MS got away with decoding the MP3's in an API without forcing everyone to pay MP3 royalties...but then again they are Microsoft.

  14. #14
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    If you still haven't found how to link:

    Go to Settings->Compiler and Debugger

    Click the Linker Settings tab

    In the Link Libraries group box, click add and type "winmm"

    Click OK

    That should do it

  15. #15
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Code:
    #pragma comment(lib,"winmm.lib");
    I've seen quite a few people trying to use this pragma in gcc/g++ lately. Last time I checked gcc ignored it, has that changed now a days?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Audio Programming
    By dwalters in forum Tech Board
    Replies: 0
    Last Post: 02-07-2008, 04:20 PM
  2. Audio Drivers
    By Tonto in forum Tech Board
    Replies: 8
    Last Post: 08-30-2006, 09:07 PM
  3. audio programming (from scratch)
    By simpleid in forum C Programming
    Replies: 6
    Last Post: 07-26-2006, 09:32 AM
  4. Bluetooth Audio Stream
    By Charmy in forum Windows Programming
    Replies: 2
    Last Post: 05-06-2006, 12:10 AM
  5. Port Audio
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 12-07-2005, 11:43 AM

Tags for this Thread