From 2588b1e4a32c6a1bc7a192453213e5003f61dfa1 Mon Sep 17 00:00:00 2001 From: Recrown Date: Sun, 8 Jul 2018 18:36:39 -0500 Subject: [PATCH] mp3 metadata complete --- build.gradle | 3 +- .../rhythmbullet/audio/AudioMetadata.java | 41 +++--- .../rhythmbullet/audio/MP3Metadata.java | 132 ++++++++++++++++++ .../audio/RhythmBulletMetadata.java | 29 ++++ .../rhythmbullet/util/Base64Preferences.java | 49 ------- .../rhythmbullet/desktop/audio/MusicList.java | 1 + .../{ => processor}/MP3AudioProcessor.java | 2 +- 7 files changed, 184 insertions(+), 73 deletions(-) create mode 100755 core/src/zero1hd/rhythmbullet/audio/MP3Metadata.java create mode 100755 core/src/zero1hd/rhythmbullet/audio/RhythmBulletMetadata.java delete mode 100755 core/src/zero1hd/rhythmbullet/util/Base64Preferences.java rename desktop/src/zero1hd/rhythmbullet/desktop/audio/{ => processor}/MP3AudioProcessor.java (94%) diff --git a/build.gradle b/build.gradle index 6696155..5e5c43c 100755 --- a/build.gradle +++ b/build.gradle @@ -46,7 +46,6 @@ project(":desktop") { compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion" compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop" - } } @@ -70,7 +69,6 @@ project(":android") { natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64" - } } @@ -84,6 +82,7 @@ project(":core") { compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" compile "com.github.rwl:jtransforms:2.4.0" compile "org.apache.commons:commons-math3:3.2" + compile group: 'com.mpatric', name: 'mp3agic', version: '0.9.1' } } diff --git a/core/src/zero1hd/rhythmbullet/audio/AudioMetadata.java b/core/src/zero1hd/rhythmbullet/audio/AudioMetadata.java index dd776fa..798e27f 100755 --- a/core/src/zero1hd/rhythmbullet/audio/AudioMetadata.java +++ b/core/src/zero1hd/rhythmbullet/audio/AudioMetadata.java @@ -4,29 +4,28 @@ import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.utils.Disposable; -public class AudioMetadata implements Disposable { - private String author; - private String length; - private Texture albumCover; - private String lastPlayed; - private FileHandle fileHandle; +public interface AudioMetadata extends Disposable { - public AudioMetadata(FileHandle fileHandle) { - this.fileHandle = fileHandle; - } + public void loadAlbumCover(); - public void loadAlbumCover() { - - } - - public void unloadAlbumCover() { - - } + public void unloadAlbumCover(); + public String getAuthor(); + + public String getLength(); + + public Texture getAlbumCover(); + + public String getLastPlayed(); + + public String getGenre(); + + public FileHandle getFileHandle(); + + public RhythmBulletMetadata getRBMetadata(); + + public void flushRBMetadata(); + @Override - public void dispose() { - // TODO Auto-generated method stub - } - - + public void dispose(); } diff --git a/core/src/zero1hd/rhythmbullet/audio/MP3Metadata.java b/core/src/zero1hd/rhythmbullet/audio/MP3Metadata.java new file mode 100755 index 0000000..39b4002 --- /dev/null +++ b/core/src/zero1hd/rhythmbullet/audio/MP3Metadata.java @@ -0,0 +1,132 @@ +package zero1hd.rhythmbullet.audio; + +import java.io.IOException; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.files.FileHandle; +import com.badlogic.gdx.graphics.Pixmap; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.utils.Base64Coder; +import com.badlogic.gdx.utils.Json; +import com.mpatric.mp3agic.ID3v1; +import com.mpatric.mp3agic.InvalidDataException; +import com.mpatric.mp3agic.Mp3File; +import com.mpatric.mp3agic.UnsupportedTagException; + +public class MP3Metadata implements AudioMetadata { + private String author = "N/A"; + private String length; + private String genre; + private String lastPlayed; + private Texture albumCover; + private FileHandle fileHandle; + private RhythmBulletMetadata rbMetadata; + private Json json; + public MP3Metadata() { + json = new Json(); + try { + Mp3File mp3file = new Mp3File(fileHandle.file()); + long lenInSec = mp3file.getLengthInSeconds(); + int min = (int) (lenInSec/60); + length = (lenInSec/60) + ":" + (lenInSec - (min*60)); + + if (mp3file.hasId3v1Tag()) { + ID3v1 tag = mp3file.getId3v1Tag(); + author = tag.getArtist(); + genre = tag.getGenreDescription(); + } + + + if (mp3file.hasCustomTag()) { + rbMetadata = json.fromJson(RhythmBulletMetadata.class, new String(Base64Coder.decode(new String(mp3file.getCustomTag())))); + } else { + rbMetadata = new RhythmBulletMetadata(); + mp3file.setCustomTag(Base64Coder.encodeString(json.toJson(rbMetadata)).getBytes()); + } + + } catch (UnsupportedTagException e) { + Gdx.app.debug("MP3Metadata", "Tag not supported by MP3agic."); + } catch (InvalidDataException e) { + Gdx.app.debug("MP3Metadata", "This files metadata has errors."); + } catch (IOException e) { + Gdx.app.debug("MP3Metadata", "IO error while reading metadata."); + } + } + + @Override + public void loadAlbumCover() { + try { + Mp3File mp3file = new Mp3File(fileHandle.file()); + if (mp3file.hasId3v2Tag()) { + byte[] imageData = mp3file.getId3v2Tag().getAlbumImage(); + Pixmap pixmap = new Pixmap(imageData, 0, imageData.length); + albumCover = new Texture(pixmap); + pixmap.dispose(); + } + } catch (UnsupportedTagException | InvalidDataException | IOException e) { + e.printStackTrace(); + } + + } + + @Override + public void unloadAlbumCover() { + albumCover.dispose(); + albumCover = null; + } + + @Override + public String getAuthor() { + return author; + } + + @Override + public String getLength() { + return length; + } + + @Override + public String getGenre() { + return genre; + } + + @Override + public Texture getAlbumCover() { + return albumCover; + } + + @Override + public String getLastPlayed() { + return lastPlayed; + } + + @Override + public FileHandle getFileHandle() { + return fileHandle; + } + + @Override + public RhythmBulletMetadata getRBMetadata() { + return rbMetadata; + } + + @Override + public void flushRBMetadata() { + try { + Mp3File mp3file = new Mp3File(fileHandle.file()); + mp3file.setCustomTag(Base64Coder.encodeString(json.toJson(rbMetadata)).getBytes()); + } catch (UnsupportedTagException | InvalidDataException | IOException e) { + e.printStackTrace(); + } + } + + @Override + public void dispose() { + if (albumCover != null) { + albumCover.dispose(); + albumCover = null; + + } + } + +} diff --git a/core/src/zero1hd/rhythmbullet/audio/RhythmBulletMetadata.java b/core/src/zero1hd/rhythmbullet/audio/RhythmBulletMetadata.java new file mode 100755 index 0000000..b486b19 --- /dev/null +++ b/core/src/zero1hd/rhythmbullet/audio/RhythmBulletMetadata.java @@ -0,0 +1,29 @@ +package zero1hd.rhythmbullet.audio; + +public class RhythmBulletMetadata { + private String highScore = "0", lastPlayed = "N/A", difficulty = "N/A"; + + public void setHighScore(String highScore) { + this.highScore = highScore; + } + + public void setLastPlayed(String lastPlayed) { + this.lastPlayed = lastPlayed; + } + + public void setDifficulty(String difficulty) { + this.difficulty = difficulty; + } + + public String getHighScore() { + return highScore; + } + + public String getDifficulty() { + return difficulty; + } + + public String getLastPlayed() { + return lastPlayed; + } +} diff --git a/core/src/zero1hd/rhythmbullet/util/Base64Preferences.java b/core/src/zero1hd/rhythmbullet/util/Base64Preferences.java deleted file mode 100755 index fd82d94..0000000 --- a/core/src/zero1hd/rhythmbullet/util/Base64Preferences.java +++ /dev/null @@ -1,49 +0,0 @@ -package zero1hd.rhythmbullet.util; - -import java.util.HashMap; - -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.files.FileHandle; -import com.badlogic.gdx.utils.Base64Coder; -import com.badlogic.gdx.utils.Json; - -public class Base64Preferences { - FileHandle base64File; - HashMap prefKeys; - Json json; - - public Base64Preferences(String prefName) { - base64File = Gdx.files.external(".prefs/.priv/" + prefName); - json = new Json(); - - flush(); - } - - public void putString(String key, String string) { - prefKeys.put(key, string); - } - - public void putObj(String key, Object object) { - prefKeys.put(key, json.toJson(object)); - } - - public void writeBase64String(String text) { - base64File.writeString(Base64Coder.encodeString(text), true); - } - - public void writeBase64Object(Object obj) { - base64File.writeString(Base64Coder.encodeString(json.toJson(obj)), true); - } - - @SuppressWarnings("unchecked") - public void flush() { - prefKeys = json.fromJson(HashMap.class, new String(Base64Coder.decode(base64File.readString()))); - if (prefKeys == null) { - prefKeys = new HashMap<>(); - } - base64File.writeString(null, false); - writeBase64Object(json); - } - - -} diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/audio/MusicList.java b/desktop/src/zero1hd/rhythmbullet/desktop/audio/MusicList.java index 187e0cd..e6130d9 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/audio/MusicList.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/audio/MusicList.java @@ -11,6 +11,7 @@ import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Sort; import zero1hd.rhythmbullet.audio.MusicManager; +import zero1hd.rhythmbullet.desktop.audio.processor.MP3AudioProcessor; public class MusicList extends Observable { private Array musicArray; diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/audio/MP3AudioProcessor.java b/desktop/src/zero1hd/rhythmbullet/desktop/audio/processor/MP3AudioProcessor.java similarity index 94% rename from desktop/src/zero1hd/rhythmbullet/desktop/audio/MP3AudioProcessor.java rename to desktop/src/zero1hd/rhythmbullet/desktop/audio/processor/MP3AudioProcessor.java index 881dd51..2581d28 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/audio/MP3AudioProcessor.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/audio/processor/MP3AudioProcessor.java @@ -1,4 +1,4 @@ -package zero1hd.rhythmbullet.desktop.audio; +package zero1hd.rhythmbullet.desktop.audio.processor; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle;