Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I play a simple sound (NO MIDI) through of the PC Speaker with

Status
Not open for further replies.

Capo

Programmer
Feb 8, 2001
10
0
0
US
How Can I play a simple sound (NO MIDI) through of the PC Speaker with C/C++?


1000 Thanks Matias.

 
Do you mean the internal speaker or the external speakers connected through something like a sound card?

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
I'm talking about the internal speaker.
 
Look at the sound and nosound functions in dos.h. The sound function takes an unsigned integer to make a sound of that frequency. The example I have makes a 440 Hz tone for 1 second.
Code:
#include <dos.h>

int main()
{
    sound(440);
    delay(1000);
    nosound();
    return 0;
}

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
James, thank for your help, but I can´t compile the program because the compiler does not find the functions (sound, delay, no sound) in the dos.h. Do u know why?
 
try to use WindowsMediaPlayer AciveX control. John Fill
1c.bmp


ivfmd@mail.md
 
Capo,
[tab]What version of the compiler are you using?

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
I'm using the Free Command Line Tools of Borland.

Matias Cobiella
 
You're right, Borland C++ 5.5 does not have that in their dos.h file. A quick search on (which you can join for free, by the way) brought up this article.
Technical Notes Database

TN1928C.txt Playing sound from a Windows application
Category :C++
Platform :All

Description:

QUESTION:
How do I play sound through the PC Speaker in a Windows Application?

ANSWER:
Use the following source code...
Code:
     void FAR PASCAL UIErrorBeep(void)
     {
         OpenSound();
         SetVoiceNote(1, 49, 16, 1);
         SetVoiceNote(1, 54, 32, 1);
         StartSound();
         WaitSoundState(S_QUEUEEMPTY);
         StopSound();
         CloseSound();
     }
Note: This source is provided as an EXAMPLE, not the definitive rule on
how to do something in Windows.

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top