Experimental patch: An attempt to improve PC Speaker emulation accuracy

Developer's Forum, for discussion of bugs, code, and other developmental aspects of DOSBox.

Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby NY00123 » 2011-8-26 @ 14:07

Hey all,

Before writing about its purpose, I'd just tell that a minor (experimental) patch to DOSBox v0.74 is attached. I've chosen v0.74, as the SVN changes often, while it also doesn't seem to differ where the patch is to be applied.

Now to explain the patch's purpose: Some of you have possibly spotted some differences between sounds outputted via DOSBox' emulated PC Speaker and the same ones outputted via a real speaker. This is especially noticeable in games like Commander Keen 1-3 and Dangerous Dave, at least for some of the sound effects.

Here's a way to illustrate the cause (even if not fully accurate). Suppose that some game outputs a few successive square waves (beeps) of exactly the same frequency. Then:
- On a real speaker, when a new wave begins, it really begins from scratch, "forgetting" any earlier wave.
- In DOSBox, the waves are "merged" into a single long(er) successive square wave of the same frequency.

So, this patch is an attempt to solve this.
It's confirmed to *not* break the ability to output proper digitized sound via the speaker, at least in Ken's Labyrinth.

KNOWN ISSUES:
- Some who try the patch may feel that the sound quality is lower, or that the sound is less "smooth". In fact, this is closer to the way it works with an actual speaker, and I consider it to be more "authentic" ;).
- Maybe it's just me, but I have a feeling that a few unexpected high-volume noises are outputted in Dangerous Dave, at least upon obtaining a trophy, a gun or a jetpack. You still get something more true to the original, though.

AN IDEA (possibly low priority):
- Let the user choose between the current DOSBox way of emulating and the patched way. May be used for backwards compatibility with a few things originally developed and tested within DOSBox (i.e. games or MODs).

Note that I haven't posted this on the "Patches" sub-forum, as this is experimental, and there may be a better (code-wise) way of improving the emulation's accuracy.

Have fun!
Attachments
dosbox-0.74-pcspkr-patch.diff
(654 Bytes) Downloaded 347 times
NY00123
Member
 
Posts: 217
Joined: 2010-2-13 @ 19:42

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby NY00123 » 2011-8-27 @ 21:24

Hmm, looks like I can't edit the post. Alright lets add a new short one :)

Basically, I'm attaching two recordings of sound effects from Commander Keen 1 for comparison: One recorded on the unmodified DOSBox v0.74, and the other one on the patched DOSBox. So, a few should now have a ready comparison, possibly clarifying what I've originally referred to. It shouldn't be a surprise if the patched form sounds much more similar to a real thing than the non-patched.
Attachments
keen1_dosbox074_pcspkrpatched.ogg
Recording from CK1 with patched DOSBox
(133.52 KiB) Downloaded 402 times
keen1_dosbox074.ogg
Recording from CK1 with vanilla DOSBox v0.74
(124.74 KiB) Downloaded 373 times
NY00123
Member
 
Posts: 217
Joined: 2010-2-13 @ 19:42

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby robertmo » 2011-8-27 @ 22:57

edit button is in the top right corner of every post, although it is better to add new post as otherwise it won't be noticed. I don't think many people (if any) open every thread to check if one of posters edit something. That is simply not possible.

Btw is there any way to highlight edited threads? Are there any forums on Internet with such a feature?
User avatar
robertmo
l33t
 
Posts: 4160
Joined: 2003-6-18 @ 10:35

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby leileilol » 2011-8-27 @ 23:09

FINALLY SOMEONE'S FIXING KEEN'S SOUND

THANK YOU!
User avatar
leileilol
l33t++
 
Posts: 8780
Joined: 2006-12-16 @ 18:03

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby bloodbat » 2011-8-28 @ 04:06

Great patch :)
Here it is for SVN.
Attachments
pc_speaker_for_svn.diff
(539 Bytes) Downloaded 269 times
User avatar
bloodbat
Oldbie
 
Posts: 791
Joined: 2009-12-06 @ 07:11

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby VileRancour » 2011-8-28 @ 04:19

VERY NICE! This seems to fix many things, a lot of them way more basic than Keen's sfx. Tried some games that use the PC speaker for simple one-note music, and all of them sound better w/ this patch - i.e., successive notes of the same pitch can be distinguished.

Heck, if you just open GWBASIC and type "BEEP:BEEP:BEEP", you can actually hear three distinct beeps now :D

