Parse Vorbis comments from Ogg Vorbis stream.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / io / OggVorbisIdentifier.java
index f70a432..bbbecd2 100644 (file)
@@ -20,7 +20,7 @@ package net.pterodactylus.sonitus.io;
 import java.io.IOException;
 import java.io.InputStream;
 
-import net.pterodactylus.sonitus.data.Format;
+import net.pterodactylus.sonitus.data.Metadata;
 
 import com.google.common.base.Optional;
 import com.jcraft.jogg.Packet;
@@ -31,10 +31,8 @@ import com.jcraft.jorbis.Comment;
 import com.jcraft.jorbis.Info;
 
 /**
- * Identifies Ogg Vorbis files.
- * <p>
- * All knowledge used in this class has been taken from <a
- * href="http://www.jcraft.com/jorbis/tutorial/Tutorial.html">jcraft.com/jorbis/tutorial/Tutorial.html</a>.
+ * Identifies Ogg Vorbis files. <p> All knowledge used in this class has been
+ * taken from <a href="http://www.jcraft.com/jorbis/tutorial/Tutorial.html">jcraft.com/jorbis/tutorial/Tutorial.html</a>.
  * </p>
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
@@ -51,16 +49,16 @@ public class OggVorbisIdentifier {
 
        /**
         * Tries to parse the given stream as Ogg Vorbis file and returns a {@link
-        * Format} describing the stream.
+        * Metadata} describing the stream.
         *
         * @param inputStream
         *              The input stream to identify as Ogg Vorbis
-        * @return The identified format, or {@link com.google.common.base.Optional#absent()}
-        *         if the stream could not be identified
+        * @return The identified metadata, or {@link Optional#absent()} if the stream
+        *         could not be identified
         * @throws IOException
         *              if an I/O error occurs
         */
-       public static Optional<Format> identify(InputStream inputStream) throws IOException {
+       public static Optional<Metadata> identify(InputStream inputStream) throws IOException {
 
                /* stuff needed to decode Ogg. */
                Packet packet = new Packet();
@@ -117,7 +115,39 @@ public class OggVorbisIdentifier {
                        buffer = syncState.data;
                }
 
-               return Optional.of(new Format(info.channels, info.rate, "Vorbis"));
+               Metadata metadata = new Metadata(info.channels, info.rate, "Vorbis");
+               for (int c = 0; c < comment.comments; ++c) {
+                       String field = comment.getComment(c);
+                       Optional<String> extractedField = extractField(field, "ARTIST");
+                       if (extractedField.isPresent()) {
+                               metadata = metadata.artist(extractedField.get());
+                               continue;
+                       }
+                       extractedField = extractField(field, "TITLE");
+                       if (extractedField.isPresent()) {
+                               metadata = metadata.name(extractedField.get());
+                               continue;
+                       }
+               }
+               return Optional.of(metadata);
+       }
+
+       /**
+        * Extracts the content of the field from the comment if the comment contains
+        * the given field.
+        *
+        * @param comment
+        *              The comment to extract the value from
+        * @param fieldName
+        *              The name of the field to extract
+        * @return The extracted field, or {@link Optional#absent()} if the comment
+        *         does not contain the given field
+        */
+       private static Optional<String> extractField(String comment, String fieldName) {
+               if (comment.startsWith(fieldName + "=")) {
+                       return Optional.of(comment.substring(fieldName.length() + 1));
+               }
+               return Optional.absent();
        }
 
 }