AY emulation testing

Struggling with Fuse or trying to find an emulator with a specific feature. Ask your questions here.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

AY emulation testing

Post by djnzx48 »

One thing I've noticed with regards to AY in emulators is that they all have a slightly different sound. In particular, the noise generator sounds a bit off in some of the emulators I've tried. I decided to run some tests to investigate this further, and here are the results I've obtained.

First I recorded the output of a simple noise filter sweep from a random selection of emulators I had installed, and put it through a frequency analyser.
Image

Here AY_Emul and Fuse give similar looking output, and the graphs from the other emulators also match up with each other. (For some reason the output isn't very clear, but the ZEsarUX graph seems to match up with the Fuse and AY_Emul graphs.)

When I compared the differing frequencies of the graphs, a suspicious-looking pattern began to emerge. Here I've stretched the output from AY_Emul to double width, and then overlaid the Spectaculator graph on the left hand side at regular size.
Image

They match up perfectly! So it looks like Spectaculator and the other emulators are using half the noise period of Fuse and AY_Emul. To test this, I modified the PT3 player code found here to double all the noise periods before outputting them, thus reducing the frequency to compensate:

Code: Select all

pl2:
    ld ix,ChanA
    ld hl,(AYREGS+AR_TonA)
    call chregs
    ld (AYREGS+AR_TonA),hl
    ld a,(Ampl)
    ld (AYREGS+AR_AmplA),a
    ld ix,ChanB
    ld hl,(AYREGS+AR_TonB)
    call chregs
    ld (AYREGS+AR_TonB),hl
    ld a,(Ampl)
    ld (AYREGS+AR_AmplB),a
    ld ix,ChanC
    ld hl,(AYREGS+AR_TonC)
    call chregs
    ld (AYREGS+AR_TonC),hl
    ld hl,(ns_base_AddToNs)
    ld a,h
    add a,l

    ; add this bit to double the noise period
    add a,a
    cp $20
    jr c, skip_noise_cap

    ld a, $1f

skip_noise_cap:

    ; rest of code
    
    ld (AYREGS+AR_Noise),a
Testing this code in Spectaculator gave a consistent result to the unmodified player in Fuse, except for the noise periods above $0f being clipped to $1f and so half the frequency range being lost.

What's the reason for the discrepancies between emulators? I thought it might have something to do with how the chip divides the clock internally, but I'm just guessing here. I don't own a 128 so I can't test this to see which implementation is correct. Does anyone have any measurements from real hardware to confirm?

If anyone's interested, here's the program I used to test. You can use BREAK and RUN 40 to restart it when it stops.
noise.tap
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: AY emulation testing

Post by Ast A. Moore »

Can’t run the test on real hardware at the moment, but I just ran it in Fuse, and my results don’t match yours. I get a nice picture with a cutoff at 22 kHz, like you have in other emulators. Maybe you have some additional filtering enabled in Fuse?

Image


I’ll try to run your test on my Toast Rack later tonight, if I have the time.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: AY emulation testing

Post by Ast A. Moore »

Here’s the spectrogram from my Toast Rack. It’s pretty close to what I get in Fuse. There’s a horizontal line at about 15.5 kHz, though. Not sure what this is. Might be interference from another component, or some weird harmonic. Also note, that there’s no rolloff at higher frequencies.

Image

However, I noticed a strong DC offset in the waveform generated by Fuse:

Image

The output from the Spectrum swings both more or less equally above and below zero, but is a little less perfectly square:

Image

Note: Both waveform screenshots are taken from the very end of the generated noise sequence.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Fred
Drutt
Posts: 38
Joined: Tue Feb 27, 2018 3:15 am

Re: AY emulation testing

Post by Fred »

Turn on the filter to get the dc offset addressed in Fuse.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: AY emulation testing

Post by djnzx48 »

Thanks for taking the time to run those tests. Could the differences in the Fuse spectrograms be some kind of folding due to aliasing? It's possible it was just a filter though.

So Fuse is showing correct behaviour here, and the emulators that are halving the period are doing it wrong? Interesting that this went unnoticed for so long. Maybe I'll run the test on some more emulators and get some more results.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: AY emulation testing

Post by Ast A. Moore »

Fred wrote: Wed Oct 10, 2018 9:35 pm Turn on the filter to get the dc offset addressed in Fuse.
I normally have the “TV speaker” filter on and I only turned it off for the test.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: AY emulation testing

Post by Ast A. Moore »

djnzx48 wrote: Wed Oct 10, 2018 9:43 pm Could the differences in the Fuse spectrograms be some kind of folding due to aliasing? It's possible it was just a filter though.
Possibly. But I’d rerun the test, just to be sure. Also, it might be platform-dependent. I run Fuse on macOS.
djnzx48 wrote: Wed Oct 10, 2018 9:43 pmSo Fuse is showing correct behaviour here, and the emulators that are halving the period are doing it wrong? Interesting that this went unnoticed for so long. Maybe I'll run the test on some more emulators and get some more results.
That would be interesting.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Sokurah
Manic Miner
Posts: 283
Joined: Tue Nov 14, 2017 10:38 am
Contact:

Re: AY emulation testing

Post by Sokurah »

I love it. I don't understand much about sound generation and frequencies in general, but I love the fact some some people delve into something as specific as sound and analyse how well some emulators perform compared to "the rest". Interesting read. Good job :)
Website: Tardis Remakes / Mostly remakes of Arcade and ZX Spectrum games.
My games for the Spectrum: Dingo, The Speccies, The Speccies 2, Vallation & Sqij.
Twitter: Sokurah
User avatar
Fred
Drutt
Posts: 38
Joined: Tue Feb 27, 2018 3:15 am

Re: AY emulation testing

Post by Fred »

Ast A. Moore wrote: Wed Oct 10, 2018 10:25 pm Possibly. But I’d rerun the test, just to be sure. Also, it might be platform-dependent. I run Fuse on macOS.
There are no platform specific behaviours in this part of Fuse so you don’t need to worry about that :)
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: AY emulation testing