However, here's the bad news.... I tried testing a few more games/programs that tweak the PC speaker, and found one thing that seems to break so far - the fanfare in Digger (when you complete a level) sounds choppy and misses notes.
If you'd like a recording, feel free to ask, though I've only compared MB6 vs. my own SVN build (to which I applied your patch), so I might be in the wrong here.
User avatar
VileRancour
Oldbie
 
Posts: 1554
Joined: 2003-5-14 @ 22:11
Location: 1-01-80 0:00a

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby NY00123 » 2011-8-28 @ 06:41

Thanks to anyone who has tried this or even just listened to the previously attached samples!
robertmo wrote:edit button is in the top right corner of every post, although it is better to add new post as otherwise it won't be noticed.

Interestingly, while looking for it, I've got to the text on the bottom-right, telling what I can and cannot do. I've been so sure it tells I cannot do so many things, editing posts being one of them...but now I see that I can...oh well...Ah, and thanks for telling about the edit button, of course!
bloodbat wrote:Great patch Happy
Here it is for SVN.

This is good for the many with SVN or so :)
VileRancour wrote:However, here's the bad news.... I tried testing a few more games/programs that tweak the PC speaker, and found one thing that seems to break so far - the fanfare in Digger (when you complete a level) sounds choppy and misses notes.

Yeah, I've just that that Digger clone from 2001 and could spot it. One good reason to really consider the patch "experimental" then.
As for possible causes:
- There's a specific case I haven't taken care of. To compare, originally, the patch had no the check for mode 3, resulting in the digitized sounds within Ken's Labyrnith not working as expected (of course talking about sounds outputted to the PC speaker).
- There are two "problematic" sound effects in Dangerous Dave (obtaining a trophy and obtaining a gun/jetpack). These may now sound much similar to the originals, but there are still a few bits of a little high volume heard, or so I believe. I have a very basic guess for the cause but I'm unsure.

Well, if it is incorporated, it could be wiser to have two modes then: Original (i.e. for Digger or "backwards compatibility" with a few things) and patched (usually more true to the real thing).
NY00123
Member
 
Posts: 217
Joined: 2010-2-13 @ 19:42

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby leileilol » 2011-8-28 @ 11:00

Another game to test this with is Hard Drivin' II. The engine makes a powerful motor "buzzing" noise, but in standard Dosbox it's pitched and doesn't have its "buzz".
User avatar
leileilol
l33t++
 
Posts: 8780
Joined: 2006-12-16 @ 18:03

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby bloodbat » 2011-8-28 @ 12:45

Based on what NY00123 said and for ease of testing, here's a quick patch (for SVN) that allows you to turn off or on the experimental patch. The code could probably be improved, but...hey...I'm sleepless right now, so I had to do something. :P

In your .conf file.

Code: Select all
[speaker]
pcspeaker=exp


The possible values are true, exp and false.
True gives "vanilla" DosBox emulation, exp provides experimental patch and false behaves just as you'd expect.

It can be changed on the fly without quitting or restarting DosBox (with config).

It includes NY00123's patch.
Attachments
svn_speaker_selector_patch.diff
(2.63 KiB) Downloaded 251 times
User avatar
bloodbat
Oldbie
 
Posts: 791
Joined: 2009-12-06 @ 07:11

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby VileRancour » 2011-8-28 @ 14:24

some thoughts about fixing Digger w/ this patch... I have a hunch that an extra conditional check or two would fix it:

the end-of-level music tries to produce non-square waves by modulating the speaker cone position (changing the on/off values *really* quickly, so that the physical speaker doesn't have the time to reach fully ON/OFF status, producing intermediate levels). This doesn't happen with Keen's sfx or with simple single-tone music, which this patch does fix well.
So perhaps some of those "last" vars for the previous speaker/PIT status should also be checked, to determine whether or not we really need to set spkr.pit_index = spkr.pit_max. Right now it's done unconditionally and that's what seems to break (silence) certain sections of the music in this case.

unfortunately, I don't know enough about the DOSBox pcspeaker code, what the various "modes" mean or what each status var stands for. Maybe someone who does can tackle this, but hopefully i'm on the right direction with this.

bloodbat: nice - works fine here with your config patch.
User avatar
VileRancour
Oldbie
 
Posts: 1554
Joined: 2003-5-14 @ 22:11
Location: 1-01-80 0:00a

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby NY00123 » 2011-8-28 @ 15:11

VileRancour wrote:the end-of-level music tries to produce non-square waves by modulating the speaker cone position (changing the on/off values *really* quickly, so that the physical speaker doesn't have the time to reach fully ON/OFF status, producing intermediate levels).

This is, in fact, *exactly* my guess for the cause of a few high volume samples in the Dangerous Dave sounds mentioned beforehand. The current patch raises the emulated speaker's cone to the maximum immediately, not gradually; So it shouldn't be a surprise if an unexpected little noise is being heard.

Unfortunately, I have a feeling that correcting that wouldn't be so trivial, and, in fact, might even require a rewrite of most of the speaker code, if not all.
As of now, most of the time, the speaker's volume is really one of the edges: The very maximum or the very minimum. The only case when it isn't, is when so-called "Timer mode 0" is used. This is basically how digitized sound works in games like Ken's Labyrnith. For the usual square waves, mode 2 is used.

For such a rewrite, I suppose, all that the speaker should really know is its input; If 1, the speaker's (emulated) cone will gradually move to the maximum. Otherwise it'll gradually move to the minimum. When it comes to the different timer modes (i.e. square waves), these are to be handled by the timer only.

Therefore, I think that for now, the solution would be in bloodbat's way: Let one toggle the speaker mode. Use the patched mode in games like Commander Keen and the original in ones like Digger.
NY00123
Member
 
Posts: 217
Joined: 2010-2-13 @ 19:42

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby ripa » 2011-8-28 @ 18:10

I made some modifications to PC speaker emulation a while ago (tried to fix popping). If you're interested in incorporating my changes, check out:
viewtopic.php?p=158145

It would be incredibly helpful if someone had a scope and captured the pre-filtered (e.g. on or off voltage) output from the speaker wires.
ripa
Oldbie
 
Posts: 546
Joined: 2005-4-18 @ 00:53
Location: Finland

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby VileRancour » 2011-8-28 @ 18:23

NY00123: oh yeah - for now the ability to switch between the two modes is perfect. Just trying to think how to help this progress beyond experimental stage, precisely because I'd really like to see it committed into DOSBox (trunk) someday :)

Anyway,

NY00123 wrote:As of now, most of the time, the speaker's volume is really one of the edges: The very maximum or the very minimum. The only case when it isn't, is when so-called "Timer mode 0" is used.


are you sure? I just made a recording of the end-of-level Digger music (with the 'original' speaker mode), opened it up in Audacity, and I can clearly see various sample levels in between min and max. Unless we're not talking about the same thing here, in which case I again have to plead ignorance of what the various code bits really do.

curiously though, similar intermediate levels also appear in other bits of 'tweaked' speaker audio (i.e. in Styx), which do sound fine with your patch. So I don't know if a total rewrite woud be needed, but I'm also not sure anymore why this piece of music seems to break, but not others.
Perhaps it's still reasonable to somehow check for the previous "speaker" position and then do the reset it to pit_max only if it wasn't at an intermediate level. Really don't know though.
User avatar
VileRancour
Oldbie
 
Posts: 1554
Joined: 2003-5-14 @ 22:11
Location: 1-01-80 0:00a

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby NY00123 » 2011-8-28 @ 20:24

ripa wrote:I made some modifications to PC speaker emulation a while ago (tried to fix popping). If you're interested in incorporating my changes, check out:
viewtopic.php?p=158145


Hey, nice to see that you've attempted to solve another speaker-related issue. Truly, it seems to be a bit different than what I've originally aimed to solve (for Keen and the like).

However, I've attempted to use your patch, combined with mine. Interestingly, the digger game doesn't output the "popcorn" theme music anymore :o. The level completion fanfare still plays, and it is maformed as it is just with my patch (and not the two combined).
BTW, I've used a sample rate 48000Hz for this test (along with a few other ones), to accommodate to a few values you've got from somewhere.

VileRancour wrote:
NY00123 wrote:As of now, most of the time, the speaker's volume is really one of the edges: The very maximum or the very minimum. The only case when it isn't, is when so-called "Timer mode 0" is used.


are you sure? I just made a recording of the end-of-level Digger music (with the 'original' speaker mode), opened it up in Audacity, and I can clearly see various sample levels in between min and max. Unless we're not talking about the same thing here, in which case I again have to plead ignorance of what the various code bits really do.

curiously though, similar intermediate levels also appear in other bits of 'tweaked' speaker audio (i.e. in Styx), which do sound fine with your patch. So I don't know if a total rewrite woud be needed, but I'm also not sure anymore why this piece of music seems to break, but not others.
Perhaps it's still reasonable to somehow check for the previous "speaker" position and then do the reset it to pit_max only if it wasn't at an intermediate level. Really don't know though


Hmm... well, logging calls to some function like PCSPEAKER_SetCounter, it seems like timer mode 3 (square waves) is the only one being used, at least when it comes to speaker output. So, while some games with digitized sound may take advantage of mode 0 for that, this isn't the case here.
I've also recorded the Digger fanfare on vanilla DOSBox v0.74. The recorded contents appear to be made of many short spikes, with (relatively) long periods of silences in-between.

So I have a few guesses regarding this:
- Maybe Digger does commonly turn off the sound in a few places (i.e. disconnect the speaker from the timer), if not all. It could explain why, in a few occasions, the volume level goes up from negative to 0 and then down again.
- As vanilla DOSBox doesn't restart tones the way it's done in the patch, it may, actually, be more similar to the real thing, with the speaker cone taking a short while to move.
NY00123
Member
 
Posts: 217
Joined: 2010-2-13 @ 19:42

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby ripsaw8080 » 2011-8-28 @ 22:03

A tweak to the modification seems to keep the effect on Keen while not harming Digger:
Code: Select all
     case 3:        /* Square wave generator */
         if (cntr<spkr.min_tr) {
             /* skip frequencies that can't be represented */
             spkr.pit_last=0;
             spkr.pit_mode=0;
             return;
         }
+        if (spkr.mode==SPKR_PIT_ON) spkr.pit_index=spkr.pit_max;
         spkr.pit_new_max=(1000.0f/PIT_TICK_RATE)*cntr;
         spkr.pit_new_half=spkr.pit_new_max/2;
         break;
User avatar
ripsaw8080
DOSBox Author
 
Posts: 4122
Joined: 2006-4-25 @ 23:24

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby VileRancour » 2011-8-29 @ 05:16

^ hats off! Does that go instead of, or in addition to, the patch in the OP?
User avatar
VileRancour
Oldbie
 
Posts: 1554
Joined: 2003-5-14 @ 22:11
Location: 1-01-80 0:00a

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby robertmo » 2011-8-29 @ 06:32

instead
User avatar
robertmo
l33t
 
Posts: 4160
Joined: 2003-6-18 @ 10:35

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby bloodbat » 2011-8-29 @ 07:52

Thanks Ripsaw ;) it seems so far to work fine, though I don't have Digger.
User avatar
bloodbat
Oldbie
 
Posts: 791
Joined: 2009-12-06 @ 07:11

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby NY00123 » 2011-8-29 @ 12:44

Oh wow, a simple "relocation" of the original patch seems to solve not just the digger fanfare issue, but also the volume issues with these DDave sound effects! Good find Ripsaw!
So, the problematic sound effects in Dangerous Dave are now being heard even more like the originals! And, there are some clearly doubts regarding my guess about "short square waves" and the like...

Oh well, possibly a bit late, I've decided to attach a few more sound samples for comparison; This time from Dangerous Dave, and taking advantage of the new patch. You may clearly hear how you practically get whole different sound effects! (And after patching, actually recognized as the originals.)

Ah, and yes, the Keen 1 sounds are still correct now.
Thanks Ripsaw!
Attachments
dave_gottrophy_dosbox_vanilla.ogg
DDave "got trophy" sound, vanilla DOSBox
(23.19 KiB) Downloaded 308 times
dave_gottrophy_dosbox_newpatch.ogg
DDave "got trophy" sound with the new patch
(22.45 KiB) Downloaded 307 times
dave_gotsomething_dosbox_vanilla.ogg
DDave "got gun/jetpack" sound, vanilla DOSBox
(9.58 KiB) Downloaded 289 times
dave_gotsomething_dosbox_newpatch.ogg
DDave "got gun/jetpack" sound with the new patch
(9.09 KiB) Downloaded 297 times
NY00123
Member
 
Posts: 217
Joined: 2010-2-13 @ 19:42

Re: Experimental patch: An attempt to improve PC Speaker emulation accuracy

Postby ripsaw8080 » 2011-8-29 @ 13:41

There's a bit more than a simple relocation; the added spkr.mode condition is what helps Digger.

Some other early games by id Software like Dangerous Dave in The Haunted Mansion, Rescue Rover, and Shadow Knights sound more like what I remember. Not too surprising because the sound code was probably reused amongst their games.
User avatar
ripsaw8080
DOSBox Author
 
Posts: 4122
Joined: 2006-4-25 @ 23:24

Next

Return to DOSBox Development

Who is online

Users browsing this forum: wien and 2 guests