Sound Tutorial for z80 ASM - Ben Ryves


How does sound work?

Sound is easier than it may first appear! Sound from all electronic sources works in the same way - electronic signals are passed into a loudspeaker which vibrates a cone. This makes the air between it and your ear vibrate, which makes your eardrums vibrate, making a 'sound'. Well, with a calculator sound can be produced in much the same way. All that is needed is a way to pass an electrical signal into a speaker or earphone. This can be achieved using the calculator link-port and an adapter for the earphone. Before you start on any of the 'practical', I suggest you get an adapter. You can use a Nokia-compatible hands-free kit for mono testing, but for the 'real deal' a 2.5mm to 3.5mm stereo jack-plug adapter should be used with a standard pair of earphones.


Converting sounds

For any sound to be produced on your calculator, it first needs to be converted from a standard 'wav' file. You should have found a program called 'wav2db' bundled with this tutorial. This program (written by Andrea Ess) should allow you to convert your wav. Firstly, though it needs to be in the right form for your calculator - in terms of the right bit-rate and channel. To do this, you can use good-ol' sound recorder (yes, that thing that comes with Microsoft Windows). Start it up, and load in a wav file of a few seconds at most. Click File:Properties and click 'Convert Now' in the bottom of the screen that pops up. Select the setting '16kHz, 8bit Mono'. Convert the file, then click File:Save As and save the new file (do not just click File:Save - your precious original will be replaced with a rubbish copy!). Now, copy wav2db into the same folder as your converted wav file. Double-click it, then enter the name of your wav. It should then produce a file called '<WAVFILENAME>.asm' with the name of your original in there (if not, remember that DOS cannot handle files with more than 8 letters in its name - rename your file as you see fit!). Now, if you open that file in notepad, you will see a file like this:

SOUND.WAV:
.dw 2513

.db $FF,$FD,$80,$00,$00,$00,$00,$00,$00,$30,$00,$80,$3F,$3F,$FF,$FE
.db $4F,$BC,$00,$24,$70,$3F,$FF,$FF,$99,$FB,$E4,$00,$00,$32,$00,$00
.db $03,$0F,$FF,$FF,$FF,$FF,$65,$80,$08,$04,$FE,$7F,$E7,$FF,$FF,$80
.db $00,$1C,$08,$00,$00,$00,$7F,$FF,$FF,$A0,$00,$00,$00,$03,$FF,$FF
.db $FF,$FF,$FF,$FB,$FE,$7F,$FE,$00,$00,$00,$00,$0E,$7F,$FF,$FF,$F8
.db $E0,$00,$7F,$FF,$F8,$3F,$FF,$FF,$E0,$7F,$FF,$C0,$00,$00,$00,$63
.db $EF,$FC,$00,$00,$00,$00,$13,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$E0
.db $00,$00,$00,$00,$00,$00,$00,$D0,$00,$80,$FF,$FF,$FF,$FF,$FF,$FF
.db $F8,$04,$01,$F0,$00,$00,$00,$00,$03,$7F,$DE,$00,$00,$00,$01,$FF
.db $FF,$FF,$FF,$FF,$FE,$FF,$FF,$FC,$00,$00,$00 ...

...and so on. There will be quite a lot of it! This is what it means: the first statement (.dw xxxx) is how many bytes of data there is in the wav file. The rest of the data is the actual sound. How, then, is the sound held as numbers? Ah. Well, here is what has happened. To produce the sound, the program will have to make the link port pulse high and low to pass the sound 'vibrations' to the speaker. What has happened is that the wav2db program has converted the sound into hex numbers - instead of binary (it doesn't matter, though). You know that FF = 255 = 11111111. Well, that means that the speaker is held in one position for 8 'cycles'. If the binary was 01010101 it would produce a high tone as the sound is fed out to the speaker- it goes high-low 4 times in the 8 cycle period.

Do not worry if none of that means anything to you - I have kindly produced a file that helps you!


My funky sound routine

You should have a file in the zip called 'sound.inc'. Start a blank ASM program, and stick on the last line '#include "sound.inc"', making sure that sound.inc is in the same folder as your source file. Convert a sound file using wav2db, copy in the data produced. Now, to play the sound, all you need to know is which channel you want to play the sound from, and the 'name' of your wav file in the code. Look in the file 'sample.z80' for help.