How to make them computer beep?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Guest

How to make them computer beep?

Post by Guest »

Is there a way to make the computer beep without writing a fully fledged sound driver (like write to some port or something) ? Help is appreciated...
Also... I found the realmode way to do it with int 21h... I need a way that works without BIOS interrupts..

Thanks in advance. ;)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:How to make them computer beep?

Post by Candy »

Guest wrote: Is there a way to make the computer beep without writing a fully fledged sound driver (like write to some port or something) ? Help is appreciated...
Also... I found the realmode way to do it with int 21h... I need a way that works without BIOS interrupts..

Thanks in advance. ;)
The system speaker is connected to the PIT. Try searching the internet for ways to reprogram the PIT and to which port it's connected (don't have any books handy right now).
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:How to make them computer beep?

Post by Brendan »

Hi,

For the PC speaker, there's a 2 bits in I/O port 0x61:
  • Bit 0: Connect speaker to PIT channel 2 if set
    Bit 1: Speaker state
By clearing bit 0, you can control the PC speaker directly by setting and clearing bit 1. This allows for very advanced effects (e.g. pulse width modulated polyphonic sounds), but consumes a huge amount of CPU time and it isn't easy to get good results. I've heard games get surprising sound effects and tunes using this approach, but that was a long time ago...

By programming PIT channel 2 and then setting bit 0, you can produce monotonic sounds (a beep). You can initialize PIT channel 2 once during boot and then set/clear bit 0 set get the same sound every time, or you could reprogram the PIT channel each time to allow for different frequency sounds.

For information on programming the PIT, see:
http://www.osdev.org/osfaq2/index.php/PIT

Unfortunately, some computers don't have a PC speaker at all, some (most desktops) use small cheap speakers and some use smaller cheaper peizo's (which have problems generating low frequency sounds) - there's no way to detect what hardware's present or predict what it will sound like.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:How to make them computer beep?

Post by bubach »

Code: Select all

;----------------------------------;
; PC-speaker; beep                 ;
;----------------------------------;
beep:
     push     eax

     in     al, 0x61               ; turn the speaker on.
     or     al, 3
     out     0x61, al

     ;; delay for 150ms or something here...

     in     al, 0x61
     and     al, 0xFC               ; some do 0xFD, some 0xFC... :/
     out     0x61, al

     pop     ecx
     ret
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Post Reply