Hi.
Can you play music in c++ in a program?
if so does anyone have any code on how it works?
Thanks
Printable View
Hi.
Can you play music in c++ in a program?
if so does anyone have any code on how it works?
Thanks
Search the fourms or Google for playsound. It's a common question.
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
and this is the beepsCode:cout<< "Washing...\n";
for (n=30; n>0; n--)
{
printf ("%d\n",n);
wait (1);
}
cout << ("WASHING COMPLETE\n");
return 0;
}
at the moment it will either play the music then countdown or countdown then play but i would like it to do both.Code:Beep(523,500);
Beep(784,500);
Beep(784,500);
Beep(872,500);
Beep(872,500);
Beep(784,600);
Beep(698,500);
do something like
that is just a of the top of my head suggestion, but I think it will work, of course your question was quiet vagueCode:for ( int x = 0; x != 30; ++x ) {
Beep(int, int);
printf("&d\n", n );
wait(1);
}
I think Raigne means:
but seeing as it's the C++ board you should use cout<< n << '\n'; reallyCode:for ( int n = 0; n != 30; ++n) {
Beep(int, int);
printf("%d\n", n );
wait(1);
}
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.Quote:
Originally Posted by twomers
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.
That sleeps for 100 mili seconds. For your example, change it to Sleep( 1000 );Code:#include <windows.h>
int main( void )
{
... whatever
Sleep( 100 );
... whatever
}
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.Quote:
Originally Posted by l2u
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
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?
Ok, how long to the beeps last for? Seconds wise? You can play the beep, then wait() for ( 1 second - beep time ). Get me?
each beep last 500th of a second n got about 20
Ok then. Does your wait function only take integers?
Altertatively, you can make a timing class to time the beep function, and then subtract that number from 1 second.Code:for( limits )
{
beep()
wait( 0.5 ); // or wait( 1.0 - 0.5 )
cout<< "BEEP!";
}
this is the wait function
Code:void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}
If you change it to the above. A question though, is CLK_TCK 1000?Code:void wait ( double seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK;
while (clock() < endwait);
}
now you can say - wait( 0.5 ), and it'll work ... hopefully
EDIT ::
Also, here's a REALLY simple timing class I made before - http://cboard.cprogramming.com/showp...4&postcount=10
To use it, do something like this
EDIT:: NP RaigneCode:TimeIt TimeForBeep;
for ( limits )
{
TimeForBeep.StartTimer();
// your beep stuff here
TimeForBeep.EndTimer();
wait( 1 - TimeForBeep.GetTime() );
cout<< "Whatever";
TimeForBeep.Clear(); // Not really needed I suppose
}
If I understand correctly you need to make your program wait the exact length of each Beep so that it is relatively right, but if you want you could use multi-threading to doit as well, that is if you want to make somehting like a audio mixer or something, and thanks for fixing my mistake in my first post twomers ( geeze i cant type today)
i cant remember what the CLK_TCK is but i think it is 1000
says there is an error with
Code:endwait = clock () + seconds * CLK_TCK;
What's the error, peckitt99?Quote:
Originally Posted by peckitt99
Also, look at my edit on my previous post.
it says
[warning] assignment
sorry bout all the questions im actually new to this n gotta learn it since im doing computing forensics in uni, lol
It's warning you cause the types are different. ints doubles and clock_t's
You have a few options, revert to an int, but instead of having seconds, use miliseconds, so 500 would be half a second ect.
Or cast the result. I think the above is a better option.Code:void wait ( int miliseconds )
{
clock_t endwait;
endwait = clock () + miliseconds
while (clock() < endwait);
}
ah right ok thanx,
ill give it all ago n see if i can get it to work.
thanks for all your help
for some reason it dont like wait(0.5). says there is a warning with the passing.
Are you using the milisecond version? If so, say - wait( 500 ); 0.5 is full seconds, remember.
With the portability thing though I am sure I saw that your code was using (or you were going to use) system ("CLS") making it un-portable so therefore it is a lot easier to use Sleep ();
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Windows Console Option. Still using windows, but after that, use option 5
yeah i have changed it to 500 but it still cant play both at the same time it is always waits for one to finish before doing something again.
Course it does! Those functions aren't asynchronous! Sorry, I thought I'd expressed as much in an earlier post. I don't know any asynch functions to play sound.
Did you mean CLOCKS_PER_SEC?Code:CLK_TCK
Quote:
The time in seconds is the value returned by the clock function, divided by CLOCKS_PER_SEC. CLK_TCK is equivalent, but considered obsolete.
That's what you get when you pass a floating point number (.5) to a function that expects an integer:Quote:
for some reason it dont like wait(0.5). says there is a warning with the passing.
(I know you got it to work with 500, I'm just explaining the warning.)Code:void wait ( int miliseconds )
[offtopic]
Hey twomers, in your signature, the last ' in this
isn't part of the link.Quote:
Being watched very closely by 'Ken Fitlike'
[/offtopic]
umm just an idea but this music API is really good I use FMOD It allows just about anything you want, Just a thought though