mgba-emu / mgba

mGBA Game Boy Advance Emulator
https://mgba.io/
Mozilla Public License 2.0
5.42k stars 747 forks source link

[GB Audio] crackly instruments in Adventures of Lolo music #1925

Closed bryc closed 2 years ago

bryc commented 3 years ago

I've noticed the title screen music of Adventures of Lolo (SGB) sounds noticeably scratchy/crackly in mGBA compared to other emulators.

It's hard to explain the issue in words, so here is an audio comparison between mGBA, NEZplug (dedicated .GBS player) and VBA-M. I would've included SameBoy and Gambatte, but can't seem to mute channels in there, but they sound fine. This is just Pulse 1&2.

Here: lolo_compare_flac.zip, contains mgba.flac, nezplug.flac, vbam.flac.

Basically, everything except mGBA sounds 'smoother' in this song and generally more pleasant. And I don't mean that it's less "clicky", but it seems the transition between volume levels in the instrument envelope itself is much harsher or less smooth in mGBA.

ahigerd commented 3 years ago

Has this been tested on hardware? The GB audio hardware has surprisingly coarse instrument envelopes. This may be a case of mGBA being more accurate.

bryc commented 3 years ago

@ahigerd I don't have any hardware to test :( . but if someone could get a decent audio recording it would be pretty easy to compare. But if true, then SameBoy and Gambatte would be inaccurate as well.

SameBoy wont let me mute individual channels, but the first note of the melody is isolated and can be compared for a short duration:

https://user-images.githubusercontent.com/1408749/111851470-a3b15f00-88e9-11eb-961d-cfac160e75ac.mp4

This shows mGBA has highly noticeable stepping in the volume envelope, whereas SameBoy's stepping is more smooth. 4-bit volume levels would have to somewhat coarse, but not to the extent mGBA is.

Correction: They have the same amount of stepping, but there is a short burst of a 'click' each time a volume step occurs in the envelope, and this can create artifacts, which is more pronounced in mGBA than Sameboy.

endrift commented 3 years ago

I'm not entirely sure why but you don't appear to be comparing apples to oranges here--if you look closely you can see that the sameboy clip has a hard cut off before the weird steps at the end of the mGBA clip--and further they're not normalized in comparison to each other (the mGBA clip is slightly louder)

endrift commented 3 years ago

Further, BGB's envelope looks very similar (although it does sound very different so I'm not 100% sure what's going on there), and VBA-M's GB emulation is uh...lackluster at best. I would not trust it for audio comparisons, and nor would I trust NEZplug which seems to be...quite ancient.

I would trust Sameboy's audio though.

bryc commented 3 years ago

Dunno if it helps, but here's some more observations from my limited ability:

mGBA

image

Notice that the signal strength in the lower end doesn't seem to drop between each discrete step, as compared to the higher end, which shows the steps more clearly.

BGB

image

Side note: Apparently BGB's timing of the envelope volume changes is slightly different than mGBA or Sameboy, which both seem to have the same timing, whereas BGB appears to 'snap' or 'quantize', causing certain volume change events to be closer than others.

There appears to be 9 discrete steps in the instrument envelope. Each time a volume level changes, there is a boost of volume (or signal strength), which would sound like a very fast 'click', or otherwise sound like a distortion. This seems normal across all emulators and part of the audio hardware.

But in this case, mGBA appears to be boosting some of these artifacts, which sound most noticeable in the 200-220 hz range.

Here is an audio comparison of mGBA and BGB, both were slowed down to 20 fps so the artifacting is easier to hear:

https://user-images.githubusercontent.com/1408749/111870185-96cc5400-8959-11eb-97e0-cce4f4b7a769.mp4

If I had to take a wild guess what it might be:

I would test SameBoy, but it has no ability to set the emulation speed, or mute any audio channels. So I can't get any clean audio except from specific parts where only 1&2 are active.

VBA-M and NEZPlug happen to be the only emulations available where I can mute channels. Couldn't seem to do it in Gambatte either. I assumed its hard to screw up simple square waves and that their emulation was acceptable, but perhaps there are more subtle aspects to the APU than I realize.

ahigerd commented 3 years ago

I think you may be onto something with the DC offset issue -- the Game Boy's sound hardware is entirely DC, and looking at the actual waveform instead of the spectrogram shows some pretty dramatic DC drifting instead of a pure DC waveform or a rectified AC waveform. Regardless of whether or not the oscillators or the envelopes are being emulated correctly, this is definitely not what the output waveform should look like.

I'm going to go focus my attention on the output side of mGBA's audio pipeline. I may have an idea. Stay tuned!

ahigerd commented 3 years ago

As an aside: I would still greatly appreciate some recordings from various Game Boy hardware revisions. The audio is significantly different from revision to revision even within the same model, so while I'm pretty sure I can get rid of the clicks so that it sounds better, there are some considerations that I'll want to take into account for accuracy in the long run.

endrift commented 3 years ago

Now that I think about it, the DC offset may be being applied at the wrong part of the audio processing. I apply it before the simulated capacitor charging, but perhaps it needs to be done after?

ahigerd commented 3 years ago

That's unrelated. The fact that the GB audio hardware has a DC bias is separate from the fact that it has a DC audio signal -- that is to say, it generates unsigned samples, but the code is feeding those samples into a signed output buffer. This results in a massive DC offset.

ahigerd commented 3 years ago

I've poked at this some more and I've identified that the DC offset is a red herring -- that is, after fixing the DC offset, the noise doesn't go away. The zero crossing theory is also wrong, but it's closer: it's actually a lack of an appropriate zero crossing: image

I spoke with endrift and it looks like the root of the issue is that the game code is explicitly retriggering the note, and mGBA's implementation details aren't matching how the original hardware handles that. We've got a lead on where the discrepancy might be.

ahigerd commented 3 years ago

Also on the subject of DC: Turns out that while the PSG code IS generating a massive DC offset, the band-limited output buffer cleans it up already. You can still see a little bit of it in the waveform but the effect is inaudible.

bryc commented 2 years ago

Just tested 0.10-7299-50e0fe997. Amazing work! GB Audio is perceivably flawless now. ❤️

bryc commented 2 years ago

Actually, it seems there's a regression, some audio is not being played during the intro of Pokemon Crystal (GBC) and Pokemon Yellow (GBC).

Not sure if it's due to these changes, worth opening a new issue?

endrift commented 2 years ago

Actually, it seems there's a regression, some audio is not being played during the intro of Pokemon Crystal (GBC) and Pokemon Yellow (GBC).

Not sure if it's due to these changes, worth opening a new issue?

It probably is due to this. I tried to mitigate issues that cropped up immediately in Telefang, so yeah please do open a new issue.

bryc commented 2 years ago

Hmm... I'm doing more listening tests and it looks like there's still some similar issues with GB Audio 😞.

The game Aladdin (USA).gbc has a similar crackling issue on the title screen music (in the bassline), that's not occurring on SameBoy or bgb.

And upon closer listen, the Adventures of Lolo fix might not entirely be correct either, it's almost 'too clean', as if removing a detuning effect that can still be heard in SameBoy / bgb.

Exact version here is: 0.10-7512-0a674dd37

endrift commented 2 years ago

There were some major changes to GB audio yesterday, this might be fallout from that.

bryc commented 2 years ago

Here are some audio tests (SameBoy and mGBA) that hopefully show the differences: audio tests.zip (From mGBA 0.10-7512-0a674dd37)

The Aladdin issue seems to still exist in 0.9.3, so it's not a regression I think.