aboutsummaryrefslogtreecommitdiff
path: root/src/github/daneren2005
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2014-09-20 08:03:52 -0700
committerScott Jackson <daneren2005@gmail.com>2014-09-20 08:03:52 -0700
commit88c9b7050ce03a4a9bd37f0238dff56fb5c514f4 (patch)
tree39090997b2c8a7e12c7945e77232313ef41fb05e /src/github/daneren2005
parenta3f983629f092d880cc2861bb57cf101bbe511b2 (diff)
downloaddsub-88c9b7050ce03a4a9bd37f0238dff56fb5c514f4.tar.gz
dsub-88c9b7050ce03a4a9bd37f0238dff56fb5c514f4.tar.bz2
dsub-88c9b7050ce03a4a9bd37f0238dff56fb5c514f4.zip
Add tags lib from adrian-bl/vanilla
Diffstat (limited to 'src/github/daneren2005')
-rw-r--r--src/github/daneren2005/dsub/util/tags/Bastp.java85
-rw-r--r--src/github/daneren2005/dsub/util/tags/BastpUtil.java78
-rw-r--r--src/github/daneren2005/dsub/util/tags/Common.java111
-rw-r--r--src/github/daneren2005/dsub/util/tags/FlacFile.java85
-rw-r--r--src/github/daneren2005/dsub/util/tags/ID3v2File.java148
-rw-r--r--src/github/daneren2005/dsub/util/tags/LameHeader.java70
-rw-r--r--src/github/daneren2005/dsub/util/tags/OggFile.java114
7 files changed, 691 insertions, 0 deletions
diff --git a/src/github/daneren2005/dsub/util/tags/Bastp.java b/src/github/daneren2005/dsub/util/tags/Bastp.java
new file mode 100644
index 00000000..aa0a2e25
--- /dev/null
+++ b/src/github/daneren2005/dsub/util/tags/Bastp.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+package github.daneren2005.dsub.util.tags;
+
+import java.io.RandomAccessFile;
+import java.io.IOException;
+import java.util.HashMap;
+
+
+public class Bastp {
+
+ public Bastp() {
+ }
+
+ public HashMap getTags(String fname) {
+ HashMap tags = new HashMap();
+ try {
+ RandomAccessFile ra = new RandomAccessFile(fname, "r");
+ tags = getTags(ra);
+ ra.close();
+ }
+ catch(Exception e) {
+ /* we dont' care much: SOMETHING went wrong. d'oh! */
+ }
+
+ return tags;
+ }
+
+ public HashMap getTags(RandomAccessFile s) {
+ HashMap tags = new HashMap();
+ byte[] file_ff = new byte[4];
+
+ try {
+ s.read(file_ff);
+ String magic = new String(file_ff);
+ if(magic.equals("fLaC")) {
+ tags = (new FlacFile()).getTags(s);
+ }
+ else if(magic.equals("OggS")) {
+ tags = (new OggFile()).getTags(s);
+ }
+ else if(file_ff[0] == -1 && file_ff[1] == -5) { /* aka 0xfffb in real languages */
+ tags = (new LameHeader()).getTags(s);
+ }
+ else if(magic.substring(0,3).equals("ID3")) {
+ tags = (new ID3v2File()).getTags(s);
+ if(tags.containsKey("_hdrlen")) {
+ Long hlen = Long.parseLong( tags.get("_hdrlen").toString(), 10 );
+ HashMap lameInfo = (new LameHeader()).parseLameHeader(s, hlen);
+ /* add gain tags if not already present */
+ inheritTag("REPLAYGAIN_TRACK_GAIN", lameInfo, tags);
+ inheritTag("REPLAYGAIN_ALBUM_GAIN", lameInfo, tags);
+ }
+ }
+ tags.put("_magic", magic);
+ }
+ catch (IOException e) {
+ }
+ return tags;
+ }
+
+ private void inheritTag(String key, HashMap from, HashMap to) {
+ if(!to.containsKey(key) && from.containsKey(key)) {
+ to.put(key, from.get(key));
+ }
+ }
+
+}
+
diff --git a/src/github/daneren2005/dsub/util/tags/BastpUtil.java b/src/github/daneren2005/dsub/util/tags/BastpUtil.java
new file mode 100644
index 00000000..99279f62
--- /dev/null
+++ b/src/github/daneren2005/dsub/util/tags/BastpUtil.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package github.daneren2005.dsub.util.tags;
+
+import android.util.LruCache;
+import java.util.HashMap;
+import java.util.Vector;
+
+public class BastpUtil {
+ private RGLruCache rgCache;
+
+ public BastpUtil() {
+ rgCache = new RGLruCache(16); /* Cache up to 16 entries */
+ }
+
+
+ /** Returns the ReplayGain values of 'path' as <track,album>
+ */
+ public float[] getReplayGainValues(String path) {
+ float[] cached = rgCache.get(path);
+
+ if(cached == null) {
+ cached = getReplayGainValuesFromFile(path);
+ rgCache.put(path, cached);
+ }
+ return cached;
+ }
+
+
+
+ /** Parse given file and return track,album replay gain values
+ */
+ private float[] getReplayGainValuesFromFile(String path) {
+ String[] keys = { "REPLAYGAIN_TRACK_GAIN", "REPLAYGAIN_ALBUM_GAIN" };
+ float[] adjust= { 0f , 0f };
+ HashMap tags = (new Bastp()).getTags(path);
+
+ for (int i=0; i<keys.length; i++) {
+ String curKey = keys[i];
+ if(tags.containsKey(curKey)) {
+ String rg_raw = (String)((Vector)tags.get(curKey)).get(0);
+ String rg_numonly = "";
+ float rg_float = 0f;
+ try {
+ String nums = rg_raw.replaceAll("[^0-9.-]","");
+ rg_float = Float.parseFloat(nums);
+ } catch(Exception e) {}
+ adjust[i] = rg_float;
+ }
+ }
+ return adjust;
+ }
+
+ /** LRU cache for ReplayGain values
+ */
+ private class RGLruCache extends LruCache<String, float[]> {
+ public RGLruCache(int size) {
+ super(size);
+ }
+ }
+
+}
+
diff --git a/src/github/daneren2005/dsub/util/tags/Common.java b/src/github/daneren2005/dsub/util/tags/Common.java
new file mode 100644
index 00000000..51344d90
--- /dev/null
+++ b/src/github/daneren2005/dsub/util/tags/Common.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+package github.daneren2005.dsub.util.tags;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.HashMap;
+import java.util.Vector;
+
+public class Common {
+ private static final long MAX_PKT_SIZE = 524288;
+
+ public void xdie(String reason) throws IOException {
+ throw new IOException(reason);
+ }
+
+ /*
+ ** Returns a 32bit int from given byte offset in LE
+ */
+ public int b2le32(byte[] b, int off) {
+ int r = 0;
+ for(int i=0; i<4; i++) {
+ r |= ( b2u(b[off+i]) << (8*i) );
+ }
+ return r;
+ }
+
+ public int b2be32(byte[] b, int off) {
+ return swap32(b2le32(b, off));
+ }
+
+ public int swap32(int i) {
+ return((i&0xff)<<24)+((i&0xff00)<<8)+((i&0xff0000)>>8)+((i>>24)&0xff);
+ }
+
+ /*
+ ** convert 'byte' value into unsigned int
+ */
+ public int b2u(byte x) {
+ return (x & 0xFF);
+ }
+
+ /*
+ ** Printout debug message to STDOUT
+ */
+ public void debug(String s) {
+ System.out.println("DBUG "+s);
+ }
+
+ public HashMap parse_vorbis_comment(RandomAccessFile s, long offset, long payload_len) throws IOException {
+ HashMap tags = new HashMap();
+ int comments = 0; // number of found comments
+ int xoff = 0; // offset within 'scratch'
+ int can_read = (int)(payload_len > MAX_PKT_SIZE ? MAX_PKT_SIZE : payload_len);
+ byte[] scratch = new byte[can_read];
+
+ // seek to given position and slurp in the payload
+ s.seek(offset);
+ s.read(scratch);
+
+ // skip vendor string in format: [LEN][VENDOR_STRING]
+ xoff += 4 + b2le32(scratch, xoff); // 4 = LEN = 32bit int
+ comments = b2le32(scratch, xoff);
+ xoff += 4;
+
+ // debug("comments count = "+comments);
+ for(int i=0; i<comments; i++) {
+
+ int clen = (int)b2le32(scratch, xoff);
+ xoff += 4+clen;
+
+ if(xoff > scratch.length)
+ xdie("string out of bounds");
+
+ String tag_raw = new String(scratch, xoff-clen, clen);
+ String[] tag_vec = tag_raw.split("=",2);
+ String tag_key = tag_vec[0].toUpperCase();
+
+ addTagEntry(tags, tag_key, tag_vec[1]);
+ }
+ return tags;
+ }
+
+ public void addTagEntry(HashMap tags, String key, String value) {
+ if(tags.containsKey(key)) {
+ ((Vector)tags.get(key)).add(value); // just add to existing vector
+ }
+ else {
+ Vector vx = new Vector();
+ vx.add(value);
+ tags.put(key, vx);
+ }
+ }
+
+}
diff --git a/src/github/daneren2005/dsub/util/tags/FlacFile.java b/src/github/daneren2005/dsub/util/tags/FlacFile.java
new file mode 100644
index 00000000..de3584d1
--- /dev/null
+++ b/src/github/daneren2005/dsub/util/tags/FlacFile.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package github.daneren2005.dsub.util.tags;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.HashMap;
+import java.util.Enumeration;
+
+
+public class FlacFile extends Common {
+ private static final int FLAC_TYPE_COMMENT = 4; // ID of 'VorbisComment's
+
+ public FlacFile() {
+ }
+
+ public HashMap getTags(RandomAccessFile s) throws IOException {
+ int xoff = 4; // skip file magic
+ int retry = 64;
+ int r[];
+ HashMap tags = new HashMap();
+
+ for(; retry > 0; retry--) {
+ r = parse_metadata_block(s, xoff);
+
+ if(r[2] == FLAC_TYPE_COMMENT) {
+ tags = parse_vorbis_comment(s, xoff+r[0], r[1]);
+ break;
+ }
+
+ if(r[3] != 0)
+ break; // eof reached
+
+ // else: calculate next offset
+ xoff += r[0] + r[1];
+ }
+ return tags;
+ }
+
+ /* Parses the metadata block at 'offset' and returns
+ ** [header_size, payload_size, type, stop_after]
+ */
+ private int[] parse_metadata_block(RandomAccessFile s, long offset) throws IOException {
+ int[] result = new int[4];
+ byte[] mb_head = new byte[4];
+ int stop_after = 0;
+ int block_type = 0;
+ int block_size = 0;
+
+ s.seek(offset);
+
+ if( s.read(mb_head) != 4 )
+ xdie("failed to read metadata block header");
+
+ block_size = b2be32(mb_head,0); // read whole header as 32 big endian
+ block_type = (block_size >> 24) & 127; // BIT 1-7 are the type
+ stop_after = (((block_size >> 24) & 128) > 0 ? 1 : 0 ); // BIT 0 indicates the last-block flag
+ block_size = (block_size & 0x00FFFFFF); // byte 1-7 are the size
+
+ // debug("size="+block_size+", type="+block_type+", is_last="+stop_after);
+
+ result[0] = 4; // hardcoded - only returned to be consistent with OGG parser
+ result[1] = block_size;
+ result[2] = block_type;
+ result[3] = stop_after;
+
+ return result;
+ }
+
+}
diff --git a/src/github/daneren2005/dsub/util/tags/ID3v2File.java b/src/github/daneren2005/dsub/util/tags/ID3v2File.java
new file mode 100644
index 00000000..7710654e
--- /dev/null
+++ b/src/github/daneren2005/dsub/util/tags/ID3v2File.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package github.daneren2005.dsub.util.tags;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.HashMap;
+import java.util.Enumeration;
+
+
+
+public class ID3v2File extends Common {
+ private static int ID3_ENC_LATIN = 0x00;
+ private static int ID3_ENC_UTF16LE = 0x01;
+ private static int ID3_ENC_UTF16BE = 0x02;
+ private static int ID3_ENC_UTF8 = 0x03;
+
+ public ID3v2File() {
+ }
+
+ public HashMap getTags(RandomAccessFile s) throws IOException {
+ HashMap tags = new HashMap();
+
+ final int v2hdr_len = 10;
+ byte[] v2hdr = new byte[v2hdr_len];
+
+ // read the whole 10 byte header into memory
+ s.seek(0);
+ s.read(v2hdr);
+
+ int id3v = ((b2be32(v2hdr,0))) & 0xFF; // swapped ID3\04 -> ver. ist the first byte
+ int v3len = ((b2be32(v2hdr,6))); // total size EXCLUDING the this 10 byte header
+ v3len = ((v3len & 0x7f000000) >> 3) | // for some funky reason, this is encoded as 7*4 bits
+ ((v3len & 0x007f0000) >> 2) |
+ ((v3len & 0x00007f00) >> 1) |
+ ((v3len & 0x0000007f) >> 0) ;
+
+ // debug(">> tag version ID3v2."+id3v);
+ // debug(">> LEN= "+v3len+" // "+v3len);
+
+ // we should already be at the first frame
+ // so we can start the parsing right now
+ tags = parse_v3_frames(s, v3len);
+ tags.put("_hdrlen", v3len+v2hdr_len);
+ return tags;
+ }
+
+ /* Parses all ID3v2 frames at the current position up until payload_len
+ ** bytes were read
+ */
+ public HashMap parse_v3_frames(RandomAccessFile s, long payload_len) throws IOException {
+ HashMap tags = new HashMap();
+ byte[] frame = new byte[10]; // a frame header is always 10 bytes
+ long bread = 0; // total amount of read bytes
+
+ while(bread < payload_len) {
+ bread += s.read(frame);
+ String framename = new String(frame, 0, 4);
+ int slen = b2be32(frame, 4);
+
+ /* Abort on silly sizes */
+ if(slen < 1 || slen > 524288)
+ break;
+
+ byte[] xpl = new byte[slen];
+ bread += s.read(xpl);
+
+ if(framename.substring(0,1).equals("T")) {
+ String[] nmzInfo = normalizeTaginfo(framename, xpl);
+ String oggKey = nmzInfo[0];
+ String decPld = nmzInfo[1];
+
+ if(oggKey.length() > 0 && !tags.containsKey(oggKey)) {
+ addTagEntry(tags, oggKey, decPld);
+ }
+ }
+ else if(framename.equals("RVA2")) {
+ //
+ }
+
+ }
+ return tags;
+ }
+
+ /* Converts ID3v2 sillyframes to OggNames */
+ private String[] normalizeTaginfo(String k, byte[] v) {
+ String[] rv = new String[] {"",""};
+ HashMap lu = new HashMap<String, String>();
+ lu.put("TIT2", "TITLE");
+ lu.put("TALB", "ALBUM");
+ lu.put("TPE1", "ARTIST");
+
+ if(lu.containsKey(k)) {
+ /* A normal, known key: translate into Ogg-Frame name */
+ rv[0] = (String)lu.get(k);
+ rv[1] = getDecodedString(v);
+ }
+ else if(k.equals("TXXX")) {
+ /* A freestyle field, ieks! */
+ String txData[] = getDecodedString(v).split(Character.toString('\0'), 2);
+ /* Check if we got replaygain info in key\0value style */
+ if(txData.length == 2 && txData[0].matches("^(?i)REPLAYGAIN_(ALBUM|TRACK)_GAIN$")) {
+ rv[0] = txData[0].toUpperCase(); /* some tagwriters use lowercase for this */
+ rv[1] = txData[1];
+ }
+ }
+
+ return rv;
+ }
+
+ /* Converts a raw byte-stream text into a java String */
+ private String getDecodedString(byte[] raw) {
+ int encid = raw[0] & 0xFF;
+ int len = raw.length;
+ String v = "";
+ try {
+ if(encid == ID3_ENC_LATIN) {
+ v = new String(raw, 1, len-1, "ISO-8859-1");
+ }
+ else if (encid == ID3_ENC_UTF8) {
+ v = new String(raw, 1, len-1, "UTF-8");
+ }
+ else if (encid == ID3_ENC_UTF16LE) {
+ v = new String(raw, 3, len-3, "UTF-16LE");
+ }
+ else if (encid == ID3_ENC_UTF16BE) {
+ v = new String(raw, 3, len-3, "UTF-16BE");
+ }
+ } catch(Exception e) {}
+ return v;
+ }
+
+}
diff --git a/src/github/daneren2005/dsub/util/tags/LameHeader.java b/src/github/daneren2005/dsub/util/tags/LameHeader.java
new file mode 100644
index 00000000..720ee87f
--- /dev/null
+++ b/src/github/daneren2005/dsub/util/tags/LameHeader.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package github.daneren2005.dsub.util.tags;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.HashMap;
+import java.util.Enumeration;
+
+
+public class LameHeader extends Common {
+
+ public LameHeader() {
+ }
+
+ public HashMap getTags(RandomAccessFile s) throws IOException {
+ return parseLameHeader(s, 0);
+ }
+
+ public HashMap parseLameHeader(RandomAccessFile s, long offset) throws IOException {
+ HashMap tags = new HashMap();
+ byte[] chunk = new byte[4];
+
+ s.seek(offset + 0x24);
+ s.read(chunk);
+
+ String lameMark = new String(chunk, 0, chunk.length, "ISO-8859-1");
+
+ if(lameMark.equals("Info") || lameMark.equals("Xing")) {
+ s.seek(offset+0xAB);
+ s.read(chunk);
+
+ int raw = b2be32(chunk, 0);
+ int gtrk_raw = raw >> 16; /* first 16 bits are the raw track gain value */
+ int galb_raw = raw & 0xFFFF; /* the rest is for the album gain value */
+
+ float gtrk_val = (float)(gtrk_raw & 0x01FF)/10;
+ float galb_val = (float)(galb_raw & 0x01FF)/10;
+
+ gtrk_val = ((gtrk_raw&0x0200)!=0 ? -1*gtrk_val : gtrk_val);
+ galb_val = ((galb_raw&0x0200)!=0 ? -1*galb_val : galb_val);
+
+ if( (gtrk_raw&0xE000) == 0x2000 ) {
+ addTagEntry(tags, "REPLAYGAIN_TRACK_GAIN", gtrk_val+" dB");
+ }
+ if( (gtrk_raw&0xE000) == 0x4000 ) {
+ addTagEntry(tags, "REPLAYGAIN_ALBUM_GAIN", galb_val+" dB");
+ }
+
+ }
+
+ return tags;
+ }
+
+}
diff --git a/src/github/daneren2005/dsub/util/tags/OggFile.java b/src/github/daneren2005/dsub/util/tags/OggFile.java
new file mode 100644
index 00000000..d0b31671
--- /dev/null
+++ b/src/github/daneren2005/dsub/util/tags/OggFile.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2013 Adrian Ulrich <adrian@blinkenlights.ch>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package github.daneren2005.dsub.util.tags;
+
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.HashMap;
+
+
+public class OggFile extends Common {
+
+ private static final int OGG_PAGE_SIZE = 27; // Static size of an OGG Page
+ private static final int OGG_TYPE_COMMENT = 3; // ID of 'VorbisComment's
+
+ public OggFile() {
+ }
+
+ public HashMap getTags(RandomAccessFile s) throws IOException {
+ long offset = 0;
+ int retry = 64;
+ HashMap tags = new HashMap();
+
+ for( ; retry > 0 ; retry-- ) {
+ long res[] = parse_ogg_page(s, offset);
+ if(res[2] == OGG_TYPE_COMMENT) {
+ tags = parse_ogg_vorbis_comment(s, offset+res[0], res[1]);
+ break;
+ }
+ offset += res[0] + res[1];
+ }
+ return tags;
+ }
+
+
+ /* Parses the ogg page at offset 'offset' and returns
+ ** [header_size, payload_size, type]
+ */
+ private long[] parse_ogg_page(RandomAccessFile s, long offset) throws IOException {
+ long[] result = new long[3]; // [header_size, payload_size]
+ byte[] p_header = new byte[OGG_PAGE_SIZE]; // buffer for the page header
+ byte[] scratch;
+ int bread = 0; // number of bytes read
+ int psize = 0; // payload-size
+ int nsegs = 0; // Number of segments
+
+ s.seek(offset);
+ bread = s.read(p_header);
+ if(bread != OGG_PAGE_SIZE)
+ xdie("Unable to read() OGG_PAGE_HEADER");
+ if((new String(p_header, 0, 5)).equals("OggS\0") != true)
+ xdie("Invalid magic - not an ogg file?");
+
+ nsegs = b2u(p_header[26]);
+ // debug("> file seg: "+nsegs);
+ if(nsegs > 0) {
+ scratch = new byte[nsegs];
+ bread = s.read(scratch);
+ if(bread != nsegs)
+ xdie("Failed to read segtable");
+
+ for(int i=0; i<nsegs; i++) {
+ psize += b2u(scratch[i]);
+ }
+ }
+
+ // populate result array
+ result[0] = (s.getFilePointer() - offset);
+ result[1] = psize;
+ result[2] = -1;
+
+ /* next byte is most likely the type -> pre-read */
+ if(psize >= 1 && s.read(p_header, 0, 1) == 1) {
+ result[2] = b2u(p_header[0]);
+ }
+
+ return result;
+ }
+
+ /* In 'vorbiscomment' field is prefixed with \3vorbis in OGG files
+ ** we check that this marker is present and call the generic comment
+ ** parset with the correct offset (+7) */
+ private HashMap parse_ogg_vorbis_comment(RandomAccessFile s, long offset, long pl_len) throws IOException {
+ final int pfx_len = 7;
+ byte[] pfx = new byte[pfx_len];
+
+ if(pl_len < pfx_len)
+ xdie("ogg vorbis comment field is too short!");
+
+ s.seek(offset);
+ s.read(pfx);
+
+ if( (new String(pfx, 0, pfx_len)).equals("\3vorbis") == false )
+ xdie("Damaged packet found!");
+
+ return parse_vorbis_comment(s, offset+pfx_len, pl_len-pfx_len);
+ }
+
+};