began work on adding object to store audio info
This commit is contained in:
parent
5d9a84e9f1
commit
3ef1e1a9a3
122
core/src/zero1hd/polyjet/audio/AudioInfo.java
Executable file
122
core/src/zero1hd/polyjet/audio/AudioInfo.java
Executable file
@ -0,0 +1,122 @@
|
||||
package zero1hd.polyjet.audio;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.jaudiotagger.audio.AudioFile;
|
||||
import org.jaudiotagger.audio.AudioFileIO;
|
||||
import org.jaudiotagger.audio.exceptions.CannotReadException;
|
||||
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
|
||||
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
|
||||
import org.jaudiotagger.audio.mp3.MP3File;
|
||||
import org.jaudiotagger.audio.wav.WavTag;
|
||||
import org.jaudiotagger.tag.FieldKey;
|
||||
import org.jaudiotagger.tag.TagException;
|
||||
|
||||
import com.badlogic.gdx.Preferences;
|
||||
import com.badlogic.gdx.files.FileHandle;
|
||||
import com.badlogic.gdx.graphics.Pixmap;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
|
||||
public class AudioInfo implements Disposable {
|
||||
private long durationInSeconds;
|
||||
private String songName;
|
||||
private Texture albumCover;
|
||||
private String author;
|
||||
private int previousTop;
|
||||
private int ratedDifficulty;
|
||||
private byte[] albumWorkBytes;
|
||||
private boolean invalidMusic;
|
||||
|
||||
public AudioInfo(FileHandle musicFile, Preferences musicData) {
|
||||
if (musicFile.extension().toLowerCase().equals("mp3")) {
|
||||
MP3File mp3File;
|
||||
try {
|
||||
mp3File = new MP3File(musicFile.file());
|
||||
durationInSeconds = mp3File.getAudioHeader().getTrackLength();
|
||||
|
||||
if (mp3File.getTag() != null && mp3File.getTag().getFirstArtwork() != null) {
|
||||
albumWorkBytes = mp3File.getTag().getFirstArtwork().getBinaryData();
|
||||
}
|
||||
|
||||
songName = mp3File.getTag().getFirst(FieldKey.TITLE);
|
||||
author = mp3File.getTag().getFirst(FieldKey.ARTIST);
|
||||
} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
AudioFile audioFile = AudioFileIO.read(musicFile.file());
|
||||
WavTag wavTag = (WavTag) AudioFileIO.read(musicFile.file()).getTag();
|
||||
songName = wavTag.getFirst(FieldKey.TITLE);
|
||||
author = wavTag.getFirst(FieldKey.ARTIST);
|
||||
durationInSeconds = audioFile.getAudioHeader().getTrackLength();
|
||||
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException
|
||||
| InvalidAudioFrameException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (durationInSeconds > 60 * 5) {
|
||||
invalidMusic = true;
|
||||
}
|
||||
|
||||
if (songName == null || songName.isEmpty()) {
|
||||
songName = musicFile.nameWithoutExtension();
|
||||
}
|
||||
|
||||
previousTop = musicData.getInteger(songName + ":previous top", -1);
|
||||
ratedDifficulty = musicData.getInteger(songName + ":difficulty", -1);
|
||||
|
||||
if (author == null || author.isEmpty()) {
|
||||
author = "N/A";
|
||||
}
|
||||
}
|
||||
|
||||
public void setupTexture(Texture defaultAlbumCover) {
|
||||
if (albumWorkBytes != null) {
|
||||
Pixmap albumCoverFromBytes = new Pixmap(albumWorkBytes, 0, albumWorkBytes.length);
|
||||
albumCover = new Texture(albumCoverFromBytes);
|
||||
albumCoverFromBytes.dispose();
|
||||
} else {
|
||||
this.albumCover = defaultAlbumCover;
|
||||
}
|
||||
}
|
||||
|
||||
public long getDurationInSeconds() {
|
||||
return durationInSeconds;
|
||||
}
|
||||
|
||||
public String getSongName() {
|
||||
return songName;
|
||||
}
|
||||
|
||||
public Texture getAlbumCover() {
|
||||
return albumCover;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public int getPreviousTop() {
|
||||
return previousTop;
|
||||
}
|
||||
|
||||
public int getRatedDifficulty() {
|
||||
return ratedDifficulty;
|
||||
}
|
||||
|
||||
public boolean isInvalidMusic() {
|
||||
return invalidMusic;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
albumCover.dispose();
|
||||
}
|
||||
|
||||
}
|
@ -1,22 +1,8 @@
|
||||
package zero1hd.polyjet.ui.builders;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.jaudiotagger.audio.AudioFile;
|
||||
import org.jaudiotagger.audio.AudioFileIO;
|
||||
import org.jaudiotagger.audio.exceptions.CannotReadException;
|
||||
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
|
||||
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
|
||||
import org.jaudiotagger.audio.mp3.MP3File;
|
||||
import org.jaudiotagger.audio.wav.WavTag;
|
||||
import org.jaudiotagger.tag.FieldKey;
|
||||
import org.jaudiotagger.tag.TagException;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Preferences;
|
||||
import com.badlogic.gdx.files.FileHandle;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.Pixmap;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Button;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Image;
|
||||
@ -27,15 +13,9 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
|
||||
import zero1hd.polyjet.audio.AudioInfo;
|
||||
|
||||
public class MusicSelectable extends Button implements Disposable {
|
||||
private boolean invalidMusic;
|
||||
private long durationInSeconds;
|
||||
private String songName;
|
||||
private Texture albumCover;
|
||||
private String author;
|
||||
private int previousTop;
|
||||
private int ratedDifficulty;
|
||||
private byte[] albumWorkBytes;
|
||||
|
||||
private Image imageIcon;
|
||||
private ScrollText displayName;
|
||||
@ -46,96 +26,52 @@ public class MusicSelectable extends Button implements Disposable {
|
||||
private Skin skin;
|
||||
|
||||
private FileHandle musicFile;
|
||||
|
||||
private Texture defaultAlbumCover;
|
||||
AudioInfo audioInfo;
|
||||
public MusicSelectable(FileHandle musicFile, Preferences musicData, final Skin skin, Texture defaultAlbumC) {
|
||||
super(skin, "info-button");
|
||||
this.skin = skin;
|
||||
|
||||
this.defaultAlbumCover = defaultAlbumC;
|
||||
this.musicFile = musicFile;
|
||||
|
||||
setName(musicFile.name());
|
||||
|
||||
this.albumCover = defaultAlbumC;
|
||||
|
||||
if (musicFile.extension().toLowerCase().equals("mp3")) {
|
||||
MP3File mp3File;
|
||||
|
||||
try {
|
||||
mp3File = new MP3File(musicFile.file());
|
||||
durationInSeconds = mp3File.getAudioHeader().getTrackLength();
|
||||
|
||||
if (mp3File.getTag() != null && mp3File.getTag().getFirstArtwork() != null) {
|
||||
albumWorkBytes = mp3File.getTag().getFirstArtwork().getBinaryData();
|
||||
}
|
||||
|
||||
songName = mp3File.getTag().getFirst(FieldKey.TITLE);
|
||||
author = mp3File.getTag().getFirst(FieldKey.ARTIST);
|
||||
} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
AudioFile audioFile = AudioFileIO.read(musicFile.file());
|
||||
WavTag wavTag = (WavTag) AudioFileIO.read(musicFile.file()).getTag();
|
||||
songName = wavTag.getFirst(FieldKey.TITLE);
|
||||
author = wavTag.getFirst(FieldKey.ARTIST);
|
||||
durationInSeconds = audioFile.getAudioHeader().getTrackLength();
|
||||
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException
|
||||
| InvalidAudioFrameException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (durationInSeconds > 60 * 5) {
|
||||
invalidMusic = true;
|
||||
}
|
||||
|
||||
if (songName == null || songName.isEmpty()) {
|
||||
songName = musicFile.nameWithoutExtension();
|
||||
}
|
||||
|
||||
previousTop = musicData.getInteger(songName + ":previous top", -1);
|
||||
ratedDifficulty = musicData.getInteger(songName + ":difficulty", -1);
|
||||
|
||||
if (author == null || author.isEmpty()) {
|
||||
author = "N/A";
|
||||
}
|
||||
audioInfo = new AudioInfo(musicFile, musicData);
|
||||
|
||||
defaults().pad(10f);
|
||||
}
|
||||
|
||||
|
||||
public void addInfoToPanel(ScrollPane scroller, float coverSize) {
|
||||
displayName = new ScrollText(songName, skin, true, scroller);
|
||||
displayName = new ScrollText(audioInfo.getSongName(), skin, true, scroller);
|
||||
|
||||
defaults().align(Align.top);
|
||||
|
||||
add(displayName).expandX().pad(0).fillX().padTop(10f).top().padBottom(10f);
|
||||
row();
|
||||
|
||||
String formattedTime = "Run time: "+ String.valueOf(durationInSeconds/60) + ":";
|
||||
if (durationInSeconds - (durationInSeconds/60)*60 < 10) {
|
||||
String formattedTime = "Run time: "+ String.valueOf(audioInfo.getDurationInSeconds()/60) + ":";
|
||||
if (audioInfo.getDurationInSeconds() - (audioInfo.getDurationInSeconds()/60)*60 < 10) {
|
||||
formattedTime = formattedTime.concat("0");
|
||||
}
|
||||
|
||||
Table songInfoTable = new Table();
|
||||
|
||||
formattedTime = formattedTime.concat(String.valueOf(durationInSeconds - (durationInSeconds/60)*60));
|
||||
formattedTime = formattedTime.concat(String.valueOf(audioInfo.getDurationInSeconds() - (audioInfo.getDurationInSeconds()/60)*60));
|
||||
runTime = new Label(formattedTime, skin, "sub-font", skin.getColor("default"));
|
||||
songInfoTable.add(runTime).expandX().left();
|
||||
songInfoTable.row();
|
||||
|
||||
authorLabel = new Label("Author: " + author, skin, "sub-font", skin.getColor("default"));
|
||||
authorLabel = new Label("Author: " + audioInfo.getAuthor(), skin, "sub-font", skin.getColor("default"));
|
||||
songInfoTable.add(authorLabel).left();
|
||||
songInfoTable.row();
|
||||
|
||||
previousTopLabel = new Label("High Score: " + (previousTop != -1 ? previousTop : "N/A"), skin, "sub-font", skin.getColor("default"));
|
||||
previousTopLabel = new Label("High Score: " + (audioInfo.getPreviousTop() != -1 ? audioInfo.getPreviousTop() : "N/A"), skin, "sub-font", skin.getColor("default"));
|
||||
songInfoTable.add(previousTopLabel).left();
|
||||
songInfoTable.row();
|
||||
|
||||
ratedDifficultyLabel = new Label("Difficulty: " + (ratedDifficulty != -1 ? ratedDifficulty : "N/A"), skin, "sub-font", skin.getColor("default"));
|
||||
ratedDifficultyLabel = new Label("Difficulty: " + (audioInfo.getRatedDifficulty() != -1 ? audioInfo.getRatedDifficulty() : "N/A"), skin, "sub-font", skin.getColor("default"));
|
||||
songInfoTable.add(ratedDifficultyLabel).left();
|
||||
songInfoTable.row();
|
||||
|
||||
@ -143,42 +79,23 @@ public class MusicSelectable extends Button implements Disposable {
|
||||
add(songInfoTable).expandY().center();
|
||||
row();
|
||||
|
||||
if (albumWorkBytes != null && !invalidMusic) {
|
||||
Pixmap albumArt = new Pixmap(albumWorkBytes, 0, albumWorkBytes.length);
|
||||
|
||||
albumCover = new Texture(albumArt);
|
||||
imageIcon = new Image(albumCover);
|
||||
albumArt.dispose();
|
||||
} else {
|
||||
imageIcon = new Image(albumCover);
|
||||
Gdx.app.debug("UI", "album cover invalid or null for song: " + songName);
|
||||
}
|
||||
audioInfo.setupTexture(defaultAlbumCover);
|
||||
|
||||
if (invalidMusic) {
|
||||
imageIcon = new Image(audioInfo.getAlbumCover());
|
||||
if (audioInfo.isInvalidMusic()) {
|
||||
imageIcon.setColor(Color.RED);
|
||||
}
|
||||
add(imageIcon).expandY().center().pad(15f).size(coverSize);
|
||||
}
|
||||
|
||||
public int getPreviousTop() {
|
||||
return previousTop;
|
||||
}
|
||||
|
||||
public int getRatedDifficulty() {
|
||||
return ratedDifficulty;
|
||||
}
|
||||
|
||||
public Texture getAlbumCover() {
|
||||
return albumCover;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
albumCover.dispose();
|
||||
}
|
||||
|
||||
public FileHandle getMusicFile() {
|
||||
return musicFile;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
audioInfo.dispose();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -33,6 +33,9 @@ public class AnalyzePage extends Page {
|
||||
cameraPos.x = 1.5f*getWidth();
|
||||
this.music = music;
|
||||
audioAnalyzer.startAnalyticalThread(music);
|
||||
|
||||
addActor(songInfo);
|
||||
|
||||
songInfo.add(uiMusicInfo.get(0)).spaceBottom(20f);
|
||||
songInfo.add(uiMusicInfo.pop()).expandX().center();
|
||||
songInfo.row();
|
||||
|
Loading…
Reference in New Issue
Block a user