music refresh works; minor visualizer logic change;

This commit is contained in:
Harrison Deng 2018-08-24 01:29:10 -05:00
parent a6dc7c2559
commit b0b7be9141
7 changed files with 61 additions and 30 deletions

View File

@ -16,7 +16,7 @@ import zero1hd.rhythmbullet.audio.metadata.WAVMetadata;
public class AudioMetadataController extends Observable implements Disposable, Observer { public class AudioMetadataController extends Observable implements Disposable, Observer {
private MusicList musicList; private MusicList musicList;
private Array<AudioMetadata> metadataArray; private volatile Array<AudioMetadata> metadataArray;
private MetadataLoadingThread loadingThread; private MetadataLoadingThread loadingThread;
private volatile boolean searching; private volatile boolean searching;
private Comparator<AudioMetadata> metadataComparer; private Comparator<AudioMetadata> metadataComparer;
@ -54,7 +54,7 @@ public class AudioMetadataController extends Observable implements Disposable, O
* @return whether or not both sizes are equal. * @return whether or not both sizes are equal.
*/ */
public boolean isSameSizeMusicList() { public boolean isSameSizeMusicList() {
return (metadataArray.size == musicList.getMusicArray().size); return (metadataArray.size == musicList.getTotal());
} }
@Override @Override
@ -79,7 +79,7 @@ public class AudioMetadataController extends Observable implements Disposable, O
return metadataArray.get(i); return metadataArray.get(i);
} }
} }
return null; throw new IllegalArgumentException("Couldn't find file " + filehandle.name());
} }
public boolean isSearching() { public boolean isSearching() {
@ -88,17 +88,25 @@ public class AudioMetadataController extends Observable implements Disposable, O
private class MetadataLoadingThread implements Runnable { private class MetadataLoadingThread implements Runnable {
private Thread thread; private Thread thread;
private String name = "Metadata-Load"; private String name = "AudioMetadata-Load";
private volatile boolean work = true; private volatile boolean work = true;
@Override @Override
public void run() { public void run() {
Gdx.app.debug(name, "loading..."); Gdx.app.debug(name, "loading...");
clear(); clear();
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
searching = true; searching = true;
Array<AudioMetadata> tempMetadataArray = new Array<>(); Array<AudioMetadata> tempMetadataArray = new Array<>();
for (int i = 0; i < musicList.getTotal() && work; i++) { for (int i = 0; i < musicList.getTotal() && work; i++) {
FileHandle musicFile = musicList.getMusicArray().get(i); FileHandle musicFile = musicList.getAudioFileHandle(i);
if (musicFile == null) return;
switch (SupportedFormats.valueOf(musicFile.extension().toUpperCase())) { switch (SupportedFormats.valueOf(musicFile.extension().toUpperCase())) {
case MP3: case MP3:
tempMetadataArray.add(new MP3Metadata(musicFile)); tempMetadataArray.add(new MP3Metadata(musicFile));
@ -145,9 +153,14 @@ public class AudioMetadataController extends Observable implements Disposable, O
} }
public void clear() { public void clear() {
for (int i = 0; i < metadataArray.size; i++) { Gdx.app.postRunnable(() -> {
metadataArray.get(i).dispose(); for (int i = 0; i < metadataArray.size; i++) {
} metadataArray.get(i).dispose();
metadataArray.clear(); }
metadataArray.clear();
synchronized (loadingThread) {
loadingThread.notify();
}
});
} }
} }

View File

@ -46,11 +46,14 @@ public class MusicController extends Observable implements OnCompletionListener,
*/ */
public void play() { public void play() {
if (music != null) { if (music != null) {
Gdx.app.debug("MusicController", "Playing from MLC."); Gdx.app.debug("MusicController", "Playing from controller.");
music.play(); music.play();
music.setVolume(prefs.getFloat("music vol", 1f)); music.setVolume(prefs.getFloat("music vol", 1f));
setChanged(); setChanged();
notifyObservers(states.PLAYING); notifyObservers(states.PLAYING);
} else {
Gdx.app.debug("MusicController", "Music isn't loaded!");
Thread.dumpStack();
} }
} }
@ -78,7 +81,7 @@ public class MusicController extends Observable implements OnCompletionListener,
* @param fileHandle to use. * @param fileHandle to use.
*/ */
public void setMusicByFileHandle(FileHandle fileHandle) { public void setMusicByFileHandle(FileHandle fileHandle) {
setMusicByIndex(musicList.getMusicArray().indexOf(fileHandle, true)); setMusicByIndex(musicList.getIndexOfFileHandle(fileHandle));
} }
/** /**
@ -148,7 +151,7 @@ public class MusicController extends Observable implements OnCompletionListener,
* Loads the current selected song. * Loads the current selected song.
*/ */
public void loadMusic() { public void loadMusic() {
Gdx.app.debug("MusicListController", "music is being loaded and listeners are being notified."); Gdx.app.debug("MusicListController", "music is being loaded from music list with " + musicList.getTotal() + " songs.");
boolean playing = isPlaying(); boolean playing = isPlaying();
musicHeader = null; musicHeader = null;
if (music != null) { if (music != null) {
@ -161,13 +164,15 @@ public class MusicController extends Observable implements OnCompletionListener,
currentlyPlayingIndex = 0; currentlyPlayingIndex = 0;
} }
if (musicList.getTotal() != 0) { if (musicList.getTotal() != 0) {
this.music = Gdx.audio.newMusic(musicList.getMusicArray().get(currentlyPlayingIndex)); FileHandle musicFile = musicList.getAudioFileHandle(currentlyPlayingIndex);
if (musicFile == null) return;
this.music = Gdx.audio.newMusic(musicFile);
music.setOnCompletionListener(this); music.setOnCompletionListener(this);
setChanged(); setChanged();
notifyObservers(states.LOADED); notifyObservers(states.LOADED);
if (playing) { if (playing || autoPlay) {
play(); play();
} }
} }
@ -199,9 +204,6 @@ public class MusicController extends Observable implements OnCompletionListener,
shuffle(); shuffle();
} }
loadMusic(); loadMusic();
if (autoPlay) {
play();
}
} }
} }
} }

