Split metadata into format and content metadata.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / io / OggVorbisIdentifier.java
1 /*
2  * Sonitus - OggVorbisIdentifier.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.io;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import net.pterodactylus.sonitus.data.ContentMetadata;
24 import net.pterodactylus.sonitus.data.FormatMetadata;
25 import net.pterodactylus.sonitus.data.Metadata;
26
27 import com.google.common.base.Optional;
28 import com.jcraft.jogg.Packet;
29 import com.jcraft.jogg.Page;
30 import com.jcraft.jogg.StreamState;
31 import com.jcraft.jogg.SyncState;
32 import com.jcraft.jorbis.Comment;
33 import com.jcraft.jorbis.Info;
34
35 /**
36  * Identifies Ogg Vorbis files. <p> All knowledge used in this class has been
37  * taken from <a href="http://www.jcraft.com/jorbis/tutorial/Tutorial.html">jcraft.com/jorbis/tutorial/Tutorial.html</a>.
38  * </p>
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class OggVorbisIdentifier {
43
44         /** The default size of the read buffer. */
45         private static final int BUFFER_SIZE = 4096;
46
47         /** Suppress default constructor. */
48         private OggVorbisIdentifier() {
49                 /* nothing here. */
50         }
51
52         /**
53          * Tries to parse the given stream as Ogg Vorbis file and returns a {@link
54          * Metadata} describing the stream.
55          *
56          * @param inputStream
57          *              The input stream to identify as Ogg Vorbis
58          * @return The identified metadata, or {@link Optional#absent()} if the stream
59          *         could not be identified
60          * @throws IOException
61          *              if an I/O error occurs
62          */
63         public static Optional<Metadata> identify(InputStream inputStream) throws IOException {
64
65                 /* stuff needed to decode Ogg. */
66                 Packet packet = new Packet();
67                 Page page = new Page();
68                 StreamState streamState = new StreamState();
69                 SyncState syncState = new SyncState();
70
71                 /* stuff needed to decode Vorbis. */
72                 Comment comment = new Comment();
73                 Info info = new Info();
74
75                 /* initialize jorbis. */
76                 syncState.init();
77                 int bufferSize = BUFFER_SIZE;
78                 int index = syncState.buffer(bufferSize);
79                 byte[] buffer = syncState.data;
80
81                 boolean streamStateInitialized = false;
82                 int packetsRead = 0;
83
84                 /* read until we have read the three packets required to decode the header. */
85                 while (packetsRead < 3) {
86                         int read = inputStream.read(buffer, index, bufferSize);
87                         syncState.wrote(read);
88                         switch (syncState.pageout(page)) {
89                                 case -1:
90                                         return Optional.absent();
91                                 case 1:
92                                         if (!streamStateInitialized) {
93                                                 /* init stream state. */
94                                                 streamState.init(page.serialno());
95                                                 streamState.reset();
96                                                 info.init();
97                                                 comment.init();
98                                                 streamStateInitialized = true;
99                                         }
100                                         if (streamState.pagein(page) == -1) {
101                                                 return Optional.absent();
102                                         }
103                                         switch (streamState.packetout(packet)) {
104                                                 case -1:
105                                                         return Optional.absent();
106                                                 case 1:
107                                                         info.synthesis_headerin(comment, packet);
108                                                         packetsRead++;
109                                                 default:
110                                                         /* continue. */
111                                         }
112
113                                 default:
114                                         /* continue. */
115                         }
116                         index = syncState.buffer(bufferSize);
117                         buffer = syncState.data;
118                 }
119
120                 FormatMetadata formatMetadata = new FormatMetadata(info.channels, info.rate, "Vorbis");
121                 ContentMetadata contentMetadata = new ContentMetadata("");
122                 for (int c = 0; c < comment.comments; ++c) {
123                         String field = comment.getComment(c);
124                         Optional<String> extractedField = extractField(field, "ARTIST");
125                         if (extractedField.isPresent()) {
126                                 contentMetadata = contentMetadata.artist(extractedField.get());
127                                 continue;
128                         }
129                         extractedField = extractField(field, "TITLE");
130                         if (extractedField.isPresent()) {
131                                 contentMetadata = contentMetadata.name(extractedField.get());
132                                 continue;
133                         }
134                 }
135                 return Optional.of(new Metadata(formatMetadata, contentMetadata));
136         }
137
138         /**
139          * Extracts the content of the field from the comment if the comment contains
140          * the given field.
141          *
142          * @param comment
143          *              The comment to extract the value from
144          * @param fieldName
145          *              The name of the field to extract
146          * @return The extracted field, or {@link Optional#absent()} if the comment
147          *         does not contain the given field
148          */
149         private static Optional<String> extractField(String comment, String fieldName) {
150                 if (comment.startsWith(fieldName + "=")) {
151                         return Optional.of(comment.substring(fieldName.length() + 1));
152                 }
153                 return Optional.absent();
154         }
155
156 }