From 13e0bdb81e32b9bb3951e7713c650262e5324be9 Mon Sep 17 00:00:00 2001 From: MatteoS Date: Mon, 16 Nov 2015 14:30:49 +0100 Subject: [PATCH 1/2] Make music pitch and speed go down on fail (like in osu!) --- .../opsu/audio/MusicController.java | 13 ++++- .../opsu/states/GamePauseMenu.java | 3 +- src/org/newdawn/slick/Music.java | 47 ++++++++++++++++++- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/itdelatrisu/opsu/audio/MusicController.java b/src/itdelatrisu/opsu/audio/MusicController.java index c5aac333..5ecfa76d 100644 --- a/src/itdelatrisu/opsu/audio/MusicController.java +++ b/src/itdelatrisu/opsu/audio/MusicController.java @@ -237,6 +237,15 @@ public class MusicController { player.fade(duration, 0f, true); } + /** + * Fades out the pitch (and speed) of the track. + * @param duration the pitch fade time (in ms) + */ + public static void pitchFadeOut(int duration) { + if (isPlaying()) + player.pitchFade(duration, 0f); + } + /** * Returns the position in the current track, in milliseconds. * If no track is loaded, 0 will be returned. @@ -316,7 +325,7 @@ public class MusicController { * @param pitch the new pitch */ public static void setPitch(float pitch) { - SoundStore.get().setMusicPitch(pitch); + player.setPitch(pitch); } /** @@ -464,4 +473,4 @@ public class MusicController { ErrorHandler.error("Failed to destroy OpenAL.", e, true); } } -} \ No newline at end of file +} diff --git a/src/itdelatrisu/opsu/states/GamePauseMenu.java b/src/itdelatrisu/opsu/states/GamePauseMenu.java index 6a10b965..9bf142ca 100644 --- a/src/itdelatrisu/opsu/states/GamePauseMenu.java +++ b/src/itdelatrisu/opsu/states/GamePauseMenu.java @@ -48,7 +48,7 @@ import org.newdawn.slick.state.transition.FadeOutTransition; */ public class GamePauseMenu extends BasicGameState { /** Music fade-out time, in milliseconds. */ - private static final int FADEOUT_TIME = 1000; + private static final int FADEOUT_TIME = 2000; /** Track position when the pause menu was loaded (for FADEOUT_TIME). */ private long pauseStartTime; @@ -207,6 +207,7 @@ public class GamePauseMenu extends BasicGameState { pauseStartTime = System.currentTimeMillis(); if (gameState.getRestart() == Game.Restart.LOSE) { MusicController.fadeOut(FADEOUT_TIME); + MusicController.pitchFadeOut(FADEOUT_TIME); SoundController.playSound(SoundEffect.FAIL); } else MusicController.pause(); diff --git a/src/org/newdawn/slick/Music.java b/src/org/newdawn/slick/Music.java index 36dd3ca5..7ebad85c 100644 --- a/src/org/newdawn/slick/Music.java +++ b/src/org/newdawn/slick/Music.java @@ -98,7 +98,19 @@ public class Music { private boolean positioning; /** The position that was requested */ private float requiredPosition = -1; - + + /** The pitch of this music */ + private float pitch = 1.0f; + /** Start pitch for fading pitch */ + private float pitchStart; + /** End pitch for fading pitch */ + private float pitchEnd; + /** Countdown for fading pitch */ + private int pitchTime; + /** Duration for fading pitch */ + private int pitchDuration; + + /** * Create and load a piece of music (either OGG or MOD/XM) * @@ -373,6 +385,17 @@ public class Music { SoundStore.get().setCurrentMusicVolume(volume); } } + /** + * Set the pitch of the music as a factor of it's normal pitch + * + * @param pitch The pitch to play music at. + */ + public void setPitch(float pitch) { + this.pitch = pitch; + if (currentMusic == this) { + SoundStore.get().setMusicPitch(pitch); + } + } /** * Get the individual volume of the music @@ -397,6 +420,19 @@ public class Music { fadeTime = duration; } + /** + * Fade the pitch and speed of this music to the pitch specified + * + * @param duration Pitch fade time in milliseconds + * @param endPitch The target pitch (and speed) + */ + public void pitchFade (int duration, float endPitch) { + pitchStart = pitch; + pitchEnd = endPitch; + pitchDuration = duration; + pitchTime = duration; + } + /** * Update the current music applying any effects that need to updated per * tick. @@ -421,6 +457,15 @@ public class Music { float offset = (fadeEndGain - fadeStartGain) * (1 - (fadeTime / (float)fadeDuration)); setVolume(fadeStartGain + offset); } + + if (pitchTime > 0) { + pitchTime -= delta; + if (pitchTime < 0) { + pitchTime = 0; + } + float offset = (pitchEnd - pitchStart) * (1 - (pitchTime / (float)pitchDuration)); + setPitch(pitchStart + offset); + } } /** From d0069c20e5f23d890aa2bc4fd58f114239a47c9a Mon Sep 17 00:00:00 2001 From: MatteoS Date: Mon, 16 Nov 2015 21:21:28 +0100 Subject: [PATCH 2/2] Fix pitch changes (hopefully) --- src/itdelatrisu/opsu/states/Game.java | 3 --- src/itdelatrisu/opsu/states/GamePauseMenu.java | 7 +++++++ src/itdelatrisu/opsu/states/SongMenu.java | 1 + src/org/newdawn/slick/Music.java | 18 +++++++++--------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/itdelatrisu/opsu/states/Game.java b/src/itdelatrisu/opsu/states/Game.java index 0bfa1d54..505f5232 100644 --- a/src/itdelatrisu/opsu/states/Game.java +++ b/src/itdelatrisu/opsu/states/Game.java @@ -1262,9 +1262,6 @@ public class Game extends BasicGameState { // replays if (isReplay) GameMod.loadModState(previousMods); - - // reset playback speed - MusicController.setPitch(1f); } /** diff --git a/src/itdelatrisu/opsu/states/GamePauseMenu.java b/src/itdelatrisu/opsu/states/GamePauseMenu.java index 9bf142ca..1b67da42 100644 --- a/src/itdelatrisu/opsu/states/GamePauseMenu.java +++ b/src/itdelatrisu/opsu/states/GamePauseMenu.java @@ -216,6 +216,13 @@ public class GamePauseMenu extends BasicGameState { backButton.resetHover(); } + @Override + public void leave(GameContainer container, StateBasedGame game) + throws SlickException { + // reset pitch fade out + MusicController.pitchFadeOut(0); + } + /** * Loads all game pause/fail menu images. */ diff --git a/src/itdelatrisu/opsu/states/SongMenu.java b/src/itdelatrisu/opsu/states/SongMenu.java index 91f7a997..77b4e312 100644 --- a/src/itdelatrisu/opsu/states/SongMenu.java +++ b/src/itdelatrisu/opsu/states/SongMenu.java @@ -1118,6 +1118,7 @@ public class SongMenu extends BasicGameState { else if (resetTrack) { MusicController.pause(); MusicController.playAt(MusicController.getBeatmap().previewTime, true); + MusicController.setPitch(1.0f); resetTrack = false; } diff --git a/src/org/newdawn/slick/Music.java b/src/org/newdawn/slick/Music.java index 7ebad85c..b891eb5f 100644 --- a/src/org/newdawn/slick/Music.java +++ b/src/org/newdawn/slick/Music.java @@ -325,6 +325,7 @@ public class Music { currentMusic = this; sound.playAsMusic(pitch, volume, loop); setVolume(volume); + setPitch(pitch); if (requiredPosition != -1) { setPosition(requiredPosition); } @@ -444,6 +445,14 @@ public class Music { return; } + if (pitchTime > 0) { + pitchTime -= delta; + if (pitchTime < 0) { + pitchTime = 0; + } + float offset = (pitchEnd - pitchStart) * (1 - (pitchTime / (float)pitchDuration)); + setPitch(pitchStart + offset); + } if (fadeTime > 0) { fadeTime -= delta; if (fadeTime < 0) { @@ -457,15 +466,6 @@ public class Music { float offset = (fadeEndGain - fadeStartGain) * (1 - (fadeTime / (float)fadeDuration)); setVolume(fadeStartGain + offset); } - - if (pitchTime > 0) { - pitchTime -= delta; - if (pitchTime < 0) { - pitchTime = 0; - } - float offset = (pitchEnd - pitchStart) * (1 - (pitchTime / (float)pitchDuration)); - setPitch(pitchStart + offset); - } } /**