Click here to Skip to main content
13,247,448 members (79,824 online)
Click here to Skip to main content
Add your own
alternative version

Stats

24K views
20 bookmarked
Posted 26 Aug 2015

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

, 5 Jul 2017
Rate this:
Please Sign up or sign in to vote.
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:
    #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);
            Out32(0x42, (System::Byte)(freq & 0xFF));
            Out32(0x42, (System::Byte)(freq >> 9));
            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.

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

    In case of C#, use...

    ....    
    [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);
            Out32(0x42, (Byte)(freq & 0xFF));
            Out32(0x42, (Byte)(freq >> 9));
            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)

Share

About the Author

Andreoli Carlo
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.

You may also be interested in...

Comments and Discussions

 
QuestionPlaying samples Pin
Hynek Syrovatka10-Jul-17 1:58
memberHynek Syrovatka10-Jul-17 1:58 
QuestionLink to University of Illinois returns 404 Pin
saturn_16-Jul-17 16:31
membersaturn_16-Jul-17 16:31 
AnswerRe: Link to University of Illinois returns 404 Pin
Volker Huschke7-Jul-17 9:05
memberVolker Huschke7-Jul-17 9:05 
QuestionHave you tried using System.Media? Pin
George Swan5-Jul-17 20:56
memberGeorge Swan5-Jul-17 20:56 
AnswerRe: Have you tried using System.Media? Pin
Andreoli Carlo20-Jul-17 23:40
professionalAndreoli Carlo20-Jul-17 23:40 
QuestionMatching frequencies Pin
Member 1267973420-Dec-16 7:14
memberMember 1267973420-Dec-16 7:14 
AnswerRe: Matching frequencies Pin
StereoBucket11-Mar-17 14:22
memberStereoBucket11-Mar-17 14:22 
QuestionCould you supply the full code please? Pin
Member 1275680723-Sep-16 19:37
memberMember 1275680723-Sep-16 19:37 
AnswerRe: Could you supply the full code please? Pin
Garth J Lancaster23-Sep-16 21:22
professionalGarth J Lancaster23-Sep-16 21:22 
QuestionHow to calculare ferequency Pin
avisal1-Sep-15 11:05
memberavisal1-Sep-15 11:05 
AnswerRe: How to calculare ferequency Pin
Andreoli Carlo2-Sep-15 0:20
professionalAndreoli Carlo2-Sep-15 0:20 
QuestionThe early versions of IBM ROM BASIC and Quick Basic had the BEEP command. Pin
Member 1059181627-Aug-15 14:02
memberMember 1059181627-Aug-15 14:02 
QuestionWhy No Assembler? Pin
David A. Gray27-Aug-15 11:28
memberDavid A. Gray27-Aug-15 11:28 
AnswerRe: Why No Assembler? Pin
George I. Birbilis27-Aug-15 13:02
memberGeorge I. Birbilis27-Aug-15 13:02 
GeneralRe: Why No Assembler? Pin
Andreoli Carlo27-Aug-15 21:18
professionalAndreoli Carlo27-Aug-15 21:18 
GeneralMy vote of 5 Pin
David A. Gray27-Aug-15 11:25
memberDavid A. Gray27-Aug-15 11:25 
QuestionQuestion Pin
PIEBALDconsult26-Aug-15 5:56
protectorPIEBALDconsult26-Aug-15 5:56 
AnswerRe: Question Pin
Andreoli Carlo26-Aug-15 8:04
professionalAndreoli Carlo26-Aug-15 8:04 
GeneralRe: Question Pin
PIEBALDconsult26-Aug-15 8:22
protectorPIEBALDconsult26-Aug-15 8:22 
GeneralRe: Question Pin
Andreoli Carlo26-Aug-15 8:52
professionalAndreoli Carlo26-Aug-15 8:52 
GeneralRe: Question Pin
PIEBALDconsult26-Aug-15 9:08
protectorPIEBALDconsult26-Aug-15 9:08 
GeneralRe: Question Pin
Andreoli Carlo26-Aug-15 9:39
professionalAndreoli Carlo26-Aug-15 9:39 
GeneralRe: Question Pin
Tomaž Štih27-Aug-15 0:05
memberTomaž Štih27-Aug-15 0:05 
GeneralRe: Question Pin
the.Kris27-Aug-15 3:22
memberthe.Kris27-Aug-15 3:22 
GeneralRe: Question Pin
George I. Birbilis27-Aug-15 13:04
memberGeorge I. Birbilis27-Aug-15 13:04 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 5 Jul 2017
Article Copyright 2015 by Andreoli Carlo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid