Simple PC speaker .WAV player source

Programming, for all ages and all languages.
Post Reply
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Simple PC speaker .WAV player source

Post by inflater »

Hey, just finished this gizmo to my OS this afternoon :) You can add it to your OS or modify it freely.

First, you need your OS (I assume 32bit, but it may work on 16-bit too) to have IDT functional, because you need to work with the timer interrupt. You also need to know the .WAV file size and it's sampling rate, and it must be loaded at ESI. Before continuing, unmask the IRQ0 if you have it disabled. You also need a function to change the PIT's IRQ0 frequency (if you have PIT running on e.g. 100hz, you already have one). Also, the WAV file must be uncompressed PCM!

Declare these variables as global:

Code: Select all

WAVSamplingRate dw 0
WAVFileSize         dd 0
EnableDigitized     db 0
WAVSamplingRate can be anything from 5000 to 22000 (Hz). Make it right as the wave sound has.

Insert this into your IRQ 0 routine:

Code: Select all

irq0_int:
...
	cmp [EnableDigitized],1                  ;If it's set to 1, process next lines of code
	jne NoDigitizedSound                     ;If not, do the standard irq0 routine
	
	cmp al,0x80                                 ;If the byte taken from the memory is less than 80h,
                                                         ;turn off the speaker to prevent "unwanted" sounds,
	jb TurnOffBeeper                          ;like: ASCII strings (e.g. "WAVEfmt" signature etc).
	mov bx,[WAVSamplingRate]              ;Turn on the speaker with the WAV's sampling rate.
	call Sound_On
	jmp Sound_Done
TurnOffBeeper:
	call Sound_Off
Sound_Done:
	inc esi                                         ;Increment ESI to load the next byte
NoDigitizedSound:	
	...
;;;; BE SURE TO SEND THE EOI SIGNAL TO THE PIC AT THE END OF THE IRQ!! ;;;;;
;;;; Here's how it can be done: ;;;;;
	...
	mov al,0x20
	out 0x20,al
	iret
Then, you can create a new ASM include file for the player itself (if you want to make it look more clean) and put in it this:

Code: Select all

;; PC speaker wave player PCSPEAKR.ASM

Sound_On:                                     ; A routine to make sounds with BX = frequency in Hz
	mov ax,0x34dd                        ; The sound lasts until NoSound is called
	mov dx,0x0012                
	cmp dx,bx                
	jnc Done1               
	div bx                
	mov bx,ax
	in al,0x61
	test al,3
	jnz A99                
	or al,3                                 ;Turn on the speaker itself
	out 0x61,al                
	mov al,0xb6
	out 0x43,al
A99:    
	mov al,bl 
	out 0x42,al              
	mov al,bh
	out 0x42,al
Done1:
	ret

Sound_Off:
	in al,0x61                 
	and al,11111100b                               ;Turn off the speaker
	out 0x61,al
	ret

PlayWAV:
	mov [WAVSamplingRate],;;;;;;;;;;;Your sampling rate;;;;;;;;;;;;
	mov [WAVFileSize],;;;;;;;;;;;; Size of your WAV file ;;;;;;;;;;;;;
	mov esi,FileLoaded ;;;;;;;;;;;;;; ESI = offset where the file is ;;;;;;;;;;;;;
	mov cx,[WAVSamplingRate]                    ;IRQ0 fires e.g. 6000 times a second
	call ProgramPIT                                  ;when a 6000Hz WAV file is played. This is how
                                                              ;the speaker can play the digitized sound:
                                                              ;it turns on and off very fast with the specified
                                                              ;wave sample rate.
	mov ecx,[WAVFileSize]                         ;Sets the loop point
	mov [EnableDigitized],1                       ;Tells the irq0 handler to process the routines
Play_Repeat:
	lodsb                                              ;Loads a byte from ESI to AL
	hlt                                                 ;Wait for IRQ to fire
	loop Play_Repeat                              ;and the whole procedure is looped ECX times
	mov [EnableDigitized],0                     ;Tells the irq0 handler to disable the digitized functions
	mov cx,0x12                                 
	call ProgramPIT                                       ;Return to 18.2 kHz IRQ0
	call Sound_Off                                        ;Turn the speaker off just in case
	ret
And there you go. :) It's far away to quality of a normal sound card, but it works. ;)

-EDIT: Corrections made here and there :)
-EDIT 2: Added comments.
-EDIT 3: Fixed the bracket opcodes. (19:41 CET)

Regards
inflater
Last edited by inflater on Mon Jun 23, 2008 11:40 am, edited 3 times in total.
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Re: Simple PC speaker .WAV player source

Post by lukem95 »

that's really cool. wiki it? :D

i may port it to my OS as a driver sometime, it looks pretty neat :)
~ Lukem95 [ Cake ]
Release: 0.08b
Image
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Simple PC speaker .WAV player source

Post by Combuster »

Copyrighted, can't do. Besides, a link to PWM is IMO more interesting than some blackbox code :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: Simple PC speaker .WAV player source

Post by inflater »

Copyright line removed,sorry about that. Public domain now.
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: Simple PC speaker .WAV player source

Post by inflater »

Added comments. If you still have questions regarding the code, don't hestitate to ask! :)

//EDIT: Added two sample WAV files: one is the original 7.333kHz "siren" sound, the other one is how it sounds on a computer with a tiny little piezoelectric PC speaker... :) Of course, it wasn't the best "sound choice" to demonstrate on, but ... at least it works. :)

To achieve the best results, use a bigger PC speaker (you can find one in old german laptops, e.g. Siemens nixdorf had big and loud ones) or instead of the piezoelectric one, solder it out of the mainboard and solder in e.g. a headphone or something. :)

About the Siemens Nixdorf laptop types, I had a PCD-4ND, which had a soundcard (ESS or something). The funny thing was, that the sound was coming from a speaker that was hardly 2 cm in diameter, and the speaker had three functions:

1. sound card output
2. microphone (!)
3. PC speaker /beeping noises etc/

It's a pity that I didn't had the opportunity to test the "7-bit digitized sound" on it... :(

//EDIT 2:
IMPORTANT NOTICE FOR FASM USERS
(For those who compiled it already in FASM)
To improve the sound quality, add brackets to these opcodes:

Code: Select all

...
mov cx,WAVSamplingRate 
mov ecx,WAVFileSize
...
mov bx,WAVSamplingRate 
...
so they'll look like this:

Code: Select all

mov cx,[WAVSamplingRate]
mov ecx,[WAVFileSize]
...
mov bx,[WAVSamplingRate]
This is now fixed above. My fault. :oops:
Attachments
alarm.zip
alarm_original.wav - original "siren" sound, 7.333kHz, PCM.

alarm_piezo.wav - recorded "siren" sound in front of a PC case
(32.76 KiB) Downloaded 452 times
Last edited by inflater on Mon Jun 23, 2008 11:38 am, edited 1 time in total.
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
z180
Member
Member
Posts: 32
Joined: Tue Mar 04, 2008 12:32 pm

Re: Simple PC speaker .WAV player source

Post by z180 »

It is even possible to play midi through the PC speaker,but not with many voices.I thought max. 4 notes at same time.
I thought it would be nice to have a startup melody that runs on all computers.
1988 PCs had bigger speakers,but mostly no sound card.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: Simple PC speaker .WAV player source

Post by inflater »

z180 wrote:I thought it would be nice to have a startup melody that runs on all computers.
Sure. :)

http://www.oldskool.org/sound/pc/sound/ ... ar_loop.au

But this'll add up ~70 kB to your OS kernel when converted to 6000Hz 8bit :shock: - unless you compress the executable.
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
Stevo14
Member
Member
Posts: 179
Joined: Fri Mar 07, 2008 3:40 am
Location: Arad, Romania

Re: Simple PC speaker .WAV player source

Post by Stevo14 »

inflater wrote:But this'll add up ~70 kB to your OS kernel when converted to 6000Hz 8bit :shock: - unless you compress the executable.
If you have a file system in your OS this is a non-issue. Just load the sound as a file and free it when you are done.

Also, I linked to this topic from the PC Speaker article on the wiki. I assumed that it would be fine after you placed the code in the public domain.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Simple PC speaker .WAV player source

Post by Combuster »

If you want to do midi the classic style, go arpeggio :)

for x ms, play note 0
for x ms, play note 1
for x ms, play note 2
for x ms, play note 3
for x ms, play note 0
for x ms, play note 1
you should get the idea by now :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
z180
Member
Member
Posts: 32
Joined: Tue Mar 04, 2008 12:32 pm

Re: Simple PC speaker .WAV player source

Post by z180 »

Combuster explained it better then me and had the right word.
Via MIDI it would be only a small memory size increase if your OS loads the full sound from disk to RAM.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Simple PC speaker .WAV player source

Post by Combuster »

Actually, I thought you were planning on mixing (adding) the midi waveforms, which obviously is an option too :)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply