Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for broken Lowpass Filter #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bbbradsmith
Copy link
Contributor

I had created an atari-forum thread to mention this issue but I just noticed that Hatari has a github mirror, so I've decided to present it as a pull request here.

This PR addresses two issues.

Issue 1: Lowpass Filter Samplerate

When the sound emulation was updated to work with an internal 250 kHz buffer, the lowpass filters which were designed with coefficients for audio-output samplerate frequencies were not updated and are now operating at a much higher frequency.

The result is that the lowpass filter is effectively disabled, by having a cutoff that's about 5x too high, above the range of hearing. There is a lot of undesirable high frequency noise/aliasing in Hatari's current audio output.

The fix for this is straightforward. In sound.c I moved the application of the lowpass filters into YM2149_NextSample_250, which is where Hatari downsamples the 250 kHz into the audio output samplerate. The filters now function as they did before here.

Issue 2: Lowpass Filter Quality

The second problem is that the existing LowPassFilter and PWMaliasPassFilter (current default) both produces an extremely distorted sound due to the nature of their implementation.

The comments attached to them indicate that they may have some theoretical basis in modelling an asymmetrical push-pull amplifier circuit in the ST. However, the resulting sound is extremely harsh and distorted. I have compared Hatari's output against many recordings of real ST hardware (my own and others) and I do not feel that it accurately matches any of them. It is much more distorted.

There may exist real hardware that does sound this distorted, but in my experience this sound is not representative, and furthermore it is unpleasant and drastically reduces the quality of the audio in Hatari.

  1. Provide a new default lowpass filter IIRLowPassFilter which gives a simpler symmetrical IIR, applying the same cutoff frequency as one of the two phases of PWMaliasPassFilter without the discontinuity of switching between two different phases. The result is a normal, clean sounding, standard lowpass filter effect.

  2. Add YmLpf and YmHpf to the configuration set, so that users can select which of the existing Lowpass or Highpass filters should be used. We are already selecting them in software, but it was never exposed to the user. This allows anyone who prefers a past behaviour to select it instead of the default in their configuration files.

  • YmLpf settings:
    • 0 = YM2149_LPF_FILTER_NONE - Reproduces the current behaviour of a disabled lowpass.
    • 1 = YM2149_LPF_FILTER_LPF_STF - An older LPF used before PWM became the default?
    • 2 = YM2149_LPF_FILTER_PWM - The current default.
    • 3 = YM2149_LPF_FILTER_IIR - The new default, a clean lowpass filter.
  • YmHpf settings:
    • 0 = YM2149_HPF_FILTER_NONE - Disable the highpass.
    • 1 = YM2149_HPF_FILTER_IIR - The default.

The existing highpass filter was not affected by the samplerate problem, as it was already being applied at the audio samplerate.

add simpler better sounding IIR lowpass
@bbbradsmith
Copy link
Contributor Author

Here is a sound comparison of Bubble Bobble with the old and new lowpass filters. I think it should make clear the issue I am describing regarding distortion:
bb_compare_sound.zip

@bbbradsmith
Copy link
Contributor Author

The broken freebsd build appears to be a pre-existing condition and not related to my PR.

@bbbradsmith
Copy link
Contributor Author

bbbradsmith commented Aug 12, 2023

I made a recording of my 1040STF and the 4 filter options for comparison:
filter_comparison_bubble_bobble.zip

Each contains 16 seconds of audio, 2 excerpts from Bubble Bobble, the start of the tune at boot, and the introduction jingle that starts when you press the "1" or "2" key to begin the game. I would recommend listening to them side by side, or line them up in an audio-editor and switch between them to compare.

There are 5 recordings:

  • 1040stf.flac - Recorded from my ST.
  • hatari_iir.flac - The new filter I am proposing to use as default. (IIR)
  • hatari_stf.flac - The previous default ST filter (LPF_STF)
  • hatari_pwm.flac - The alternate filter which applied to later machine types? (PWM)
  • hatari_none.flac - No lowpass filer (NONE)

Commentary:

  • 1040STF - We can hear some lowpass, but the distortion effect is quite low.
  • IIR - I feel it's the closest in sound to the STF recording, but slightly cleaner, idealized.
  • STF - The distortion here is severe, the square waves do not sound square, it like a different instrument altogether.
  • PWM - Very distorted as well, with hardly any lowpass effect. Harsh sound.
  • NONE - No distortion, but far too much high frequency. Harsh and piercing. We need some lowpass by default.

The comments in the code indicate some theoretical basis of circuit modelling for the LPF_STF and PWM filters, but I think in practice at least the LPF_STF one does do not sound anything at all like the machine they are intended to emulate. I can't offer direct commentary on the PWM version because I do not have an STE or Falcon to record, but since its filter implementation is a direct modification of LPF_STF (disabling the filter for half of its input phase), I suspect it may suffer from the same problem of introducing uncharacteristic amounts of distortion.

Unfortunately I do not have a theoretical model to substitute at this time, I am only offering a simpler model of a standard RC lowpass filter, derived from the existing LPF_STF filter but removing the part that causes distortion by operating on the two phases asymmetrically.

However, theory or not, ultimately the theory is no good if it leads us to the wrong sound. I would love to see a re-evaluation of the circuitry, and to test a new model against reference hardware recordings, but for now I want to suggest the new filter as a default, as I think it does sound much closer in the STF case, and otherwise is a more pleasant sound.

.

Some additional recordings of Falcon and STE were provided by Anders Eriksson on the mailing list:
https://listengine.tuxfamily.org/lists.tuxfamily.org/hatari-devel/2023/09/msg00020.html

@bbbradsmith
Copy link
Contributor Author

For comparison, here's a waveform comparison between the 1040STF recording, the IIR emulation, and the LPF_STF emulation:

1040stf_vs_hatari_iir_vs_hatari_stf

Note how extremely non-square the waveform shape is for the LPF_STF filter. This is the asymmetrical distortion effect I was describing.

@huth
Copy link
Member

huth commented Aug 12, 2023

@npomarede : Could you please have a look when you've got some spare time? Thanks!

@bbbradsmith
Copy link
Contributor Author

bbbradsmith commented Jan 26, 2024

Current status: I still feel the changes above are complete and worthwhile as-is, but after some disccusion from the mailing list, I wished to do some further hardware recording tests and comparisons to help justify the change.

The complaint was that the old method had a theoretical basis, attempting to model a function of the YM's current output, whereas my intended replacement is instead a simple lowpass filter, based only on empirical observations of recorded hardware output.

Some clarification was given as to where the theory for the existing filters came from:
https://listengine.tuxfamily.org/lists.tuxfamily.org/hatari-devel/2023/09/msg00021.html

My belief is this theory about the asymmetrical output is incorrect, or at least over-emphasizing its effect, leading to the very distorted sound of the current implementation. My plan is to write a test program that can be run on hardware that should demonstrate the theoretical asymmetry in a way that can be measured easily from an audio recording.

This would help evaluate whether, and by how much, the old method may be divergent from the real hardware, and could possibly lead to a better theoretical understanding of how to model the output in a way that more closely matches it.

@npomarede
Copy link
Contributor

Thanks for looking into, hopefully this will help to understand the nature of the filter (but it might not be possible to change filters before Hatari 2.5 is released)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants