Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C#
Tip/Trick

Make Buzzer (Internal Speaker) Sound in Windows 7/8/10 32bit / 64bit

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
14 Jan 2022CPOL2 min read 94.6K   27   38
In our company, we find ourselves in need of using the internal PC (buzzer) on Windows7 64bit. At the end, we figured out how to do it.

Introduction

In our company, we find ourselves in need of using the internal PC speaker (buzzer) on Windows7 64bit.
Googling around searching for the problem, we found that this problem is fully explained here.

There is no trace of an easy solution, at least searching for something like "Windows 7 64bit buzzer" or something like this.

So we started from searching on how the buzzer is implemented and we found this very useful article:

I report here the most interesting part:

  1. Send the value 182 to port 43h. This sets up the speaker.

  2. Send the frequency number to port 42h. Since this is an 8-bit port, you must use two <tt class="LITERAL">OUT</tt> instructions to do this. Send the least significant byte first, then the most significant byte.

  3. To start the beep, bits 1 and 0 of port 61h must be set to 1. Since the other bits of port 61h have other uses, they must not be modified. Therefore, you must use an <tt class="LITERAL">IN</tt> instruction first to get the value from the port, then do an OR to set the two bits, then use an <tt class="LITERAL">OUT</tt> instruction to send the new value to the port.

  4. Pause for the duration of the beep.

  5. Turn off the beep by resetting bits 1 and 0 of port 61h to 0. Remember that since the other bits of this port must not be modified, you must read the value, set just bits 1 and 0 to 0, then output the new value.

So with this idea in mind, and as we didn't want to write an assembler piece of code, we looked for a library that let us access the hardware ports and we find this very useful library:

As said from the authors, "InpOut32" is an open source windows DLL and Driver to give direct access to hardware ports".

Using the Code

Once we have downloaded the driver, we have to:

  1. Install the driver from highrez, there is a 32bit or 64bit different installer depending on your PC configuration.
  2. In CLI environment, we can include the .h file, we link the .lib library and use the given DLL (there are two directories binaries\Win32 and binaries\x64 depending on your application configuration).
    First, we test if the Input-Output Driver is open, then:
    C++
    #include "inpout32.h"
    
    ...
    
        /**
         * method to make the motherboard internal speaker sound a certain freq
         * for a certain period of time
         *
         * @param[in]        freq frquency
         * @param[in]        ms sound duration
         */
        void MyClass::Beep(unsigned int freq, int ms)
        {
            Out32(0x43, 0xB6);
            int div = 0x1234dc / frequency;
            Out32(0x42, (System::Byte)(div & 0xFF));
            Out32(0x42, (System::Byte)(div >> 8));
            System::Threading::Thread::Sleep(10);
            Out32(0x61, (System::Byte)(System::Convert::ToByte(Inp32(0x61)) | 0x03));
            System::Threading::Thread::Sleep(ms);
            StopBeep();
        }
        
        /**
         * Method to stop the sound from the buzzer
         */
        void MyClass::StopBeep()
        {
            Out32(0x61, (System::Byte)(System::Convert::ToByte(Inp32(0x61)) & 0xFC));
        }
    
    ...    

    On the other hand, we can DLLImport (https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute%28v=vs.90%29.aspx) the DLL without having to link the library and use the .h file.

    C++
    ...
    [DllImport("inpout32.dll")]
    extern void Out32(short PortAddress, short Data);
    [DllImport("inpout32.dll")]
    extern char Inp32(short PortAddress);
    ...    

    In case of C#, use...

    C#
    ....    
    [DllImport("inpout32.dll")]
    extern static void Out32(short PortAddress, short Data);
    [DllImport("inpout32.dll")]
    extern static char Inp32(short PortAddress);
    ...
        /**
         * method to make the motherboard internal speaker sound a certain freq
         * for a certain period of time
         *
         * @param[in]        freq frequency
         * @param[in]        ms sound duration
         */
        public void Beep(uint freq, int ms)
        {
            Out32(0x43, 0xB6);
            int div = 0x1234dc / frequency;
            Out32(0x42, (Byte)(div & 0xFF));
            Out32(0x42, (Byte)(div >> 8));
            System.Threading.Thread.Sleep(10);
            Out32(0x61, (Byte)(System.Convert.ToByte(Inp32(0x61)) | 0x03));
            System.Threading.Thread.Sleep(ms);
            StopBeep();
        }
        
        /**
         * Method to call the stop of the buzzer!!
         */
        public void StopBeep()
        {
            Out32(0x61, (Byte)(System.Convert.ToByte(Inp32(0x61)) & 0xFC));
        }    
    ...

Points of Interest