Post by djnzx48 »

I tried a few more emulators, but a surprising proportion of them suffered from serious audio dropouts, at least on my low end hardware (that's the reason for the black bars on the Unreal Speccy pic). I was going to test Spectramine as well but the only link I found for it was a password protected zip.

Anyway, these AY emulators all match the output from a real Spectrum:
Image

Also, I reran the Fuse test making sure the filter was disabled, but still got the same result.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: AY emulation testing

Post by Ast A. Moore »

djnzx48 wrote: Thu Oct 11, 2018 9:23 am Also, I reran the Fuse test making sure the filter was disabled, but still got the same result.
So much for that, then: ;)
Fred wrote: Thu Oct 11, 2018 6:27 am There are no platform specific behaviours in this part of Fuse so you don’t need to worry about that :)
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: AY emulation testing

Post by djnzx48 »

It looks like I'm still on version 1.5.0, so that could have something to do with it? I haven't try messing with any of the other sound settings either.
User avatar
druellan
Dynamite Dan
Posts: 1466
Joined: Tue Apr 03, 2018 7:19 pm

Re: AY emulation testing

Post by druellan »

Awesome work. Just yesterday I was on a discussion about AY sound emulation, and I was thinking on doing some tests, but this is just way beyond my humble skills. Very interesting stuff!
User avatar
druellan
Dynamite Dan
Posts: 1466
Joined: Tue Apr 03, 2018 7:19 pm

Re: AY emulation testing

Post by druellan »

djnzx48 wrote: Thu Oct 11, 2018 9:23 am I was going to test Spectramine as well but the only link I found for it was a password protected zip.
I have the ZIP for version 1.01b (09/26/2017) https://send.firefox.com/download/ac766 ... DYGRfycSrQ
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: AY emulation testing

Post by Ast A. Moore »

djnzx48 wrote: Thu Oct 11, 2018 9:37 am It looks like I'm still on version 1.5.0, so that could have something to do with it?
I doubt it. Although I’m not following the Fuse development process very closely, I don’t think the sound engine has undergone any major (or even minor) rewrites in the past few releases. For the record, I did run the test in v1.5.5, though. I also used Audio Hijack to capture the audio, thus taking any potential artifacts created by the DAC/ADC/cable out of the equation. (Not that they would have had any appreciable or measurable effect in this instance, anyway.)
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
hikoki
Manic Miner
Posts: 576
Joined: Thu Nov 16, 2017 10:54 am

Re: AY emulation testing

Post by hikoki »

druellan wrote: Thu Oct 11, 2018 12:02 pm
djnzx48 wrote: Thu Oct 11, 2018 9:23 am I was going to test Spectramine as well but the only link I found for it was a password protected zip.
I have the ZIP for version 1.01b (09/26/2017) https://send.firefox.com/download/ac766 ... DYGRfycSrQ
There you have Spectramine version 1.03b https://yadi.sk/d/Mz2J8P3vMuz3vw
User avatar
Fred
Drutt
Posts: 38
Joined: Tue Feb 27, 2018 3:15 am

Re: AY emulation testing

Post by Fred »

Ast A. Moore wrote: Thu Oct 11, 2018 9:29 am
djnzx48 wrote: Thu Oct 11, 2018 9:23 am Also, I reran the Fuse test making sure the filter was disabled, but still got the same result.
So much for that, then: ;)
I don't know - it's very much what I would expect from Fuse's stronger filters being enabled.
User avatar
Fred
Drutt
Posts: 38
Joined: Tue Feb 27, 2018 3:15 am

Re: AY emulation testing

Post by Fred »

Ast A. Moore wrote: Thu Oct 11, 2018 12:17 pm
djnzx48 wrote: Thu Oct 11, 2018 9:37 am It looks like I'm still on version 1.5.0, so that could have something to do with it?
I doubt it. Although I’m not following the Fuse development process very closely, I don’t think the sound engine has undergone any major (or even minor) rewrites in the past few releases. For the record, I did run the test in v1.5.5, though. I also used Audio Hijack to capture the audio, thus taking any potential artifacts created by the DAC/ADC/cable out of the equation. (Not that they would have had any appreciable or measurable effect in this instance, anyway.)
The most direct way is to use the movie recording feature and extract the sound with fmfconv.

