206 lines
5.8 KiB
Java
Executable File

package zero1hd.rhythmbullet.audio;
import java.io.IOException;
import java.util.List;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.CannotWriteException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
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.TagOptionSingleton;
import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.ID3v23FieldKey;
import org.jaudiotagger.tag.id3.ID3v23Frame;
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.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;
public class MP3Metadata implements AudioMetadata {
private String title;
private String author;
private String length;
private String genre;
private Texture albumCover;
private FileHandle fileHandle;
private RhythmBulletMetadata rbMetadata;
private Json json;
private boolean id3v24;
public MP3Metadata(FileHandle fileHandle) {
this.fileHandle = fileHandle;
json = new Json();
try {
MP3File mp3file = (MP3File) AudioFileIO.read(fileHandle.file());
ID3v23Tag tag;
if (mp3file.hasID3v1Tag()) {
mp3file.setID3v2Tag(mp3file.getID3v1Tag());
mp3file.setID3v1Tag(null);
mp3file.commit();
tag = (ID3v23Tag) mp3file.getTagAndConvertOrCreateAndSetDefault();
}
tag = (ID3v23Tag) mp3file.getTagAndConvertOrCreateAndSetDefault();
int lenInSec = mp3file.getAudioHeader().getTrackLength();
int min = (int) (lenInSec/60);
length = (lenInSec/60) + ":" + (lenInSec - (min*60));
author = tag.getFirst(ID3v23FieldKey.ARTIST);
genre = tag.getFirst(ID3v23FieldKey.GENRE);
title = tag.getFirst(ID3v23FieldKey.TITLE);
if (tag.hasFrame("TXXX")) {
ID3v23Frame frame = null;
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();
}
}
@Override
public void loadAlbumCover() {
MP3File mp3file;
try {
mp3file = (MP3File) AudioFileIO.read(fileHandle.file());
if (mp3file.hasID3v2Tag()) {
byte[] imageData = mp3file.getID3v2Tag().getFirstArtwork().getBinaryData();
Pixmap pixmap = new Pixmap(imageData, 0, imageData.length);
albumCover = new Texture(pixmap);
pixmap.dispose();
}
} catch (CannotReadException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TagException e) {
e.printStackTrace();
} catch (ReadOnlyFileException e) {
e.printStackTrace();
} catch (InvalidAudioFrameException 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 FileHandle getFileHandle() {
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
public void dispose() {
if (albumCover != null) {
albumCover.dispose();
albumCover = null;
}
}
}