2 * Sonitus - OggVorbisIdentifier.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.io;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import net.pterodactylus.sonitus.data.Format;
25 import com.google.common.base.Optional;
26 import com.jcraft.jogg.Packet;
27 import com.jcraft.jogg.Page;
28 import com.jcraft.jogg.StreamState;
29 import com.jcraft.jogg.SyncState;
30 import com.jcraft.jorbis.Comment;
31 import com.jcraft.jorbis.Info;
34 * Identifies Ogg Vorbis files. <p> All knowledge used in this class has been
35 * taken from <a href="http://www.jcraft.com/jorbis/tutorial/Tutorial.html">jcraft.com/jorbis/tutorial/Tutorial.html</a>.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class OggVorbisIdentifier {
42 /** The default size of the read buffer. */
43 private static final int BUFFER_SIZE = 4096;
45 /** Suppress default constructor. */
46 private OggVorbisIdentifier() {
51 * Tries to parse the given stream as Ogg Vorbis file and returns a {@link
52 * Format} describing the stream.
55 * The input stream to identify as Ogg Vorbis
56 * @return The identified format, or {@link com.google.common.base.Optional#absent()}
57 * if the stream could not be identified
59 * if an I/O error occurs
61 public static Optional<Format> identify(InputStream inputStream) throws IOException {
63 /* stuff needed to decode Ogg. */
64 Packet packet = new Packet();
65 Page page = new Page();
66 StreamState streamState = new StreamState();
67 SyncState syncState = new SyncState();
69 /* stuff needed to decode Vorbis. */
70 Comment comment = new Comment();
71 Info info = new Info();
73 /* initialize jorbis. */
75 int bufferSize = BUFFER_SIZE;
76 int index = syncState.buffer(bufferSize);
77 byte[] buffer = syncState.data;
79 boolean streamStateInitialized = false;
82 /* read until we have read the three packets required to decode the header. */
83 while (packetsRead < 3) {
84 int read = inputStream.read(buffer, index, bufferSize);
85 syncState.wrote(read);
86 switch (syncState.pageout(page)) {
88 return Optional.absent();
90 if (!streamStateInitialized) {
91 /* init stream state. */
92 streamState.init(page.serialno());
96 streamStateInitialized = true;
98 if (streamState.pagein(page) == -1) {
99 return Optional.absent();
101 switch (streamState.packetout(packet)) {
103 return Optional.absent();
105 info.synthesis_headerin(comment, packet);
114 index = syncState.buffer(bufferSize);
115 buffer = syncState.data;
118 return Optional.of(new Format(info.channels, info.rate, "Vorbis"));