don't click here

Project SCDPSR

Discussion in 'Engineering & Reverse Engineering' started by Tamkis, Nov 5, 2011.

  1. Tamkis

    Tamkis

    Banned
    116
    0
    0
    Pennsylvania
    Megaman 2: The Robotnik Wars, Unnamed S3&K hack
    If one looks hard enough in the E&RE forums, one can find that many members have ripped the DAC/PCM/PWM samples from various Sonic games into standard .wav files, for use in trackers/sequencers and other things. Such Sonic games include

    Sonic 1
    Sonic 2B/F
    Sonic 3 and its variants
    Knuckles Chaotix (PWM)

    One such classic Sonic game that hasn't had its samples ripped correctly from yet is the elusive Sonic CD. Perfectly timed with the re-release of Sonic CD to modern consoles via ports, I have decided to rip these samples from Sonic CD myself, with the highest-possible sound quality. This project is called SCDPSR, or "Sonic CD PCM Sound Ripping/Research".
    -------------------------

    Research:


    Sonic CD and its betas (510, 712, and 920) store some of its music and samples into a raw type of PCM bank file, instead of in a streaming CD-DA/mp3 audio file.

    Contents of the PCM files:

    510 Beta:
    The 510 beta only has one PCM file, called PCM000.bin, which is used for the SEGA sfx. There might be other PCM samples hidden in other files...

    930 Beta and Final:
    Sonic CD 930 and Sonic CD final probably have identical PCM files, used for the past music and its SMPS-PCM samples; these files are labeled as SNCBNKnB.bin, where "n" is the zone number. They both also have 2 additional files, labeled SNCBNKB1 and SNCBNKB2, theorized to be for unused loops from the Jap OST's Boss musics. Both SNCBNKB files are leftovers from Sonic CD 720.

    The order of the samples are as follows:
    Unique samples (samples unique to the bank)
    Global samples (samples that are in all SNCBNK files, e.g. "Future", "Past", "Alright!")
    Level specific samples (samples that are used as level sfx, such as the bubble break sfx in CCZ)


    720 Beta:
    Sonic CD 720, on the other hand, not only has the PCM files for the past and bosses, but also for every other time period for each Round. These files are labeled SNCBNKn$, where n is the Round number, and $ is a letter representing the time zone. All of the non-past PCM files are theorized to have unused loops for the Jap OST.

    The order of the samples are as follows:
    Unique samples
    Global Samples Pt1 ("Future", "Past")
    Level specific samples (usually in reverse order from Sonic CD FINAL)
    Global samples Pt 2 ("Alright!", "I'm outta here!", "Yes!", "Yeah!")

    SNCBNKB1 & SNCBNKB2 follow the order of Sonic CD FINAL


    List of global samples:
    Future
    Past
    Alright
    I'm outta Here
    Yes
    Yeah


    List of level specific samples:
    Sneeze/Mech thump?
    Bumper
    Glass/Ice
    Amy Love
    Amy Scream


    Pointer information
    Much of the pointer information for the SNCBNK files came from the SCHG's Music Hacking Guide. Kudos to whoever wrote that (Tweaker?) ! The first "pointer" in the PCM table actually indicates how many PCM samples are in the SNCBNK file. Each pointer (except for the last) in the PCM table actually points to another 32-bit pointer, which then points to the starting location of the PCM sample. The last pointer is a direct pointer to the starting location of the first PCM sample. I will eventually add all of this information to the SCHG.

    ------------------

    Ripping:

    Using the pointer information from the SCHG Music Hacking Guide, I extracted each PCM sample into separate files. Each file was converted into a Wave file by putting it into a program called Snatcher PCM2Wav, which can be found here. This program, originally used for converting PCM files from the Sega CD game "Snatcher" into Wave files, works well for many other Sega CD games. Unlike another similar program, called Lunar PCM2Wav, Snatcher PCM2Wav usually converts the samples to the highest-quality Wave file possible. Lunar PCM2Wav was originally used for converting PCM files from the Sega CD game "Lunar 2: Eternal Blue", but at a lower quality than Snatcher PCM2Wav usually does. Unlike Snatcher PCM2Wav, Lunar PCM2Wav can convert the input PCM file into any valid 2-digit Khz frequency (e.g. 16khz). PCM2Wav can be downloaded here. Both have their pros & cons; nevertheless, both are good to use.

    ------------------

    Download:

    The following download contains PCM sample rips from Sonic CD. Most rips are perfect, while others are not. Please read the included readme! Currently, only sample rips from Sonic CD FINAL are included in this version (V0.25). Future rips will include Sonic CD 510 beta and Sonic CD 712 Beta. Sonic CD 920 beta rips might NOT be included, if the SNCBNK files are exactly the same as Sonic CD FINAL.

    Download!
    ------------------

    More info about SMPS-PCM files:
    Spritesmind
    Segaxtreme
    Devster
    ------------------

    Credits:

    Andlabs - Info about SMPS-PCM (see the above links)
    Contributors of the SCHG MHG
    et. al. (see the readme)
    ------------------

    EOF EOP
     
  2. Fred

    Fred

    Taking a break Oldbie
    1,563
    117
    43
    Portugal
    Sonic 3 Unlocked
    I appreciate the effort, but I still don't get why the RAR you posted is half filled with shortcuts.

    Anyway, I don't know if the format contains information about the sampling rate, because it's all over the place. I just listened to all the unused clips from the 712 prototype, and worked out the intended sampling rate by ear:

    Palmtree Panic:
    SNCBNK1A (Present) - 24000 Hz
    SNCBNK1C (Good Future) - 16000 Hz
    SNCBNK1D (Bad Future) - 16000 Hz

    Collision Chaos:
    SNCBNK3A (Present) - 16000 Hz
    SNCBNK3C (Good Future) - 16000 Hz
    SNCBNK3D (Bad Future) - 16000 Hz

    Tidal Tempest:
    SNCBNK4A (Present) - 22050 Hz
    SNCBNK4C (Good Future) - 24000 Hz
    SNCBNK4D (Bad Future) - 24000 Hz

    Quartz Quadrant:
    SNCBNK5A (Present) - 22050 Hz
    SNCBNK5C (Good Future) - 22050 Hz
    SNCBNK5D (Bad Future) - 24000 Hz

    Wacky Workbench:
    SNCBNK6A (Present) - 22050 Hz
    SNCBNK6C (Good Future) - 24000 Hz
    SNCBNK6D (Bad Future) - 24000 Hz

    Stardust Speedway:
    SNCBNK7A (Present) - 22050 Hz
    SNCBNK7C (Good Future) - 24000 Hz
    SNCBNK7D (Bad Future) - 24000 Hz

    Metallic Madness:
    SNCBNK8A (Present) - 24000 Hz
    SNCBNK8C (Good Future) - 24000 Hz
    SNCBNK8D (Bad Future) - 16000 Hz

    SNCBNKB1 (Boss) - 22050 Hz
    SNCBNKB2 (Final Boss) - 16000 Hz

    As a bonus, the SEGA chant from the 510 prototype is at 12000 Hz.
     
  3. Tamkis

    Tamkis

    Banned
    116
    0
    0
    Pennsylvania
    Megaman 2: The Robotnik Wars, Unnamed S3&K hack
    The shortcuts are there to show that those samples are one of the shared samples (e.g. "Yes!" and Amy Love). I did that to save space, instead of making putting, say, the "future" sfx in each folder. More for personal use than anything...

    Maybe in the header of the PCM samples there might be a sampling rate indicator. How else would the Sega CD know how to properly play the files with different sampling rates correctly? I'll look into this

    I knew/guessed that the SEGA chant from the 510 prototype was a different frequency, since almost every other Sonic game iirc uses a similar frequency. But I didn't know that the non-Past SNCBNKs from 712 had different frequencies! That might be problematic for trying to get HQ rips from the 712 beta.

    As quoted from the manual of Snatcher PCM2Wav

    Since in Lunar PCM2Wav, you can change the output frequency, I tried converting some of those files with the frequency you suggested, but the outputted wav files still sounded like geezers or chipmunks. Interesting. Luckily the source (in C) is included with Snatcher PCM2Wav. Maybe somebody can modify the program so that different input frequencies can be outputted correctly. I don't remember much C programming, nor do I understand the contents of the source, even with what I know of C.
     
  4. Fred

    Fred

    Taking a break Oldbie
    1,563
    117
    43
    Portugal
    Sonic 3 Unlocked
    The sampling rate could be hardcoded on a sample-by-sample basis.

    The data in the wavefile would be the same, the only thing different would be the header. I assumed all the program did was wrap the bin file in a Windows wave format header.
     
  5. GerbilSoft

    GerbilSoft

    RickRotate'd. Administrator
    2,971
    76
    28
    USA
    rom-properties
    Sega CD does *not* use standard 8-bit unsigned PCM. If you attempt to play an 8-bit unsigned PCM sample on Sega CD by simply stripping the WAV header, you're going to end up with static.

    Sega CD's PCM format is 8-bit sign/magnitude, which can represent values from -127 to +127. (Unsigned 8-bit is -128 to +127, biased at 0 == 0x80.) Here's a simple conversion table:

    Code (Text):
    1.  
    2. +--------+----------+------+
    3. | Sample |   Bits   | Byte |
    4. +--------+----------+------+
    5. |  -127  | 11111111 | 0xFF |
    6. |  -126  | 11111110 | 0xFE |
    7. |  -125  | 11111101 | 0xFD |
    8. |  -124  | 11111100 | 0xFC |
    9. +--------+----------+------+
    10. |    -3  | 10000011 | 0x83 |
    11. |    -2  | 10000010 | 0x82 |
    12. |    -1  | 10000001 | 0x81 |
    13. |    -0  | 10000000 | 0x80 |
    14. |    +0  | 00000000 | 0x00 |
    15. |    +1  | 00000001 | 0x01 |
    16. |    +2  | 00000010 | 0x02 |
    17. |    +3  | 00000011 | 0x03 |
    18. +--------+----------+------+
    19. |  +124  | 01111100 | 0x7C |
    20. |  +125  | 01111101 | 0x7D |
    21. |  +126  | 01111110 | 0x7E |
    22. |  +127  | 01111111 | 0x7F |
    23. +--------+----------+------+
    24.  
    Note that since this is sign-magnitude representation, there's two zeroes. Also, 0xFF is used as a "repeat" marker of some sort, so -127 isn't actually representable.
     
  6. Chilly Willy

    Chilly Willy

    Tech Member
    751
    11
    18
    Doom 32X
    0xFF tells the PCM chip to load the wave pointer for the channel from the loop pointer. Note that you need enough 0xFF bytes to allow for the fixed point increment added to the wave pointer. So if the channel can step by up to four, you need at least four 0xFF bytes at the end of the sample to make it loop properly.
     
  7. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    1
    0
    Writing my own MD/Genesis sound driver :D
    If I come off as a jerk with this post then I apologize in advance.

    I wrote the stuff on the SCHG and have had 90% of this done already, in a fully correct state, for several months (if not a year) now. The only issue is, as I've repeatedly stated in at least two forums now (here and SpritesMind), figuring out what the correct frequency that the RF5C164(?) chip runs at. I got several answers, but still seeing conflicting information — I can try what I do have and give the results to see which one sounds most correct, though sometime I'd like to see some chip measurements from NTSC hardware in particular, since I'm working with that version of Sonic CD (and even still, it could help improve the accuracy of emulation code, which is always a plus). I also have the driver itself disassembled and half documented.

    Caveat if you're going to go about this the manual way, though: SNCBNKB1 has a sample > 64KB. If you're just reading the data raw this won't be a problem, however within the context of the Mega CD's hardware, it will. I think there's separate code in the driver for handling this.

    Neo: the frequency of each sample has to be determined manually, by analyzing the sound data to find what the effective frequency number for C4 is. I have this as part of my setup. If you'd like, I can upload what I made, however again it's still hindered by inaccurate timing (plus the timing quirks of the Gens chip emulator).

    Also I haven't looked at the prototypes at all :/
     
  8. Shadow Hog

    Shadow Hog

    "I'm a superdog!" Member
    Sorry for being a pedant, but if it's -128 to +127, then technically it's a signed 8-bit number. Unsigned 8-bit is 0 to +255.
     
  9. Fred

    Fred

    Taking a break Oldbie
    1,563
    117
    43
    Portugal
    Sonic 3 Unlocked
    I hereby stand corrected. I immediately noticed this when I opened up the SNCBNK file in a hex editor.

    I was actually wondering how the game does it, whether if each sample has information about its sampling rate, or if the program is hardcoded to play this sample at this frequency, etc.
     
  10. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    1
    0
    Writing my own MD/Genesis sound driver :D
    Nope: the raw notes are taken, offset by some predefined amount that changes throughout the song to make sure they are in range of the chip, then the chip frequency is looked up in an array, possibly(?) added to by some other raw constant, and finally sent to the chip for output. The samples have no inherent rate, so what I was doing was figuring out what frequency would spit them out at either the frequency they're always playing in (drusm, SFX) or the equivalent to C4 (music). I don't know what tools Sega used when making the sound (probably holdovers from their RF5C68-based arcades (the RF5C164 is either a clone of this chip or made with a different fabrication process), like the System 18) so I don't know what input they used.
     
  11. GerbilSoft

    GerbilSoft

    RickRotate'd. Administrator
    2,971
    76
    28
    USA
    rom-properties
    Unsigned refers to the fact that the byte value is treated as unsigned. The waveform is always -128 to +127, but with unsigned 8-bit, it's handled as an unsigned byte, and the "0" point is at 0x80. (Well, it's technically 0 to 255 with centerpoint of 128, but the centerpoint is effectively zero.)

    Signed 8-bit would use standard 2's complement notation instead of biasing. That is, 0 would be 0x00, +127 would be 0x7F, and -128 would be 0x80.

    From Wikipedia:
     
  12. Chilly Willy

    Chilly Willy

    Tech Member
    751
    11
    18
    Doom 32X
    The CD runs off a 12.5 MHz master clock. This is also used for the PCM chip, which divides the clock by 384 for the base frequency used to update the pointers. Updates are ALWAYS at this fixed rate! The way you produce different tones is to change the increment used by the pointers for the channels. Each channel has it's own fixed point increment that can vary between 0 and 31.999755859 (5.11 fixed point number). Adding 1.0 each time will play the sample at the "natural" rate - the same sample rate as the clock (12.5M/384). Adding 2.0 each time will play the sample one octave higher, and adding 0.5 will play the sample one octave lower.
     
  13. ICEknight

    ICEknight

    Researcher Researcher
    Doesn't the game itself get confused about it sometimes, in the sound test?

    I remember it occasionally playing them in different pitches than usual, and even mixing parts of adjacent samples for some odd reason.


    PAL version, by the way.
     
  14. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    1
    0
    Writing my own MD/Genesis sound driver :D
    IDK, but the routine for determining chip frequency is simple and is the same for YM2612 and SN76489 SMPS (and even the non-SMPS games used by arcade games, from which SMPS evolved from): there are two effects/coordination flags; I'll use $E0 and $E1. $E0 adds to the note value taken from the song, and $E1 adds to the chip frequency. So the routine is
    1. Take note value from song
    2. Add value of $E0 coordination flag — the note offset
    3. Look up new value in frequency table to get chip frequency
    4. Add value of $E1 coordination flag — the frequency offset
    5. Send result to chip