How to Generate/Play Custom Beeps and Sound Tones in VB 6.0 and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (22 votes, average: 3.95 out of 5)
 

Once in awhile you may want to play certain sounds for certain reasons in your applications. You may want more than the generic Beep() that is in Visual Basic and .NET. But if you ONLY want a simple Beep to sound through the speakers, then use the simple built-in Sub that is available with ALL versions of Visual Basic as seen below…

Update: Thanks to Andreas for mentioning that Win95/Win98/WinME will only play the default beep sound using the API call below. Custom Freq/Durations are not supported until Win NT/2000 and above including XP/Vista.

Update Again: Windows Vista 64-bit will only play the default beep with this API code. Probably all 64-bit versions. So it looks like this code has become obsolete. Sorry about that 🙁 


'
'This Sub plays a simple beep sound through the computer. Supported in ALL versions of VB.

Beep()

 

BUT, what if you wanted to make different beeps or tones that could mean different things using certain frequencys and/or different duration combinations? What if you wanted to make a Morse Code application?

The code in this article will show a way to beep in VB 6.0 and Visual Basic.NET and an extra method for Visual Basic 2008/2010, and newer.  The way to do it with ALL versions of Visual Basic it to use a simple little Microsoft Windows API call. The API code is below…

 

Visual Basic 6.0 Declaration…

'
'This is the little API call to give us some say in the duration and frequency of the sound...
Private Declare Function Beep Lib"kernel32" (ByVal soundFrequency As Long, ByVal soundDuration As Long) As Long

 

Visual Basic.NET 02/03 and 05/08/2010

'
'This is the little API call to give us some say in the duration and frequency of the sound...
Private Declare Function Beep Lib"kernel32" (ByVal soundFrequency As Int32, ByVal soundDuration As Int32) As Int32

 

The soundFrequency parameter is where you can specify how high or low you want the frequency to be. I looks to have a Minimum supported value of 37hz and Maximum of 32767hz.

The soundDuration parameter is for how long you want the sound to play in milliseconds. IF you set the parameter to 5000, is will keep generating that tone for 5 seconds before it stops. Something to remember too is that this function will consume the calling thread. So if you set a tone for long periods of time your application will have to wait until the tone is finished generating before it will be responsive again if you call it inside your apps thread.

Simple example:

'
'This Function plays a simple beep sound through the computer at a frequency of 1000hz and for 3x seconds.
Beep 1000, 3000

 

You can get the return value of the Function if you want. Especially if it doesn’t appear to be working. IF the Return Value is Zero(0) then the call failed. This could happen if the Frequency you set it to Low and/or to High. Nothing more I can really say…

The next method works with Visual Basic 2008/2010 and higher. Its actually a managed wrapper around the Beep() api function. Its located in the: System.Console Class.

This is the Sub Declaration:

System.Console.Beep(Integer, Integer)

 

You can simply call this Sub the same way you did the API version…

'
'This Sub uses the Beep() Subroutine under the Console Class and plays a simple beep sound through the computer at a frequency of 4000hz and for 1x second.
Console.Beep(4000, 1000)

That’s all there is to it. If you want to make Morse code this code should be perfect for doing that. Hope you got something useful here. 🙂

Jason

Revised: 2015

13 thoughts on “How to Generate/Play Custom Beeps and Sound Tones in VB 6.0 and VB.NET

  1. Andreas Jönsson

    “If I do remember correctly, this API ‘should’ work with Windows 98 and Above… But I could be wrong. ”

    It will only play the default system sound in Win98. It’s only NT-based operative systems that make use of frequency and duration. So if you need that this will only work on NT/2000/XP/2003/Vista.

    Reply
  2. Harith D. Yhya

    how can i control the channels (left or right) and how can i control the amplitude of the beep?!

    using MS visual basic 6.0 or any version of .Net

    Thank you,
    Harith D. Yhya

    Reply
  3. Omer

    Try this on vb2008, it will play random sounds without stopping until you quit the application…
    Dim rnd As New Random
    While (1 = 1)
    Console.Beep(rnd.Next(500, 10000), 500)
    End While

    Reply
  4. Mahmoud

    The API call mentioned and the console.Beep(int,int) only play sounds through the console speaker (the “BIOS speaker”) NOT through the computer speakers or headphones!!
    At least for me : (
    (i have XP SP2)

    Reply
  5. Mihai

    Hi Jason,

    for automatic morse code is fine, but if you want to manually transmit ???
    I mean generate sound when push a key (until isn’t released).
    Probably with Key_Down (generate continuous sound)
    and Key_Up (stop)
    in this case we do not need the second parameter(Time)
    so …
    there is a solution ???

    wonder that the idea of ??using one of the keys like a real Morse transmitter and you can train

    Thanks

    Reply

Leave a Reply

Your email address will not be published.