From 51b1e870839d0b49824d1bdba0e03217507f86ca Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 17 Mar 2013 10:58:07 +0100 Subject: [PATCH] Parse Vorbis comments from Ogg Vorbis stream. --- .../sonitus/io/OggVorbisIdentifier.java | 34 +++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java b/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java index 769acf2..bbbecd2 100644 --- a/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java +++ b/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java @@ -115,7 +115,39 @@ public class OggVorbisIdentifier { buffer = syncState.data; } - return Optional.of(new Metadata(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 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 extractField(String comment, String fieldName) { + if (comment.startsWith(fieldName + "=")) { + return Optional.of(comment.substring(fieldName.length() + 1)); + } + return Optional.absent(); } } -- 2.7.4