How can I make different basic tones (which function?). The windows beep() function is only one sound. So is there any windows function? (or other header file?)
How can I make different basic tones (which function?). The windows beep() function is only one sound. So is there any windows function? (or other header file?)
Stan The Man. Beatles fan
When I was a child,
I spoke as a child,
I thought as a child,
I reasoned as a child.
When I became a man,
I put childish ways behind me"
(the holy bible, Paul, in his first letter to the Cor. 13:11)
No, not if you're using a real (NT,2k,XP) version of Windows.Originally posted by Stan100
The windows beep() function is only one sound.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
You might look for information on programming the 8254 "PIT" (Programmable Interface Timer). The PC speaker is connected to the PIT.
I've done a tiny bit of research, but haven't written any code or really studied the stuff I printed-out yet.
Here's what I think I know... The PIT is driven by an approximately 19kHz clock. There are two timers. You load one of the timers with a count that sets the frequency (19kHz / count = freq ???). The other timer gets loaded with a count that sets the duration.
[edit]-----------------------------------------------------------
It's a PIC = Programmable Interrupt Controller and the base clock frequency is 1,193,180 Hz.
I couldn't find sound() in MSVC++, but if your compiler has it, great! Neither approach is portable, so use the simplest solution!
[edit]-----------------------------------------------------------
If you're running NT, Win 2k, or XP, you'll also have to look-up how to get-around the hardware "protection". (Look for information on writing drivers.)
Windows 98 shouldn't have any problems. I'm writing this on a Win 98 machine without a sound card or external speakers. It runs test programs written in Quick-BASIC. I got tired of the thing beeping when the test passed, beeping when the test failed, and beeping when I was supposed to do something. So I wrote three subroutines for "pass sound", fail sound" and "alert sound". The sound routines get fouled-up (extra long duration) when I'm running multiple applications (fooling around on the internet - like now!)
Last edited by DougDbug; 01-06-2003 at 09:38 PM.
There is a function called sound, defined in the header file dos.h. It turns on the speaker at a specified frequency. The function nosound turns off the speaker.
void sound(unsigned frequency);
void nosound(void);
The value of the frequency is in hertz(Hz).
You can use the function called delay, also defined in dos.h, to make the tone last for a specified number of milliseconds.
void delay(unsigned milliseconds);
However, I think the sound function works only for DOS. I do not know about a similar function for Windows.Code:/*Program to play a 100Hz tone for 5 seconds*/ #include<dos.h> int main() { sound(100); delay(5000); nosound(); return(0); }
Compiler:Turbo C++ v3.01.
Last edited by sundeeptuteja; 01-07-2003 at 06:09 AM.
my dos.h does not have sound(), delay(), or nosound()
I'm using MVC++ 6.0
Is there another header or one I can download?
Last edited by Trauts; 01-08-2003 at 09:39 AM.
I believe the functions sound(), nosound() and delay() are available only with Borland compilers. You can get the free Borland compiler here : http://www.borland.com
Last edited by sundeeptuteja; 01-08-2003 at 09:47 AM.
Would it be possible to use those headers with MVC++ 6.0? If so, can you please upload the dos.h file and any other required ones for the sound() and nosound()? Thanks!
Last edited by Trauts; 01-08-2003 at 09:51 AM.
Unfortunately the header files/libraries are usually provided as part of the compiler. I doubt that the header file created for Borland C++ will work with MSVC++(I used to think like you at one time myself, I found out the hard way).
Last edited by sundeeptuteja; 01-08-2003 at 09:56 AM.
You could try using sndPlaySound() but it only works in Windows because it's a windows api function..
0100001
So what? Win98 was a bootleg model? No, I don't believe so.No, not if you're using a real (NT,2k,XP) version of Windows
I know how to play a sound, I want basic tones. C-D-E-F-G-A-B-C
I making a piano program, and I know there must be a way to do it w/out downloading each tone (which I'm NOT)
Song of the day:
If I could write some magic code, that everyone could read.
I'd write some code, of RPG's, where everyone is free.
And when I write my magic code, it would not be wrote in C.
Not in sharp, nor in BASIC, some code for all of us.
"My code is magic, you see right here, 'cuz it was written in C++!"
Stan The Man. Beatles fan
When I was a child,
I spoke as a child,
I thought as a child,
I reasoned as a child.
When I became a man,
I put childish ways behind me"
(the holy bible, Paul, in his first letter to the Cor. 13:11)
I'm confused. Are you using a sound card? Because I've only used beep() to get a sound from the internal PC speaker. Otherwise the WinAPI plays the Windows Default Sound (I think ?)
If you are using a sound card... well, what you're trying to do has to be the easiest thing that a sound card does! OK... I've never looked into how to do it, but I'm sure that there is a way to send a pitch (or frequency) and ADSR (Attack-Delay-Sustain-Release) command to a sound card.
You might try posting a question on the Game Board. Gamers make lots of sounds and they don't want to use CPU/bus cycles to read and decode wave files! Or, search for sample MIDI programs on the web... A piano program sounds like a MIDI application to me... I think they call these things MIDI even if you don't use the actual MIDI hardware interface.
There are probably generic sound-blaster compatable libraries available too.
You may have to make your own table to convert note-letters to frequency.
If you're trying to make sounds from the internal speaker (why?) then you have to write directly to the hardware (PIT / Speaker), or use a different compiler. The internal speaker is very limited... It's connected to a one-bit digital output (on-off). So, it normally outputs square waves and has no volume control. You can vary the pulse width to reduce the volume, but this changes the harmonic structure and is apparently complicated too (CPU intensive and requiring interrupts). Or, you can get really fancy and use pulse-width modulation to simulate sine waves (or complex wave forms). I can't think of any good reason for generating complex sounds through a speaker that's never going to sound as good as the cheapest sound-card-speaker combination.
Yes!!! Sang-drax is RIGHT!!! (He/She usually is)No, not if you're using a real (NT,2k,XP) version of Windows.
Beep(Frequency, Duration)
The paramaters are ignored with Win95, 98 and ME
This is from Microsoft:
Beep
The Beep function generates simple tones on the speaker. The function is synchronous; it does not return control to its caller until the sound finishes.
BOOL Beep(
DWORD dwFreq,
DWORD dwDuration
);
Parameters
dwFreq
[in] Frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).
Windows 95/98/Me: The Beep function ignores this parameter.
dwDuration
[in] Duration of the sound, in milliseconds.
Windows 95/98/Me: The Beep function ignores this parameter.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
Terminal Services: The beep is redirected to the client.
Windows 95/98/Me: On computers with a sound card, the function plays the default sound event. On computers without a sound card, the function plays the standard system beep.
Example Code
For an example, see Registering a Control Handler Function.
Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, Windows 95.
Server: Included in Windows .NET Server 2003, Windows 2000 Server, Windows NT Server.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
I tried using winbase.h's
Beep(100, 200);
like this:
and that gave 94 errrors, so I tried this:Code:#include<winbase.h> #include<windows.h> int main() { Beep(100, 200); return(0); }
Which worked fine. I'm working on something I'll post up when I finishCode:#include<windows.h> #include<winuser.h> int main() { for (int x = 100; x<500; x++) Beep(x, 50); return(0); }
Last edited by Trauts; 01-09-2003 at 05:47 PM.
Hmm... I got home and tried working on my header file on MVC++ 6.0 but with Windows XP, and it doesn't play anything. I then remembered this:
On computers with a sound card, the function plays the default sound event. On computers without a sound card, the function plays the standard system beep.
Is that why it isn't working, or is it not compatible with XP?
quote:
--------------------------------------------------------------------------------
No, not if you're using a real (NT,2k,XP) version of Windows.
--------------------------------------------------------------------------------
Yes!!! Sang-drax is RIGHT!!! (He/She usually is)
Yes, I know it doesn't play on Win98,95,etc. And yes he/she (plz clear this up for us snag!) is usually right but to say that the models besides NT,2k,and Xp aren't real is going a tad bit far.
(Yes, I'm using Win98)