hex007 / freej2me

A free J2ME emulator with libretro, awt and sdl2 frontends.
Other
469 stars 72 forks source link

WAV sounds play only once on Diamond Rush #127

Closed Nokia64 closed 2 years ago

Nokia64 commented 2 years ago

Gameloft's Diamond Rush (tested version 240x320 v1.0.1,md5: 9cab3f7319f2f4853040b466884a7514 on FreeJ2ME f9f91e57e01ccd34438a1b509397b7bc4e1dd233) plays several WAV sound samples at certain gameplay events. Under FreeJ2ME each of the sounds plays only once, after what they stop working

Upon a closer look at the playback code in FreeJ2ME side's, found out the issue is related to the subsequent start() calls not rewinding the Clip back https://github.com/hex007/freej2me/blob/f9f91e57e01ccd34438a1b509397b7bc4e1dd233/src/org/recompile/mobile/PlatformPlayer.java#L287-L294 The actual rewinding is only performed while the player is still running but not after playback had finished, making the sound effectively unplayable after a full playback

This is the behaviour of the start() method as described by the MIDP-2.0 javadocs:

Starts the Player as soon as possible. If the Player was previously stopped by calling stop, it will resume playback from where it was previously stopped. If the Player has reached the end of media, calling start will automatically start the playback from the start of the media. [...] If start is called when the Player is in the STARTED state, the request will be ignored. _(From:https://nikita36078.github.io/J2ME_Docs/docs/midp-2.0/javax/microedition/media/Player.html#start())_

I propose the following patch for the start() method to better match the specification (and fix this game 😉):

public void start()
{
    if(isRunning()) { return; } //Ignore playback requests if the Player is still running
    if(wavClip.getFramePosition() == wavClip.getFrameLength()) { //If the end of media was reached, rewind it back
        wavClip.setFramePosition(0);
    }
    time = wavClip.getMicrosecondPosition();
    wavClip.start();
    state = Player.STARTED;
    notifyListeners(PlayerListener.STARTED, time);
}

If it seems okay I'll proceed opening a pull request with it Thank you!

recompileorg commented 2 years ago

Looks okay, there are only two chances I'd make and they're mostly aesthetic. I'd change the == to >=, just to be defensive, and I'd move the brace on that same line to it's own line, to match the modified Allman style used in the rest of the project.