abelykh0 / esp32-z80emu

Spectrum ZX Emulator on ESP32 VGA32 board
30 stars 4 forks source link

Fix for bad AY-3-891x sound #3

Closed dcrespo3d closed 3 years ago

dcrespo3d commented 3 years ago

Hello,

I have tested your emulator and it has sound, but the melodies are reversed in pitch.

The AY-3-891x chip "pitch" parameter corresponds to the wave period, not its frequency. Your code passes directly pitch as frequency, which is not correct.

For an AY running at 4MHz, reference frequency is 125000 Hz. But on a Spectrum 128K, it runs at 3.5469MHz, so the frequency is 125000 * 3.5469 / 4 = 110840.

You should substitute line 141 in /lib/Sound/ay3-8912-state.cpp from _channel[channel].setFrequency(this->channelFrequency[channel]); to _channel[channel].setFrequency(fixPitch(this->channelFrequency[channel]));

and defining somewhere in your code a fixPitch function like this:

inline int fixPitch(int p)
{
#define FREQUENCY_REFERENCE 110840
    int fixed = (p > 0) ? (FREQUENCY_REFERENCE / p) : 0;
    return fixed;
}

Regards, David

abelykh0 commented 3 years ago

Thank you for your suggestion. Can you please submit a pull request, I will merge it.

abelykh0 commented 3 years ago

Fixed. However other things are still broken, I am in the middle of big refactoring

abelykh0 commented 3 years ago

Everything should work now