assemblyiotasmspeaker

Playing music notes through speaker in TASM assembly


I'm trying to write a program in TASM that plays music notes. I couldn't find any documentation online and the closest thing I found was this stackoverflow question, which is able to produce a "beep" sound.
What I don't know is:

  1. How this code works

  2. What the sound mapping is

  3. How I can play specific notes (do re mi...)


Solution

  • Playing a single tone with PC speaker may use method mentioned in the quoted question. It uses square-wave generator from system timer, activated by setting mode of operation 0xB6 to I/O port 0x43. See Timer Ports. The value 182=0xB6=0y10110110 written to port 0x43 specifies

    When you have programmed the frequency, ask the Programmable Peripheral Interface to connect speaker to the square-wave generator. This is done by setting the two least significat bits if PPI port 0x61, see PPI Ports.

       IN AL,0x61   ; Get the previous value of PPI port B.
       OR AL,0x03   ; Set the two least significant bits.
       OUT 0x61,AL  ; Write it back to PPI port B.
    

    Now you should wait some time to let the tone play and then reset the two bits back to 0 at PPI port B.

    Mapping of musical tones to frequencies you can find here. You will need a table to convert the frequency of do, re, mi etc tones to 16bit integer numbers which you will put to the system timer as LowByte and HighByte.

    See sample code how to get LowByte and HighByte. PC timer ticks at 1193180 Hz. When you want to play the tone do (note C4=261.63 Hz), calculate 1193180/261.63=4560=0x11D0. LowByte=0xD0 and HighByte=0x11.