don't click here

Mega PCM 2 - Ultra-clean PCM playback

Discussion in 'Engineering & Reverse Engineering' started by vladikcomper, May 31, 2024.

  1. vladikcomper

    vladikcomper

    Tech Member
    211
    170
    43
    Sonic Warped
    Mega PCM 2

    Have your ROM hacks suffered from scratchy sample playback 99% of Mega-Drive games are known for? Have you dreamed of ultra-clean high sample rate playback closer to CD quality on real hardware? Let me present to you, Mega PCM 2!

    The following demo of ultra-clean in-game PCM playback at whopping 32 kHz was captured on real hardware:

    (recorded from VA6.5 Model 1 NTSC-U; special thanks to @MDTravis for the recording)

    Mega PCM 2 is a complete re-write of Mega PCM 1.x released back in 2012, which improves upon it in every single aspect. As always, it aims to be user-friendly, easy to use and reliable.

    Feature highlights
    • Ultra-clean PCM playback on real hardware! No more scratchy playback that 99% of games suffer from. Mega PCM 2 implements DMA protection / buffering system to bypass hardware limitations.
    • Pitch and volume control, DAC fade in/out support.
    • Native .WAV file support. Forget about manually specifying pitches: Mega PCM 2 now reads .WAV files natively and plays them at the correct sample rate.
    • Basic SFX support: you can have special "SFX samples" which won't be interrupted by BGM drums (note that Mega PCM 2 is still a one-channel driver and cannot play samples simultaneously);
    • Several supported sample formats: WAV, 8-bit raw PCM, compressed 4-bit DPCM;
    • Unlimited sample size: your samples can be as large as your ROM can fit, forget about banks and other Z80 limitations;
    • Supports both AS and ASM68K assemblers;
    • Playback control: pause, stop, loop, priority;
    • High playback rates:
      • Turbo mode! 8-bit PCM playback at 32 kHz (without pitch and volume effects);
      • 8-bit PCM playback up to 25.1 kHz with pitch and volume effects;
      • 4-bit DPCM playback up to 20.5 kHz with pitch and volume effects.
    Read more in Mega PCM 2's README on Github: https://github.com/vladikcomper/MegaPCM
    It's free and open-source by the way!

    Download

    Mega PCM 2 is bundled as a single .asm file, you can download it here:
    https://github.com/vladikcomper/MegaPCM/releases

    Installation, migration from Mega PCM 1.x and help
     
    Last edited: Jun 1, 2024
    • Useful Useful x 10
    • Like Like x 8
    • Informative Informative x 1
    • List
  2. TDRR

    TDRR

    Member
    This sounds absolutely stunning! Even saying crystal clear falls short. Interested on trying out how 4-bit DPCM sounds now, since the max sample rate for it seems to be higher now.

    Will there be a guide for Sonic 2? I imagine that'd need something like the Clone Driver, though.
     
  3. Chimes

    Chimes

    The One SSG-EG Maniac Member
    934
    643
    93
    What the fuck? How?????
    I thought the DAC couldn't go above 20khz, what???? This is magic!
     
  4. Devon

    Devon

    La mer va embrassé moi et délivré moi lakay. Tech Member
    1,425
    1,743
    93
    your mom
    For a while, I thought the limit was 26 kHz due to a limit with the rate in which the YM2612 could handle register writes, but apparently, vladikcomper's tests have shown otherwise, according to him. I wonder what that's all about...
     
  5. ashthedragon

    ashthedragon

    Sonic Paradise Researcher
    1,442
    86
    28
    Spain
    Sonic Paradise & Sonic Ages
    This is absolutely amazing
     
  6. Clownacy

    Clownacy

    Tech Member
    1,099
    689
    93
    The YM2612 has a sample rate of 52kHz. I never saw anything that would suggest that DAC is limited to 26kHz when reading through Nuked-OPN2, so 52kHz should be the real limit.
     
  7. Blast Brothers

    Blast Brothers

    Member
    132
    77
    28
    I believe the 26kHz number came about because it's half of the max frequency, so any oscillating waveform (like a sine wave) would become inaudible above that point. But I also remember hearing people speak of "rejected writes" to the DAC, so there must have been some other explanation floating around.
     
  8. saxman

    saxman

    Oldbie Tech Member
    There's all kinds of stuff like that. For the longest time, everyone and their brother said there was a hard 4MB ROM limit on the Genesis, never realizing that without add-ons, it could handle 8MB. I have also heard the repeated claim that OPN2 envelope generator on the Genesis has different timing intervals than Yamaha synthesizers (which according to my TX81Z tests from years ago, appears to be incorrect... unless of course Genesis emulators are straight-up inaccurate in that regard). Lota of assumptions are made about hardware; or one guy says the wrong thing, and the claim gets repeated by everyone else.

    Anyway, this is really neat! Never knew myself that this could be achieved. Might have to incorporate bank swapping if all WAV files all the time are to be used. Or does it already support that?
     
  9. ezra

    ezra

    No ticket, no ride. Member
    Absolutely incredible stuff. So honored to have tested this. o7
     
  10. vladikcomper

    vladikcomper

    Tech Member
    211
    170
    43
    Sonic Warped
    26 kHz limitation wasn't true in the end... 10+ years ago someone tested DAC limitations on the hardware, but made a programming mistake which led to incorrect conclusion, so many (myself included) believed in it for years. Like Clownacy said, now that we have projects like Nuked-OPN2 based on actual chip dies, we can be certain about how YM internals work.

    YM2612 is clocked at 7670453.6 Hz (NTSC) and it takes 144 cycles to update all channels, this translates into sampling rate of 7670453.6 / 144 ~= 53267 Hz (53.2 kHz). Yes, both FM and DAC are using the same clock and are refreshed at ~53.2 kHz. That 26 kHz was a wrong assumption that DAC was updated at half the rate (53.2 / 2 ~= 26.6 kHz).

    As you can see from the real hardware capture I've shown, 32 kHz works perfectly fine, there aren't any missed updates. Which is exciting actually, because we can push hardware limitations even further!

    Well, actually, while PCM playback rate is noticeably higher (32 kHz), DPCM is lower compared to Mega PCM 1.x (20.5 kHz). Sample rates are compared here: https://github.com/vladikcomper/Meg...on_from_MegaPCM_1.x.md#supported-sample-rates
    The thing is, I originally designed my streaming system around PCM, and DPCM was really late addition, so there were a few drawbacks. But it sounds crystal-clear as usual.

    I think if I add "Turbo DPCM" mode, it can get closer to 25 kHz (without volume support of course). I had plans for both this and DPCM-HQ (a higher-quality DPCM using an optimized table), but it didn't make it into 2.0 release. I didn't want to delay the release by another year, so I left some neat features for future versions =P

    Yes, Clone Driver is doable. Someone already showed me a quick & dirty Mega PCM 2 on Clone Driver for Sonic Clean Engine (SCE), and it took like half an hour (but that person was skilled of course =P).
     
    • Like Like x 6
    • Informative Informative x 1
    • List
  11. Devon

    Devon

    La mer va embrassé moi et délivré moi lakay. Tech Member
    1,425
    1,743
    93
    your mom
    Cool stuff. Anyways, it is nice to have another PCM driver in the community that circumvents the DMA issue. The improved PCM SFX support is also great as well.
     
  12. RetroKoH

    RetroKoH

    Member
    1,728
    107
    43
    S1Fixed: A successor to ReadySonic
    To say this is impressive is an absolute understatement. Funny, a week ago I was about to go looking for MegaPCM 1.x for a minihack. and now you drop this on us. I feel like a child on Christmas morning.
     
  13. RetroKoH

    RetroKoH

    Member
    1,728
    107
    43
    S1Fixed: A successor to ReadySonic
    I have a small question. Sometimes when editing unrelated code, I get this error in relation to the driver:

    fatal "Tracks for Sonic 1 songs must come after the start of the song"
    fatal error, assembly terminated

    I'm looking at the GitHub for hints on what could be the issue, in case this has already been addressed. Any help is appreciated. Cheers!
     
  14. TDRR

    TDRR

    Member
    I keep getting this as well and I have no idea what is causing it. I usually just add more code or some padding bytes to work around it, but it's not very nice to have to do that, and it adds a lot of clutter.

    EDIT: This seems to have been fixed with this commit. https://github.com/sonicretro/s1disasm/commit/10e7792967a38ed22c7871e77749bb13c45e2ae4
    It was unrelated to MegaPCM 2 after all (which should've been obvious but I'm a bit dumb).
     
    Last edited: Jun 16, 2024
  15. Devon

    Devon

    La mer va embrassé moi et délivré moi lakay. Tech Member
    1,425
    1,743
    93
    your mom
    Yeah, I have gotten that error before from unrelated stuff.
     
  16. vladikcomper

    vladikcomper

    Tech Member
    211
    170
    43
    Sonic Warped
    So speaking of this error, as it's pointed about already, it's unrelated to Mega PCM 2, it was another bug in AS itself, that was thankfully patched quickly after I reported its root cause. Smps2asm definitions in all AS versions of Sonic disassemblies were also updated, so I recommend everyone to apply the same patch (linked in the TDRR's post).

    For anyone wondering about the bug, it's a pretty serious one, AS was broken to a point where it could (under certain conditions) think that "StartOffset > EndOffset" for arbitrary data:

    [​IMG]

    This bug occurs because AS retains EndOffset value from the previous pass.
    • On the first pass, AS evaluates bra as bra.s, so they take 4 bytes and StartOffset = 4, EndOffset = 6
    • On the second pass, AS realizes bra should be bra.w, because label in bra Return is a bit far away (>256 bytes). Instructions take 8 bytes now, so StartOffset = 8 now, but EndOffset = 6, it still wasn't updated from the previous pass by the time check is executed.
    Many comparisons can thus yield incorrect results. AS shouldn't have allowed to resolve forward references from the previous pass in the first place.
     
  17. Kilo

    Kilo

    Deathly afraid of the YM2612 Tech Member
    1,053
    1,042
    93
    Canada
    Sonic 1 Source Code Recreation + Source Code Wiki Page
    People could also just stop using AS and finally switch to ASMX...
     
  18. vladikcomper

    vladikcomper

    Tech Member
    211
    170
    43
    Sonic Warped
    Major Docs Update: DAC fade in/out in SMPS, adding PCM BGMs and more

    Back when Mega PCM 2.0 was released a little over 2 months ago, I didn't document some of its advanced features unfortunately. I was too exhausted after months of hardwork and initial installation guides already took too much time. Now, after a long-needed break, I finally have just enough energy to finish another huge chunk of the docs and hopefully make extended usage more accessible! (I swear writing and testing good documentation as harder than development sometimes)

    So meet advanced docs:
     
    Last edited: Aug 7, 2024