View File

@ -68,8 +68,9 @@ public class MusicList extends Observable {
} }
public void setSearchPath(String searchPath) { public void setSearchPath(String searchPath) {
setChanged(); if (this.searchPath != null && this.searchPath.equals(searchPath)) return;
this.searchPath = searchPath; this.searchPath = searchPath;
setChanged();
} }
/** /**
@ -120,8 +121,19 @@ public class MusicList extends Observable {
return musicList.get(index); return musicList.get(index);
} }
public Array<FileHandle> getMusicArray() { public FileHandle getAudioFileHandle(int index) {
return musicList; FileHandle file = musicList.get(index);
if (file != null) {
return file;
}
asyncSearch(true);
return null;
}
public int getIndexOfFileHandle(FileHandle file) {
return musicList.indexOf(file, true);
} }
public boolean isSearched() { public boolean isSearched() {

View File

@ -18,6 +18,7 @@ public interface AudioMetadata extends Disposable {
/** /**
* Unloads album art from memory. * Unloads album art from memory.
* Requires OpenGL context.
*/ */
public void unloadAlbumCover(); public void unloadAlbumCover();

View File

@ -81,13 +81,13 @@ public class DoubleHorizontalVisualizer implements Disposable {
pixelsMoved = MathUtils.floor(difference*targetDelta*barChangeRate); pixelsMoved = MathUtils.floor(difference*targetDelta*barChangeRate);
if (pixelsMoved >= 0) { if (pixelsMoved >= 0) {
if (barHeights[bar] + pixelsMoved > amplitudes[bar]) { if (barHeights[bar] + pixelsMoved > amplitudes[bar]) {
barHeights[bar] = (int) amplitudes[bar]; barHeights[bar] += MathUtils.round(difference*targetDelta);
} else { } else {
barHeights[bar] += pixelsMoved; barHeights[bar] += pixelsMoved;
} }
} else { } else {
if (barHeights[bar] + pixelsMoved < amplitudes[bar]) { if (barHeights[bar] + pixelsMoved < amplitudes[bar]) {
barHeights[bar] = (int) amplitudes[bar]; barHeights[bar] += MathUtils.round(difference*targetDelta);
} else { } else {
barHeights[bar] += pixelsMoved; barHeights[bar] += pixelsMoved;
} }

View File

@ -28,7 +28,7 @@ import zero1hd.rhythmbullet.util.ScreenConfiguration;
public class MainPage extends Page implements Observer { public class MainPage extends Page implements Observer {
private MusicController mc; private MusicController mc;
private AudioMetadataController mmc; private AudioMetadataController amc;
private Label versionLabel; private Label versionLabel;
private Image title; private Image title;
@ -47,8 +47,8 @@ public class MainPage extends Page implements Observer {
super(0, 0); super(0, 0);
this.mc = musicController; this.mc = musicController;
this.mc.addObserver(this); this.mc.addObserver(this);
this.mmc = mmc; this.amc = mmc;
this.mmc.addObserver(this); this.amc.addObserver(this);
dhv = new DoubleHorizontalVisualizer((int) getWidth(), (int) 0, getHeight(), 0, screenConfiguration.getTargetFramesPerSecond(), mc, new PCMObtainer(mc)); dhv = new DoubleHorizontalVisualizer((int) getWidth(), (int) 0, getHeight(), 0, screenConfiguration.getTargetFramesPerSecond(), mc, new PCMObtainer(mc));
dhv.setPosition(0, (int) ((getHeight() - dhv.getHeight())/2f)); dhv.setPosition(0, (int) ((getHeight() - dhv.getHeight())/2f));
@ -122,13 +122,15 @@ public class MainPage extends Page implements Observer {
@Override @Override
public void update(Observable o, Object arg) { public void update(Observable o, Object arg) {
if (o == mc) { if (o == mc) {
if (mmc.isSameSizeMusicList()) { if (amc.isSameSizeMusicList()) {
scrollText.setText("Currently playing: " + mmc.getAudioMetadata(mc.getCurrentMusicFileHandle()).getTitle(), null); scrollText.setText("Currently playing: " + amc.getAudioMetadata(mc.getCurrentMusicFileHandle()).getTitle(), null);
} else { } else {
scrollText.setText("Currently playing: " + mc.getCurrentMusicFileHandle().nameWithoutExtension().replace('_', ' '), null); scrollText.setText("Currently playing: " + mc.getCurrentMusicFileHandle().nameWithoutExtension().replace('_', ' '), null);
} }
} else if (o == mmc) { } else if (o == amc) {
scrollText.setText("Currently playing: " + mmc.getAudioMetadata(mc.getCurrentMusicFileHandle()).getTitle(), null); if (amc.size() != 0) {
scrollText.setText("Currently playing: " + amc.getAudioMetadata(mc.getCurrentMusicFileHandle()).getTitle(), null);
}
} }
} }
} }

View File

@ -230,8 +230,9 @@ public class MusicSelectionPage extends Page implements Observer {
if (o == mmc) { if (o == mmc) {
selectionLoaderThread.start(); selectionLoaderThread.start();
} else if (o == mc) { } else if (o == mc) {
if (mc.getMusicList().getTotal() == selectables.size() && mc.getCurrentMusicFileHandle() != selectables.getChecked().getMetadata().getFileHandle()) { if (selectables.getChecked() != null && mc.getMusicList().getTotal() == selectables.size() && mc.getCurrentMusicFileHandle() != selectables.getChecked().getMetadata().getFileHandle()) {
selectables.setChecked(mc.getCurrentMusicFileHandle()); selectables.setChecked(mc.getCurrentMusicFileHandle());
scrollPane.scrollTo(selectables.getChecked().getX(), selectables.getChecked().getY(), selectables.getChecked().getWidth(), selectables.getChecked().getHeight());
} }
} else if (o == mc.getMusicList()) { } else if (o == mc.getMusicList()) {
if (arg == mc.getMusicList().states.LOADING) { if (arg == mc.getMusicList().states.LOADING) {