Fuse did change the noise handling in 2012 for version 1.1.0 based on the investigation from this WoS thread: https://www.worldofspectrum.org/forums/ ... ion/39644/
User avatar
Pegaz
Dynamite Dan
Posts: 1209
Joined: Mon Nov 13, 2017 1:44 pm

Re: AY emulation testing

Post by Pegaz »

For most emulators I have tried with noise.tap file, the noise goes from silent to louder in several visible steps.
Only SpecEmu and Spin have a smooth noise transition but it looks like the sound level is the same from the beginning to the end.
Which of these two cases is closer to the real hardware?
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: AY emulation testing

Post by Ast A. Moore »

Fred wrote: Thu Oct 11, 2018 2:39 pm Fuse did change the noise handling in 2012 for version 1.1.0 based on the investigation from this WoS thread: https://www.worldofspectrum.org/forums/ ... ion/39644/
Oh, this is interesting!
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: AY emulation testing

Post by Ast A. Moore »

Pegaz wrote: Thu Oct 11, 2018 5:13 pm For most emulators I have tried with noise.tap file, the noise goes from silent to louder in several visible steps.
Only SpecEmu and Spin have a smooth noise transition but it looks like the sound level is the same from the beginning to the end.
Which of these two cases is closer to the real hardware?
The amplitude should remain more or less the same. This is from my Toast Rack:

Image
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: AY emulation testing

Post by djnzx48 »

Thanks for the Spectramine links [mention]druellan[/mention] and [mention]hikoki[/mention].

Dunno why Fuse was different when I tested it but it still sounded fine IMO. Turning the filter on still affects the sound quite a lot.

I wonder what the best solution to this is for new releases, as the difference between the two noise generators can be very noticeable in some cases. Of course it would be really nice if the emulators could be fixed, but the workaround for doubling noise periods doesn't actually sound too bad. The clunky part would be having to stick in a menu option for it and getting the user to pick the right one.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: AY emulation testing

Post by Ast A. Moore »

djnzx48 wrote: Thu Oct 11, 2018 9:58 pm The clunky part would be having to stick in a menu option for it and getting the user to pick the right one.
Yeah, that would be a bad thing to do.

Unfortunately, in this day and age of a gazillion of emulators, developers are faced with a choice—to code for the Spectrum or to code for a particular emulator. I chose the former, and, frankly, couldn’t give a rat’s behind about how emulators handle my code. That’s why I feel completely justified in calling myself a Spectrum developer.

Now, I’m way past the age of coming down hard on people who decide to tailor their code to a specific emulator—software or hardware (e.g. the various clones of the Spectrum, both old and new)—it’s their choice. I may curmudgeonly mutter something along the lines of “And they dare call themselves Spectrum developers?” but only under my breath, and mostly in jest. At the same time, if I’m in the right mood, I’ll happily point out that something in their code doesn’t work quite right on a real Spectrum. :D

The purist in me would suggest refining your code so that it behaves the way you want it to on real hardware and forget about tweaking it to work the same way in an emulator.

That said, Fuse with the “TV speaker” filter on sounds pretty darned close to the real thing to my ear (and by “the real thing” I mean an actual Spectrum hooked up to an actual TV—a CRT one, to boot). So, I’d say—stick with that setting. ;)
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: AY emulation testing

Post by djnzx48 »

Yeah, I agree that targeting the actual machine is preferable. The reality is though that not everyone owns real Spectrums, and many people are forced to use emulators as their only option. Personally I'd like to write programs for platforms that people will run them on, and I'd say Spin, SpecEmu and Spectaculator together probably make up the majority of emulators in use, without considering clones.

Even if you ignore emulators you still have to deal with compatibility issues, such as the contended memory banks being different on the +2A/+3. I know emulators aren't official Spectrum models, but they're in wide use so it's reasonable to want to support them.

And it's not just a binary decision. You can develop for the Spectrum primarily but still have emulation/additional hardware in mind, for example with ULAplus.
User avatar
Fred
Drutt
Posts: 38
Joined: Tue Feb 27, 2018 3:15 am

Re: AY emulation testing

Post by Fred »

Ast A. Moore wrote: Thu Oct 11, 2018 6:10 pm
Pegaz wrote: Thu Oct 11, 2018 5:13 pm For most emulators I have tried with noise.tap file, the noise goes from silent to louder in several visible steps.
Only SpecEmu and Spin have a smooth noise transition but it looks like the sound level is the same from the beginning to the end.
Which of these two cases is closer to the real hardware?
The amplitude should remain more or less the same. This is from my Toast Rack:
This would be caused by filtering on those frequencies - you can see the difference in Fuse when you turn filtering on and off.
Fuse manual wrote: This option allows the emulation of the sound output system to be modified. Different choices of speaker limit the bass and treble response that can be produced from the machine. Choose between a “TV” type speaker and a small beeper type speaker that significantly limits bass and treble response or Unfiltered for the full frequency range like from the MIC output.
Post Reply