Page 1 of 1
Does the PC Speaker PWM trick work on emulators?
Posted: Fri Sep 02, 2022 3:34 pm
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?
Re: Does the PC Speaker PWM trick work on emulators?
Posted: Fri Sep 02, 2022 4:41 pm
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.
Re: Does the PC Speaker PWM trick work on emulators?
Posted: Fri Sep 02, 2022 4:45 pm
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,
Re: Does the PC Speaker PWM trick work on emulators?
Posted: Fri Sep 02, 2022 5:34 pm
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.
Re: Does the PC Speaker PWM trick work on emulators?
Posted: Sat Sep 03, 2022 12:31 am
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.
Re: Does the PC Speaker PWM trick work on emulators?
Posted: Mon Sep 05, 2022 3:21 pm
by xeyes
After patching dosbox and playing with various options on my side, the PWM trick worked
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
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.