Skip to content

Commit

Permalink
Resolved some reported issues.
Browse files Browse the repository at this point in the history
- Threaded OGG loading (in addition to MP3) to eliminate delays in song select menu. (reported by xasuma)
- Changed default OSZ unpacking location to a "SongPacks" directory to prevent unintended unpacking. (reported by Lanturn)
- Fixed a null pointer for a corner case in 'getRandomNode()'. (reported by @iceblade112)

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
  • Loading branch information
itdelatrisu committed Jul 15, 2014
1 parent f0f8160 commit 943c2af
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
/.opsu_tmp/
/Screenshots/
/Skins/
/SongPacks/
/Songs/
/.opsu.log
/.opsu.cfg
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -23,7 +23,7 @@ configuration file to the path of the root song directory.

Note that beatmaps are typically delivered as OSZ files. These can be extracted
with any ZIP tool, and opsu! will automatically extract them into the songs
folder if placed in the root directory.
folder if placed in the `SongPacks` directory.

### First Run
The `Music Offset` value will likely need to be adjusted when playing for the
Expand Down
41 changes: 23 additions & 18 deletions src/itdelatrisu/opsu/MusicController.java
Expand Up @@ -56,9 +56,9 @@ public class MusicController {
private static File wavFile;

/**
* Thread for MP3 conversions.
* Thread for loading tracks.
*/
private static Thread conversion;
private static Thread trackLoader;

// This class should not be instantiated.
private MusicController() {}
Expand All @@ -71,11 +71,11 @@ public static void play(final OsuFile osu, final boolean loop) {
boolean play = (lastOsu == null || !osu.audioFilename.equals(lastOsu.audioFilename));
lastOsu = osu;
if (play) {
// TODO: properly interrupt instead of using deprecated conversion.stop();
// interrupt the conversion
if (isConverting())
// conversion.interrupt();
conversion.stop();
// TODO: properly interrupt instead of using deprecated Thread.stop();
// interrupt the conversion/track loading
if (isTrackLoading())
// trackLoader.interrupt();
trackLoader.stop();

if (wavFile != null)
wavFile.delete();
Expand All @@ -85,19 +85,24 @@ public static void play(final OsuFile osu, final boolean loop) {

switch (OsuParser.getExtension(osu.audioFilename.getName())) {
case "ogg":
loadTrack(osu.audioFilename, osu.previewTime, loop);
trackLoader = new Thread() {
@Override
public void run() {
loadTrack(osu.audioFilename, osu.previewTime, loop);
}
};
trackLoader.start();
break;
case "mp3":
// convert MP3 to WAV in a new conversion
conversion = new Thread() {
trackLoader = new Thread() {
@Override
public void run() {
convertMp3(osu.audioFilename);
// if (!Thread.currentThread().isInterrupted())
loadTrack(wavFile, osu.previewTime, loop);
}
};
conversion.start();
trackLoader.start();
break;
default:
break;
Expand Down Expand Up @@ -149,10 +154,10 @@ private static File convertMp3(File file) {
}

/**
* Returns true if a conversion is running.
* Returns true if a track is being loaded.
*/
public static boolean isConverting() {
return (conversion != null && conversion.isAlive());
public static boolean isTrackLoading() {
return (trackLoader != null && trackLoader.isAlive());
}

/**
Expand Down Expand Up @@ -246,11 +251,11 @@ public static boolean setPosition(int position) {

/**
* Gets the length of the track, in milliseconds.
* Returns 0 if no file is loaded or an MP3 is currently being converted.
* Returns 0 if no file is loaded or a track is currently being loaded.
* @author bdk (http://slick.ninjacave.com/forum/viewtopic.php?t=2699)
*/
public static int getTrackLength() {
if (!trackExists() || isConverting())
if (!trackExists() || isTrackLoading())
return 0;

float duration = 0f;
Expand All @@ -273,10 +278,10 @@ public static int getTrackLength() {

/**
* Gets the length of the track as a formatted string (M:SS).
* Returns "--" if an MP3 is currently being converted.
* Returns "--" if a track is currently being loaded.
*/
public static String getTrackLengthString() {
if (isConverting())
if (isTrackLoading())
return "...";

int duration = getTrackLength();
Expand Down
2 changes: 1 addition & 1 deletion src/itdelatrisu/opsu/OsuGroupList.java
Expand Up @@ -135,7 +135,7 @@ public OsuGroupNode getBaseNode(int index) {
*/
public OsuGroupNode getRandomNode() {
OsuGroupNode node = getBaseNode((int) (Math.random() * size()));
if (node.index == expandedIndex) // don't choose an expanded group node
if (node != null && node.index == expandedIndex) // don't choose an expanded group node
node = node.next;
return node;
}
Expand Down
6 changes: 3 additions & 3 deletions src/itdelatrisu/opsu/states/MainMenu.java
Expand Up @@ -188,7 +188,7 @@ else if (backgroundImage != null) {
g.setColor(Utils.COLOR_BLACK_ALPHA);
g.fillRoundRect(width - 168, 54, 148, 5, 4);
g.setColor(Color.white);
if (!MusicController.isConverting())
if (!MusicController.isTrackLoading())
g.fillRoundRect(width - 168, 54,
148f * MusicController.getPosition() / MusicController.getTrackLength(), 5, 4);

Expand All @@ -197,7 +197,7 @@ else if (backgroundImage != null) {
int lineHeight = Utils.FONT_MEDIUM.getLineHeight();
g.drawString(String.format("Loaded %d songs and %d beatmaps.",
Opsu.groups.size(), Opsu.groups.getMapCount()), 25, 25);
if (MusicController.isConverting())
if (MusicController.isTrackLoading())
g.drawString("Track loading...", 25, 25 + lineHeight);
else if (MusicController.trackExists()) {
g.drawString((MusicController.isPlaying()) ? "Now Playing:" : "Paused:", 25, 25 + lineHeight);
Expand Down Expand Up @@ -290,7 +290,7 @@ public void mousePressed(int button, int x, int y) {
if (musicPlay.contains(x, y)) {
if (MusicController.isPlaying())
MusicController.pause();
else if (!MusicController.isConverting())
else if (!MusicController.isTrackLoading())
MusicController.resume();
} else if (musicNext.contains(x, y)) {
SongMenu menu = (SongMenu) game.getState(Opsu.STATE_SONGMENU);
Expand Down
3 changes: 2 additions & 1 deletion src/itdelatrisu/opsu/states/Options.java
Expand Up @@ -1193,7 +1193,8 @@ public static File getOSZDir() {
if (oszDir != null && oszDir.isDirectory())
return oszDir;

oszDir = new File("").getAbsoluteFile();
oszDir = new File("SongPacks/");
oszDir.mkdir();
return oszDir;
}

Expand Down
2 changes: 1 addition & 1 deletion src/itdelatrisu/opsu/states/SongMenu.java
Expand Up @@ -606,7 +606,7 @@ public OsuGroupNode setFocus(OsuGroupNode node, int pos, boolean flag) {
* @param osu the OsuFile to send to the game
*/
private void startGame() {
if (MusicController.isConverting())
if (MusicController.isTrackLoading())
return;

SoundController.playSound(SoundController.SOUND_MENUHIT);
Expand Down

0 comments on commit 943c2af

Please sign in to comment.