Thread: How to handle a wav files in C? (not C++ or other)

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    11

    How to handle a wav files in C? (not C++ or other)

    Hello. I am fairly new to windows programing. I only have basic knowledge in C programming. I want to make an aplication that uses sounds (wav files to be more exact).I would like to know it there is a fairly easy way to use some calls like playsound, stop, pause, etc. from a dll? I've researched on google and i've come up with winmm.dll. But i really don't know how to use a dll. I've also come up with <mmsystem.h>. Can i use this to handle sounds similar to the way i use <string.h>, for example, to process strings? If so, where can i find a help file for it?, because it's not a standard library. Can anyone point me in the right direction. Thank you.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I am going to move this question to the windows programming board because the functions you are asking about are windows specific.

    The simplest function to use is PlaySound and there have been a number of questions in the past concerning its use on these boards so a search should turn up helpful information. All the windows api functions are documented on msdn online. To use any of these functions you just need to link with the relevant library(there is another, more complex technique but given your stated level of skill and the fact that it offers no advantage in the context of your question I'll say nothing more about it). Something like:
    Code:
    /*link with winmm.lib (-lwinmm with MinGW)*/
    #include <stdio.h>
    #include <windows.h>
    #include <tchar.h>
    
    int main(void)
    {
    
    PlaySound(name_and_path_of_your_wav_file,0,
              SND_ASYNC|SND_FILENAME|SND_NODEFAULT);
    return 0;
    where name_and_path_of_your_wav_file is self-explanatory. Don't forget to link to winmm.lib (libwinmm.a with MinGW; just use -lwinmm) otherwise you'll get linker errors about the PlaySound funtion. If you tell us which compiler you are using then more specific information can be given.

    Welcome to the boards - the faq is here.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    Thank you for your help. For the moment i am using the C-free IDE. Can you tell me more precisely how to link with the dll; where do i type -lwinmm? By the way, is C-free good in your opinion? Should i be using something else, like Dev C? I've heard of it but haven't used it. So, in your opinion, should i stick with C-free or do you recommend something else?

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I've never used C-Free, but it doesn't seem to be completely free. It seems to use MinGW though, so I guess you should find the linker options somewhere and type -lwinmm.

    Dev-C++ can compile C code.
    So does Pelles C.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    I'm still having trouble. I've modified the code above a little , because it was giving compiler errors. I read in WIN32SDK.HLP that Playsound is "related" to winmm.dll and mmsystem.h so i included it as well. I made a project and in the "Furher linker objects files and linker options" i added -lwinmm (i'm using Dev-C++4). I've added the modified file to the project:

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <mmsystem.h>
    
    int main()
    {
    
    PlaySound("C:\\test.wav",NULL,
              SND_ASYNC|SND_FILENAME|SND_NODEFAULT);
    return 0;
    }
    Now it seems to be ok, but when i run it nothing happens. Why? I'm preety lost.
    Last edited by Deviance; 07-10-2006 at 02:34 PM.

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Your program is ending all too quickly. Try playing the sound with SND_SYNC and not SND_ASYNC. If I run your code, occaisionally I hear the first split second of the wave file.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    11

    Success

    Quote Originally Posted by Cactus_Hugger
    Your program is ending all too quickly. Try playing the sound with SND_SYNC and not SND_ASYNC. If I run your code, occaisionally I hear the first split second of the wave file.
    Thank you, thank you , thank you. It does work

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Should have mentioned this previously...
    You should read the MSDN documentation on PlaySound(). SND_SYNC isn't a cure-all; there are times for it, and times where SND_ASYNC is more appropriate. SND_SYNC causes PlaySound() to block, ie, from main()'s point of view, the program pauses (as the sound is played).
    OTOH, if you have a full window'd application that isn't going anywhere, but should be responsive to other use input while the sound is being played, then SND_ASYNC is a better choice.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    while were on the topic, does anyone know of any good libraries to handle basic audio file manipulation (such as mp3-to-wav conversion)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. how to play play and stop wav files
    By cnu_sree in forum Linux Programming
    Replies: 4
    Last Post: 08-14-2006, 11:11 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Combining multiple wav files into one
    By eam in forum Tech Board
    Replies: 3
    Last Post: 01-17-2005, 11:08 AM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM