Page 1 of 1

How to make a Beeping Program plays a simple beep, with a customisable Frequency and Length. Rate Topic: ****- 2 Votes

#1 krismania  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 148
  • Joined: 03-January 10

Posted 16 October 2010 - 08:54 AM

Hi all, and Welcome to my second Tutorial ;)

I will show you how to make a simple beeping application. you can customize the Frequency and Length of the beep to suit your needs and then play it with a simple line of code.

Start a new project (or add to an existing one) and just make a simple form with a button on it. I will leave the default name of Button1 on this button. I made the form look a little better but that's not necessary.

Posted Image

Now we are going to go to Code View. First, we need to have Imports System.Runtime.InteropServices right up the top there, above where it says Public Class Form1.
Next we have this little chunk of code here:
    <DllImport("KERNEL32.DLL", EntryPoint:="Beep", SetLastError:=True, _
CharSet:=CharSet.Unicode, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function _
       aBeep(ByVal dwFreq As Integer, ByVal dwDuration As Integer) _
         As Boolean
    End Function

This imports KERNEL32.DLL (obviously) and sets up the function that makes the beep based on what's in the file. This all goes underneath Public Class Form1 so that your code should look like this:
Imports System.Runtime.InteropServices

Public Class Form1

    <DllImport("KERNEL32.DLL", EntryPoint:="Beep", SetLastError:=True, _
CharSet:=CharSet.Unicode, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function _
       aBeep(ByVal dwFreq As Integer, ByVal dwDuration As Integer) _
         As Boolean
    End Function
End Class


Now go back into the Form Designer and Double Click on the Button you made, which will bring you back to the code window with a new button_Clicked Sub. In that sub, write aBeep(1000, 1000) and Debug your app. Click the button and enjoy the sound of Progress :)
*Beware: progress can be loud so turn the volume down!*
You should hear a plain and simple beep. Now to customise it.

You may have noticed that aBeep(1000, 1000) has two integer values. The first one is used for Frequency and the Second for Length. Length is measured in Milliseconds, meaning the current beep lasts for one second. Frequency is measured in Hz, so the noise you hear is at 1kHz (1000Hz) you can very easily change these numbers to change the sound. You can also use variables to make it a bit customisable for users too. For Example, I made a simple program that allows you to change the length/frequency on the spot.

Posted Image

I simply replaced the numbers in the aBeep function with my own variables.

*BEWARE* Windows will NOT stop you from setting the frequency too high or low. PLEASE be careful when messing around with the frequency of the beep because setting it to high or low could possible blow your speakers or even cause damage to your ears. Also, when playing the beep, the application will not move at all, so be careful when setting longer lengths.

I hope that this tutorial could help you ;)
-Krismania


Is This A Good Question/Topic? 4
  • +

Replies To: How to make a Beeping Program

#2 SerokSimko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-January 11

Posted 19 January 2011 - 06:38 AM

Nice Tutorial, Thank you it was useful. :^:
Was This Post Helpful? 0
  • +
  • -

#3 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Posted 23 January 2011 - 02:49 PM

thanks , stuck it on a media player im making in college :) as a sound check
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1