Library code snippets

Make the PC beep

It would appear at first that VB's standard Beep function would do just this. However, if a sound card is installed, even if the speakers are turned off, they will be used instead. To force the PC's internal speaker to sound, you need to use the Windows API beep function. Using this, you can even set the pitch and length of the beep.

Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long

To sound the beeper, simply use

Beep 500, 1000

The first value sets the frequency (or pitch). I find my PC's beeper performs best in the 500-3000 range. The second value is the duration of the beep in milliseconds (so 1000 is one second).

Comments

  1. 15 Jan 2007 at 20:05

    Hi my names chris barbour <bobjamin> and im new on this site but im far from new at programming ive programmed for about 6 or 7 years now and im 17 years old. I started with visual basic then moved to c++ and now i use C#, C# is a mixture between the two languages, it has the power of c++ and the simpleness of vb. about a month ago i realised you could use the beep function in vb to send a frequency and duration to the internal beeper and so naturally i played about with that. in C# the syntax is simple(Console.Beep(frequency,duration))and there is also a sleep function (System.threading.thread.Sleep(duration)), i used these to create a piano machine, it basically displays a piano onscreen with inputs for tempo,length of beat,file name and file path. every time you press a button it plays the corresponding beeps frequency.If the record checkbox is pressed then it writes to a file every notes length,frequency,starting position and ending position.Then if you click on left hand it alows you to write the left hand notes.Now when you press play it reads each file seperately and because you cant play two notes together it looks for the notes the cut each other at a point and it seperates them so it sounds like two notes are being played. This only really works for fast songs like fantasy impromptu which i think i put with the zip file(which i cant send here for some reason, if you want it email me at bobjamin@aol.com ill send you it free) because when long notes are played can then another note comes in it just stops. For that reason im trying to work with midis<if anyone can help please do,ive been trying to use the winmm.dll library but ive had no luck sounding anything> If i cant get that to work then im goin to work on a program that allows you to use the internal speaker like a normal speaker by sending 1 millisecond pulses at the right frequency to act like two simultaneos waves coming together. I can convert from visual basic easily so if you could send me anything that would help me get the midi situation sorted out then please send me it.

     thanx!

  2. 25 May 2006 at 13:33

    Hi,

    I have successfully get the beep sound out. However, how to display a form and then only the beep sound? Currently the beep sound is out before the form is displayed.Where should I put the this line : Beep 500, 9000 ?? If I were to put inside the form, then it will call the sound even before the form is displayed. Please advice.

    Thanks.

     

     

     

  3. 14 Sep 2005 at 02:51

    Is it possible to make VB create two tones at the same time?

  4. 05 Feb 2005 at 13:37

    i am trying get a sound out of the internal pc speaker for 2 days and i am still trying. i have win 98, i consider myself prety advanced in vb language . i searched more on google. i found a driver for pc speaker. it still didn't work . i downloaded a program that said that it makes the internal speaker beep, but no, it made the same visual basic beep. whatever frequency or how many miliseconds i set for the beep its still makes the vb beep through my headphones.
    can someone pls answer these problems?

  5. 29 Apr 2004 at 13:21

    I coded a simple program that makes different tones, the programs runs fine on users with windows xp, but it gives the same tone all the time with ppl running Windows 98, what can I do to give em all the tones

  6. 06 Oct 2003 at 22:23

    Are there values to control the interval between beeps and to control the volume of the beep?
    Thanks,
    David.

  7. 18 Jul 2003 at 21:08
    As it turns out out this is a widely known problem once you know what to look for.  It is the result of uninstalling Norton Anti-Virus; it deletes "Default Beep" for the current user.  A variety of solutions are apparently available; I chose what appeared to be the most idiot-proof:

    1) Create a new user (assuming you have admin auth)
    2) As new user run regedit.
    3) Expand HKEY_CURRENT_USER
    4) Right click on HKEY_CURRENT_USER\AppEvents
    5) Export as somefilename.reg
    6) Log off new user and back on as original user
    7) Double click somefilename.reg
    8) Answer 'yes" to the prompt.

    That's it; no re-boot or anything.
  8. 18 Jul 2003 at 12:10
    Thanks anyway.  I'm also relatively new to VB.  I think that this is a ".Net" issue and I'm sure your code works with VB6.  All the "instring" stuff you note is generated by VB.Net; this is, as you suggest, just a form with a button that is supposed to beep.  Thanks again.
  9. 18 Jul 2003 at 11:29
    I know just enough of VB to be dangerous.  I am not a software engineer by any means.  I can tell you this code runs on windows NT, 98, and XP boxes just fine (at least the way i did it).  Also, I wrote it using VB 6.  

    i would suggest opening a new .exe and just putting the command button in with just my source.  i am not at all familiar with your instring declarations.

    sorry i couldn't help more.

  10. 18 Jul 2003 at 10:58
    The recommended "kernel32" function didn't work for me (source below).  Notes: VB.Net, Visual Studio, XP Pro.  Every hardware and software sound setting is PEGGED and unmuted (in case you were thinking of asking).

    ???s
    * The beep() doc goes on and on about UIPermissions; any advice/thoughts here?  If this were the issue I would think it would throw a runtime error, which it doesn't.  Try/Catch caught nothing.
    * Numerous of the "sound profile" settings in XP's setup are set to nothing.  Does beep() use one of these (which)?

    Any help appreciated.
    =PDG=
    ---
    Public Class Form1
    Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "    

    Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long

    Private Sub btnBeep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBeep.Click
           Beep(500, 1000)
           MsgBox("Did the beep thing")
    End Sub

    End Class
    ---
  11. 13 Jun 2003 at 09:10

    I've made it a hobby of mine making up my own english...
    Sowwy d00d

  12. 13 Jun 2003 at 08:06

    huh?  is this english?

  13. 12 Jun 2003 at 11:44

    Tha a Lot Palley!

  14. 12 Jun 2003 at 11:20

    hey dzjowk,
    i had the same problem.  i figured it out.  simply place the declare function .... line in your program declarations.  it is not declared as a separate funciton.  here's  my code:


    Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long


    Private Sub Command1_Click()


    Beep 500, 100


    End Sub

  15. 07 Feb 2003 at 06:52

    Hi,
    I'm merely a N00b in VB-programming...
    I tried docmd.beep for beeping on errors and such.
    But as you said, this only works when there's a soundcard installed ánd speakers attached.


    I tried to use your beep code, but I just can't seem to get it right.


    declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
    beep 500 1000


    could U specify what comes where ?
    (inside function, new function... ?)


    Tha in Adva

  16. 01 Jan 1999 at 00:00

    This thread is for discussions of Make the PC beep.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!