Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  The timer controls the speaker by sending periodic signals that pulse the
  speaker in and out. You can do the same thing with a program that sends in
  or out signals directly to the speaker. Do this by setting bit 0 of port
  61H (decimal 97) to 0 to turn the speaker off and then alternately setting
  bit 1 on and off to pulse the speaker. When you use this method, the speed
  of the program determines the frequency of the sound; the faster the
  program executes, the higher the pitch. The following BASIC program is an
  example of this method:

  10 X = INP (&H61) AND &HFC   ' read port value, turn off bits 1 and 0
  20 OUT &H61, X               ' pull speaker in
  30 OUT &H61, X OR 2          ' push speaker out
  40 GOTO 20

  The actions in lines 20 and 30 pulse the speaker in and out. Each one is a
  half-cycle, and the two together produce one complete sound cycle.

  This example runs as fast as BASIC can process it, producing as high a
  note as possible. If you needed more range in your application, you could
  use a faster language and insert deliberate delays equal to half the
  frequency cycle time between each complete cycle (half the cycle time,
  because each ON or OFF operation is a half-cycle). No matter what language
  you use, you must include a duration count to end the sound. To produce
  different sounds at a particular frequency, such as clicking or buzzing
  sounds, just vary the delays between pulses.

  Despite all these wonderful possibilities, generating sounds through the
  speaker by direct program action is not a good way to make sounds. It has
  three big disadvantages compared to the use of the timer:

  .  A program requires the constant attention of the CPU, so the computer
     has a hard time getting any other work done.

  .  The frequency is at the mercy of the speed of the computer; that is,
     the same program would make a lower or higher sound on a slower or
     faster model.

  .  The clock-tick interrupts interfere with the smoothness of the sound,
     making a warble. The only way to avoid this is to suspend the clock
     tick by disabling the interrupts--and that disrupts the computer's
     sense of time.

  As far as we know, there is only one advantage to making sounds using the
  direct method: With the proper control over the program delays, the direct
  method lets you make a low-fidelity polyphonic sound. Be forewarned,
  though, that this requires some very clever and tedious programming and,
  all in all, may not be worth the trouble.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson