1,105,538 Community Members

Sound in C++

Member Avatar
Demetrius
Newbie Poster
3 posts since Dec 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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-

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

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.

Member Avatar
Demetrius
Newbie Poster
3 posts since Dec 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

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/programming/bcx/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++.

// 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;
}
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
1
 

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.

Member Avatar
yb1pls
Newbie Poster
24 posts since Dec 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

#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;
}

Member Avatar
Demetrius
Newbie Poster
3 posts since Dec 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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-

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

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.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
1
 

There is another way to make a sound, quite simple, you load a wave file and play it:

// 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;
}
Member Avatar
Rezzo
Newbie Poster
1 post since Jul 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 :?:

Member Avatar
Lizel
Newbie Poster
1 post since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I Used Borland C++ Version 5.02 With OS Windows... can you all help me to write source code to play music/sound in C/C++ language and adding picture to C... THX...

Member Avatar
zandiago
Nearly a Posting Maven
2,463 posts since Jun 2007
Reputation Points: 115 [?]
Q&As Helped to Solve: 55 [?]
Skill Endorsements: 0 [?]
Featured
 
0
 

Lizel.....welcome aboard.I reccommend taking a look at the Rules & FAQ for the daniweb forum. Please do not re-open old threads....look at the posting date of the question before you reply. Again ,welcome. Please start your own thread with what you've tried!

Member Avatar
vb6exp32
Newbie Poster
2 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks for the link to BCX ... i've found a few other links along the way.
i was hoping this BCX could help me translate some vb6 projects to native C/C++ apps so i could study the code and learn more.

Member Avatar
gabi_a
Newbie Poster
1 post since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

how do the numbers correspond to a sound? I want to be able to play a song snippet...

Matsuoka
Newbie Poster
1 post since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

A few note frequencies

E = 329
B = 493
Fs = 698
Es = 659
Gs = 783
G = 392
A = 440
D = 587
F = 349

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article