Wohlstand / libOPNMIDI

A Software MIDI Synthesizer library with OPN2 (YM2612) emulator
GNU Lesser General Public License v3.0
91 stars 7 forks source link

Waveform clipping #73

Closed freq-mod closed 3 years ago

freq-mod commented 5 years ago

YM2612's built-in DAC has a hilariously shitty dynamic range. If an instrument has more than one carrier operator (algorithms 4-7), and volume is too high, it will be clipped. https://instaud.io/2Wbi -sine, alg=7, total level of all four operators=18 https://instaud.io/2Wbk - the same instument, total level of all operaors raised to 0. It doesn't sound like sine wave anymore, it was clipped to pseudo-square wave. It's a normal, intended behaviour. OPN-BE has it. Deflemask has it. Real Mega Drives also have it most likely. It's a problem of YM2612's, YM2608 or any other FM synths is free of that crap. But libOPNMIDI player seems to "ignore" it. Instruments that are distorted in bank editor play "normally" in opnmidiplay, they're not clipped, they're just louder, no matter what emu core is used. Gens 2.10 doesn't emulate waveform clipping, but MAME and Nuked cores do.

freq-mod commented 5 years ago

Here you can have distorted organ patch to compare how it sounds between bank editor and opnmidiplay - dist organ.zip

Wohlstand commented 5 years ago

What about YM3438? It's improved version of YM2612.

freq-mod commented 5 years ago

I don't know. Wikipedia article on YM2612 in mention of high-volume distortion cites Japanese YM3438 application manual. There is a better way to check if YM3438 does clip waveform so I will look at it now.

freq-mod commented 5 years ago

Just need to download RetroArch...

freq-mod commented 5 years ago

Looks like YM3438 also clips waveforms. Doesn't matter whether ASIC or enhanced, MAME or Nuked.

Wohlstand commented 5 years ago

Recently, @jpcima have added Mame OPNA emulator, and yeah, I hear that instruments aren't clipping on high volume levels in comparison to OPN2. Yeah, but the question to @nukeykt, what about real chip?

nukeykt commented 5 years ago

Both YM2612 and YM3438 use 9-bit accumulator per channel and thus waveform gets clipped.

Wohlstand commented 5 years ago

I meat YM2608 and YM2612/YM3438. As YM2612 and YM3448 are implementing "same".

jpcima commented 5 years ago

@Wohlstand I found MAME YM2608 to compute its channels with 1 more bit of quantization while I imported the panning code. out_fm[ch]>>1 was found where used to be out_fm[ch]>>0, so I changed accordingly.

freq-mod commented 5 years ago

@Wohlstand @jpcima YM2608 and pretty much every FM synth chip except YM2413 used external DAC. YM2608 uses YM3016, a 16-bit floating-point stereo DAC: https://4donline.ihs.com/images/VipMasterIC/IC/YAMA/YAMAS00436/YAMAS00436-1.pdf

It has much higher dynamic range (again, 16-bit) so waveforms don't get clipped I think

Wohlstand commented 5 years ago

About of GENS case, it's obvious it's very outdated and inaccurate, but yeah, we can update it with latest and try to make some fixes on it

nukeykt commented 5 years ago

Checked YM2610 die shot. At first it drops LSB bit of operator output(from 14-bit to 13-bit) and then sums them into 18-bit global accumulators for left and right channels. Then these accummulated values gets clamped to 16-bits. I'm pretty much sure that YM2608 does the same.

freq-mod commented 5 years ago

Hmm... 0006861 might be a reason for library's behaviour. Volume is divided by 2 to eliminate clipping?

Wohlstand commented 5 years ago

Hmm... 0006861 might be a reason for library's behaviour. Volume is divided by 2 to eliminate clipping?

I did this division of final output to prevent common clipping between multiple chips without touching of internal logic which is still be same in a case of per-channel clipping. Also, Nuked OPN2 1.0.7 was 2x louder than all other emulators.

freq-mod commented 5 years ago

While testing VGM converter, I accidentally discovered that when "Win9x" volume model is set in PGEMusplay OPNMIDI settings, waveforms get clipped, just like it's supposed to! Rest of volume models don't do that, and in case of "Generic/Auto" clipping distortion is barely noticeable. Strangely, in VLC plugin all volume models sound the same as "Generic", so the clipping is less pronounced than it should 🤷‍♂

Wohlstand commented 5 years ago

While testing VGM converter, I accidentally discovered that when "Win9x" volume model is set in PGEMusplay OPNMIDI settings, waveforms get clipped, just like it's supposed to!

The reason for that in PGE MusPlay (and MixerX) when you have set music volume, is the 2x factor on music volume which is intended to make music louder and not be too silent, however, with some songs, some GME and ADLMIDI/OPNMIDI songs may get clipped. You can try to reduce the volume level to avoid clipping.

freq-mod commented 5 years ago

When turning volume down, clipping distortion is still there. By "clipping" I mean that, with YM2612, when multiple carriers' TL level is close to 0, sine wave will be clipped to square-ish wave - as I described in original post.

Wohlstand commented 5 years ago

Oh, that, and yeah, the Win9x volume model does louder volume levels while all others are algorithmic with lower volume. To avoid clipping is need to reduce carrier volume in OPN2-BE to avoid that clipping on maximal volume level.

freq-mod commented 5 years ago

Yet clipping seems to be proper behaviour and there is no need of avoiding it - see @nukeykt expanations:

Both YM2612 and YM3438 use 9-bit accumulator per channel and thus waveform gets clipped.

freq-mod commented 3 years ago

OK, disegard that, libOPNMIDI behaviour is right, turns out. It's MIDI volume - only some MIDI use values above 120 that cause clipping.