digital-sound-antiques / emu2413

A YM2413 emulator written in C.
MIT License
74 stars 18 forks source link

Does quality>0 result in no resampling and bad aliasing/imaging? #2

Closed nyanpasu64 closed 4 years ago

nyanpasu64 commented 4 years ago

I'm not too familiar with emu2413, but am working on developing a new tracker which will support VRC7 FM in the future. 0CC-FamiTracker includes a copy of emu2413 from 2004, but I found this repo which seems to be updated.

https://github.com/digital-sound-antiques/emu2413/blob/a0b0c613f0955efc66dff355555e87b82c8a77fb/emu2413.c#L1371-L1388

Looking at master OPLL_calc(), it seems like if opll->quality != 0, you run an unknown number of OPLL cycles (varies with sampling rate), then return the latest sample. Because you do not perform resampling, this will drop synthesized samples (or duplicate them if the speaker sampling rate is over 49716 Hz). Is this right?

Dropping/duplicating audio samples will seriously degrade audio quality. As a resampling method, it converts each discrete-time sample into a continuous-time rectangular non-bandlimited pulse (creating frequency imaging), then samples from that rectangle train (turning the images into aliases). This process generates many unwanted frequencies in the outputted audio.

https://github.com/digital-sound-antiques/emu2413/blob/3226177174e064db2e438227f7864692481a2a21/emu2413.c#L1456-L1472

By comparison, the 2004 version of the library (related to that found in 0CC-FamiTracker) appears to perform linear interpolation, which is less bad for audio quality.

(I think 0CC runs the synth at quality=0, which runs emu2413 at the PC sampling rate, rather than the chip's original rate.)


Also I may use https://www.vogons.org/viewtopic.php?f=9&t=37782 instead. Is that code fast enough for real-time usage?

okaxaki commented 4 years ago

Hi, thank you for your kind comments

The latest emu2413 v0.70, because of significant change of the envelope generator, its output sound becomes worsend in many cases but it becomes more similar to a real chip output. The 2004 version of emu2413 output is too pure.

Anyway, as for resample, the current logic does not drop samples. If it drops sample, resulting output contains noise more.

In OPLL_calc(), it currently does weighted interporation rathar than dropping. As we see the following code, any samples are added to opll->ch_out[i] (never overwritten) and divided by 2 on any update cycles.. So the resulting output is: out(t) = f(t)+1/2f(t-1) + 1/4f(t) + 1/8f(t) ...

https://github.com/digital-sound-antiques/emu2413/blob/a0b0c613f0955efc66dff355555e87b82c8a77fb/emu2413.c#L1354-L1360

This output is LPF-ed, thus it contains less alias noise.

However, I will fix this logic soon because the cut-off frequency is too strong. Recently I connected a real YM2413 chip directly to my mac through USB and I heard that the real output sound is more clear than I thought.

okaxaki commented 4 years ago

As for https://www.vogons.org/viewtopic.php?f=9&t=37782, there are many good OPLx emulators. But they do not directly emulate VRC7/YM2413. Its envelop speed and shape is different from OPLx.

I've been at last back to emu2413 development and planning to polish emu2413 ASAP.

okaxaki commented 4 years ago

I have fixed sample rate conversion (similar alg. to 2004 version) at e35cb25. Comparison movie is here: https://twitter.com/ym2413/status/1185636291519475712

nyanpasu64 commented 4 years ago

Thanks for the update. I can't verify if it works or not for my purposes, since I haven't reached the point in my tracker where I'm actually adding ym2413/vrc7 yet.

okaxaki commented 4 years ago

Thank you. I continue to polish emu2413 here for a while. I hope its sound reaches enough quality when your tracker project reaches at that point.