don't click here

Sonic 1 DAC drum samples unleashed!

Discussion in 'Engineering & Reverse Engineering' started by jman2050, Apr 25, 2006.

  1. jman2050

    jman2050

    Teh Sonik Haker Tech Member
    634
    4
    18
    For the record, the S1 sound driver in a proper S1 rom beings at offset $72E7C and is $1760 bytes long. You'll notice in Hivebrain's disassembly that the z80 driver is split into two parts, with 5 bytes defined in between them (look at label Kos_Z80). This is because the Sega sound's location and length is stored in the sound driver, so this allows the Sega sound to be easily relocated without having to mess with the sound driver itself. I digress, but I do bring out that if you plan on changing the sound driver and reinputting it into the rom, you'll either have to put it in as one piece, or split it up again, remove the 5 bytes defining the sega sound's attributes, and arrange it the same way as Hivebrain did. This isn't about that, so I'll leave that aspect up to your own devices.

    Anyway, this is about the DAC drum samples in Sonic 1. The reason I told you about extracting the S1 sound driver is simply because the PCM samples used for the drums is actually located in the sound driver itself! however, it isn't raw PCM data. The z80 ram area wouldn't have enough space to do that properly. Instead, they are in a lossy (I believe) compressed form, and are actually decompressed in real-time by the z80. How does this work?

    First, open a decompressed sound driver (I'll provide one here http://www.cgi101.com/~jman2050/z80_whole_decomp.bin , but if you were able to get the compressed data from the S1 rom, you could easily decompress it using the Koszinski format). You'll notice lots of hex values and crap. What's interesting is that the actual code is actually very small. Less than $100 bytes small actually. The rest of the sound driver is the compressed data. Look at byte $D6. What we have here is a table that's $18 (24) bytes long. Each entry is 8 bytes long, so this table describes exactly 3 DAC drum samples as used by Sonic 1. The format is:

    bytes 1/2 - The location in Z80 memory of the DAC compressed data (little endian)
    bytes 3/4 - The size in bytes of the compressed data (little endian)
    bytes 5 - pitch/sample size
    bytes 6-8 - unused

    As for how the data is compressed, lemme explain that real quick: First, it is a lossy compression. So if you recompress a PCM sample this way, you will lose precision in data. Anyway, what happens is that each compressed data is seperated into nybbles (1 4-bit section of a byte). This first nybble of data is read, and used as an index to a table containing the following data: 0,1,2,4,8,$10,$20,$40,$80,$FF,$FE,$FC,$F8,$F0,$E0,$C0. So if the nybble were equal to F, it'd extract $C0 from the tale. If it were 8, it would extract $80 from the table. Etc. etc. Anyway, there is also another byte of data that we'll call 'd'. At the start of decompression, d is $80. What happens is that d is then added to the data extracted from the table using the nybble. So if the nybble were 4, the 8 would be extracted from the table, then added to d, which is $80, resulting in $88. This result is then put bank into d, then fed into the YM2612 for processing. Then the next nybble is read, the data is extracted from the table, then is added to d (remember, d is now changed because of the previous operation), then is put bank into d, then is fed into the YM2612. This process is repeated until the number of bytes as defined in the table above are read and decompressed.

    So basically, to add your own samples, you'd have to decompress the sound driver, find the table, input your own compressed PCM samples into the driver (you can't use the last few bytes of memory, but you do have about 1KB worth of extra space to use), change the tables to reflect the locations, length, and pitch of the new samples, then somehow recompress the driver and input it back into the rom. And remember, you can change the tables in the z80 using the 68k. in fact, Sonic 1 does this quite a bit when it plays the third drum sample, in order to change the sample/rate pitch.

    For reference, I've uploaded the decompressed raw PCM samples, as well as three wav files of the drum files that are closely approximated in sound to the real thing

    http://www.cgi101.com/~jman2050/dac1d.bin
    http://www.cgi101.com/~jman2050/dac1d.wav
    http://www.cgi101.com/~jman2050/dac2d.bin
    http://www.cgi101.com/~jman2050/dac2d.wav
    http://www.cgi101.com/~jman2050/dac3d.bin
    http://www.cgi101.com/~jman2050/dac3d.wav

    I hope this information will help fellow S1 music hackers in their quest to make awesome sounding music :rolleyes:

    EDIT - And for the record, I'm working on a decompressor and compressor for this, but if anyone else can do it faster than me, be my guest :)
     
  2. Tweaker

    Tweaker

    Banned
    12,387
    2
    0
    You have no fucking clue, sir, what you just did.

    Compressor pls. =O
     
  3. jman2050

    jman2050

    Teh Sonik Haker Tech Member
    634
    4
    18
    http://www.cgi101.com/~jman2050/s1sndcomp.exe

    Decompressor/Compressor for the S1 sample format. arguments are:

    (-d/-c) <input file> <num of bytes> <output file>

    -d to decompress, -c to compress. Num of bytes refers to the number in the INPUT file. Also note that the num of bytes is IN DECIMAL NOT HEX BECAUSE I WAS TOO LAZY TO DO IT IN HEX. That is all
     
  4. Hivebrain

    Hivebrain

    Administrator
    3,047
    154
    43
    53.4N, 1.5W
    Github
    Interesting stuff. I gather this will be useful for porting music more accurately.

    It will probably be necessary to leave the sound driver uncompressed, in order to actually replace the DAC samples with new ones. You can easily load the driver from the ROM to the Z80 as you would any other piece of uncompressed data.
     
  5. Gregster

    Gregster

    Oldbie
    45
    2
    8
    I wonder if eventually we'll be able to use SoundFonts with Sonic ROMs.

    A man can dream.
     
  6. MarkeyJester

    MarkeyJester

    Original, No substitute Resident Jester
    2,192
    406
    63
    Japan
    hmm...

    O.K. I know this guide was posted 2 years ago, but I have to say, Thank you man!!

    I've just understood what you ment, and I've managed to add in the "GO" DAC from Sonic 3 without destroying the other samples.

    Although I don't understand how to use your compressor, but I'm just a beginner at this DAC business, so again Thanks, is there anything new I should know about the DAC's?
     
  7. PsychoSk8r

    PsychoSk8r

    PsychedelAnt | Tone Turner Oldbie
    2,641
    57
    28
    Birmingham, UK
    30 Day Project: Revisited.A New Release!
    Sorry to bump an old thread, but the download link is broken. The Wiki points to here, so I was wondering if anybody had a mirror?
     
  8. Selbi

    Selbi

    The Euphonic Mess Member
    1,497
    48
    28
    Northern Germany
    Sonic ERaZor
    Here: http://selbi.se.funpic.de/s1sndcomp.exe
     
  9. MarkeyJester

    MarkeyJester

    Original, No substitute Resident Jester
    2,192
    406
    63
    Japan
    you might wanna check this out too:

    http://forums.sonicretro.org/index.php?showtopic=6920

    Although the download link to that driver's fucked also, I'd upload one but the link'll be dead in a couple of months after (It really needs a perminant place to be uploaded to).
     
  10. nineko

    nineko

    I am the Holy Cat Tech Member
    6,298
    475
    63
    italy
    Send me the files you want to be hosted and I'll put them on my unlimited webspace.
     
  11. MarkeyJester

    MarkeyJester

    Original, No substitute Resident Jester
    2,192
    406
    63
    Japan
  12. Master3k

    Master3k

    Member
    278
    0
    0
    Shouldn't this program be on the Wiki?

    If it is, disregard/delete this post, but I couldn't find the program anywhere other than in this topic.