floooh / chips-test

Tests and sample code for https://github.com/floooh/chips
MIT License
380 stars 44 forks source link

ay-3-8910 aliasing issue #16

Open nippur72 opened 4 years ago

nippur72 commented 4 years ago

on the CPC, if you type SOUND 1,1,500,15 you hear an high pitched note but actually it should be inaudible (it's a 62 kHz tone). Same if tone is 1,2,3... (any low numbered value).

I think this is due to aliasing generating when downsampling from 62.5 kHz (chip frequency) to 44.1 kHz. The solution is apply a low pass filter to the 62.5 kHz signal before the decimation step.

Unfortunately I'm not good at filter at design, I guess a simple IIR filter should be ok, the only issue is that it has to be variable because you can vary both the chip and the final audio frequency.

nippur72 commented 4 years ago

I applied a low pass filter and the effect is way much less noticeable (it depends on the design of the LP filter).

Basically you calculate the sample at chip frequency instead of audio frequency, and then apply the filter on the sample. At audio frequency you only pick the filtered sample.

Here is an experimental version in my emulator: https://github.com/nippur72/lm80c-emu/blob/master/wasm/chips/ay38910.h#L472-L510

I am not satisfied yet because the filter needs to be calculated offline, making chip instantiation more complex. Perhaps there could be a sort of filter callback ?

Another downside is that it's more CPU intensive and the resulting audio is less rich in higher frequencies.