Mega PCM 2 - Ultra-clean PCM playback

Discussion in 'Utilities' started by vladikcomper, May 31, 2024.

  1. vladikcomper

    vladikcomper Well-Known Member Member

    Joined:
    Dec 2, 2009
    Messages:
    421
    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
  2. Red2010 is now

    Red2010 is now A Normal RomHacker with occupations. Member

    Joined:
    Apr 24, 2023
    Messages:
    58
    Location:
    Somewhere in Spain
    JGamer2151 likes this.
  3. vladikcomper

    vladikcomper Well-Known Member Member

    Joined:
    Dec 2, 2009
    Messages:
    421
    Yes, it can. Someone already showed me a quick & dirty Mega PCM on Clone Driver for Sonic Clean Engine (SCE), so it's quite doable. It only took like half an hour, but that person was quite skilled.

    I may consider doing an "official" Clone Driver port sometime in the future.
     
  4. That-One-Mega-Man

    That-One-Mega-Man Doofus Maximus Member

    Joined:
    Jun 18, 2023
    Messages:
    40
    Location:
    Right behind you
    WHAT ABSOLUTE BLACK MAGIC IS THIS
    HOW
     
    Nik Pi likes this.
  5. FerotheZeroFlag

    FerotheZeroFlag Just da motherfucking Great Teacher Onizuka! Member

    Joined:
    Jul 10, 2023
    Messages:
    36
    Came back after a long hiatus to say: THIS IS FUCKING AMAZING, DUDE!!! But i had a problem. After finally implementing this to my rom hack, Fusion refused to load the game, saying: "This is probably not a Genesis/32X ROM!" Just WTF? I made it totally right, and when I loaded my hack, it was supposedly "corrupted". What happened?
     
  6. Red2010 is now

    Red2010 is now A Normal RomHacker with occupations. Member

    Joined:
    Apr 24, 2023
    Messages:
    58
    Location:
    Somewhere in Spain
    I assume you exceeded the weight of a normal Sega Genesis cartridge, you require Mappers for the game to work again in Kega Fusion
     
  7. Devon

    Devon La mer va embrassé moi et délivré moi lakay. Member

    Joined:
    Aug 26, 2013
    Messages:
    1,412
    Location:
    your mom
    Yeah, the maximum ROM size is 4 MiB without mappers (because, by default, the cartridge ROM memory is mapped to the 1st 4 MiB of the 68000 address space). Even then, when you set one up, you have to make sure that the banking is properly configured, because the way the Z80 accesses the ROM is basically via the 68000 bus, so any bank configuration that is set on the 68000 side will also be set for the Z80. Also, IIRC, even then, Fusion limits the size of a ROM file to 6 MiB, even with a mapper (and IIRC, Regen limits it to 5 MiB). On hardware and other emulators, though, the limit is 32 MiB. As great as it is that Mega PCM can even play sampled music at a good quality, you still need to adhere to the ROM size limitations.

    And before anyone mentions it, yes, I am aware that if you properly set up the cartridge hardware, you can go past the 4 MiB space in the 68000 memory without having to deal with bankswitching, up to 10 MiB... but unfortunately, that clashes with the Sega CD and 32X, because they use up that extra space in memory for their own stuff, so I don't think it's even worth bothering looking into that.
     
  8. Red2010 is now

    Red2010 is now A Normal RomHacker with occupations. Member

    Joined:
    Apr 24, 2023
    Messages:
    58
    Location:
    Somewhere in Spain
    Like Devon said. The MegaPCM 2 should not be used for very high quality PCM music. since it will be very fast that you will have to install a Mapper to expand the rom space to 32 MB and even with that it is not enough space for many high quality PCM music.

    If you really want music CD. I would recommend looking elsewhere or trying to turn on the MSU-MD (Mode 1).

    The MegaPCM 2 is best suited for easily porting DAC from other Sonic games and/or samples as DAC. But for PCM music its use is not recommended due to the space it consumes.
     
  9. Devon

    Devon La mer va embrassé moi et délivré moi lakay. Member

    Joined:
    Aug 26, 2013
    Messages:
    1,412
    Location:
    your mom
    I think you can get away with some sampled music if you are clever. For instance, you could have chopped up parts of a larger song, discard any duplicate/redundant parts, and then set up an SMPS track to play the parts however you want. I believe this is what Markey did for Jester's Challenge and Sonic 1 The Next Level for their sampled music (though, I believe Markey handled it on the Z80 level, but nonetheless, the method I described *should* work okay enough).
     
    DeltaW and JGamer2151 like this.
  10. vladikcomper

    vladikcomper Well-Known Member Member

    Joined:
    Dec 2, 2009
    Messages:
    421
    To add to Devon's point, yes, there are quite a few ways to fit more PCM music in. I made a few Sonic hacks myself that relied on high quality PCM music and samples a lot.

    Another efficient way to save a ton of ROM space is 4-bit DPCM, which effectively cuts your sample size in half. Yes, quality takes a small hit depending on sound wave's entropy, but it sounds quite good most of the time, especially considering its "compression rate".

    Mega PCM 2.0 currently supports 4-bit DPCM at up to 20 kHz and I'm working on "DPCM-HQ", a slightly modified version with even better quality. Here's a demo ROM if you're interested: https://drive.google.com/file/d/1ujCNdtqEp-VgXgbr2h7NhepIFpWvfhe1/view?usp=sharing - it compresses 3.8 MiB PCM to just 1.9 MiB without major quality loss. This hopefully will be available in v.2.1 or later.
     
    Nik Pi, JGamer2151, Hame and 2 others like this.
  11. Sonbeta

    Sonbeta Newcomer Trialist

    Joined:
    Jul 2, 2023
    Messages:
    9
    I hope this project is going well for you! You must have worked hard.
     
  12. peachy

    peachy Newcomer Trialist

    Joined:
    May 31, 2023
    Messages:
    7
    Location:
    Australia
    They could've skipped making the Sega CD entirely if you were around back then
     
  13. RealMalachi

    RealMalachi You look like an atheist seeing an angel... Member

    Joined:
    Oct 16, 2022
    Messages:
    20
    Location:
    Australia
    Neat. I had plans to use 2.0 in one project (out of like, more then three) but DPCM-HQ might be viable for another; though I might still have to add NES-DMC support on top of that, for compression reasons

    Probably an annoying suggestion given how DPCM works, but is loop points for PCM songs on the table? To clarify, I mean like how Green Hill has that intro bit at the start of its song that doesn't get looped.
    From what (frankly little) I know about the standard DPCM format this would be somewhat annoying to do, since the difference in data from the start of the loop to the end of the loop would cause disastrous results. The best solution I can think of is calculating the PCMs output without playing it back until you reach the loop point, saving the output and rom bank positions, beginning sample playback as normal, then when it reaches the end working back from that saved loop points output. This prevents having to check if you're at the loop point to save the needed data on every single sample, which may make loop points for turbo pcm mode somewhat viable, and generally not bring down the quality of playback
    Ideally, the PCM loop points would be calculated on the 68K when the sample is being provided via MegaPCM_LoadSampleTable, or optionally pre-calculated, and standard looping would use the same protocol, just with the loop data being at the very start, in order to maintain consistency. Doing the calculations on the Z80 would be very slow and eat up Z80 RAM, though it would make the PCM calculation code easier to copy and paste, since y'know, different CPU architectures and all.
    I think this alongside DPCM-HQ will make full song playback way more appealing then it currently is, though maybe DPCM-HQ alone will be enough? If Nats idea for SONAR is anything to go off of, it could be used for a full game (with a 12MB bank switched rom, but still)
     
  14. vladikcomper

    vladikcomper Well-Known Member Member

    Joined:
    Dec 2, 2009
    Messages:
    421
    For PCM, they are doable, yes, in fact I've already written PCM playback in a way that it has a loop handler with restart location and bank which can be potentially manipulated. What didn't make it into 2.0 release is actual logic to make this work and extra bytes to store a loop point. I have plans to support PCM loops in one form or another in future versions.

    For DPCM, looping will require too much overhead I'm afraid (with the solutions you've described). A much better solution would be to physically split intro and loop into separate DPCM samples and just play them one after the other.

    This brings us to another feature I plan: built-in sample sequencer, where Mega PCM can be programmed to play given samples in order with basic loop and jump commands (like an extremely barebones SMPS track; I think SONAR also implements it).

    I don't have any estimates on when and how any of these features may be implemented though; since Mega PCM is used by other people in very different contexts, I take its "API design", easy of use and compatibility very seriously and I need time to something flexible and durable enough, so when a new version comes out, it will be a drop-in replacement, not something that requires re-working and adapting previous installation.
     
    RealMalachi likes this.
  15. vladikcomper

    vladikcomper Well-Known Member Member

    Joined:
    Dec 2, 2009
    Messages:
    421
    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
    ProjectFM, Selbi, Nik Pi and 3 others like this.