import java.io.IOException;
import java.io.InputStream;
-import net.pterodactylus.sonitus.data.Format;
+import net.pterodactylus.sonitus.data.ContentMetadata;
+import net.pterodactylus.sonitus.data.FormatMetadata;
+import net.pterodactylus.sonitus.data.Metadata;
import com.google.common.base.Optional;
import com.jcraft.jogg.Packet;
/**
* 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();
buffer = syncState.data;
}
- return Optional.of(new Format(info.channels, info.rate, "Vorbis"));
+ FormatMetadata formatMetadata = new FormatMetadata(info.channels, info.rate, "Vorbis");
+ ContentMetadata contentMetadata = new ContentMetadata("");
+ for (int c = 0; c < comment.comments; ++c) {
+ String field = comment.getComment(c);
+ Optional<String> extractedField = extractField(field, "ARTIST");
+ if (extractedField.isPresent()) {
+ contentMetadata = contentMetadata.artist(extractedField.get());
+ continue;
+ }
+ extractedField = extractField(field, "TITLE");
+ if (extractedField.isPresent()) {
+ contentMetadata = contentMetadata.name(extractedField.get());
+ continue;
+ }
+ }
+ return Optional.of(new Metadata(formatMetadata, contentMetadata));
+ }
+
+ /**
+ * 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();
}
}