942,788 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 154751
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 8th, 2004
0

Sound in C++

Expand Post »
Hey I'm pretty new to C++. I'm taking a class on it now but I don't know how to make sounds in C++... I've done QBasic so I have a little programming background. I'm right now thinking of what to do for a program, and it largely depends on if I can make sounds or not and what sounds. I'm hoping to make some sort of dial up sound. If you can point me in the right direction or help me in any way, it'd be greatly appreciated. Thanks! :mrgreen:

-Dem-
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Demetrius is offline Offline
3 posts
since Dec 2004
Dec 8th, 2004
0

Re: Sound in C++

C and C++ do not have built-in standard support for sound. You will need to mention your compiler and OS and look into sound libraries.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 8th, 2004
0

Re: Sound in C++

I'm using DevC++ from Bloodshed and my OS is XP. I looked at the libraries and have no idea which is sound :cry:

Dem
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Demetrius is offline Offline
3 posts
since Dec 2004
Dec 8th, 2004
0

Re: Sound in C++

I assume you are using Windows. With your knowledge in basic you should look into BCX. It programs in a modern basic and then translates to compile with a number of C or C++ compilers. I look at C is just a subset of C++, well let's say 99% of it is.

BCX comes with a fair number of sound examples. Look over the generated C code to get an understanding of the API calls it makes.

You can download the whole system for free from:
http://www.rjpcomputing.com/programm.../devsuite.html

I have used BCX to learn the finer points of C and C++ myself for quite a while, and recommend it as a learning tool!

For simple sounds on the internal speaker use a Windows API call to Beep(), the code below should work with most any C++ compiler. I tested it with the Dev C++ IDE that uses the very generic GNU compiler G++.

[php]// simple sounds via Beep(frequency_hrz, duration_ms)

#include <iostream>
#include <windows.h> // WinApi header

using namespace std;

int main()
{
Beep(523,500); // 523 hertz (C5) for 500 milliseconds
Beep(587,500);
Beep(659,500);
Beep(698,500);
Beep(784,500);

cin.get(); // wait
return 0;
}
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Dec 8th, 2004
1

Re: Sound in C++

Quote originally posted by Demetrius ...
I'm using DevC++ from Bloodshed and my OS is XP. I looked at the libraries and have no idea which is sound :cry:

Dem
Okay, that helps. Now you can get beyond Beep() and use your soundchip. There is a little code snippet called "Play a MIDI voice (Dev C++ console program)" on DaniWeb at:

http://www.daniweb.com/code/snippet97.html

For those who need some hand holding with the Dev C++ IDE:
In the IDE go to FILE, then NEW, then Project, select (in this case) Console Application, give it a name like Sound1 then click OK. A filesave dialog box comes up, create a new folder, might as well call it Sound1, open it and save project file Sound1.dev there. The DevCpp IDE comes up with a bare bones template, select and delete that and cut and paste the snippet code into the empty editor page. Now compile and run. Don't forget to turn your speakers on.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Dec 9th, 2004
0

Re: Sound in C++

#include <iostream>
#include <windows.h> // WinApi header

using namespace std;

int main()
{
Beep(523,500); // 523 hertz (C5) for 500 milliseconds
Beep(587,500);
Beep(659,500);
Beep(698,500);
Beep(784,500);

cin.get(); // wait
return 0;
}
Reputation Points: 11
Solved Threads: 1
Newbie Poster
yb1pls is offline Offline
24 posts
since Dec 2004
Dec 9th, 2004
0

Re: Sound in C++

Heey ok thanks, the beeping worked and the other code for the 'voices' worked but now I'm trying to figure out how that code works. Any hints would help. The script is located in the URL given in an above post.

Thanks everyone!
-Dem-
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Demetrius is offline Offline
3 posts
since Dec 2004
Dec 9th, 2004
0

Re: Sound in C++

Quote originally posted by Demetrius ...
Heey ok thanks, the beeping worked and the other code for the 'voices' worked but now I'm trying to figure out how that code works. Any hints would help. The script is located in the URL given in an above post.

Thanks everyone!
-Dem-
Much of the code has to do with converting something you and I understand, like frequency, duration, and instrument to something the midi part of the sound chip in your computer understands.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Dec 9th, 2004
0

Re: Sound in C++

There is another way to make a sound, quite simple, you load a wave file and play it:
[php]// play a wave file sound using the winmm library
// For DevCpp add the library libwinmm.a to linker via
// Project/Project Options/Parameters/Add Library
// remember this is a console program!

#include <iostream>
#include <windows.h> // for PlaySound()

#define SND_FILENAME 0x20000
#define SND_LOOP 8
#define SND_ASYNC 1

using namespace std;

int main()
{
// play sound as loop (remove SND_LOOP for one time play)
// file boing.wav has be in the working directory
PlaySound("boing.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);

cin.get(); // wait
return 0;
}
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Jul 23rd, 2005
0

Re: Sound in C++

Is it possible for a console to make the sound be heard on other players computers ??
Like in a game.. Without changing the client the game is played on
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rezzo is offline Offline
1 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Query about loops. For, do...while or switch ?
Next Thread in C++ Forum Timeline: Migrating from VC6 to VC9





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC