decided on not using custom tag due to limitations of formatting;

completed (untested) both wav and mp3 metadata objects.
This commit is contained in:
Harrison Deng 2018-07-21 01:11:22 -05:00
parent 0da840b691
commit 7782a6a44b
3 changed files with 86 additions and 163 deletions

View File

@ -27,6 +27,12 @@ public interface AudioMetadata extends Disposable {
*/ */
public String getAuthor(); public String getAuthor();
/**
*
* @return the title of the song in the metadata.
*/
public String getTitle();
/** /**
* *
* @return the length of the song with proper fomatting. * @return the length of the song with proper fomatting.
@ -51,19 +57,6 @@ public interface AudioMetadata extends Disposable {
*/ */
public FileHandle getFileHandle(); public FileHandle getFileHandle();
/**
*
* @return the extra metadata for RhythmBullet.
*/
public RhythmBulletMetadata getRBMetadata();
/**
* This method effectively overwrites the mp3 file by creating a copy of it with the new metadata, and the deleting the old one, and renaming the new on to the old ones name.
* This is the suggested way of doing things by the library author in this thread: https://github.com/mpatric/mp3agic/issues/69
* @return true if succeeded in overwrite, and false if failed.
*/
public boolean flushRBMetadata();
@Override @Override
public void dispose(); public void dispose();
} }

View File

@ -1,7 +1,6 @@
package zero1hd.rhythmbullet.audio; package zero1hd.rhythmbullet.audio;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import org.jaudiotagger.audio.AudioFileIO; import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException; import org.jaudiotagger.audio.exceptions.CannotReadException;
@ -9,41 +8,23 @@ import org.jaudiotagger.audio.exceptions.CannotWriteException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException; import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File; import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.FieldDataInvalidException;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException; import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.TagOptionSingleton;
import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.ID3v23FieldKey; import org.jaudiotagger.tag.id3.ID3v23FieldKey;
import org.jaudiotagger.tag.id3.ID3v23Frame;
import org.jaudiotagger.tag.id3.ID3v23Tag; import org.jaudiotagger.tag.id3.ID3v23Tag;
import org.jaudiotagger.tag.id3.ID3v24FieldKey;
import org.jaudiotagger.tag.id3.ID3v24Tag;
import org.jaudiotagger.tag.id3.framebody.FrameBodyTXXX;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.Json;
public class MP3Metadata implements AudioMetadata { public class MP3Metadata implements AudioMetadata {
private String title; private String title, author, length, genre;
private String author;
private String length;
private String genre;
private Texture albumCover; private Texture albumCover;
private FileHandle fileHandle; private FileHandle fileHandle;
private RhythmBulletMetadata rbMetadata;
private Json json;
private boolean id3v24;
public MP3Metadata(FileHandle fileHandle) { public MP3Metadata(FileHandle fileHandle) {
this.fileHandle = fileHandle; this.fileHandle = fileHandle;
json = new Json();
try { try {
MP3File mp3file = (MP3File) AudioFileIO.read(fileHandle.file()); MP3File mp3file = (MP3File) AudioFileIO.read(fileHandle.file());
ID3v23Tag tag; ID3v23Tag tag;
@ -65,43 +46,8 @@ public class MP3Metadata implements AudioMetadata {
genre = tag.getFirst(ID3v23FieldKey.GENRE); genre = tag.getFirst(ID3v23FieldKey.GENRE);
title = tag.getFirst(ID3v23FieldKey.TITLE); title = tag.getFirst(ID3v23FieldKey.TITLE);
if (tag.hasFrame("TXXX")) { } catch (IOException | CannotWriteException | CannotReadException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
ID3v23Frame frame = null; Gdx.app.error("MP3Metadata", "Failed to read metadata of file: " + fileHandle.name());
if (tag.getFrame("TXXX") instanceof List) {
@SuppressWarnings("unchecked")
List<ID3v23Frame> list = (List<ID3v23Frame>) tag.getFrame("TXXX");
for (int i = 0; i < list.size(); i++) {
if (((FrameBodyTXXX) list.get(i).getBody()).getDescription().equals("RBMD")) {
frame = list.get(i);
break;
}
}
} else {
frame = (ID3v23Frame) tag.getFrame("TXXX");
}
FrameBodyTXXX txxx = (FrameBodyTXXX) frame.getBody();
rbMetadata = json.fromJson(RhythmBulletMetadata.class, new String(Base64Coder.decode(txxx.getText())));
} else {
rbMetadata = new RhythmBulletMetadata();
}
} catch (IOException e) {
Gdx.app.debug("MP3Metadata", "IO error while reading metadata.");
} catch (CannotReadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TagException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ReadOnlyFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAudioFrameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CannotWriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} }
@ -111,36 +57,34 @@ public class MP3Metadata implements AudioMetadata {
try { try {
mp3file = (MP3File) AudioFileIO.read(fileHandle.file()); mp3file = (MP3File) AudioFileIO.read(fileHandle.file());
if (mp3file.hasID3v2Tag()) { byte[] imageData = mp3file.getTag().getFirstArtwork().getBinaryData();
byte[] imageData = mp3file.getID3v2Tag().getFirstArtwork().getBinaryData();
Pixmap pixmap = new Pixmap(imageData, 0, imageData.length); Pixmap pixmap = new Pixmap(imageData, 0, imageData.length);
albumCover = new Texture(pixmap); albumCover = new Texture(pixmap);
pixmap.dispose(); pixmap.dispose();
}
} catch (CannotReadException e) { } catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TagException e) {
e.printStackTrace();
} catch (ReadOnlyFileException e) {
e.printStackTrace();
} catch (InvalidAudioFrameException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override @Override
public void unloadAlbumCover() { public void unloadAlbumCover() {
if (albumCover != null) {
albumCover.dispose(); albumCover.dispose();
albumCover = null; albumCover = null;
} }
}
@Override @Override
public String getAuthor() { public String getAuthor() {
return author; return author;
} }
@Override
public String getTitle() {
return title;
}
@Override @Override
public String getLength() { public String getLength() {
return length; return length;
@ -161,45 +105,10 @@ public class MP3Metadata implements AudioMetadata {
return fileHandle; return fileHandle;
} }
@Override
public RhythmBulletMetadata getRBMetadata() {
return rbMetadata;
}
@Override
public boolean flushRBMetadata() {
MP3File mp3file;
try {
mp3file = (MP3File) AudioFileIO.read(fileHandle.file());
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException
| InvalidAudioFrameException e1) {
e1.printStackTrace();
return false;
}
ID3v23Tag tag = (ID3v23Tag) mp3file.getID3v2Tag();
ID3v23Frame frame = new ID3v23Frame("TXXX");
FrameBodyTXXX txxxBody = new FrameBodyTXXX();
frame.setBody(txxxBody);
txxxBody.setDescription("RBMD");;
txxxBody.setText(Base64Coder.encodeString(json.toJson(rbMetadata)));
try {
tag.setField(frame);
} catch (FieldDataInvalidException e) {
e.printStackTrace();
return false;
}
return true;
}
@Override @Override
public void dispose() { public void dispose() {
if (albumCover != null) { unloadAlbumCover();
albumCover.dispose();
albumCover = null;
}
} }
} }

View File

@ -1,73 +1,94 @@
package zero1hd.rhythmbullet.audio; package zero1hd.rhythmbullet.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.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Json;
public class WAVMetadata implements AudioMetadata { public class WAVMetadata implements AudioMetadata {
private String author, length, genre; private String title, author, length, genre;
private RhythmBulletMetadata rbMetadata;
private Texture albumCover; private Texture albumCover;
private FileHandle fileHandle; private FileHandle fileHandle;
private Json json;
public WAVMetadata(FileHandle fileHandle) {
this.fileHandle = fileHandle;
try {
AudioFile wav = AudioFileIO.read(fileHandle.file());
int lenInSec = wav.getAudioHeader().getTrackLength();
int min = (int) (lenInSec/60);
this.length = (lenInSec/60) + ":" + (lenInSec - (min*60));
Tag tag = wav.getTag();
title = tag.getFirst(FieldKey.TITLE);
author = tag.getFirst(FieldKey.ARTIST);
genre = tag.getFirst(FieldKey.GENRE);
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
Gdx.app.error("WAVMetadata", "Failed to read metadata of file: " + fileHandle.name());
}
}
@Override @Override
public void loadAlbumCover() { public void loadAlbumCover() {
// TODO Auto-generated method stub try {
AudioFile wav = AudioFileIO.read(fileHandle.file());
byte[] imageData = wav.getTag().getFirstArtwork().getBinaryData();
Pixmap pixmap = new Pixmap(imageData, 0, imageData.length);
albumCover = new Texture(pixmap);
pixmap.dispose();
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
e.printStackTrace();
} }
}
@Override @Override
public void unloadAlbumCover() { public void unloadAlbumCover() {
// TODO Auto-generated method stub if (albumCover != null) {
albumCover.dispose();
albumCover = null;
}
} }
@Override @Override
public String getAuthor() { public String getAuthor() {
// TODO Auto-generated method stub return author;
return null; }
@Override
public String getTitle() {
return title;
} }
@Override @Override
public String getLength() { public String getLength() {
// TODO Auto-generated method stub return length;
return null;
} }
@Override @Override
public Texture getAlbumCover() { public Texture getAlbumCover() {
// TODO Auto-generated method stub return albumCover;
return null;
} }
@Override @Override
public String getGenre() { public String getGenre() {
// TODO Auto-generated method stub return genre;
return null;
} }
@Override @Override
public FileHandle getFileHandle() { public FileHandle getFileHandle() {
// TODO Auto-generated method stub return fileHandle;
return null;
} }
@Override
public RhythmBulletMetadata getRBMetadata() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean flushRBMetadata() {
// TODO Auto-generated method stub
return false;
}
@Override @Override
public void dispose() { public void dispose() {
// TODO Auto-generated method stub unloadAlbumCover();
}
} }
}