Does the PC Speaker PWM trick work on emulators?

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
xeyes
Member
Member
Posts: 212
Joined: Mon Dec 07, 2020 8:09 am

Does the PC Speaker PWM trick work on emulators?

Post by xeyes »

From wiki:
Playing the Audio

When the PIT interrupts you you need to program PIT timer 2 so that it puts the PC Speaker in the "out" position for a fraction of 60 microseconds. For 8 bit audio the number of microseconds can be calculated like this:

microseconds = (sample * 60) / 255;

For more information see the Wikipedia article Pulse-width modulation. There is also sample code posted in the forum here.
Wasn't able to get this to work in Qemu, maybe the following assumption made by the wiki page doesn't apply to emulators?
The PC Speaker takes approximately 60 millionths of a second to change positions.
Based on my "research", without this kind of trick, it will be extremely dfficult to play a simple wav file through the eumlated pc speaker.



Could there be another short cut that I somehow overlooked?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Does the PC Speaker PWM trick work on emulators?

Post by Octocontrabass »

PC speaker PWM is pretty much nonexistent outside of 30-year-old games, so you might have better luck with an emulator that can run 30-year-old games like DOSBox or MAME.
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: Does the PC Speaker PWM trick work on emulators?

Post by devc1 »

My PC has it aswell as QEMU, I tried it on both so I think there is a problem with your code.

Devc1,
xeyes
Member
Member
Posts: 212
Joined: Mon Dec 07, 2020 8:09 am

Re: Does the PC Speaker PWM trick work on emulators?

Post by xeyes »

Octocontrabass wrote:PC speaker PWM is pretty much nonexistent outside of 30-year-old games, so you might have better luck with an emulator that can run 30-year-old games like DOSBox or MAME.
Yeah this trick probably belongs to 30 years ago in terms of practical usage. Yet it is so ingenious that I somehow can't resist the temptation to re-implement it. Yet I don't have access to a box with a real pc speaker in it now :(

Good point about the game emulators, I never knew that dosbox is also a system emulator.
Last edited by xeyes on Mon Sep 05, 2022 3:23 pm, edited 1 time in total.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Does the PC Speaker PWM trick work on emulators?

Post by Gigasoft »

I remember my 3 year old self being impressed by Digger's PWM soundtrack for its novelty, but PC speakers are not known for their high fidelity, so this will generally sound awful. The traditional speakers that were used in very old PCs would do okay but the piezo ones are probably unsuitable due to high distortion and poor frequency response. I once hacked a Java toy robot to do this with its tiny sad beeper and it was pretty much impossible to make out what the words were.
xeyes
Member
Member
Posts: 212
Joined: Mon Dec 07, 2020 8:09 am

Re: Does the PC Speaker PWM trick work on emulators?

Post by xeyes »

After patching dosbox and playing with various options on my side, the PWM trick worked :D

I'd say that the playing back is over 90% intelligible and the sound quality, or the richness of the sound, is way above what a simple frequency generator is supposed to be able achieve.

Dosbox turns out to be a capable and fast emulator as well. The ability to precisely control the emulated clock rate also helps with timing. As long as you don't use certain features such as PAE, and be careful about how you handle pagefaults, it works well without much patching.

There is also a fork named Dosbox-x that has a lot more features, including TTF fonts and IDE emulation, but seems to be slightly more buggy.
Gigasoft wrote:I remember my 3 year old self being impressed by Digger's PWM soundtrack for its novelty, but PC speakers are not known for their high fidelity, so this will generally sound awful. The traditional speakers that were used in very old PCs would do okay but the piezo ones are probably unsuitable due to high distortion and poor frequency response. I once hacked a Java toy robot to do this with its tiny sad beeper and it was pretty much impossible to make out what the words were.
Call me as impressed as a 3 year old :lol:

Of course, rather than hifi, this is more like lofi. But it's certainly fun to see (hear) the awesome result of the creativity that people had when working with such limited hardware back then, lively.
Post Reply