Thread: Music

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    75

    Exclamation Music

    How can I put a full length song (3-4 Minutes) in a game. Please list the code needed and the headers that I need to add. Thank You Very Much!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Eh?

    There is [I presume] no difference between playing a 2 second, 20 second or 3-4 minute .WAV file.

    Perhaps this article will explain what you need to do:
    http://www.microsoft.com/msj/0698/sound/sound.aspx

    This assumes you are using Windows. If you are using Linux/Unix to write your game, I can't help, and you'll need to explain this to us.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    Thanks for the response, but I was looking for something more like the playsound function.

    Thanks

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And why do you think you can't use the PlaySound function in the first place - the above was just what I found when searching for "Play DirectSound" - if you are not using DirectSound bur prefer to use the multimedia functions, then I don't see a problem with that.

    So, what is it you are after, a full example version of PlaySound?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    Thanks, I have looked on the internet and I have been unable to figure out how to use playsound. I am looking for a example on how to use it. Im very confused on how to use playsound.

    Thanks

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, why don't you post a small piece of the code you are working on, with suitable questions on the bits you don't understand/can't get to work.

    It looks to me like the PlaySound is really simple to use - something like:
    Code:
    #include <Windows.h>
    
    int main()
    {
         TCHAR name = "c://blah.wav";
         BOOL f;
    
         f = PlaySound(name, NULL, SND_FILENAME);
         if (!f) {
             printf("Error\n");
         }
         return 0;
    }
    I haven't tried that, and of course, the "name" has to be a valid wave-file of some sort.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    Here is the Code
    Code:
    #include <iostream>
    #include <string>
    #include <windows>
    #include <cstdlib>
    #include <mmsystem.h>
    #pragma comment(lib,"Winmm.lib")
    using namespace std;
    
    int main()
    {
    	cout << "Hello world" << endl;
    	system ("PAUSE");
    	
    PlaySoundA("fanfare.wav", 0, SND_RESOURCE | SND_NODEFAULT);
    return 0;
    
    	return 0;
    }
    That was the best I could do (and it wont complile)

    Thanks

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What compiler errors do you get?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    I updated to Visual 2008 and no longer get compiler erros. Unfortunately I am unable to hear the sound file. Please help. Thanks

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    PlaySoundA("fanfare.wav", 0, SND_RESOURCE | SND_NODEFAULT);
    Are you sure that the red code is correct?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    no. Do you know what the right code is? Thanks.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I thought "SND_FILENAME" would make more sense from the parameters you use - but I'm not sure at all.
    Quote Originally Posted by MSDN
    Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is interpreted as an alias for a system event, a filename, or a resource identifier. If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a filename.
    I don't think "fanfare.wav" is a resource, but rather a filename.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    Please explain by posting code. I do not understand. Thanks Very Much

  14. #14
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    How can I put a full length song (3-4 Minutes) in a game.
    For PlaySound(), there is a limit to the WAV file size. When I once tried to play a full song with PlaySound() I got an error.

    MSDN says:
    Remarks
    The sound specified by pszSound must fit into available physical memory and be playable by an installed waveform-audio device driver....
    I assume "available physical memory" depends on how much RAM you have, how much RAM is being used by Windows and other applications.... And maybe how much physical RAM Windows is willing to allocate to your program....

    codeproject.com has a DirectSound example that can play a whole song.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MaGaIn View Post
    Please explain by posting code. I do not understand. Thanks Very Much
    You mean, my example that I posted earlier isn't good enough? It does exactly what I said you should change, using SND_FILENAME as the documentation says. I'm sorry, I'm not going to write the same thing again. Of course, you WILL have to change [or copy a file to match] blah.wav. But I think you are capable of that.

    But since I wanted to make sure I wasn't telling porkies, I produced this, that runs on my machine:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    	BOOL f;
    	f = PlaySound("c:\\winnt\\media\\tada.wav", 0, SND_FILENAME);
    	if (!f) 
    		printf("Error\n");
    	else
    		printf("Done!\n");
    	return 0;
    }
    Of course, I missed the note about the wav file fitting in memory, so if you want to play 4 minutes of 40 KHz samples, you are looking at needing 40MB or thereabouts. (40 KHz * 2 channels * 2 bytes per sampel * 240 seconds)

    --
    Mats
    Last edited by matsp; 10-29-2007 at 04:49 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone saves us from music
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 78
    Last Post: 07-02-2008, 08:59 AM
  2. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  3. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  4. Is AOL music crazy?
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-24-2006, 07:24 PM
  5. crashing when switch music type
    By lambs4 in forum Game Programming
    Replies: 7
    Last Post: 05-03-2004, 12:50 PM