From: David ‘Bombe’ Roden Date: Sun, 17 Mar 2013 09:58:07 +0000 (+0100) Subject: Parse Vorbis comments from Ogg Vorbis stream. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=51b1e870839d0b49824d1bdba0e03217507f86ca;p=sonitus.git Parse Vorbis comments from Ogg Vorbis stream. --- 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(); } }