Thread: Music

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Music

    Hi.

    Can you play music in c++ in a program?

    if so does anyone have any code on how it works?

    Thanks

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Search the fourms or Google for playsound. It's a common question.
    Last edited by twomers; 09-04-2006 at 08:19 AM.

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    Thanks i got it

    just another question

    i have created a sound using beeps and was wondering if i can have a count down happening at the same time as the music?

    if so how will i do it

    here is the code for the timer

    Code:
             cout<< "Washing...\n";
             for (n=30; n>0; n--)
               {
                   printf ("%d\n",n);
                       wait (1);
                         }
                           cout << ("WASHING COMPLETE\n");
                             return 0;
                             }
    and this is the beeps

    Code:
      Beep(523,500);
      Beep(784,500);
      Beep(784,500);
      Beep(872,500); 
      Beep(872,500);
      Beep(784,600);
      Beep(698,500);
    at the moment it will either play the music then countdown or countdown then play but i would like it to do both.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    do something like
    Code:
    for ( int x = 0; x != 30; ++x ) {
        Beep(int, int);
        printf("&d\n", n );
        wait(1);
        }
    that is just a of the top of my head suggestion, but I think it will work, of course your question was quiet vague

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I think Raigne means:

    Code:
    for ( int n = 0; n != 30; ++n) {
        Beep(int, int);
        printf("%d\n", n );
        wait(1);
        }
    but seeing as it's the C++ board you should use cout<< n << '\n'; really

    Also, I've been meaning to ask you, why don't you use Sleep() instead of making your wait() function?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by twomers
    Also, I've been meaning to ask you, why don't you use Sleep() instead of making your wait() function?
    Because Sleep() is not standard while wait() potentially is.
    Sent from my iPadŽ

  7. #7
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    cool, thanx, ill give it a go

    the reason why i have not use sleep is because i dont knw how to use that command.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    #include <windows.h>
    
    int main( void )
    {
      ... whatever
      Sleep( 100 );
      ... whatever
    }
    That sleeps for 100 mili seconds. For your example, change it to Sleep( 1000 );

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    630
    Whats the difference betwen Sleep and wait?

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by l2u
    Whats the difference betwen Sleep and wait?
    As I said, one is horribly not portable to Linux, the other is a home made function that could have been written using <ctime> functions and could potentially be standard. Why twomers still insists on suggesting it, I have no idea. There are standard alternatives.
    Sent from my iPadŽ

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    The OP made the wait() function, and it's portable potential etc, Sleep() is in <windows.h>


    >> Why twomers still insists on suggesting it, I have no idea.
    Initially I was curious as to why the OP didn't use it, and then when he didn't know how to use it, I showed him. Plus, I see absolutely no reason in not giving alternatives to people. But I agree that there are standard alternatives. How could I not Sleep(), IMO (I mainly use windows you see), is a quicker to write
    Last edited by twomers; 09-04-2006 at 11:26 AM.

  12. #12
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    just tried the code and it has to play the beep before counting down to the next number,

    is it possible to get the both of them going at the same time so i have a melody whilst it is counting down because i have 20 or so beeps and i have to wait for them to finish untill it counts down 10 then again for 9 and so on till the counter gets to zero?

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Ok, how long to the beeps last for? Seconds wise? You can play the beep, then wait() for ( 1 second - beep time ). Get me?

  14. #14
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    each beep last 500th of a second n got about 20

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Ok then. Does your wait function only take integers?

    Code:
    for( limits )
    {
      beep()
      wait( 0.5 ); // or wait( 1.0 - 0.5 )
    
      cout<< "BEEP!";
    }
    Altertatively, you can make a timing class to time the beep function, and then subtract that number from 1 second.

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