don't click here

The sound drivers of Sonic the Hedgehog

Discussion in 'General Sonic Discussion' started by Clownacy, Apr 18, 2021.

  1. bookman the stinky

    bookman the stinky

    literal trash Member
    the dong
    find motivation
    This might not be completely on topic, but I think Ristar uses a modification of the S3K driver, with the ability to play back multiple samples. Almost makes me wish they had toyed with that sort of thing sooner, so we'd see better multi-pcm implementations before the end of the system's life cycle. I think the sample output itself is crushed to a certain degree, but I noticed that multiple samples playing on top of each other don't exactly lose quality (rather they're just a bit quieter).
  2. Clownacy


    Tech Member
    It seems to be a bug in the song itself:
    Code (ASM):
    1.    smpsFMAlterVol      8
    2.    dc.b   nG4, $06, nG5, nG4, nRst, $18
    3.    smpsFMAlterVol     -6
    It increases the volume attenuation by 8, but only lowers it by 6 afterwards. Attenuation makes the audio quieter, so what happens is that the channel gets quieter and quieter over time until the value wraps back around to maximum volume. It just takes a while because it has to go from 0 all the way to 256 before it wraps.

    Sonic & Knuckles corrected this by changing the 8 to a 6, bringing it in line with other parts of the song:
    Code (ASM):
    1.    smpsFMAlterVol      6
    2.    dc.b   nG4, $06, nG5, nG4, nRst, $18
    3.    smpsFMAlterVol     -6
    That's not the only bug this song has though - here's one not even S&K fixed:
    Code (ASM):
    1.    smpsFMAlterVol     -$03, $D3
    2.    [...]
    3.    smpsFMAlterVol      $03, $D3
    These commands are used repeatedly on the PSG3 channel, but they aren't smpsPSGAlterVol commands, so they don't work - instead they're just ignored and the volume isn't changed at all.

    The smpsFMAlterVol commands used in the PSG3 channel and the ones used in the FM channels are technically different commands though: notice how one only has one parameter while the other has two? The one with two parameters is command $E5, while the one with one parameter is $E6 - SMPS2ASM just gives them identical names as a form of syntax sugar. smpsPSGAlterVol is $EC.

    In the original SMPS source code, these commands were called CMVADD ($E6), PFVADD, ($E5), and PVADD ($EC). As you can see, the $E5 and $EC commands have very similar names, so you can tell how the developer got them confused.

    It's ironic, too, because PFVADD would have worked: while CMVADD is for changing the volume attenuation of an FM channel, and PVADD is for doing the same to a PSG channel, PFVADD was intended for both. For some reason, S3K's driver just removed its ability to alter the volume of PSG channels.

    The major problem with using PFVADD however is that, like I said, it takes two parameters instead of one. Since the dev intended to use the one-parameter PVADD, this means that the command causes a byte of note data to be skipped every time it's used. See those $D3 values in the above examples? Those are nMaxPSG1 notes. There's also one instance where the command skips an nRst note rest.

    This would have completely broken the song, but, bizarrely, the dev seems to have just worked around the issue by padding-out the song data with more notes: if you restore the skipped notes, then the entire channel becomes desynchronised. This is the case even with the version from the November prototype.

    Buggy command usage like this is actually pretty common: for example, the sound effect for sliding in Oil Ocean uses an FM volume command on a PSG channel. In Sonic 2's driver, this actually works fine, but in S1 and S3K's drivers, it causes the sound to glitch. Likewise, a very similar bug is the reason Wing Fortress's music has a really loud instrument in the Simon Wai prototype.
    Last edited: May 15, 2021
    • Informative Informative x 5
    • List
  3. Clownacy


    Tech Member
    Ristar uses SMPS 68k Type 2. It isn't based on any of the Sonic sound drivers.
    • Informative Informative x 3
    • List
  4. Brainulator


    Regular garden-variety member Member
    Oh, wow. That's pretty interesting. I suspect a very similar bug must have affected Sonic 3D's Green Grove Zone, Act 2 music:
    • Informative Informative x 1
    • List
  5. Chibisteven


    Basically an obscure bug with the melody getting quieter and vanishing completely that occurs if someone leaves the song running in the sound test for a long time or leaves the game unpaused on Green Grove Zone Act 2 for a long period of time.
  6. Clownacy


    Tech Member
    The bug seems to be a problem with the FM2 channel:

    Code (ASM):
    1. Snd_GreenGZ2_FM2:
    2.    smpsPan             panCenter, $00
    3.    smpsSetvoice        $07
    4.    smpsModSet          $07, $01, $03, $05
    5.    smpsFMAlterVol      $FC
    6.    smpsAlterPitch      $0C
    8. Snd_GreenGZ2_Loop05:
    9.    dc.b   nC4, $06, nRst, nG3, nBb3, nRst, nG3, nRst, nC4, nRst, nG3, nRst
    10.    dc.b   nBb3, nBb3, $0C, nG3
    11.    smpsLoop            $00, $03, Snd_GreenGZ2_Loop05
    12.    dc.b   nC4, $06, nRst, nG3, nBb3, nRst, nG3, nRst, nC4, nRst, nG3, nRst
    13.    dc.b   nG3, nG3, nRst, nG3, nRst
    14.    smpsFMAlterVol      $04
    15.    smpsAlterPitch      $F4
    16.    smpsCall            Snd_GreenGZ2_Call05
    17.    smpsCall            Snd_GreenGZ2_Call06
    18.    smpsCall            Snd_GreenGZ2_Call07
    20. Snd_GreenGZ2_Loop06:
    21.    dc.b   nRst, $60
    22.    smpsLoop            $00, $10, Snd_GreenGZ2_Loop06
    23.    smpsCall            Snd_GreenGZ2_Call06
    24.    smpsCall            Snd_GreenGZ2_Call07
    25.    smpsCall            Snd_GreenGZ2_Call05
    26.    smpsSetvoice        $09
    27.    smpsFMAlterVol      $02
    28.    smpsAlterPitch      $0C
    29.    dc.b   nA5, $60, nG5, nF5, smpsNoAttack, nF5, nG5, nF5, nE5, smpsNoAttack, nE5, nA5
    30.    dc.b   nG5, nF5, nFs5, nA5, nAb5, nG5
    31.    smpsFMAlterVol      $FE
    32.    smpsAlterPitch      $F4
    33.    smpsSetvoice        $04
    34.    smpsFMAlterVol      $01
    35.    smpsModSet          $07, $01, $03, $05
    36.    dc.b   nG5, $06, nA5, nB5, nC6, nA5, nB5, nC6, nD6, nB5, nC6, nD6
    37.    dc.b   nE6, nC6, nD6, nE6, nF6
    38.    smpsFMAlterVol      $FF
    39.    smpsCall            Snd_GreenGZ2_Call07
    40.    smpsJump            Snd_GreenGZ2_FM2
    In particular, Snd_GreenGZ2_Call06 increases the volume by 1, but that's okay because Snd_GreenGZ2_Call07 always decreases the volume by 1 afterwards. The problem is that there's one place at the end where Snd_GreenGZ2_Call07 is called without Snd_GreenGZ2_Call06 being called before it, so every loop of the song 'leaks' a volume level.
    Last edited: May 15, 2021
    • Informative Informative x 2
    • List
  7. Sanqui


    Random nobody Member
    Just out of curiosity, how does Sonic Jam handle audio in the included games? I remember hearing the game code is largely macro-translated to the Saturn architecture, but obviously the sound engine would require some special handling. Not to mention it sounds a lil bit different, particularly the sound effects.
  8. Clownacy


    Tech Member
    It's been a while since I looked into it, but I'm pretty sure they just used recordings of the music and SFX. Notice how when you get Speed Sneakers, the song restarts instead of seamlessly speeding up, and there's a big pause whenever a new song starts, almost like the Mega CD seeking to a new CD song?
  9. Sanqui


    Random nobody Member
    Ah, that's of course the obvious telltale sign, but I missed it while checking out a longplay. Sorta disappointing, but the path of least resistance I suppose.

    Thanks for the writeup, by the way, I dig the info.
  10. Really makes me think that Ristar isn't even really based on the Sonic the Hedgehog engine at all...
  11. Clownacy


    Tech Member
    It depends on what counts as 'the Sonic engine', I suppose. I once disassembled parts of Quackshot, and noticed it used a very similar object system to the Sonic games (it had an earlier version of the Sprite Status Table - specifically, the 'routine' variable was an index instead of an offset). Also like Sonic, it used SMPS as well as the level chunk system (I can't remember if it was 256x256 or 128x128).

    I think these games may have just been using a standard game engine that Sega of Japan (or maybe just Yuji Naka) created, which was intended to be used by multiple games. I don't think Ristar or Quackshot is based on Sonic specifically, but I think the three are all based on the same generic game engine. Much like how S1 and Ristar's drivers are based on SMPS 68k, but they aren't based on each other.
    Last edited: May 17, 2021