Remember to always stop the buzzer!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader ATS med
Italy Italy
I started programming at Teinos, in a 5 person software working team in march 2004. I got some experience interfacing our main program to various external applications. All of these experiences allowed me to get in touch with many people: end-user, technician and commercial one; learning how to interface myself with these people. After some period I was moved to 'single' software application development. This led me to learn the whole process of software development. From the definition of the specifications to the software release and many times also the installation of the product to the end-user.

In 2009, I moved to ATS. I was charged as Lead Software Developer in a team for a long term new project: Primo (released for the first time in 2010-Q3). The software part for this project was completely written from scratch. These experience led me to understand how to organize the work of a team and take critical decision.

In 2014, in collaboration with a multinational corporation, we started the development of a completely new machine: ARCO FP (released for the first time in 2015-Q3). The software part for this project was completely written from scratch. This experience teaches me how to integrate our company with the needs and rules of other company and how to mitigate different approaches to the production phases.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA14-Jan-22 20:48
professionalȘtefan-Mihai MOGA14-Jan-22 20:48 
SuggestionIncorrect frequency Pin
Hat Kid7-Oct-21 21:12
Hat Kid7-Oct-21 21:12 
GeneralRe: Incorrect frequency Pin
Andreoli Carlo14-Jan-22 3:50
professionalAndreoli Carlo14-Jan-22 3:50 
QuestionNot working on Windows 10 x64, just me? Pin
Member 143648382-Sep-19 23:29
Member 143648382-Sep-19 23:29 
AnswerRe: Not working on Windows 10 x64, just me? Pin
Andreoli Carlo5-Sep-19 22:56
professionalAndreoli Carlo5-Sep-19 22:56 
GeneralRe: Not working on Windows 10 x64, just me? Pin
Member 1436483812-Sep-19 19:39
Member 1436483812-Sep-19 19:39 
GeneralRe: Not working on Windows 10 x64, just me? Pin
Andreoli Carlo13-Sep-19 4:40
professionalAndreoli Carlo13-Sep-19 4:40 
GeneralRe: Not working on Windows 10 x64, just me? Pin
BenShell10-Feb-22 19:37
BenShell10-Feb-22 19:37 
GeneralRe: Not working on Windows 10 x64, just me? Pin
Andreoli Carlo22-Feb-22 9:04
professionalAndreoli Carlo22-Feb-22 9:04 
GeneralRe: Not working on Windows 10 x64, just me? Pin
Charlie Chang 202223-May-22 15:32
Charlie Chang 202223-May-22 15:32 
GeneralRe: Not working on Windows 10 x64, just me? Pin
Andreoli Carlo24-May-22 21:56
professionalAndreoli Carlo24-May-22 21:56 
QuestionPlaying samples Pin
Hynek Syrovatka10-Jul-17 0:58
Hynek Syrovatka10-Jul-17 0:58 
QuestionLink to University of Illinois returns 404 Pin
saturn_16-Jul-17 15:31
saturn_16-Jul-17 15:31 
AnswerRe: Link to University of Illinois returns 404 Pin
Volker Huschke7-Jul-17 8:05
Volker Huschke7-Jul-17 8:05 
QuestionHave you tried using System.Media? Pin
George Swan5-Jul-17 19:56
mveGeorge Swan5-Jul-17 19:56 
AnswerRe: Have you tried using System.Media? Pin
Andreoli Carlo20-Jul-17 22:40
professionalAndreoli Carlo20-Jul-17 22:40 
QuestionMatching frequencies Pin
Member 1267973420-Dec-16 6:14
Member 1267973420-Dec-16 6:14 
AnswerRe: Matching frequencies Pin
StereoBucket11-Mar-17 13:22
StereoBucket11-Mar-17 13:22 
QuestionCould you supply the full code please? Pin
Member 1275680723-Sep-16 18:37
Member 1275680723-Sep-16 18:37 
AnswerRe: Could you supply the full code please? Pin
Garth J Lancaster23-Sep-16 20:22
professionalGarth J Lancaster23-Sep-16 20:22 
QuestionHow to calculare ferequency Pin
avisal1-Sep-15 10:05
professionalavisal1-Sep-15 10:05 
Hello,working fine without troubled,
How the pitch is defined ? is there any formula or I can enter 440 for 440 hz ?
AnswerRe: How to calculare ferequency Pin
Andreoli Carlo1-Sep-15 23:20
professionalAndreoli Carlo1-Sep-15 23:20 
QuestionThe early versions of IBM ROM BASIC and Quick Basic had the BEEP command. Pin
Unka_Georgr27-Aug-15 13:02
professionalUnka_Georgr27-Aug-15 13:02 
QuestionWhy No Assembler? Pin
David A. Gray27-Aug-15 10:28
David A. Gray27-Aug-15 10:28 
AnswerRe: Why No Assembler? Pin
George I. Birbilis27-Aug-15 12:02
George I. Birbilis27-Aug-15 12:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.