joncampbell123 / dosbox-x

DOSBox-X fork of the DOSBox project
GNU General Public License v2.0
2.57k stars 375 forks source link

[IBM PC and PC-98] Inaccurate PC speaker emulation. #3131

Open spaztron64 opened 2 years ago

spaztron64 commented 2 years ago

Code of Conduct & Contributing Guidelines

Have you checked that no other similar bug report(s) already exists?

What operating system(s) this bug have occurred on?

Windows 7 x64, Windows 10 x64 1607

What version(s) of DOSBox-X have this bug?

0.83.20 SDL2 MinGW Win32

Describe the bug

Applications that play back digitized audio with PWM techniques have a very high amount of noise, while the actual signal is very quiet.

A notable example would be Impulse Tracker with the PC speaker driver, where even during no playback there's a constant noise output: https://www.youtube.com/watch?v=DADcHpJ6lIw

Same problem happens on the PC-98 side, with a notable example being Queen of Duelist Gaiden Alpha. Here's a comparison of real hardware and emulation: Under DOSBox-X: https://www.youtube.com/watch?v=l-WsANf8A9o On real PC-9821 V166 hardware: https://www.youtube.com/watch?v=svkjQAxeTd0

Expected behavior

No response

Steps to reproduce the behaviour

  1. Launch any application that makes use of the PC speaker for playback of digitized audio (ex. Impulse Tracker: "IT /sitpcspkr.drv")
  2. Play audio

Used configuration

No response

Emulator log

No response

Additional context

No response

joncampbell123 commented 2 years ago

I suppose some adjustments could be made to the lowpass filtering and "PC speaker speed" variable.

However remember that the "whine" you hear in the PC speaker "digitized speech" is an artifact of the PWM modulation needed to produce audible output to the speaker. Furthermore, the frequency of that whine is determined by what sample rate the DOS game is using when playing out to the PC speaker. The higher the sample rate, the better it is filtered out by the PC speaker, and the less you notice it. The lower the sample rate, the more obvious the whine.

Perhaps you or others never noticed it either because the PC speaker on your DOS machine is quiet, or the PC itself was noisy enough to mask it somewhat, or perhaps it was just mentally filtered out by concentrating on the DOS game.

I'm about to push a commit that modifies the PC speaker output to improve things somewhat:

I think long term I might make everybody happy by adding dosbox.conf settings to allow tweaking the lowpass filtering, slew rate, etc. applied to the PC speaker output so others can make it sound the way they want.

joncampbell123 commented 2 years ago

I'm pretty sure on any DOS machine, if you were to connect a USB sound card directly to the speaker connector pins on your motherboard and run a game with "digitized PC speaker sound" you would probably hear the same artifacts and whine.

spaztron64 commented 2 years ago

The PC-9821 V166 capture was from a proprietary sound card that routes the beeper's output directly to it's Speaker Out, and I can tell you that there's absolutely no distorting whine at all, apart from a "mosquito alarm" like whine (though my capture might not have done it much justice, so here's a better demonstration: https://www.mediafire.com/file/okyt3zvoe2ulitr/pc98spkr_test.flac/file ).

On the PC side, 86box has already solved this problem. If you try out Impulse Tracker with the same PC speaker driver, you'll notice that not only is the signal much louder, but there is absolutely no whine to speak of, just as it would be on real PC hardware. Here's an example (apologies for the stutters, it's just host system lag): https://www.youtube.com/watch?v=Fi905_QccjQ

You might wanna check out it's implementation here: https://github.com/86Box/86Box/blob/master/src/sound/snd_speaker.c

joncampbell123 commented 2 years ago

That's just it: DOSBox SVN and 86box have special case code for "digitized speech" (PIT 2 mode 0) to render it directly and clearly using the counter values given by the program, which sounds nice, but is not what it actually sounds like on hardware because the PWM modulation required imposes a "whine" on the sound, the same whine you can hear clearly in your PC-98 capture.

As for the distortion in the PC-98 game, I'm not sure yet what's going on there. My best initial guess is that it may have something to do with interrupt processing and whatever regular interval timer the game is using to drive the PC speaker.

EDIT: Hey, it sounds like the PC-98 game is not only capable of playing through the PC speaker but also playing multiple sounds at once as well.

joncampbell123 commented 2 years ago

I suppose the best way to show what I mean is to boot up some old 386 and 486 systems, run DOS programs that play "digitized speech" from the PC speaker, and record it with a microphone and post it here.

grapeli commented 2 years ago

A notable example would be Impulse Tracker with the PC speaker driver, where even during no playback there's a constant noise output: https://www.youtube.com/watch?v=DADcHpJ6lIw

An unusually high level of distortion would suggest that you were not digitally rip it off. I wonder how did you made these recordings?

Impulse Tracker running under x86box?

https://user-images.githubusercontent.com/452325/145634620-cf450aad-2e2c-4e29-b4ab-d6382e905bea.mp4

The same distortions are in the "dosbox" sample.

https://user-images.githubusercontent.com/452325/145634654-c586c81e-83fc-4791-a32c-89aa04f8918b.mp4

orginal

https://user-images.githubusercontent.com/452325/145634692-9fc610ae-60c1-47d7-a082-e166c45f30e5.mp4

grapeli commented 2 years ago

My recording. There are cracks. There is no distortion and the noise level to the signal is normal.

https://user-images.githubusercontent.com/452325/145646164-d4542ac6-2174-42c4-aa1d-e49eccb32b99.mp4